@@ -129,7 +129,6 @@ function process_postviews() {
129129 }
130130}
131131
132-
133132### Function: Calculate Post Views With WP_CACHE Enabled
134133add_action ('wp_enqueue_scripts ' , 'wp_postview_cache_count_enqueue ' );
135134function wp_postview_cache_count_enqueue () {
@@ -165,8 +164,8 @@ function wp_postview_cache_count_enqueue() {
165164
166165 $ should_count = apply_filters ( 'postviews_should_count ' , $ should_count , (int ) $ post ->ID );
167166 if ( $ should_count ) {
168- wp_enqueue_script ( 'wp-postviews-cache ' , plugins_url ( 'postviews-cache.js ' , __FILE__ ), array ( ' jquery ' ), '1.68 ' , true );
169- wp_localize_script ( 'wp-postviews-cache ' , 'viewsCacheL10n ' , array ( 'admin_ajax_url ' => admin_url ( 'admin-ajax.php ' ), 'post_id ' => (int ) $ post ->ID ) );
167+ wp_enqueue_script ( 'wp-postviews-cache ' , plugins_url ( 'postviews-cache.js ' , __FILE__ ), array (), '1.68 ' , true );
168+ wp_localize_script ( 'wp-postviews-cache ' , 'viewsCacheL10n ' , array ( 'admin_ajax_url ' => admin_url ( 'admin-ajax.php ' ), 'nonce ' => wp_create_nonce ( ' wp_postviews_nonce ' ), ' post_id ' => (int ) $ post ->ID ) );
170169 }
171170 }
172171}
@@ -791,7 +790,14 @@ function postviews_page_most_stats($content) {
791790add_action ( 'wp_ajax_postviews ' , 'increment_views ' );
792791add_action ( 'wp_ajax_nopriv_postviews ' , 'increment_views ' );
793792function increment_views () {
794- if ( empty ( $ _GET ['postviews_id ' ] ) ) {
793+ $ security = check_ajax_referer ( 'wp_postviews_nonce ' , 'nonce ' );
794+
795+ if ( false === $ security ) {
796+ wp_send_json_error ();
797+ wp_die ();
798+ }
799+
800+ if ( ! isset ( $ _POST ['postviews_id ' ] ) || empty ( $ _POST ['postviews_id ' ] ) ) {
795801 return ;
796802 }
797803
@@ -805,13 +811,13 @@ function increment_views() {
805811 return ;
806812 }
807813
808- $ post_id = (int ) sanitize_key ( $ _GET ['postviews_id ' ] );
814+ $ post_id = (int ) sanitize_key ( $ _POST ['postviews_id ' ] );
809815 if ( $ post_id > 0 ) {
810- $ post_views = get_post_custom ( $ post_id );
811- $ post_views = ( int ) $ post_views[ ' views ' ][ 0 ] ;
812- update_post_meta ( $ post_id , 'views ' , ( $ post_views + 1 ) );
813- do_action ( 'postviews_increment_views_ajax ' , ( $ post_views + 1 ) );
814- echo ( $ post_views + 1 );
816+ $ post_views = ( int ) get_post_meta ( $ post_id, ' views ' , true );
817+ $ post_views = $ post_views + 1 ;
818+ update_post_meta ( $ post_id , 'views ' , $ post_views );
819+ do_action ( 'postviews_increment_views_ajax ' , $ post_views );
820+ wp_send_json_success ( [ ' views ' => $ post_views ] );
815821 exit ();
816822 }
817823}
0 commit comments