Skip to content

Commit 4c1be54

Browse files
committed
Release new version 2.8.0
= 2.8.0 - 2023/05/24 = * This release removes loading PVC Stats with PHP and replaces with REST API as default with Admin-ajax as the fallback. * Feature - Support options for choose WP REST API or Admin Ajax load PVC stats * Tweak - Remove load PVC stats by PHP * Tweak - On upgrade if using PHP to load PVC stats it will update to use WP REST API * Tweak - If WP REST API is not active it will fall back to ADMIN-AJAX * Tweak - Auto detect if WP REST API and PVC endpoint are disabled, it will fall back to the ADMIN-AJAX * Tweak - Add warning notification if WP REST API or PVC Endpoint are disabled. * Tweak - Updated setting and help text on the Page View Count Load option box. * Tweak - Test for compatibility with WordPress 6.2.2
1 parent ccb2723 commit 4c1be54

File tree

8 files changed

+195
-43
lines changed

8 files changed

+195
-43
lines changed

admin/plugin-init.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function a3_pvc_plugin_init() {
3535

3636
// Build sass
3737
$GLOBALS[A3_PVC_PREFIX.'less']->plugin_build_sass();
38+
39+
\A3Rev\PageViewsCount\A3_PVC::validate_pvc_endpoint_rest_api_enable();
3840
}
3941

