1(function() { 2 'use strict'; 3 4 // ap_popular_posts_vars is required to continue, ensure the object exists 5 if ( typeof ap_popular_posts_vars === 'undefined' ) { 6 return false; 7 } 8 9 function get_fragments() { 10 const fragments = new Map(); 11 let instance = 1; 12 13 if ( ! ap_popular_posts_vars.fragments ) { 14 return fragments; 15 } 16 17 ap_popular_posts_vars.fragments.forEach( function( fragment ) { 18 const $fragments = document.querySelectorAll( fragment ); 19 20 $fragments.forEach( function( $element ) { 21 let args = $element.getAttribute( 'data-options' ); 22 args = args ? JSON.parse( args ) : false; 23 24 $element.setAttribute( 'data-ap-popular-posts-fragment', instance ); 25 26 fragments.set( instance.toString(), { 27 instance: instance, 28 key: fragment, 29 args: args, 30 } ); 31 32 instance++; 33 } ); 34 } ); 35 36 return fragments; 37 } 38 39 function refresh_fragments() { 40 const form_data = new FormData(); 41 const fragments = get_fragments(); 42 const url = ap_popular_posts_vars.ajax_url.toString().replace( '%%action%%', 'refresh_fragments' ); 43 44 if ( ! fragments.size ) { 45 return false; 46 } 47 48 fragments.forEach( ( { instance, key, args }, index ) => { 49 form_data.append( 'fragments[' + index + '][instance]', instance ); 50 form_data.append( 'fragments[' + index + '][key]', key ); 51 52 if ( args ) { 53 for ( const [ key, value ] of Object.entries( args ) ) { 54 form_data.append( 'fragments[' + index + '][args][' + key + ']', value ); 55 } 56 } else { 57 form_data.append( 'fragments[' + index + '][args]', false ); 58 } 59 } ); 60 61 window.fetch( url, { 62 method: 'POST', 63 body: form_data, 64 } ).then( ( response ) => { 65 return response.json() 66 } ).then( ( response ) => { 67 if ( response.success ) { 68 for ( const [ instance, fragment ] of Object.entries( response.data ) ) { 69 const parser = new DOMParser(); 70 const $fragment = parser.parseFromString( fragment, 'text/html' ); 71 const selector = fragments.get( instance.toString() )?.key; 72 73 if ( $fragment.querySelector( selector ) ) { 74 document.querySelector( `[data-ap-popular-posts-fragment="${instance}"]` ).innerHTML = $fragment.querySelector( selector ).innerHTML; 75 } else { 76 document.querySelector( `[data-ap-popular-posts-fragment="${instance}"]` ).innerHTML = fragment; 77 } 78 } 79 document.dispatchEvent( new Event( 'ap_popular_posts_fragments_refreshed' ) ); 80 } 81 } ); 82 } 83 84 function update_views() { 85 const url = ap_popular_posts_vars.ajax_url.toString().replace( '%%action%%', 'update_views' ); 86 window.fetch( url, { 87 method: 'POST', 88 body: new URLSearchParams( { 89 post_id: ap_popular_posts_vars.post_id 90 } ), 91 } ); 92 } 93 94 if ( 'true' === ap_popular_posts_vars.ajax_refresh_fragments ) { 95 refresh_fragments(); 96 } 97 98 if ( 'true' === ap_popular_posts_vars.ajax_update_views && 'true' === ap_popular_posts_vars.is_single ) { 99 update_views(); 100 } 101})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.