PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.4
Post Views Counter v1.2.4
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / post-views-counter.php
post-views-counter Last commit date
css 9 years ago images 9 years ago includes 9 years ago js 9 years ago languages 9 years ago index.php 9 years ago post-views-counter.php 9 years ago readme.txt 9 years ago
post-views-counter.php
396 lines
1 <?php
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.2.4
6 Author: dFactory
7 Author URI: http://www.dfactory.eu/
8 Plugin URI: http://www.dfactory.eu/plugins/post-views-counter/
9 License: MIT License
10 License URI: http://opensource.org/licenses/MIT
11 Text Domain: post-views-counter
12 Domain Path: /languages
13
14 Post Views Counter
15 Copyright (C) 2014-2016, Digital Factory - info@digitalfactory.pl
16
17 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 // exit if accessed directly
25 if ( ! defined( 'ABSPATH' ) )
26 exit;
27
28 if ( ! class_exists( 'Post_Views_Counter' ) ) :
29
30 /**
31 * Post Views Counter final class.
32 *
33 * @class Post_Views_Counter
34 * @version 1.2.4
35 */
36 final class Post_Views_Counter {
37
38 private static $instance;
39 public $options;
40 public $defaults = array(
41 'general' => array(
42 'post_types_count' => array( 'post' ),
43 'counter_mode' => 'php',
44 'post_views_column' => true,
45 'time_between_counts' => array(
46 'number' => 24,
47 'type' => 'hours'
48 ),
49 'reset_counts' => array(
50 'number' => 30,
51 'type' => 'days'
52 ),
53 'flush_interval' => array(
54 'number' => 0,
55 'type' => 'minutes'
56 ),
57 'exclude' => array(
58 'groups' => array(),
59 'roles' => array()
60 ),
61 'exclude_ips' => array(),
62 'restrict_edit_views' => false,
63 'deactivation_delete' => false,
64 'cron_run' => true,
65 'cron_update' => true
66 ),
67 'display' => array(
68 'label' => 'Post Views:',
69 'post_types_display' => array( 'post' ),
70 'page_types_display' => array( 'singular' ),
71 'restrict_display' => array(
72 'groups' => array(),
73 'roles' => array()
74 ),
75 'position' => 'after',
76 'display_style' => array(
77 'icon' => true,
78 'text' => true
79 ),
80 'link_to_post' => true,
81 'icon_class' => 'dashicons-chart-bar'
82 ),
83 'version' => '1.2.4'
84 );
85
86 /**
87 * Disable object clone.
88 */
89 private function __clone() {
90
91 }
92
93 /**
94 * Disable unserializing of the class.
95 */
96 private function __wakeup() {
97
98 }
99
100 /**
101 * Main plugin instance,
102 * Insures that only one instance of the plugin exists in memory at one time.
103 *
104 * @return object
105 */
106 public static function instance() {
107 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Post_Views_Counter ) ) {
108 self::$instance = new Post_Views_Counter;
109 self::$instance->define_constants();
110
111 add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );
112
113 self::$instance->includes();
114 self::$instance->update = new Post_Views_Counter_Update();
115 self::$instance->settings = new Post_Views_Counter_Settings();
116 self::$instance->query = new Post_Views_Counter_Query();
117 self::$instance->cron = new Post_Views_Counter_Cron();
118 self::$instance->counter = new Post_Views_Counter_Counter();
119 self::$instance->columns = new Post_Views_Counter_Columns();
120 self::$instance->crawler_detect = new Post_Views_Counter_Crawler_Detect();
121 self::$instance->frontend = new Post_Views_Counter_Frontend();
122 self::$instance->dashboard = new Post_Views_Counter_Dashboard();
123 self::$instance->widgets = new Post_Views_Counter_Widgets();
124 }
125 return self::$instance;
126 }
127
128 /**
129 * Setup plugin constants.
130 *
131 * @return void
132 */
133 private function define_constants() {
134 define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) );
135 define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) );
136 define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( plugin_basename( __FILE__ ) ) . '/' );
137 }
138
139 /**
140 * Include required files.
141 *
142 * @return void
143 */
144 private function includes() {
145 include_once( POST_VIEWS_COUNTER_PATH . 'includes/update.php' );
146 include_once( POST_VIEWS_COUNTER_PATH . 'includes/settings.php' );
147 include_once( POST_VIEWS_COUNTER_PATH . 'includes/columns.php' );
148 include_once( POST_VIEWS_COUNTER_PATH . 'includes/query.php' );
149 include_once( POST_VIEWS_COUNTER_PATH . 'includes/cron.php' );
150 include_once( POST_VIEWS_COUNTER_PATH . 'includes/counter.php' );
151 include_once( POST_VIEWS_COUNTER_PATH . 'includes/crawler-detect.php' );
152 include_once( POST_VIEWS_COUNTER_PATH . 'includes/frontend.php' );
153 include_once( POST_VIEWS_COUNTER_PATH . 'includes/dashboard.php' );
154 include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' );
155 }
156
157 /**
158 * Class constructor.
159 *
160 * @return void
161 */
162 public function __construct() {
163 register_activation_hook( __FILE__, array( $this, 'activation' ) );
164 register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
165
166 // settings
167 $this->options = array(
168 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ),
169 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) )
170 );
171
172 // actions
173 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) );
174 add_action( 'wp_loaded', array( $this, 'load_pluggable_functions' ), 10 );
175
176 // filters
177 add_filter( 'plugin_row_meta', array( $this, 'plugin_extend_links' ), 10, 2 );
178 add_filter( 'plugin_action_links', array( $this, 'plugin_settings_link' ), 10, 2 );
179 }
180
181 /**
182 * Plugin activation function.
183 */
184 public function activation() {
185 global $wpdb, $charset_collate;
186
187 // required for dbdelta
188 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
189
190 // create post views table
191 dbDelta( '
192 CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views (
193 id bigint unsigned NOT NULL,
194 type tinyint(1) unsigned NOT NULL,
195 period varchar(8) NOT NULL,
196 count bigint unsigned NOT NULL,
197 PRIMARY KEY (type, period, id),
198 UNIQUE INDEX id_period (id, period) USING BTREE,
199 INDEX type_period_count (type, period, count) USING BTREE
200 ) ' . $charset_collate . ';'
201 );
202
203 // add default options
204 add_option( 'post_views_counter_settings_general', $this->defaults['general'], '', 'no' );
205 add_option( 'post_views_counter_settings_display', $this->defaults['display'], '', 'no' );
206 add_option( 'post_views_counter_version', $this->defaults['version'], '', 'no' );
207
208 // schedule cache flush
209 $this->schedule_cache_flush();
210 }
211
212 /**
213 * Plugin deactivation function.
214 */
215 public function deactivation() {
216 // delete default options
217 if ( $this->options['general']['deactivation_delete'] ) {
218 delete_option( 'post_views_counter_settings_general' );
219 delete_option( 'post_views_counter_settings_display' );
220
221 global $wpdb;
222
223 // delete table from database
224 $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post_views' );
225 }
226
227 // remove schedule
228 wp_clear_scheduled_hook( 'pvc_reset_counts' );
229 remove_action( 'pvc_reset_counts', array( Post_Views_Counter()->cron, 'reset_counts' ) );
230
231 $this->remove_cache_flush();
232 }
233
234 /**
235 * Schedule cache flushing if it's not already scheduled.
236 *
237 * @param bool $forced
238 */
239 public function schedule_cache_flush( $forced = true ) {
240 if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) {
241 wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' );
242 }
243 }
244
245 /**
246 * Remove scheduled cache flush and the corresponding action.
247 */
248 public function remove_cache_flush() {
249 wp_clear_scheduled_hook( 'pvc_flush_cached_counts' );
250 remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->cron, 'flush_cached_counts' ) );
251 }
252
253 /**
254 * Load text domain.
255 */
256 public function load_textdomain() {
257 load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' );
258 }
259
260 /**
261 * Load pluggable template functions.
262 */
263 public function load_pluggable_functions() {
264 include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' );
265 }
266
267 /**
268 * Enqueue admin scripts and styles.
269 *
270 * @global string $post_type
271 * @param string $page
272 */
273 public function admin_scripts_styles( $page ) {
274 wp_register_style(
275 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css'
276 );
277
278 wp_register_script(
279 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array( 'jquery' ), $this->defaults['version']
280 );
281
282 wp_register_script(
283 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array( 'jquery' ), $this->defaults['version']
284 );
285
286 wp_register_script(
287 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array( 'jquery', 'inline-edit-post' ), $this->defaults['version']
288 );
289
290 // load on PVC settings page
291 if ( $page === 'settings_page_post-views-counter' ) {
292
293 wp_enqueue_script( 'pvc-admin-settings' );
294
295 wp_localize_script(
296 'pvc-admin-settings', 'pvcArgsSettings', array(
297 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' )
298 )
299 );
300
301 wp_enqueue_style( 'pvc-admin' );
302
303 // load on single post page
304 } elseif ( $page === 'post.php' || $page === 'post-new.php' ) {
305
306 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
307
308 global $post_type;
309
310 if ( ! in_array( $post_type, (array) $post_types ) )
311 return;
312
313 wp_enqueue_style( 'pvc-admin' );
314 wp_enqueue_script( 'pvc-admin-post' );
315 } elseif ( $page === 'edit.php' ) {
316 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
317
318 global $post_type;
319
320 if ( ! in_array( $post_type, (array) $post_types ) )
321 return;
322
323 wp_enqueue_style( 'pvc-admin' );
324 wp_enqueue_script( 'pvc-admin-quick-edit' );
325 }
326 }
327
328 /**
329 * Add links to plugin support forum.
330 *
331 * @param array $links
332 * @param string $file
333 * @return array
334 */
335 public function plugin_extend_links( $links, $file ) {
336
337 if ( ! current_user_can( 'install_plugins' ) )
338 return $links;
339
340 $plugin = plugin_basename( __FILE__ );
341
342 if ( $file == $plugin ) {
343 return array_merge(
344 $links, array( sprintf( '<a href="http://www.dfactory.eu/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) ) )
345 );
346 }
347
348 return $links;
349 }
350
351 /**
352 * Add link to settings page.
353 *
354 * @staticvar string $plugin
355 * @param array $links
356 * @param string $file
357 * @return array
358 */
359 public function plugin_settings_link( $links, $file ) {
360 if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
361 return $links;
362
363 static $plugin;
364
365 $plugin = plugin_basename( __FILE__ );
366
367 if ( $file == $plugin ) {
368 $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php' ) . '?page=post-views-counter', __( 'Settings', 'post-views-counter' ) );
369
370 array_unshift( $links, $settings_link );
371 }
372
373 return $links;
374 }
375
376 }
377
378 endif; // end if class_exists check
379
380 /**
381 * Initialise Post Views Counter.
382 *
383 * @return object
384 */
385 function Post_Views_Counter() {
386 static $instance;
387
388 // first call to instance() initializes the plugin
389 if ( $instance === null || ! ( $instance instanceof Post_Views_Counter ) )
390 $instance = Post_Views_Counter::instance();
391
392 return $instance;
393 }
394
395 Post_Views_Counter();
396