4042
// Set up localisation
@@ -86,6 +88,10 @@ function pvc_empty_daily_table_do_daily() {
8688
// Add ajax script to load page view count stats into footer
8789
add_action( 'wp_enqueue_scripts', array( '\A3Rev\PageViewsCount\A3_PVC', 'register_plugin_scripts' ) );
8890

91+
// AJAX hide yellow message dontshow
92+
add_action('wp_ajax_pvc_yellow_message_dontshow', array('\A3Rev\PageViewsCount\A3_PVC', 'yellow_message_dontshow') );
93+
add_action('wp_ajax_nopriv_pvc_yellow_message_dontshow', array('\A3Rev\PageViewsCount\A3_PVC', 'yellow_message_dontshow') );
94+
8995
// Check upgrade functions
9096
add_action('plugins_loaded', 'pvc_lite_upgrade_plugin');
9197
function pvc_lite_upgrade_plugin () {
@@ -136,10 +142,20 @@ function pvc_lite_upgrade_plugin () {
136142
$GLOBALS[A3_PVC_PREFIX.'less']->plugin_build_sass();
137143
}
138144

145+
if ( version_compare( get_option('a3_pvc_version'), '2.8.0' ) === -1 ) {
146+
update_option('a3_pvc_version', '2.8.0');
147+
148+
add_action( 'admin_init', function() {
149+
\A3Rev\PageViewsCount\A3_PVC::validate_pvc_endpoint_rest_api_enable();
150+
} );
151+
}
152+
139153
update_option( 'a3_pvc_version', A3_PVC_VERSION );
140154

141155
}
142156

157+
add_action( 'admin_notices', array( '\A3Rev\PageViewsCount\A3_PVC', 'pvc_endpoint_rest_api_disabled_warning' ) );
158+
143159
if ( 'responsi' === get_template() ) {
144160
remove_filter('the_content', array(
145161
'\A3Rev\PageViewsCount\A3_PVC',

admin/settings/general-settings.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public function __construct() {
9797

9898
add_action( $this->plugin_name . '_settings_' . 'pvc_page_view_count_function_box' . '_start', array( $this, 'page_view_count_function_content' ) );
9999

100+
add_action( $this->plugin_name . '-' . $this->form_key . '_after_settings_save', array( $this, 'after_settings_save_reset' ) );
101+
add_action( $this->plugin_name . '_init_scripts', array( $this, 'validate_pvc_endpoint' ) );
100102
}
101103

102104
/*-----------------------------------------------------------------------------------*/
@@ -135,6 +137,20 @@ public function clean_on_deletion() {
135137
}
136138
}
137139

140+
public function after_settings_save_reset() {
141+
if ( isset( $_POST['bt_reset_settings'] ) && 1 != get_option( 'pvc_current_rest_api_enabled', 1 ) ) {
142+
$pvc_settings = get_option( 'pvc_settings', array() );
143+
$pvc_settings['ajax_load_type'] = 'admin_ajax';
144+
update_option( 'pvc_settings', $pvc_settings );
145+
}
146+
}
147+
148+
public function validate_pvc_endpoint() {
149+
if ( isset( $_POST['bt_save_settings'] ) || isset( $_POST['bt_reset_settings'] ) ) {
150+
\A3Rev\PageViewsCount\A3_PVC::validate_pvc_endpoint_rest_api_enable();
151+
}
152+
}
153+
138154
/*-----------------------------------------------------------------------------------*/
139155
/* get_settings()
140156
/* Get settings with function called from Admin Interface */
@@ -387,14 +403,14 @@ public function init_form_fields() {
387403
),
388404
array(
389405
'name' => __( 'Ajax Load', 'page-views-count' ),
390-
'desc' => __( 'ON to load page views counter on front end by ajax event (recommended). Prevents caching plugins and CDNs from caching the count. If using caching you must clear the cache to see changes after turning this setting ON or OFF.', 'page-views-count' ),
391-
'id' => 'enable_ajax_load',
392-
'type' => 'onoff_checkbox',
393-
'default' => 'no',
394-
'checked_value' => 'yes',
395-
'unchecked_value' => 'no',
396-
'checked_label' => __( 'ON', 'page-views-count' ),
397-
'unchecked_label' => __( 'OFF', 'page-views-count' ),
406+
'desc' => __( ' Default and recommended is WP REST API. ADMIN-AJAX is the fallback option.', 'page-views-count' ),
407+
'id' => 'ajax_load_type',
408+
'default' => 'rest_api',
409+
'type' => 'switcher_checkbox',
410+
'checked_value' => 'rest_api',
411+
'unchecked_value' => 'admin_ajax',
412+
'checked_label' => __( 'WP REST API', 'page-views-count' ),
413+
'unchecked_label' => __( 'ADMIN AJAX', 'page-views-count' ),
398414
),
399415

400416
array(

assets/js/pvc.backbone.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
jQuery( function( $ ) {
2-
var rest_api_url = vars.rest_api_url;
3-
2+
var call_url = typeof pvc_vars.ajax_load_type != 'undefined' && pvc_vars.ajax_load_type == 'admin_ajax' ? pvc_vars.ajax_url : pvc_vars.rest_api_url;
3+
44
pvc = { apps: {}, models: {}, collections: {}, views: {} };
55

66
_.templateSettings = {
@@ -20,7 +20,7 @@ jQuery( function( $ ) {
2020
pvc.collections.Stats = Backbone.Collection.extend({
2121
model: pvc.models.State,
2222

23-
url: rest_api_url
23+
url: call_url
2424

2525
});
2626

@@ -57,8 +57,8 @@ jQuery( function( $ ) {
5757
});
5858

5959
pvc.apps.app = {
60-
initialize: function( pvc_ids, rest_api_url ) {
61-
this.rest_api_url = rest_api_url;
60+
initialize: function( pvc_ids ) {
61+
this.call_url = call_url;
6262
//console.log('Load Page View Count of ' + JSON.stringify(pvc_ids) );
6363

6464
view_pvc_ids = [];
@@ -73,7 +73,8 @@ jQuery( function( $ ) {
7373
});
7474

7575
if ( increase_pvc_ids.length ) {
76-
$.get( this.rest_api_url + '/increase/' + increase_pvc_ids.join(','), function( data_pvc ) {
76+
action = typeof pvc_vars.ajax_load_type != 'undefined' && pvc_vars.ajax_load_type == 'admin_ajax' ? '?action=pvc_increase&security=' + pvc_vars.security + '&ids=' : '/increase/';
77+
$.get( this.call_url + action + increase_pvc_ids.join(','), function( data_pvc ) {
7778
//console.log(data_pvc);
7879
if ( data_pvc.success ) {
7980
$.each( data_pvc.items, function (index, data) {
@@ -87,7 +88,8 @@ jQuery( function( $ ) {
8788
}
8889

8990
if ( view_pvc_ids.length ) {
90-
$.get( this.rest_api_url + '/view/' + view_pvc_ids.join(','), function( data_pvc ) {
91+
action = typeof pvc_vars.ajax_load_type != 'undefined' && pvc_vars.ajax_load_type == 'admin_ajax' ? '?action=pvc_view&security=' + pvc_vars.security + '&ids=' : '/view/';
92+
$.get( this.call_url + action + view_pvc_ids.join(','), function( data_pvc ) {
9193
//console.log(data_pvc);
9294
if ( data_pvc.success ) {
9395
$.each( data_pvc.items , function (index, data) {
@@ -116,6 +118,6 @@ jQuery( document ).ready( function( $ ) {
116118
});
117119

118120
var app = pvc.apps.app;
119-
app.initialize( pvc_ids, vars.rest_api_url );
121+
app.initialize( pvc_ids );
120122
}
121123
});

assets/js/pvc.backbone.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

page-views-count.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
/*
33
Plugin Name: Page Views Count
44
Description: Show front end users all time views and views today on posts, pages, index pages and custom post types with the Page Views Count Plugin. Use the Page Views Count function to add page views to any content type or object created by your theme or plugins.
5-
Version: 2.7.0
5+
Version: 2.8.0
66
Requires at least: 5.6
7-
Tested up to: 6.2
7+
Tested up to: 6.2.2
88
Author: a3rev Software
99
Author URI: https://a3rev.com
1010
Text Domain: page-views-count
@@ -23,9 +23,12 @@
2323

2424
define( 'A3_PVC_KEY', 'a3_page_view_count' );
2525
define( 'A3_PVC_PREFIX', 'wp_pvc_' );
26-
define( 'A3_PVC_VERSION', '2.7.0' );
26+
define( 'A3_PVC_VERSION', '2.8.0' );
2727
define( 'A3_PVC_G_FONTS', false );
2828

29+
global $pvc_enable_ajax_load;
30+
$pvc_enable_ajax_load = apply_filters( 'pvc_enable_ajax_load', true );
31+
2932
use \A3Rev\PageViewsCount\FrameWork;
3033

3134
if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) {

readme.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Contributors: a3rev, a3rev Software, nguyencongtuan
33
Tags: wordpress page view, page view count , post views, post view count, gutenberg
44
Requires at least: 5.6
5-
Tested up to: 6.2
6-
Stable tag: 2.7.0
5+
Tested up to: 6.2.2
6+
Stable tag: 2.8.0
77
License: GPLv3
88
License URI: http://www.gnu.org/licenses/gpl-3.0.html
99

@@ -102,6 +102,17 @@ The manual installation method involves down loading our plugin and uploading it
102102

103103
== Changelog ==
104104

105+
= 2.8.0 - 2023/05/24 =
106+
* This release removes loading PVC Stats with PHP and replaces with REST API as default with Admin-ajax as the fallback.
107+
* Feature - Support options for choose WP REST API or Admin Ajax load PVC stats
108+
* Tweak - Remove load PVC stats by PHP
109+
* Tweak - On upgrade if using PHP to load PVC stats it will update to use WP REST API
110+
* Tweak - If WP REST API is not active it will fall back to ADMIN-AJAX
111+
* Tweak - Auto detect if WP REST API and PVC endpoint are disabled, it will fall back to the ADMIN-AJAX
112+
* Tweak - Add warning notification if WP REST API or PVC Endpoint are disabled.
113+
* Tweak - Updated setting and help text on the Page View Count Load option box.
114+
* Tweak - Test for compatibility with WordPress 6.2.2
115+
105116
= 2.7.0 - 2023/04/08 =
106117
* This release changes the daily views reset from fixed GMT = 00 to use the timezone that is set in the site's WordPress General Settings.
107118
* Feature - Use the timezone from WordPress General Settings as the reset time for daily views counter.
@@ -559,6 +570,9 @@ The manual installation method involves down loading our plugin and uploading it
559570

560571
== Upgrade Notice ==
561572

573+
= 2.8.0 =
574+
This release removes loading PVC Stats with PHP and replaces with REST API as default with Admin-ajax as the fallback.
575+
562576
= 2.7.0 =
563577
This release changes the daily views reset from fixed GMT = 00 to use the timezone that is set in the site's WordPress General Settings.
564578

src/api/pvc-api.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ class API
1818

1919
public function __construct() {
2020
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
21+
add_action( 'wp_ajax_pvc_increase', array( $this, 'pvc_increase_ajax' ) );
22+
add_action( 'wp_ajax_pvc_view', array( $this, 'pvc_view_ajax' ) );
23+
}
24+
25+
public function pvc_increase_ajax() {
26+
check_ajax_referer( 'pvc_stats_counter', 'security' );
27+
if ( ! empty( $_GET['ids'] ) ) {
28+
echo $this->pvc_increase( sanitize_text_field( $_GET['ids'] ) );
29+
}
30+
31+
die();
32+
}
33+
34+
public function pvc_view_ajax() {
35+
check_ajax_referer( 'pvc_stats_counter', 'security' );
36+
if ( ! empty( $_GET['ids'] ) ) {
37+
echo $this->pvc_view( sanitize_text_field( $_GET['ids'] ) );
38+
}
39+
40+
die();
2141
}
2242

2343
public function rest_api_init() {
@@ -76,7 +96,28 @@ public function increase_stats( \WP_REST_Request $request ) {
7696
) );
7797
}
7898

79-
$post_ids = explode( ',', $post_ids_text );
99+
return $this->pvc_increase( $post_ids_text );
100+
}
101+
102+
public function view_stats( \WP_REST_Request $request ) {
103+
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
104+
@ini_set( 'display_errors', false ); // Turn off display_errors to prevent malformed JSON.
105+
}
106+
107+
$post_ids_text = $request->get_param( 'post_ids' );
108+
109+
if ( '' == trim( $post_ids_text ) ) {
110+
return wp_send_json( array(
111+
'success' => false,
112+
'message' => __( "Post Ids is empty", 'page-views-count' ),
113+
) );
114+
}
115+
116+
return $this->pvc_view( $post_ids_text );
117+
}
118+
119+
public function pvc_increase( $post_ids ) {
120+
$post_ids = explode( ',', $post_ids );
80121

81122
if ( empty ( $post_ids ) ) {
82123
return wp_send_json( array(
@@ -103,21 +144,8 @@ public function increase_stats( \WP_REST_Request $request ) {
103144
return wp_send_json( $json_data );
104145
}
105146

106-
public function view_stats( \WP_REST_Request $request ) {
107-
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
108-
@ini_set( 'display_errors', false ); // Turn off display_errors to prevent malformed JSON.
109-
}
110-
111-
$post_ids_text = $request->get_param( 'post_ids' );
112-
113-
if ( '' == trim( $post_ids_text ) ) {
114-
return wp_send_json( array(
115-
'success' => false,
116-
'message' => __( "Post Ids is empty", 'page-views-count' ),
117-
) );
118-
}
119-
120-
$post_ids = explode( ',', $post_ids_text );
147+
public function pvc_view( $post_ids ) {
148+
$post_ids = explode( ',', $post_ids );
121149

122150
if ( empty ( $post_ids ) ) {
123151
return wp_send_json( array(

0 commit comments

Comments
 (0)