WordPress Popular Posts - Version 5.3.2

Version Description

  • wpp_get_views(): fixed an issue where the function would return 0 views under certain conditions (thanks to everyone who helped with this!)

Release notes

Download this release

Release Info

Developer hcabrera
Plugin Icon 128x128 WordPress Popular Posts
Version 5.3.2
Comparing to
See all releases

Code changes from version 5.3.1 to 5.3.2

Files changed (3) hide show
  1. readme.txt +10 -2
  2. src/template-tags.php +95 -45
  3. wordpress-popular-posts.php +2 -2
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: popular, posts, widget, popularity, top
5
  Requires at least: 4.9
6
  Tested up to: 5.7.1
7
  Requires PHP: 5.4
8
- Stable tag: 5.3.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -97,11 +97,19 @@ The FAQ section has been moved [here](https://github.com/cabrerahector/wordpress
97
 
98
  == Changelog ==
99
 
 
 
 
 
 
 
100
  = 5.3.1 =
101
 
102
- - wpp_get_views(): restores previous behavior where when no time range was set the function would return total views count (thanks @narolles!)
103
  - The WPP widget will now be loaded via AJAX by default (this affects new installs only.)
104
 
 
 
105
  = 5.3.0 =
106
 
107
  - Improves compatibility with PHP 8.
5
  Requires at least: 4.9
6
  Tested up to: 5.7.1
7
  Requires PHP: 5.4
8
+ Stable tag: 5.3.2
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
97
 
98
  == Changelog ==
99
 
100
+ = 5.3.2 =
101
+
102
+ - `wpp_get_views()`: fixed an issue where the function would return 0 views under certain conditions (thanks to everyone who helped with this!)
103
+
104
+ [Release notes](https://cabrerahector.com/wordpress/wordpress-popular-posts-5-3-improved-php-8-support-retina-display-support-and-more/#minor-updates-and-hotfixes)
105
+
106
  = 5.3.1 =
107
 
108
+ - `wpp_get_views()`: restores previous behavior where when no time range was set the function would return total views count (thanks @narolles!)
109
  - The WPP widget will now be loaded via AJAX by default (this affects new installs only.)
110
 
111
+ [Release notes](https://cabrerahector.com/wordpress/wordpress-popular-posts-5-3-improved-php-8-support-retina-display-support-and-more/#minor-updates-and-hotfixes)
112
+
113
  = 5.3.0 =
114
 
115
  - Improves compatibility with PHP 8.
src/template-tags.php CHANGED
@@ -19,69 +19,119 @@ function wpp_get_views($id = NULL, $range = NULL, $number_format = true)
19
  if ( empty($id) || is_null($id) || ! is_numeric($id) )
20
  return "-1";
21
 
 
 
 
22
  $args = [
23
  'range' => 'all',
 
 
24
  '_postID' => $id
25
  ];
26
 
27
  if (
28
  is_array($range)
29
- && isset($range['range'])
30
- && isset($range['time_unit'])
31
- && isset($range['time_quantity'])
32
  ) {
33
- $args['range'] = $range['range'];
34
- $args['time_unit'] = $range['time_unit'];
35
- $args['time_quantity'] = $range['time_quantity'];
36
  } else {
37
  $range = is_string($range) ? trim($range) : null;
38
  $args['range'] = ! $range ? 'all' : $range;
39
  }
40
 
41
- add_filter('wpp_query_fields', 'wpp_get_views_fields', 10, 2);
42
- add_filter('wpp_query_where', 'wpp_get_views_where', 10, 2);
43
- add_filter('wpp_query_group_by', 'wpp_get_views_group_by', 10, 2);
44
- add_filter('wpp_query_order_by', 'wpp_get_views_order_by', 10, 2);
45
- add_filter('wpp_query_limit', 'wpp_get_views_limit', 10, 2);
46
- $query = new \WordPressPopularPosts\Query($args);
47
- remove_filter('wpp_query_fields', 'wpp_get_views_fields', 10);
48
- remove_filter('wpp_query_where', 'wpp_get_views_where', 10);
49
- remove_filter('wpp_query_group_by', 'wpp_get_views_group_by', 10);
50
- remove_filter('wpp_query_order_by', 'wpp_get_views_order_by', 10);
51
- remove_filter('wpp_query_limit', 'wpp_get_views_limit', 10);
52
-
53
- $results = $query->get_posts();
54
-
55
- if ( empty($results) )
56
- return 0;
57
-
58
- return $number_format ? number_format_i18n(intval($results[0]->pageviews)) : $results[0]->pageviews;
59
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- function wpp_get_views_fields($fields, $options)
62
- {
63
- return 'IFNULL(v.pageviews, 0) AS pageviews';
64
- }
 
 
 
65
 
66
- function wpp_get_views_where($where, $options)
67
- {
68
- global $wpdb;
69
- return $wpdb->prepare($where . ' AND p.ID = %d ', $options['_postID']);
70
- }
71
 
72
- function wpp_get_views_group_by($groupby, $options)
73
- {
74
- return '';
75
- }
76
 
77
- function wpp_get_views_order_by($orderby, $options)
78
- {
79
- return '';
80
- }
81
 
82
- function wpp_get_views_limit($limit, $options)
83
- {
84
- return '';
85
  }
86
 
87
  /**
19
  if ( empty($id) || is_null($id) || ! is_numeric($id) )
20
  return "-1";
21
 
22
+ global $wpdb;
23
+ $table_name = $wpdb->prefix . "popularposts";
24
+
25
  $args = [
26
  'range' => 'all',
27
+ 'time_unit' => 'hour',
28
+ 'time_quantity' => 24,
29
  '_postID' => $id
30
  ];
31
 
32
  if (
33
  is_array($range)
 
 
 
34
  ) {
35
+ $args = \WordPressPopularPosts\Helper::merge_array_r($args, $range);
 
 
36
  } else {
37
  $range = is_string($range) ? trim($range) : null;
38
  $args['range'] = ! $range ? 'all' : $range;
39
  }
40
 
41
+ $args['range'] = strtolower($args['range']);
42
+
43
+ // Get all-time views count
44
+ if ( 'all' == $args['range'] ) {
45
+ $query = "SELECT pageviews FROM {$table_name}data WHERE postid = '{$id}'";
46
+ } // Get views count within time range
47
+ else {
48
+ $start_date = new \DateTime(
49
+ \WordPressPopularPosts\Helper::now(),
50
+ new \DateTimeZone(\WordPressPopularPosts\Helper::get_timezone())
51
+ );
52
+
53
+ // Determine time range
54
+ switch( $args['range'] ){
55
+ case "last24hours":
56
+ case "daily":
57
+ $start_date = $start_date->sub(new \DateInterval('P1D'));
58
+ $start_datetime = $start_date->format('Y-m-d H:i:s');
59
+ $views_time_range = "view_datetime >= '{$start_datetime}'";
60
+ break;
61
+ case "last7days":
62
+ case "weekly":
63
+ $start_date = $start_date->sub(new \DateInterval('P6D'));
64
+ $start_datetime = $start_date->format('Y-m-d');
65
+ $views_time_range = "view_date >= '{$start_datetime}'";
66
+ break;
67
+ case "last30days":
68
+ case "monthly":
69
+ $start_date = $start_date->sub(new \DateInterval('P29D'));
70
+ $start_datetime = $start_date->format('Y-m-d');
71
+ $views_time_range = "view_date >= '{$start_datetime}'";
72
+ break;
73
+ case "custom":
74
+ $time_units = ["MINUTE", "HOUR", "DAY", "WEEK", "MONTH"];
75
+
76
+ // Valid time unit
77
+ if (
78
+ isset($args['time_unit'])
79
+ && in_array(strtoupper($args['time_unit']), $time_units)
80
+ && isset($args['time_quantity'])
81
+ && filter_var($args['time_quantity'], FILTER_VALIDATE_INT)
82
+ && $args['time_quantity'] > 0
83
+ ) {
84
+ $time_quantity = $args['time_quantity'];
85
+ $time_unit = strtoupper($args['time_unit']);
86
+
87
+ if ( 'MINUTE' == $time_unit ) {
88
+ $start_date = $start_date->sub(new \DateInterval('PT' . (60 * $time_quantity) . 'S'));
89
+ $start_datetime = $start_date->format('Y-m-d H:i:s');
90
+ $views_time_range = "view_datetime >= '{$start_datetime}'";
91
+ } elseif ( 'HOUR' == $time_unit ) {
92
+ $start_date = $start_date->sub(new \DateInterval('PT' . ((60 * $time_quantity) - 1) . 'M59S'));
93
+ $start_datetime = $start_date->format('Y-m-d H:i:s');
94
+ $views_time_range = "view_datetime >= '{$start_datetime}'";
95
+ } elseif ( 'DAY' == $time_unit ) {
96
+ $start_date = $start_date->sub(new \DateInterval('P' . ($time_quantity - 1) . 'D'));
97
+ $start_datetime = $start_date->format('Y-m-d');
98
+ $views_time_range = "view_date >= '{$start_datetime}'";
99
+ } elseif ( 'WEEK' == $time_unit ) {
100
+ $start_date = $start_date->sub(new \DateInterval('P' . ((7 * $time_quantity) - 1) . 'D'));
101
+ $start_datetime = $start_date->format('Y-m-d');
102
+ $views_time_range = "view_date >= '{$start_datetime}'";
103
+ } else {
104
+ $start_date = $start_date->sub(new \DateInterval('P' . ((30 * $time_quantity) - 1) . 'D'));
105
+ $start_datetime = $start_date->format('Y-m-d');
106
+ $views_time_range = "view_date >= '{$start_datetime}'";
107
+ }
108
+ } // Invalid time unit, default to last 24 hours
109
+ else {
110
+ $start_date = $start_date->sub(new \DateInterval('P1D'));
111
+ $start_datetime = $start_date->format('Y-m-d H:i:s');
112
+ $views_time_range = "view_datetime >= '{$start_datetime}'";
113
+ }
114
 
115
+ break;
116
+ default:
117
+ $start_date = $start_date->sub(new \DateInterval('P1D'));
118
+ $start_datetime = $start_date->format('Y-m-d H:i:s');
119
+ $views_time_range = "view_datetime >= '{$start_datetime}'";
120
+ break;
121
+ }
122
 
123
+ $query = $wpdb->prepare(
124
+ "SELECT SUM(pageviews) AS pageviews FROM `{$wpdb->prefix}popularpostssummary` WHERE {$views_time_range} AND postid = %d;",
125
+ $args['_postID']
126
+ );
127
+ }
128
 
129
+ $results = $wpdb->get_var($query);
 
 
 
130
 
131
+ if ( ! $results )
132
+ return 0;
 
 
133
 
134
+ return $number_format ? number_format_i18n(intval($results)) : $results;
 
 
135
  }
136
 
137
  /**
wordpress-popular-posts.php CHANGED
@@ -16,7 +16,7 @@
16
  * Plugin Name: WordPress Popular Posts
17
  * Plugin URI: https://wordpress.org/plugins/wordpress-popular-posts/
18
  * Description: A highly customizable widget that displays the most popular posts on your blog.
19
- * Version: 5.3.1
20
  * Author: Hector Cabrera
21
  * Author URI: https://cabrerahector.com/
22
  * License: GPL-2.0+
@@ -29,7 +29,7 @@ if ( ! defined( 'WPINC' ) ) {
29
  die();
30
  }
31
 
32
- define('WPP_VERSION', '5.3.1');
33
  define('WPP_MIN_PHP_VERSION', '5.4');
34
  define('WPP_MIN_WP_VERSION', '4.9');
35
 
16
  * Plugin Name: WordPress Popular Posts
17
  * Plugin URI: https://wordpress.org/plugins/wordpress-popular-posts/
18
  * Description: A highly customizable widget that displays the most popular posts on your blog.
19
+ * Version: 5.3.2
20
  * Author: Hector Cabrera
21
  * Author URI: https://cabrerahector.com/
22
  * License: GPL-2.0+
29
  die();
30
  }
31
 
32
+ define('WPP_VERSION', '5.3.2');
33
  define('WPP_MIN_PHP_VERSION', '5.4');
34
  define('WPP_MIN_WP_VERSION', '4.9');
35