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