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