PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.12
Post Views Counter v1.3.12
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 / includes / class-settings.php
post-views-counter / includes Last commit date
class-admin.php 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-settings.php
898 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Settings class.
8 *
9 * @class Post_Views_Counter_Settings
10 */
11 class Post_Views_Counter_Settings {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'admin_init', [ $this, 'update_counter_mode' ] );
21
22 // filters
23 add_filter( 'post_views_counter_settings_data', [ $this, 'settings_data' ] );
24 add_filter( 'post_views_counter_settings_pages', [ $this, 'settings_page' ] );
25 }
26
27 /**
28 * Update counter mode.
29 *
30 * @return void
31 */
32 public function update_counter_mode() {
33 // get main instance
34 $pvc = Post_Views_Counter();
35
36 // Fast AJAX as active but not available counter mode?
37 if ( $pvc->options['general']['counter_mode'] === 'ajax' && ! array_key_exists( 'ajax', $this->get_counter_modes() ) ) {
38 // set standard javascript ajax calls
39 $pvc->options['general']['counter_mode'] = 'js';
40
41 // update database options
42 update_option( 'post_views_counter_settings_general', $pvc->options['general'] );
43 }
44 }
45
46 /**
47 * Get available counter modes.
48 *
49 * @return array
50 */
51 public function get_counter_modes() {
52 // counter modes
53 $modes = [
54 'php' => __( 'PHP', 'post-views-counter' ),
55 'js' => __( 'JavaScript', 'post-views-counter' )
56 ];
57
58 // WordPress 4.4+?
59 if ( function_exists( 'register_rest_route' ) )
60 $modes['rest_api'] = __( 'REST API', 'post-views-counter' );
61
62 return apply_filters( 'pvc_get_counter_modes', $modes );
63 }
64
65 /**
66 * Add settings data.
67 *
68 * @param array $settings
69 * @return array
70 */
71 public function settings_data( $settings ) {
72 // get main instance
73 $pvc = Post_Views_Counter();
74
75 // time types
76 $time_types = [
77 'minutes' => __( 'minutes', 'post-views-counter' ),
78 'hours' => __( 'hours', 'post-views-counter' ),
79 'days' => __( 'days', 'post-views-counter' ),
80 'weeks' => __( 'weeks', 'post-views-counter' ),
81 'months' => __( 'months', 'post-views-counter' ),
82 'years' => __( 'years', 'post-views-counter' )
83 ];
84
85 // user groups
86 $groups = [
87 'robots' => __( 'robots', 'post-views-counter' ),
88 'users' => __( 'logged in users', 'post-views-counter' ),
89 'guests' => __( 'guests', 'post-views-counter' ),
90 'roles' => __( 'selected user roles', 'post-views-counter' )
91 ];
92
93 // get user roles
94 $user_roles = $pvc->functions->get_user_roles();
95
96 // get post types
97 $post_types = $pvc->functions->get_post_types();
98
99 // add settings
100 $settings['post-views-counter'] = [
101 'label' => __( 'Post Views Counter Settings', 'post-views-counter' ),
102 'option_name' => [
103 'general' => 'post_views_counter_settings_general',
104 'display' => 'post_views_counter_settings_display'
105 ],
106 'validate' => [ $this, 'validate_settings' ],
107 'sections' => [
108 'post_views_counter_general_settings' => [
109 'tab' => 'general'
110 ],
111 'post_views_counter_display_settings' => [
112 'tab' => 'display'
113 ]
114 ],
115 'fields' => [
116 'post_types_count' => [
117 'tab' => 'general',
118 'title' => __( 'Post Types Count', 'post-views-counter' ),
119 'section' => 'post_views_counter_general_settings',
120 'type' => 'checkbox',
121 'display_type' => 'horizontal',
122 'description' => __( 'Select post types for which post views will be counted.', 'post-views-counter' ),
123 'options' => $post_types
124 ],
125 'counter_mode' => [
126 'tab' => 'general',
127 'title' => __( 'Counter Mode', 'post-views-counter' ),
128 'section' => 'post_views_counter_general_settings',
129 'type' => 'radio',
130 'description' => __( 'Select the method of collecting post views data. If you are using any of the caching plugins select JavaScript or REST API (if available).', 'post-views-counter' ),
131 'options' => $this->get_counter_modes()
132 ],
133 'post_views_column' => [
134 'tab' => 'general',
135 'title' => __( 'Post Views Column', 'post-views-counter' ),
136 'section' => 'post_views_counter_general_settings',
137 'type' => 'boolean',
138 'description' => '',
139 'label' => __( 'Enable to display post views count column for each of the selected post types.', 'post-views-counter' )
140 ],
141 'restrict_edit_views' => [
142 'tab' => 'general',
143 'title' => __( 'Restrict Edit', 'post-views-counter' ),
144 'section' => 'post_views_counter_general_settings',
145 'type' => 'boolean',
146 'description' => '',
147 'label' => __( 'Enable to restrict post views editing to admins only.', 'post-views-counter' )
148 ],
149 'time_between_counts' => [
150 'tab' => 'general',
151 'title' => __( 'Count Interval', 'post-views-counter' ),
152 'section' => 'post_views_counter_general_settings',
153 'type' => 'custom',
154 'description' => '',
155 'min' => 0,
156 'max' => 999999,
157 'options' => $time_types,
158 'callback' => [ $this, 'setting_time_between_counts' ],
159 'validate' => [ $this, 'validate_time_between_counts' ]
160 ],
161 'reset_counts' => [
162 'tab' => 'general',
163 'title' => __( 'Reset Data Interval', 'post-views-counter' ),
164 'section' => 'post_views_counter_general_settings',
165 'type' => 'custom',
166 'description' => '',
167 'min' => 0,
168 'max' => 999999,
169 'options' => $time_types,
170 'callback' => [ $this, 'setting_reset_counts' ],
171 'validate' => [ $this, 'validate_reset_counts' ]
172 ],
173 'flush_interval' => [
174 'tab' => 'general',
175 'title' => __( 'Flush Object Cache Interval', 'post-views-counter' ),
176 'section' => 'post_views_counter_general_settings',
177 'type' => 'custom',
178 'description' => '',
179 'min' => 0,
180 'max' => 999999,
181 'options' => $time_types,
182 'callback' => [ $this, 'setting_flush_interval' ],
183 'validate' => [ $this, 'validate_flush_interval' ]
184 ],
185 'exclude' => [
186 'tab' => 'general',
187 'title' => __( 'Exclude Visitors', 'post-views-counter' ),
188 'section' => 'post_views_counter_general_settings',
189 'type' => 'custom',
190 'description' => '',
191 'options' => [
192 'groups' => $groups,
193 'roles' => $user_roles
194 ],
195 'callback' => [ $this, 'setting_exclude' ],
196 'validate' => [ $this, 'validate_exclude' ]
197 ],
198 'exclude_ips' => [
199 'tab' => 'general',
200 'title' => __( 'Exclude IPs', 'post-views-counter' ),
201 'section' => 'post_views_counter_general_settings',
202 'type' => 'custom',
203 'description' => '',
204 'callback' => [ $this, 'setting_exclude_ips' ],
205 'validate' => [ $this, 'validate_exclude_ips' ]
206 ],
207 'strict_counts' => [
208 'tab' => 'general',
209 'title' => __( 'Strict counts', 'post-views-counter' ),
210 'section' => 'post_views_counter_general_settings',
211 'type' => 'boolean',
212 'description' => '',
213 'label' => __( 'Enable to prevent bypassing the counts interval (for e.g. using incognito browser window or by clearing cookies).', 'post-views-counter' )
214 ],
215 'wp_postviews' => [
216 'tab' => 'general',
217 'title' => __( 'Tools', 'post-views-counter' ),
218 'section' => 'post_views_counter_general_settings',
219 'type' => 'custom',
220 'description' => '',
221 'skip_saving' => true,
222 'callback' => [ $this, 'setting_wp_postviews' ]
223 ],
224 'deactivation_delete' => [
225 'tab' => 'general',
226 'title' => __( 'Deactivation', 'post-views-counter' ),
227 'section' => 'post_views_counter_general_settings',
228 'type' => 'boolean',
229 'description' => __( 'It will delete all data related to the plugin from the database including post views.', 'post-views-counter' ),
230 'label' => __( 'Enable to delete all plugin data on deactivation.', 'post-views-counter' )
231 ],
232 'label' => [
233 'tab' => 'display',
234 'title' => __( 'Post Views Label', 'post-views-counter' ),
235 'section' => 'post_views_counter_display_settings',
236 'type' => 'input',
237 'description' => __( 'Enter the label for the post views counter field.', 'post-views-counter' ),
238 'subclass' => 'regular-text',
239 'validate' => [ $this, 'validate_label' ],
240 'reset' => [ $this, 'reset_label' ]
241 ],
242 'post_types_display' => [
243 'tab' => 'display',
244 'title' => __( 'Post Type', 'post-views-counter' ),
245 'section' => 'post_views_counter_display_settings',
246 'type' => 'checkbox',
247 'display_type' => 'horizontal',
248 'description' => __( 'Select post types for which the views count will be displayed.', 'post-views-counter' ),
249 'options' => $post_types
250 ],
251 'page_types_display' => [
252 'tab' => 'display',
253 'title' => __( 'Page Type', 'post-views-counter' ),
254 'section' => 'post_views_counter_display_settings',
255 'type' => 'checkbox',
256 'display_type' => 'horizontal',
257 'description' => __( 'Select page types where the views count will be displayed.', 'post-views-counter' ),
258 'options' => apply_filters(
259 'pvc_page_types_display_options',
260 [
261 'home' => __( 'Home', 'post-views-counter' ),
262 'archive' => __( 'Archives', 'post-views-counter' ),
263 'singular' => __( 'Single pages', 'post-views-counter' ),
264 'search' => __( 'Search results', 'post-views-counter' ),
265 ]
266 )
267 ],
268 'restrict_display' => [
269 'tab' => 'display',
270 'title' => __( 'User Type', 'post-views-counter' ),
271 'section' => 'post_views_counter_display_settings',
272 'type' => 'custom',
273 'description' => '',
274 'options' => [
275 'groups' => $groups,
276 'roles' => $user_roles
277 ],
278 'callback' => [ $this, 'setting_restrict_display' ],
279 'validate' => [ $this, 'validate_restrict_display' ]
280 ],
281 'position' => [
282 'tab' => 'display',
283 'title' => __( 'Position', 'post-views-counter' ),
284 'section' => 'post_views_counter_display_settings',
285 'type' => 'select',
286 'description' => sprintf( __( 'Select where would you like to display the post views counter. Use %s shortcode for manual display.', 'post-views-counter' ), '<code>[post-views]</code>' ),
287 'options' => [
288 'before' => __( 'before the content', 'post-views-counter' ),
289 'after' => __( 'after the content', 'post-views-counter' ),
290 'manual' => __( 'manual', 'post-views-counter' )
291 ]
292 ],
293 'display_style' => [
294 'tab' => 'display',
295 'title' => __( 'Display Style', 'post-views-counter' ),
296 'section' => 'post_views_counter_display_settings',
297 'type' => 'custom',
298 'description' => __( 'Choose how to display the post views counter.', 'post-views-counter' ),
299 'callback' => [ $this, 'setting_display_style' ],
300 'validate' => [ $this, 'validate_display_style' ],
301 'options' => [
302 'icon' => __( 'icon', 'post-views-counter' ),
303 'text' => __( 'label', 'post-views-counter' )
304 ]
305 ],
306 'icon_class' => [
307 'tab' => 'display',
308 'title' => __( 'Icon Class', 'post-views-counter' ),
309 'section' => 'post_views_counter_display_settings',
310 'type' => 'input',
311 'description' => sprintf( __( 'Enter the post views icon class. Any of the <a href="%s" target="_blank">Dashicons</a> classes are available.', 'post-views-counter' ), 'https://developer.wordpress.org/resource/dashicons/' ),
312 'subclass' => 'regular-text'
313 ],
314 'toolbar_statistics' => [
315 'tab' => 'display',
316 'title' => __( 'Toolbar Chart', 'post-views-counter' ),
317 'section' => 'post_views_counter_display_settings',
318 'type' => 'boolean',
319 'description' => __( 'The post views chart will be displayed for the post types that are being counted.', 'post-views-counter' ),
320 'label' => __( 'Enable to display the post views chart at the toolbar.', 'post-views-counter' )
321 ]
322 ]
323 ];
324
325 return $settings;
326 }
327
328 /**
329 * Add settings page.
330 *
331 * @param array $pages
332 * @return array
333 */
334 public function settings_page( $pages ) {
335 $pages['post-views-counter'] = [
336 'type' => 'settings_page',
337 'menu_slug' => 'post-views-counter',
338 'page_title' => __( 'Post Views Counter Settings', 'post-views-counter' ),
339 'menu_title' => __( 'Post Views Counter', 'post-views-counter' ),
340 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
341 'callback' => null,
342 'tabs' => [
343 'general' => [
344 'label' => __( 'General', 'post-views-counter' ),
345 'option_name' => 'post_views_counter_settings_general'
346 ],
347 'display' => [
348 'label' => __( 'Display', 'post-views-counter' ),
349 'option_name' => 'post_views_counter_settings_display'
350 ]
351 ]
352 ];
353
354 return $pages;
355 }
356
357 /**
358 * Validate options.
359 *
360 * @global object $wpdb
361 *
362 * @param array $input Settings data
363 * @return array
364 */
365 public function validate_settings( $input ) {
366 // check capability
367 if ( ! current_user_can( 'manage_options' ) )
368 return $input;
369
370 global $wpdb;
371
372 // get main instance
373 $pvc = Post_Views_Counter();
374
375 // use internal settings API to validate settings first
376 $input = $pvc->settings_api->validate_settings( $input );
377
378 // import post views data from another plugin
379 if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
380 // make sure we do not change anything in the settings
381 $input = $pvc->options['general'];
382
383 // get views key
384 $meta_key = esc_attr( apply_filters( 'pvc_import_meta_key', 'views' ) );
385
386 // get views
387 $views = $wpdb->get_results( "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key . "'", ARRAY_A, 0 );
388
389 // any views?
390 if ( ! empty( $views ) ) {
391 $sql = [];
392
393 foreach ( $views as $view ) {
394 $sql[] = "(" . $view['post_id'] . ", 4, 'total', " . ( (int) $view['meta_value'] ) . ")";
395 }
396
397 $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)" );
398
399 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported succesfully.', 'post-views-counter' ), 'updated' );
400 } else
401 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
402 // delete all post views data
403 } elseif ( isset( $_POST['post_views_counter_reset_views'] ) ) {
404 // make sure we do not change anything in the settings
405 $input = $pvc->options['general'];
406
407 if ( $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . 'post_views' ) )
408 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'All existing data deleted succesfully.', 'post-views-counter' ), 'updated' );
409 else
410 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'Error occurred. All existing data were not deleted.', 'post-views-counter' ), 'error' );
411 // save general settings
412 } elseif ( isset( $_POST['save_post_views_counter_settings_general'] ) ) {
413 $input['update_version'] = $pvc->options['general']['update_version'];
414 $input['update_notice'] = $pvc->options['general']['update_notice'];
415 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
416 // reset general settings
417 } elseif ( isset( $_POST['reset_post_views_counter_settings_general'] ) ) {
418 $input['update_version'] = $pvc->options['general']['update_version'];
419 $input['update_notice'] = $pvc->options['general']['update_notice'];
420 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
421 }
422
423 return $input;
424 }
425
426 /**
427 * Validate label.
428 *
429 * @param array $input Input POST data
430 * @param array $field Field options
431 * @return array
432 */
433 public function validate_label( $input, $field ) {
434 // get main instance
435 $pvc = Post_Views_Counter();
436
437 if ( ! isset( $input ) )
438 $input = $pvc->defaults['display']['label'];
439
440 // use internal settings API to validate settings first
441 $input = $pvc->settings_api->validate_field( $input, 'input', $field );
442
443 if ( function_exists( 'icl_register_string' ) )
444 icl_register_string( 'Post Views Counter', 'Post Views Label', $input );
445
446 return $input;
447 }
448
449 /**
450 * Restore post views label to default value.
451 *
452 * @param array $default Default value
453 * @param array $field Field options
454 * @return array
455 */
456 public function reset_label( $default, $field ) {
457 if ( function_exists( 'icl_register_string' ) )
458 icl_register_string( 'Post Views Counter', 'Post Views Label', $default );
459
460 return $default;
461 }
462
463 /**
464 * Setting: display style.
465 *
466 * @param array $field Field options
467 * @return string
468 */
469 public function setting_display_style( $field ) {
470 // get main instance
471 $pvc = Post_Views_Counter();
472
473 $html = '
474 <input type="hidden" name="post_views_counter_settings_display[display_style]" value="empty" />';
475
476 foreach ( $field['options'] as $key => $label ) {
477 $html .= '
478 <label><input id="post_views_counter_display_display_style_' . esc_attr( $key ) . '" type="checkbox" name="post_views_counter_settings_display[display_style][]" value="' . esc_attr( $key ) . '" ' . checked( ! empty( $pvc->options['display']['display_style'][$key] ), true, false ) . ' />' . esc_html( $label ) . '</label> ';
479 }
480
481 return $html;
482 }
483
484 /**
485 * Validate display style.
486 *
487 * @param array $input Input POST data
488 * @param array $field Field options
489 * @return array
490 */
491 public function validate_display_style( $input, $field ) {
492 // get main instance
493 $pvc = Post_Views_Counter();
494
495 $data = [];
496
497 foreach ( $field['options'] as $value => $label ) {
498 $data[$value] = false;
499 }
500
501 // any data?
502 if ( ! empty( $input['display_style'] && $input['display_style'] !== 'empty' && is_array( $input['display_style'] ) ) ) {
503 foreach ( $input['display_style'] as $value ) {
504 if ( array_key_exists( $value, $field['options'] ) )
505 $data[$value] = true;
506 }
507 }
508
509 $input['display_style'] = $data;
510
511 return $input;
512 }
513
514 /**
515 * Setting: count interval.
516 *
517 * @param array $field Field options
518 * @return string
519 */
520 public function setting_time_between_counts( $field ) {
521 // get main instance
522 $pvc = Post_Views_Counter();
523
524 $html = '
525 <input size="6" type="number" min="' . ( (int) $field['min'] ) . '" max="' . ( (int) $field['max'] ) . '" name="post_views_counter_settings_general[time_between_counts][number]" value="' . esc_attr( $pvc->options['general']['time_between_counts']['number'] ) . '" />
526 <select class="pvc-chosen-short" name="post_views_counter_settings_general[time_between_counts][type]">';
527
528 foreach ( $field['options'] as $type => $type_name ) {
529 $html .= '
530 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['time_between_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
531 }
532
533 $html .= '
534 </select>
535 <p class="description">' . __( 'Enter the time between single user visit count.', 'post-views-counter' ) . '</p>';
536
537 return $html;
538 }
539
540 /**
541 * Validate count interval.
542 *
543 * @param array $input Input POST data
544 * @param array $field Field options
545 * @return array
546 */
547 public function validate_time_between_counts( $input, $field ) {
548 // get main instance
549 $pvc = Post_Views_Counter();
550
551 // number
552 $input['time_between_counts']['number'] = isset( $input['time_between_counts']['number'] ) ? (int) $input['time_between_counts']['number'] : $pvc->defaults['general']['time_between_counts']['number'];
553
554 if ( $input['time_between_counts']['number'] < $field['min'] || $input['time_between_counts']['number'] > $field['max'] )
555 $input['time_between_counts']['number'] = $pvc->defaults['general']['time_between_counts']['number'];
556
557 // type
558 $input['time_between_counts']['type'] = isset( $input['time_between_counts']['type'], $field['options'][$input['time_between_counts']['type']] ) ? $input['time_between_counts']['type'] : $pvc->defaults['general']['time_between_counts']['type'];
559
560 return $input;
561 }
562
563 /**
564 * Setting: reset data interval.
565 *
566 * @param array $field Field options
567 * @return string
568 */
569 public function setting_reset_counts( $field ) {
570 // get main instance
571 $pvc = Post_Views_Counter();
572
573 $html = '
574 <input size="6" type="number" min="' . ( (int) $field['min'] ) . '" max="' . ( (int) $field['max'] ) . '" name="post_views_counter_settings_general[reset_counts][number]" value="' . esc_attr( $pvc->options['general']['reset_counts']['number'] ) . '" />
575 <select class="pvc-chosen-short" name="post_views_counter_settings_general[reset_counts][type]">';
576
577 foreach ( array_slice( $field['options'], 2, null, true ) as $type => $type_name ) {
578 $html .= '
579 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['reset_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
580 }
581
582 $html .= '
583 </select>
584 <p class="description">' . sprintf( __( 'Delete single day post views data older than specified above. Enter %s if you want to preserve your data regardless of its age.', 'post-views-counter' ), '<code>0</code>' ) . '</p>';
585
586 return $html;
587 }
588
589 /**
590 * Validate reset data interval.
591 *
592 * @param array $input Input POST data
593 * @param array $field Field options
594 * @return array
595 */
596 public function validate_reset_counts( $input, $field ) {
597 // get main instance
598 $pvc = Post_Views_Counter();
599
600 // number
601 $input['reset_counts']['number'] = isset( $input['reset_counts']['number'] ) ? (int) $input['reset_counts']['number'] : $pvc->defaults['general']['reset_counts']['number'];
602
603 if ( $input['reset_counts']['number'] < $field['min'] || $input['reset_counts']['number'] > $field['max'] )
604 $input['reset_counts']['number'] = $pvc->defaults['general']['reset_counts']['number'];
605
606 // type
607 $input['reset_counts']['type'] = isset( $input['reset_counts']['type'], $field['options'][$input['reset_counts']['type']] ) ? $input['reset_counts']['type'] : $pvc->defaults['general']['reset_counts']['type'];
608
609 // run cron on next visit?
610 $input['cron_run'] = ( $input['reset_counts']['number'] > 0 );
611
612 // cron update?
613 $input['cron_update'] = ( $input['cron_run'] && ( $pvc->options['general']['reset_counts']['number'] !== $input['reset_counts']['number'] || $pvc->options['general']['reset_counts']['type'] !== $input['reset_counts']['type'] ) );
614
615 return $input;
616 }
617
618 /**
619 * Setting: flush object cache interval.
620 *
621 * @param array $field Field options
622 * @return string
623 */
624 public function setting_flush_interval( $field ) {
625 // get main instance
626 $pvc = Post_Views_Counter();
627
628 $html = '
629 <input size="6" type="number" min="' . ( (int) $field['min'] ) . '" max="' . ( (int) $field['max'] ) . '" name="post_views_counter_settings_general[flush_interval][number]" value="' . esc_attr( $pvc->options['general']['flush_interval']['number'] ) . '" />
630 <select class="pvc-chosen-short" name="post_views_counter_settings_general[flush_interval][type]">';
631
632 foreach ( $field['options'] as $type => $type_name ) {
633 $html .= '
634 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['flush_interval']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
635 }
636
637 $html .= '
638 </select>
639 <p class="description">' . sprintf( __( 'How often to flush cached view counts from the object cache into the database. This feature is used only if a persistent object cache is detected and the interval is greater than %s. When used, view counts will be collected and stored in the object cache instead of the database and will then be asynchronously flushed to the database according to the specified interval.<br /><strong>Notice:</strong> Potential data loss may occur if the object cache is cleared/unavailable for the duration of the interval.', 'post-views-counter' ), '<code>0</code>' ) . '</p>';
640
641 return $html;
642 }
643
644 /**
645 * Validate flush object cache interval.
646 *
647 * @param array $input Input POST data
648 * @param array $field Field options
649 * @return array
650 */
651 public function validate_flush_interval( $input, $field ) {
652 // get main instance
653 $pvc = Post_Views_Counter();
654
655 // number
656 $input['flush_interval']['number'] = isset( $input['flush_interval']['number'] ) ? (int) $input['flush_interval']['number'] : $pvc->defaults['general']['flush_interval']['number'];
657
658 if ( $input['flush_interval']['number'] < $field['min'] || $input['flush_interval']['number'] > $field['max'] )
659 $input['flush_interval']['number'] = $pvc->defaults['general']['flush_interval']['number'];
660
661 // type
662 $input['flush_interval']['type'] = isset( $input['flush_interval']['type'], $field['options'][$input['flush_interval']['type']] ) ? $input['flush_interval']['type'] : $pvc->defaults['general']['flush_interval']['type'];
663
664 // Since the settings are about to be saved and cache flush interval could've changed,
665 // we want to make sure that any changes done on the settings page are in effect immediately
666 // (instead of having to wait for the previous schedule to occur).
667 // We achieve that by making sure to clear any previous cache flush schedules and
668 // schedule the new one if the specified interval is > 0
669 $pvc->remove_cache_flush();
670
671 if ( $input['flush_interval']['number'] > 0 )
672 $pvc->schedule_cache_flush();
673
674 return $input;
675 }
676
677 /**
678 * Setting: exclude visitors.
679 *
680 * @param array $field Field options
681 * @return string
682 */
683 public function setting_exclude( $field ) {
684 // get main instance
685 $pvc = Post_Views_Counter();
686
687 $html = '';
688
689 foreach ( $field['options']['groups'] as $type => $type_name ) {
690 $html .= '
691 <label><input id="' . esc_attr( 'pvc_exclude-' . $type ) . '" type="checkbox" name="post_views_counter_settings_general[exclude][groups][' . esc_attr( $type ) . ']" value="1" ' . checked( in_array( $type, $pvc->options['general']['exclude']['groups'], true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
692 }
693
694 $html .= '
695 <p class="description">' . __( 'Use it exclude specific user groups from post views count.', 'post-views-counter' ) . '</p>
696 <div class="pvc_user_roles"' . ( in_array( 'roles', $pvc->options['general']['exclude']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
697
698 foreach ( $field['options']['roles'] as $role => $role_name ) {
699 $html .= '
700 <label><input type="checkbox" name="post_views_counter_settings_general[exclude][roles][' . $role . ']" value="1" ' . checked( in_array( $role, $pvc->options['general']['exclude']['roles'], true ), true, false ) . '>' . esc_html( $role_name ) . '</label>';
701 }
702
703 $html .= '
704 <p class="description">' . __( 'Use it exclude specific user roles from post views count.', 'post-views-counter' ) . '</p>
705 </div>';
706
707 return $html;
708 }
709
710 /**
711 * Validate exclude visitors.
712 *
713 * @param array $input Input POST data
714 * @param array $field Field options
715 * @return array
716 */
717 public function validate_exclude( $input, $field ) {
718 // any groups?
719 if ( isset( $input['exclude']['groups'] ) ) {
720 $groups = [];
721
722 foreach ( $input['exclude']['groups'] as $group => $set ) {
723 if ( isset( $field['options']['groups'][$group] ) )
724 $groups[] = $group;
725 }
726
727 $input['exclude']['groups'] = array_unique( $groups );
728 } else
729 $input['exclude']['groups'] = [];
730
731 // any roles?
732 if ( in_array( 'roles', $input['exclude']['groups'], true ) && isset( $input['exclude']['roles'] ) ) {
733 $roles = [];
734
735 foreach ( $input['exclude']['roles'] as $role => $set ) {
736 if ( isset( $field['options']['roles'][$role] ) )
737 $roles[] = $role;
738 }
739
740 $input['exclude']['roles'] = array_unique( $roles );
741 } else
742 $input['exclude']['roles'] = [];
743
744 return $input;
745 }
746
747 /**
748 * Setting: exclude IP addresses.
749 *
750 * @return string
751 */
752 public function setting_exclude_ips() {
753 // get ip addresses
754 $ips = Post_Views_Counter()->options['general']['exclude_ips'];
755
756 $html = '';
757
758 // any ip addresses?
759 if ( ! empty( $ips ) ) {
760 foreach ( $ips as $key => $ip ) {
761 $html .= '
762 <div class="ip-box">
763 <input type="text" name="post_views_counter_settings_general[exclude_ips][]" value="' . esc_attr( $ip ) . '" /> <a href="#" class="remove-exclude-ip" title="' . esc_attr__( 'Remove', 'post-views-counter' ) . '">' . esc_html__( 'Remove', 'post-views-counter' ) . '</a>
764 </div>';
765 }
766 } else {
767 $html .= '
768 <div class="ip-box">
769 <input type="text" name="post_views_counter_settings_general[exclude_ips][]" value="" /> <a href="#" class="remove-exclude-ip" title="' . esc_attr__( 'Remove', 'post-views-counter' ) . '">' . esc_html__( 'Remove', 'post-views-counter' ) . '</a>
770 </div>';
771 }
772
773 $html .= '
774 <p><input type="button" class="button button-secondary add-exclude-ip" value="' . esc_attr__( 'Add new', 'post-views-counter' ) . '" /> <input type="button" class="button button-secondary add-current-ip" value="' . esc_attr__( 'Add my current IP', 'post-views-counter' ) . '" data-rel="' . esc_attr( $_SERVER['REMOTE_ADDR'] ) . '" /></p>
775 <p class="description">' . esc_html__( 'Enter the IP addresses to be excluded from post views count.', 'post-views-counter' ) . '</p>';
776
777 return $html;
778 }
779
780 /**
781 * Validate exclude IP addresses.
782 *
783 * @param array $input Input POST data
784 * @param array $field Field options
785 * @return array
786 */
787 public function validate_exclude_ips( $input, $field ) {
788 // any ip addresses?
789 if ( isset( $input['exclude_ips'] ) ) {
790 $ips = [];
791
792 foreach ( $input['exclude_ips'] as $ip ) {
793 if ( strpos( $ip, '*' ) !== false ) {
794 $new_ip = str_replace( '*', '0', $ip );
795
796 if ( filter_var( $new_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
797 $ips[] = $ip;
798 } elseif ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
799 $ips[] = $ip;
800 }
801
802 $input['exclude_ips'] = array_unique( $ips );
803 }
804
805 return $input;
806 }
807
808 /**
809 * Setting: tools.
810 *
811 * @return string
812 */
813 public function setting_wp_postviews() {
814 $html = '
815 <input type="submit" class="button button-secondary" name="post_views_counter_import_wp_postviews" value="' . __( 'Import views', 'post-views-counter' ) . '"/> <label><input id="pvc-wp-postviews" type="checkbox" name="post_views_counter_import_wp_postviews_override" value="1" />' . __( 'Override existing views data.', 'post-views-counter' ) . '</label>
816 <p class="description">' . __( 'Import post views data from WP-PostViews plugin.', 'post-views-counter' ) . '</p>
817 <br /><input type="submit" class="button button-secondary" name="post_views_counter_reset_views" value="' . __( 'Delete views', 'post-views-counter' ) . '"/>
818 <p class="description">' . __( 'Delete all the existing post views data.', 'post-views-counter' ) . '</p>';
819
820 return $html;
821 }
822
823 /**
824 * Setting: user type.
825 *
826 * @param array $field Field options
827 * @return string
828 */
829 public function setting_restrict_display( $field ) {
830 // get main instance
831 $pvc = Post_Views_Counter();
832
833 $html = '';
834
835 foreach ( $field['options']['groups'] as $type => $type_name ) {
836 if ( $type === 'robots' )
837 continue;
838
839 $html .= '
840 <label><input id="pvc_restrict_display-' . esc_attr( $type ) . '" type="checkbox" name="post_views_counter_settings_display[restrict_display][groups][' . esc_html( $type ) . ']" value="1" ' . checked( in_array( $type, $pvc->options['display']['restrict_display']['groups'], true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
841 }
842
843 $html .= '
844 <p class="description">' . __( 'Use it to hide the post views counter from selected type of visitors.', 'post-views-counter' ) . '</p>
845 <div class="pvc_user_roles"' . ( in_array( 'roles', $pvc->options['display']['restrict_display']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
846
847 foreach ( $field['options']['roles'] as $role => $role_name ) {
848 $html .= '
849 <label><input type="checkbox" name="post_views_counter_settings_display[restrict_display][roles][' . esc_attr( $role ) . ']" value="1" ' . checked( in_array( $role, $pvc->options['display']['restrict_display']['roles'], true ), true, false ) . ' />' . esc_html( $role_name ) . '</label>';
850 }
851
852 $html .= '
853 <p class="description">' . __( 'Use it to hide the post views counter from selected user roles.', 'post-views-counter' ) . '</p>
854 </div>';
855
856 return $html;
857 }
858
859 /**
860 * Validate user type.
861 *
862 * @param array $input Input POST data
863 * @param array $field Field options
864 * @return array
865 */
866 public function validate_restrict_display( $input, $field ) {
867 // any groups?
868 if ( isset( $input['restrict_display']['groups'] ) ) {
869 $groups = [];
870
871 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
872 if ( $group === 'robots' )
873 continue;
874
875 if ( isset( $field['options']['groups'][$group] ) )
876 $groups[] = $group;
877 }
878
879 $input['restrict_display']['groups'] = array_unique( $groups );
880 } else
881 $input['restrict_display']['groups'] = [];
882
883 // any roles?
884 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
885 $roles = [];
886
887 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
888 if ( isset( $field['options']['roles'][$role] ) )
889 $roles[] = $role;
890 }
891
892 $input['restrict_display']['roles'] = array_unique( $roles );
893 } else
894 $input['restrict_display']['roles'] = [];
895
896 return $input;
897 }
898 }