Post Views Counter - Version 1.1.1

Version Description

  • Tweak: Enable edit views on new post.
  • Tweak: Extend WP_Query post data with post_views
Download this release

Release Info

Developer dfactory
Plugin Icon 128x128 Post Views Counter
Version 1.1.1
Comparing to
See all releases

Code changes from version 1.1.0 to 1.1.1

includes/counter.php CHANGED
@@ -30,6 +30,8 @@ class Post_Views_Counter_Counter {
30
 
31
  /**
32
  * Remove post views from database when post is deleted.
 
 
33
  */
34
  public function delete_post_views( $post_id ) {
35
  global $wpdb;
@@ -39,6 +41,9 @@ class Post_Views_Counter_Counter {
39
 
40
  /**
41
  * Check whether user has excluded roles.
 
 
 
42
  */
43
  public function is_user_roles_excluded( $option ) {
44
  $user = wp_get_current_user();
@@ -59,7 +64,13 @@ class Post_Views_Counter_Counter {
59
  }
60
 
61
  /**
 
62
  * Get timestamp convertion.
 
 
 
 
 
63
  */
64
  public function get_timestamp( $type, $number, $timestamp = true ) {
65
  $converter = array(
@@ -241,6 +252,10 @@ class Post_Views_Counter_Counter {
241
 
242
  /**
243
  * Save cookie function.
 
 
 
 
244
  */
245
  private function save_cookie( $id, $cookie = array(), $expired = true ) {
246
  $expiration = $this->get_timestamp( Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'type' ), Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'number' ) );
@@ -317,6 +332,9 @@ class Post_Views_Counter_Counter {
317
 
318
  /**
319
  * Check if object cache is in use.
 
 
 
320
  */
321
  public function using_object_cache( $using = null ) {
322
  $using = wp_using_ext_object_cache( $using );
@@ -332,6 +350,10 @@ class Post_Views_Counter_Counter {
332
 
333
  /**
334
  * Count visit function.
 
 
 
 
335
  */
336
  private function count_visit( $id ) {
337
  global $wpdb;
@@ -382,6 +404,8 @@ class Post_Views_Counter_Counter {
382
  * A single key is `62053.0.20150327` and that key's data is: $id = 62053, $type = 0, $period = 20150327
383
  *
384
  * This data format proved more efficient (avoids the (un)serialization overhead completely + duplicates filtering is a string search now)
 
 
385
  */
386
  private function update_cached_keys_list_if_needed( $key_names = array() ) {
387
  $existing_list = wp_cache_get( self::NAME_ALLKEYS, self::GROUP );
@@ -416,6 +440,9 @@ class Post_Views_Counter_Counter {
416
  /**
417
  * Flush views data stored in the persistent object cache into
418
  * our custom table and clear the object cache keys when done
 
 
 
419
  */
420
  public function flush_cache_to_db() {
421
  global $wpdb;
@@ -450,8 +477,15 @@ class Post_Views_Counter_Counter {
450
  return true;
451
  }
452
 
453
- /*
454
  * Insert or update views count.
 
 
 
 
 
 
 
455
  */
456
  private function db_insert( $id, $type, $period, $count = 1 ) {
457
  global $wpdb;
30
 
31
  /**
32
  * Remove post views from database when post is deleted.
33
+ *
34
+ * @param int $post_id
35
  */
36
  public function delete_post_views( $post_id ) {
37
  global $wpdb;
41
 
42
  /**
43
  * Check whether user has excluded roles.
44
+ *
45
+ * @param string $option
46
+ * @return bool
47
  */
48
  public function is_user_roles_excluded( $option ) {
49
  $user = wp_get_current_user();
64
  }
65
 
66
  /**
67
+ *
68
  * Get timestamp convertion.
69
+ *
70
+ * @param string $type
71
+ * @param int $number
72
+ * @param int $timestamp
73
+ * @return string
74
  */
75
  public function get_timestamp( $type, $number, $timestamp = true ) {
76
  $converter = array(
252
 
253
  /**
254
  * Save cookie function.
255
+ *
256
+ * @param int $id
257
+ * @param array $cookie
258
+ * @param bool $expired
259
  */
260
  private function save_cookie( $id, $cookie = array(), $expired = true ) {
261
  $expiration = $this->get_timestamp( Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'type' ), Post_Views_Counter()->get_attribute( 'options', 'general', 'time_between_counts', 'number' ) );
332
 
333
  /**
334
  * Check if object cache is in use.
335
+ *
336
+ * @param bool $using
337
+ * @return bool
338
  */
339
  public function using_object_cache( $using = null ) {
340
  $using = wp_using_ext_object_cache( $using );
350
 
351
  /**
352
  * Count visit function.
353
+ *
354
+ * @global object $wpdb
355
+ * @param int $id
356
+ * @return bool
357
  */
358
  private function count_visit( $id ) {
359
  global $wpdb;
404
  * A single key is `62053.0.20150327` and that key's data is: $id = 62053, $type = 0, $period = 20150327
405
  *
406
  * This data format proved more efficient (avoids the (un)serialization overhead completely + duplicates filtering is a string search now)
407
+ *
408
+ * @param array $key_names
409
  */
410
  private function update_cached_keys_list_if_needed( $key_names = array() ) {
411
  $existing_list = wp_cache_get( self::NAME_ALLKEYS, self::GROUP );
440
  /**
441
  * Flush views data stored in the persistent object cache into
442
  * our custom table and clear the object cache keys when done
443
+ *
444
+ * @global object $wpdb
445
+ * @return bool
446
  */
447
  public function flush_cache_to_db() {
448
  global $wpdb;
477
  return true;
478
  }
479
 
480
+ /**
481
  * Insert or update views count.
482
+ *
483
+ * @global object $wpdb
484
+ * @param int $id
485
+ * @param string $type
486
+ * @param string $period
487
+ * @param int $count
488
+ * @return bool
489
  */
490
  private function db_insert( $id, $type, $period, $count = 1 ) {
491
  global $wpdb;
includes/cron.php CHANGED
@@ -21,6 +21,8 @@ class Post_Views_Counter_Cron {
21
 
22
  /**
23
  * Reset daily counts.
 
 
24
  */
25
  public function reset_counts() {
26
  global $wpdb;
@@ -42,6 +44,9 @@ class Post_Views_Counter_Cron {
42
 
43
  /**
44
  * Add new cron interval from settings.
 
 
 
45
  */
46
  public function cron_time_intervals( $schedules ) {
47
  $schedules['post_views_counter_interval'] = array(
21
 
22
  /**
23
  * Reset daily counts.
24
+ *
25
+ * @global object $wpdb
26
  */
27
  public function reset_counts() {
28
  global $wpdb;
44
 
45
  /**
46
  * Add new cron interval from settings.
47
+ *
48
+ * @param array $schedules
49
+ * @return array
50
  */
51
  public function cron_time_intervals( $schedules ) {
52
  $schedules['post_views_counter_interval'] = array(
includes/frontend.php CHANGED
@@ -8,7 +8,7 @@ class Post_Views_Counter_Frontend {
8
 
9
  public function __construct() {
10
  // actions
11
- add_action( 'wp_loaded', array( &$this, 'register_shortcode' ) );
12
  add_action( 'wp_enqueue_scripts', array( &$this, 'frontend_scripts_styles' ) );
13
 
14
  // filters
@@ -25,6 +25,9 @@ class Post_Views_Counter_Frontend {
25
 
26
  /**
27
  * Post views shortcode function.
 
 
 
28
  */
29
  public function post_views_shortcode( $args ) {
30
  $defaults = array(
@@ -38,6 +41,9 @@ class Post_Views_Counter_Frontend {
38
 
39
  /**
40
  * Add post views counter to content.
 
 
 
41
  */
42
  public function add_post_views_count( $content ) {
43
  if ( is_singular() && in_array( get_post_type(), Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ), true ) ) {
@@ -76,6 +82,9 @@ class Post_Views_Counter_Frontend {
76
 
77
  /**
78
  * Remove post views shortcode from excerpt.
 
 
 
79
  */
80
  public function remove_post_views_count( $excerpt ) {
81
  remove_shortcode( 'post-views' );
8
 
9
  public function __construct() {
10
  // actions
11
+ add_action( 'after_setup_theme', array( &$this, 'register_shortcode' ) );
12
  add_action( 'wp_enqueue_scripts', array( &$this, 'frontend_scripts_styles' ) );
13
 
14
  // filters
25
 
26
  /**
27
  * Post views shortcode function.
28
+ *
29
+ * @param array $args
30
+ * @return mixed
31
  */
32
  public function post_views_shortcode( $args ) {
33
  $defaults = array(
41
 
42
  /**
43
  * Add post views counter to content.
44
+ *
45
+ * @param mixed $content
46
+ * @return mixed
47
  */
48
  public function add_post_views_count( $content ) {
49
  if ( is_singular() && in_array( get_post_type(), Post_Views_Counter()->get_attribute( 'options', 'display', 'post_types_display' ), true ) ) {
82
 
83
  /**
84
  * Remove post views shortcode from excerpt.
85
+ *
86
+ * @param mixed $excerpt
87
+ * @return mixed
88
  */
89
  public function remove_post_views_count( $excerpt ) {
90
  remove_shortcode( 'post-views' );
includes/functions.php CHANGED
@@ -97,10 +97,15 @@ if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
97
 
98
  $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
99
 
100
- // forces to use filters and post views as order
101
  $args['suppress_filters'] = false;
 
 
102
  $args['orderby'] = 'post_views';
103
 
 
 
 
104
  return get_posts( $args );
105
  }
106
 
97
 
98
  $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
99
 
100
+ // force to use filters
101
  $args['suppress_filters'] = false;
102
+
103
+ // force to use post views as order
104
  $args['orderby'] = 'post_views';
105
 
106
+ // force to get all fields
107
+ $args['fields'] = '';
108
+
109
  return get_posts( $args );
110
  }
111
 
includes/query.php CHANGED
@@ -8,16 +8,19 @@ class Post_Views_Counter_Query {
8
 
9
  public function __construct() {
10
  // actions
11
- add_action( 'pre_get_posts', array( &$this, 'extend_pre_query' ), 9 );
12
 
13
  // filters
14
- add_filter( 'posts_join', array( &$this, 'posts_join' ), 10, 2 );
15
- add_filter( 'posts_groupby', array( &$this, 'posts_groupby' ), 10, 2 );
16
- add_filter( 'posts_orderby', array( &$this, 'posts_orderby' ), 10, 2 );
 
17
  }
18
 
19
  /**
20
  * Extend query with post_views orderby parameter.
 
 
21
  */
22
  public function extend_pre_query( $query ) {
23
  if ( isset( $query->query_vars['orderby'] ) && $query->query_vars['orderby'] === 'post_views' )
@@ -26,38 +29,58 @@ class Post_Views_Counter_Query {
26
 
27
  /**
28
  * Modify the db query to use post_views parameter.
 
 
 
 
 
29
  */
30
  public function posts_join( $join, $query ) {
31
- global $wpdb;
32
-
33
  // is it sorted by post views?
34
- if ( isset( $query->order_by_post_views ) && $query->order_by_post_views )
 
 
35
  $join .= " LEFT JOIN " . $wpdb->prefix . "post_views pvc ON pvc.id = " . $wpdb->prefix . "posts.ID AND pvc.type = 4";
 
36
 
37
  return $join;
38
  }
39
 
40
  /**
41
  * Group posts using the post ID.
 
 
 
 
 
42
  */
43
  public function posts_groupby( $groupby, $query ) {
44
- global $wpdb;
45
-
46
  // is it sorted by post views?
47
- if ( isset( $query->order_by_post_views ) && $query->order_by_post_views )
48
- $groupby = (trim( $groupby ) !== '' ? $groupby . ', ' : '') . $wpdb->prefix . 'posts.ID';
 
 
 
 
 
 
49
 
50
  return $groupby;
51
  }
52
 
53
  /**
54
  * Order posts by post views.
 
 
 
 
 
55
  */
56
  public function posts_orderby( $orderby, $query ) {
57
- global $wpdb;
58
-
59
  // is it sorted by post views?
60
  if ( isset( $query->order_by_post_views ) && $query->order_by_post_views ) {
 
 
61
  $order = $query->get( 'order' );
62
  $orderby = 'pvc.count ' . $order . ', ' . $wpdb->prefix . 'posts.ID ' . $order;
63
  }
@@ -65,4 +88,22 @@ class Post_Views_Counter_Query {
65
  return $orderby;
66
  }
67
 
68
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  public function __construct() {
10
  // actions
11
+ add_action( 'pre_get_posts', array( $this, 'extend_pre_query' ), 9 );
12
 
13
  // filters
14
+ add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 );
15
+ add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 2 );
16
+ add_filter( 'posts_orderby', array( $this, 'posts_orderby' ), 10, 2 );
17
+ add_filter( 'posts_fields', array( $this, 'posts_fields' ), 10, 2 );
18
  }
19
 
20
  /**
21
  * Extend query with post_views orderby parameter.
22
+ *
23
+ * @param object $query
24
  */
25
  public function extend_pre_query( $query ) {
26
  if ( isset( $query->query_vars['orderby'] ) && $query->query_vars['orderby'] === 'post_views' )
29
 
30
  /**
31
  * Modify the db query to use post_views parameter.
32
+ *
33
+ * @global object $wpdb
34
+ * @param string $join
35
+ * @param object $query
36
+ * @return string
37
  */
38
  public function posts_join( $join, $query ) {
 
 
39
  // is it sorted by post views?
40
+ if ( ( isset( $query->order_by_post_views ) && $query->order_by_post_views ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true ) {
41
+ global $wpdb;
42
+
43
  $join .= " LEFT JOIN " . $wpdb->prefix . "post_views pvc ON pvc.id = " . $wpdb->prefix . "posts.ID AND pvc.type = 4";
44
+ }
45
 
46
  return $join;
47
  }
48
 
49
  /**
50
  * Group posts using the post ID.
51
+ *
52
+ * @global object $wpdb
53
+ * @param string $groupby
54
+ * @param object $query
55
+ * @return string
56
  */
57
  public function posts_groupby( $groupby, $query ) {
 
 
58
  // is it sorted by post views?
59
+ if ( ( isset( $query->order_by_post_views ) && $query->order_by_post_views ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true ) {
60
+ global $wpdb;
61
+
62
+ $groupby = trim( $groupby );
63
+
64
+ if ( strpos( $groupby, $wpdb->prefix . 'posts.ID' ) === false )
65
+ $groupby = ( $groupby !== '' ? $groupby . ', ' : '') . $wpdb->prefix . 'posts.ID';
66
+ }
67
 
68
  return $groupby;
69
  }
70
 
71
  /**
72
  * Order posts by post views.
73
+ *
74
+ * @global object $wpdb
75
+ * @param string $orderby
76
+ * @param object $query
77
+ * @return string
78
  */
79
  public function posts_orderby( $orderby, $query ) {
 
 
80
  // is it sorted by post views?
81
  if ( isset( $query->order_by_post_views ) && $query->order_by_post_views ) {
82
+ global $wpdb;
83
+
84
  $order = $query->get( 'order' );
85
  $orderby = 'pvc.count ' . $order . ', ' . $wpdb->prefix . 'posts.ID ' . $order;
86
  }
88
  return $orderby;
89
  }
90
 
91
+ /**
92
+ * Return post views in queried post objects.
93
+ *
94
+ * @global object $wpdb
95
+ * @param string $fields
96
+ * @param object $query
97
+ * @return string
98
+ */
99
+ public function posts_fields( $fields, $query ) {
100
+ // is it sorted by post views?
101
+ if ( ( isset( $query->order_by_post_views ) && $query->order_by_post_views ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true ) {
102
+ global $wpdb;
103
+
104
+ $fields = $wpdb->prefix . 'posts.ID, IFNULL( pvc.count, 0 ) AS post_views';
105
+ }
106
+
107
+ return $fields;
108
+ }
109
+ }
includes/settings.php CHANGED
@@ -529,9 +529,11 @@ class Post_Views_Counter_Settings {
529
  public function validate_settings( $input ) {
530
  if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
531
  global $wpdb;
 
 
532
 
533
  $views = $wpdb->get_results(
534
- "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = 'views'", ARRAY_A, 0
535
  );
536
 
537
  if ( ! empty( $views ) ) {
@@ -546,9 +548,9 @@ class Post_Views_Counter_Settings {
546
 
547
  $wpdb->query( "INSERT INTO " . $wpdb->prefix . "post_views(id, type, period, count) VALUES " . implode( ',', $sql ) . " ON DUPLICATE KEY UPDATE count = " . (isset( $_POST['post_views_counter_import_wp_postviews_override'] ) ? '' : 'count + ') . "VALUES(count)" );
548
 
549
- add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'WP-PostViews data imported succesfully.', 'post-views-counter' ), 'updated' );
550
  } else {
551
- add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no data to import.', 'post-views-counter' ), 'updated' );
552
  }
553
  } elseif ( isset( $_POST['save_pvc_general'] ) ) {
554
  // post types count
529
  public function validate_settings( $input ) {
530
  if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
531
  global $wpdb;
532
+
533
+ $meta_key = esc_attr( apply_filters( 'pvc_import_meta_key', 'views' ) );
534
 
535
  $views = $wpdb->get_results(
536
+ "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key . "'", ARRAY_A, 0
537
  );
538
 
539
  if ( ! empty( $views ) ) {
548
 
549
  $wpdb->query( "INSERT INTO " . $wpdb->prefix . "post_views(id, type, period, count) VALUES " . implode( ',', $sql ) . " ON DUPLICATE KEY UPDATE count = " . (isset( $_POST['post_views_counter_import_wp_postviews_override'] ) ? '' : 'count + ') . "VALUES(count)" );
550
 
551
+ add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported succesfully.', 'post-views-counter' ), 'updated' );
552
  } else {
553
+ add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
554
  }
555
  } elseif ( isset( $_POST['save_pvc_general'] ) ) {
556
  // post types count
post-views-counter.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  Plugin Name: Post Views Counter
4
  Description: Forget WP-PostViews. Display how many times a post, page or custom post type had been viewed in a simple, fast and reliable way.
5
- Version: 1.1.0
6
  Author: dFactory
7
  Author URI: http://www.dfactory.eu/
8
  Plugin URI: http://www.dfactory.eu/plugins/post-views-counter/
@@ -42,7 +42,7 @@ include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' );
42
  * Post Views Counter class
43
  *
44
  * @class Post_Views_Counter
45
- * @version 1.1.0
46
  */
47
  class Post_Views_Counter {
48
 
@@ -91,7 +91,7 @@ class Post_Views_Counter {
91
  'link_to_post' => true,
92
  'icon_class' => 'dashicons-chart-bar'
93
  ),
94
- 'version' => '1.1.0'
95
  );
96
 
97
  public static function instance() {
@@ -279,19 +279,23 @@ class Post_Views_Counter {
279
  wp_enqueue_style( 'pvc-admin' );
280
 
281
  // load on single post page
282
- } elseif ( $page === 'post.php' ) {
283
 
284
  $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
285
 
286
- if ( ! in_array( get_post_type(), (array) $post_types ) )
 
 
287
  return;
288
 
289
  wp_enqueue_style( 'pvc-admin' );
290
  wp_enqueue_script( 'pvc-admin-post' );
291
  } elseif ( $page === 'edit.php' ) {
292
  $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
 
 
293
 
294
- if ( ! in_array( get_post_type(), (array) $post_types ) )
295
  return;
296
 
297
  wp_enqueue_style( 'pvc-admin' );
2
  /*
3
  Plugin Name: Post Views Counter
4
  Description: Forget WP-PostViews. Display how many times a post, page or custom post type had been viewed in a simple, fast and reliable way.
5
+ Version: 1.1.1
6
  Author: dFactory
7
  Author URI: http://www.dfactory.eu/
8
  Plugin URI: http://www.dfactory.eu/plugins/post-views-counter/
42
  * Post Views Counter class
43
  *
44
  * @class Post_Views_Counter
45
+ * @version 1.1.1
46
  */
47
  class Post_Views_Counter {
48
 
91
  'link_to_post' => true,
92
  'icon_class' => 'dashicons-chart-bar'
93
  ),
94
+ 'version' => '1.1.1'
95
  );
96
 
97
  public static function instance() {
279
  wp_enqueue_style( 'pvc-admin' );
280
 
281
  // load on single post page
282
+ } elseif ( $page === 'post.php' || $page === 'post-new.php' ) {
283
 
284
  $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
285
 
286
+ global $post_type;
287
+
288
+ if ( ! in_array( $post_type, (array) $post_types ) )
289
  return;
290
 
291
  wp_enqueue_style( 'pvc-admin' );
292
  wp_enqueue_script( 'pvc-admin-post' );
293
  } elseif ( $page === 'edit.php' ) {
294
  $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
295
+
296
+ global $post_type;
297
 
298
+ if ( ! in_array( $post_type, (array) $post_types ) )
299
  return;
300
 
301
  wp_enqueue_style( 'pvc-admin' );
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://www.dfactory.eu/
4
  Tags: counter, hits, postviews, post views, views, count
5
  Requires at least: 4.0.0
6
  Tested up to: 4.3.1
7
- Stable tag: 1.1.0
8
  License: MIT License
9
  License URI: http://opensource.org/licenses/MIT
10
 
@@ -67,6 +67,10 @@ No questions yet.
67
 
68
  == Changelog ==
69
 
 
 
 
 
70
  = 1.1.0 =
71
  * New: Quick post views edit
72
  * New: Bulk post views edit
@@ -114,7 +118,6 @@ Initial release
114
 
115
  == Upgrade Notice ==
116
 
117
- = 1.1.0 =
118
- * New: Quick post views edit
119
- * New: Bulk post views edit
120
- * Tweak: Admin UI improvements
4
  Tags: counter, hits, postviews, post views, views, count
5
  Requires at least: 4.0.0
6
  Tested up to: 4.3.1
7
+ Stable tag: 1.1.1
8
  License: MIT License
9
  License URI: http://opensource.org/licenses/MIT
10
 
67
 
68
  == Changelog ==
69
 
70
+ = 1.1.1 =
71
+ * Tweak: Enable edit views on new post.
72
+ * Tweak: Extend WP_Query post data with post_views
73
+
74
  = 1.1.0 =
75
  * New: Quick post views edit
76
  * New: Bulk post views edit
118
 
119
  == Upgrade Notice ==
120
 
121
+ = 1.1.1 =
122
+ * Tweak: Enable edit views on new post.
123
+ * Tweak: Extend WP_Query post data with post_views