1/** 2 * Records a view over admin-ajax.php. 3 * 4 * Enqueued only when a page cache is in use, where counting during wp_head 5 * would record the request that generated the cached page and nothing after. 6 * 7 * On success a `postviews:updated` event is dispatched on `document` carrying 8 * the new count, so a theme can write it into the page. That is what the 9 * LiteSpeed Cache recipe in the readme uses; previously it told people to edit 10 * this file, which every plugin update then overwrote. 11 */ 12( function() { 13 'use strict'; 14 15 fetch( wpPostViewsL10n.ajaxUrl, { 16 method: 'POST', 17 credentials: 'same-origin', 18 headers: { 19 'Content-Type': 'application/x-www-form-urlencoded', 20 'Cache-Control': 'no-cache', 21 }, 22 body: new URLSearchParams( { 23 action: 'wp_postviews', 24 _ajax_nonce: wpPostViewsL10n.nonce, 25 postviews_id: wpPostViewsL10n.postId, 26 } ), 27 } ) 28 .then( function( response ) { 29 return response.json(); 30 } ) 31 .then( function( data ) { 32 if ( ! data || ! data.success || ! data.data ) { 33 return; 34 } 35 36 document.dispatchEvent( 37 new CustomEvent( 'postviews:updated', { 38 detail: { 39 views: data.data.views, 40 postId: wpPostViewsL10n.postId, 41 }, 42 } ), 43 ); 44 } ) 45 .catch( function( error ) { 46 // eslint-disable-next-line no-console 47 console.log( 'WP-PostViews', error ); 48 } ); 49}() );
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.