PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.4
Post Views Counter v1.1.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 10 years ago images 10 years ago includes 10 years ago js 10 years ago languages 10 years ago index.php 10 years ago post-views-counter.php 10 years ago readme.txt 10 years ago
post-views-counter.php
370 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.1.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-2015, 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.1.4
35 */
36 final class Post_Views_Counter {
37
38 private static $instance;
39 public $_cron;
40 public $_counter;
41 public $_settings;
42 public $options;
43 public $defaults = array(
44 'general' => array(
45 'post_types_count' => array( 'post' ),
46 'counter_mode' => 'php',
47 'post_views_column' => true,
48 'time_between_counts' => array(
49 'number' => 24,
50 'type' => 'hours'
51 ),
52 'reset_counts' => array(
53 'number' => 30,
54 'type' => 'days'
55 ),
56 'flush_interval' => array(
57 'number' => 0,
58 'type' => 'minutes'
59 ),
60 'exclude' => array(
61 'groups' => array(),
62 'roles' => array()
63 ),
64 'exclude_ips' => array(),
65 'restrict_edit_views' => false,
66 'deactivation_delete' => false,
67 'cron_run' => true,
68 'cron_update' => true
69 ),
70 'display' => array(
71 'label' => 'Post Views:',
72 'post_types_display' => array( 'post' ),
73 'restrict_display' => array(
74 'groups' => array(),
75 'roles' => array()
76 ),
77 'position' => 'after',
78 'display_style' => array(
79 'icon' => true,
80 'text' => true
81 ),
82 'link_to_post' => true,
83 'icon_class' => 'dashicons-chart-bar'
84 ),
85 'version' => '1.1.4'
86 );
87
88 /**
89 * Disable object clone.
90 */
91 private function __clone() {}
92
93 /**
94 * Disable unserializing of the class.
95 */
96 private function __wakeup() {}
97
98 /**
99 * Main Post_Views_Counter instance,
100 * Insures that only one instance of Post_Views_Counter exists in memory at one time.
101 *
102 * @return object
103 */
104 public static function instance() {
105 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Post_Views_Counter ) ) {
106 self::$instance = new Post_Views_Counter;
107 self::$instance->define_constants();
108
109 add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );
110
111 self::$instance->includes();
112 self::$instance->update = new Post_Views_Counter_Update();
113 self::$instance->settings = new Post_Views_Counter_Settings();
114 self::$instance->query = new Post_Views_Counter_Query();
115 self::$instance->cron = new Post_Views_Counter_Cron();
116 self::$instance->counter = new Post_Views_Counter_Counter();
117 self::$instance->columns = new Post_Views_Counter_Columns();
118 self::$instance->frontend = new Post_Views_Counter_Frontend();
119 self::$instance->widgets = new Post_Views_Counter_Widgets();
120 }
121 return self::$instance;
122 }
123
124 /**
125 * Setup plugin constants.
126 *
127 * @return void
128 */
129 private function define_constants() {
130 define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) );
131 define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) );
132 define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( plugin_basename( __FILE__ ) ) . '/' );
133 }
134
135 /**
136 * Include required files
137 *
138 * @return void
139 */
140 private function includes() {
141 include_once( POST_VIEWS_COUNTER_PATH . 'includes/update.php' );
142 include_once( POST_VIEWS_COUNTER_PATH . 'includes/settings.php' );
143 include_once( POST_VIEWS_COUNTER_PATH . 'includes/columns.php' );
144 include_once( POST_VIEWS_COUNTER_PATH . 'includes/query.php' );
145 include_once( POST_VIEWS_COUNTER_PATH . 'includes/cron.php' );
146 include_once( POST_VIEWS_COUNTER_PATH . 'includes/counter.php' );
147 include_once( POST_VIEWS_COUNTER_PATH . 'includes/frontend.php' );
148 include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' );
149 }
150
151 /**
152 * Class constructor.
153 *
154 * @return void
155 */
156 public function __construct() {
157 register_activation_hook( __FILE__, array( $this, 'activation' ) );
158 register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) );
159
160 // settings
161 $this->options = array(
162 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ),
163 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) )
164 );
165
166 // actions
167 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) );
168 add_action( 'wp_loaded', array( $this, 'load_pluggable_functions' ), 10 );
169
170 // filters
171 add_filter( 'plugin_row_meta', array( $this, 'plugin_extend_links' ), 10, 2 );
172 add_filter( 'plugin_action_links', array( $this, 'plugin_settings_link' ), 10, 2 );
173 }
174
175 /**
176 * Plugin activation function.
177 */
178 public function activation() {
179 global $wpdb, $charset_collate;
180
181 // required for dbdelta
182 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
183
184 // create post views table
185 dbDelta( '
186 CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views (
187 id bigint unsigned NOT NULL,
188 type tinyint(1) unsigned NOT NULL,
189 period varchar(8) NOT NULL,
190 count bigint unsigned NOT NULL,
191 PRIMARY KEY (type, period, id),
192 UNIQUE INDEX id_period (id, period) USING BTREE,
193 INDEX type_period_count (type, period, count) USING BTREE
194 ) ' . $charset_collate . ';'
195 );
196
197 // add default options
198 add_option( 'post_views_counter_settings_general', $this->defaults['general'], '', 'no' );
199 add_option( 'post_views_counter_settings_display', $this->defaults['display'], '', 'no' );
200 add_option( 'post_views_counter_version', $this->defaults['version'], '', 'no' );
201
202 // schedule cache flush
203 $this->schedule_cache_flush();
204 }
205
206 /**
207 * Plugin deactivation function.
208 */
209 public function deactivation() {
210 // delete default options
211 if ( $this->options['general']['deactivation_delete'] ) {
212 delete_option( 'post_views_counter_settings_general' );
213 delete_option( 'post_views_counter_settings_display' );
214 }
215
216 // remove schedule
217 wp_clear_scheduled_hook( 'pvc_reset_counts' );
218 remove_action( 'pvc_reset_counts', array( Post_Views_Counter()->_cron, 'reset_counts' ) );
219
220 $this->remove_cache_flush();
221 }
222
223 /**
224 * Schedule cache flushing if it's not already scheduled.
225 */
226 public function schedule_cache_flush( $forced = true ) {
227 if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) {
228 wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' );
229 }
230 }
231
232 /**
233 * Remove scheduled cache flush and the corresponding action.
234 */
235 public function remove_cache_flush() {
236 wp_clear_scheduled_hook( 'pvc_flush_cached_counts' );
237 remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->_cron, 'flush_cached_counts' ) );
238 }
239
240 /**
241 * Load text domain.
242 */
243 public function load_textdomain() {
244 load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' );
245 }
246
247 /**
248 * Load pluggable template functions.
249 */
250 public function load_pluggable_functions() {
251 include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' );
252 }
253
254 /**
255 * Enqueue admin scripts and styles.
256 */
257 public function admin_scripts_styles( $page ) {
258 wp_register_style(
259 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css'
260 );
261
262 wp_register_script(
263 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array( 'jquery' ), $this->defaults['version']
264 );
265
266 wp_register_script(
267 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array( 'jquery' ), $this->defaults['version']
268 );
269
270 wp_register_script(
271 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array( 'jquery', 'inline-edit-post' ), $this->defaults['version']
272 );
273
274 // load on PVC settings page
275 if ( $page === 'settings_page_post-views-counter' ) {
276
277 wp_enqueue_script( 'pvc-admin-settings' );
278
279 wp_localize_script(
280 'pvc-admin-settings', 'pvcArgsSettings', array(
281 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' )
282 )
283 );
284
285 wp_enqueue_style( 'pvc-admin' );
286
287 // load on single post page
288 } elseif ( $page === 'post.php' || $page === 'post-new.php' ) {
289
290 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
291
292 global $post_type;
293
294 if ( ! in_array( $post_type, (array) $post_types ) )
295 return;
296
297 wp_enqueue_style( 'pvc-admin' );
298 wp_enqueue_script( 'pvc-admin-post' );
299 } elseif ( $page === 'edit.php' ) {
300 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
301
302 global $post_type;
303
304 if ( ! in_array( $post_type, (array) $post_types ) )
305 return;
306
307 wp_enqueue_style( 'pvc-admin' );
308 wp_enqueue_script( 'pvc-admin-quick-edit' );
309 }
310 }
311
312 /**
313 * Add links to plugin support forum.
314 */
315 public function plugin_extend_links( $links, $file ) {
316
317 if ( ! current_user_can( 'install_plugins' ) )
318 return $links;
319
320 $plugin = plugin_basename( __FILE__ );
321
322 if ( $file == $plugin ) {
323 return array_merge(
324 $links, array( sprintf( '<a href="http://www.dfactory.eu/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) ) )
325 );
326 }
327
328 return $links;
329 }
330
331 /**
332 * Add link to settings page.
333 */
334 public function plugin_settings_link( $links, $file ) {
335 if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
336 return $links;
337
338 static $plugin;
339
340 $plugin = plugin_basename( __FILE__ );
341
342 if ( $file == $plugin ) {
343 $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php' ) . '?page=post-views-counter', __( 'Settings', 'post-views-counter' ) );
344
345 array_unshift( $links, $settings_link );
346 }
347
348 return $links;
349 }
350
351 }
352
353 endif; // end if class_exists check
354
355 /**
356 * Initialise Post Views Counter.
357 *
358 * @return object
359 */
360 function Post_Views_Counter() {
361 static $instance;
362
363 // first call to instance() initializes the plugin
364 if ( $instance === null || ! ( $instance instanceof Post_Views_Counter ) )
365 $instance = Post_Views_Counter::instance();
366
367 return $instance;
368 }
369
370 Post_Views_Counter();