Skip to content

Commit 3cd09cd

Browse files
authored
Merge pull request #50 from JiveDig/vanillajs
Remove jQuery and use fetch
2 parents a5e5ba7 + 0903583 commit 3cd09cd

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

postviews-cache.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
jQuery.ajax({type:"GET",url:viewsCacheL10n.admin_ajax_url,data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",cache:!1});
1+
fetch( viewsCacheL10n.admin_ajax_url, {
2+
method: "POST",
3+
credentials: 'same-origin',
4+
headers: {
5+
'Content-Type': 'application/x-www-form-urlencoded',
6+
'Cache-Control': 'no-cache',
7+
},
8+
body: new URLSearchParams(
9+
{
10+
action: 'postviews',
11+
nonce: viewsCacheL10n.nonce,
12+
postviews_id: viewsCacheL10n.post_id,
13+
cache: !1,
14+
}
15+
),
16+
})
17+
.then(function(response) {
18+
return response.json();
19+
})
20+
.then(function(data) {
21+
})
22+
.catch(function(error) {
23+
console.log('WP-PostViews');
24+
console.log(error);
25+
});

wp-postviews.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ function process_postviews() {
129129
}
130130
}
131131

132-
133132
### Function: Calculate Post Views With WP_CACHE Enabled
134133
add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue');
135134
function 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) {
791790
add_action( 'wp_ajax_postviews', 'increment_views' );
792791
add_action( 'wp_ajax_nopriv_postviews', 'increment_views' );
793792
function 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

Comments
 (0)