PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.8
Post Views Counter v1.3.8
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
ajax.php 7 years ago class-admin.php 4 years ago class-columns.php 4 years ago class-counter.php 4 years ago class-crawler-detect.php 4 years ago class-cron.php 4 years ago class-dashboard.php 4 years ago class-frontend.php 4 years ago class-functions.php 4 years ago class-query.php 4 years ago class-settings-api.php 4 years ago class-settings.php 4 years ago class-update.php 4 years ago class-widgets.php 4 years ago columns.php 6 years ago counter.php 6 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 6 years ago frontend.php 6 years ago functions.php 4 years ago query.php 6 years ago settings.php 6 years ago update.php 6 years ago widgets.php 6 years ago
class-settings.php
901 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' && ! in_array( 'ajax', $this->get_counter_modes(), true ) ) {
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 'post_views_counter_display_settings' => []
110 ],
111 'fields' => [
112 'post_types_count' => [
113 'tab' => 'general',
114 'title' => __( 'Post Types Count', 'post-views-counter' ),
115 'section' => 'post_views_counter_general_settings',
116 'type' => 'checkbox',
117 'display_type' => 'horizontal',
118 'description' => __( 'Select post types for which post views will be counted.', 'post-views-counter' ),
119 'options' => $post_types
120 ],
121 'counter_mode' => [
122 'tab' => 'general',
123 'title' => __( 'Counter Mode', 'post-views-counter' ),
124 'section' => 'post_views_counter_general_settings',
125 'type' => 'radio',
126 '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' ),
127 'options' => $this->get_counter_modes()
128 ],
129 'post_views_column' => [
130 'tab' => 'general',
131 'title' => __( 'Post Views Column', 'post-views-counter' ),
132 'section' => 'post_views_counter_general_settings',
133 'type' => 'boolean',
134 'description' => '',
135 'label' => __( 'Enable to display post views count column for each of the selected post types.', 'post-views-counter' )
136 ],
137 'restrict_edit_views' => [
138 'tab' => 'general',
139 'title' => __( 'Restrict Edit', 'post-views-counter' ),
140 'section' => 'post_views_counter_general_settings',
141 'type' => 'boolean',
142 'description' => '',
143 'label' => __( 'Enable to restrict post views editing to admins only.', 'post-views-counter' )
144 ],
145 'time_between_counts' => [
146 'tab' => 'general',
147 'title' => __( 'Count Interval', 'post-views-counter' ),
148 'section' => 'post_views_counter_general_settings',
149 'type' => 'custom',
150 'description' => '',
151 'min' => 0,
152 'max' => 999999,
153 'options' => $time_types,
154 'callback' => [ $this, 'setting_time_between_counts' ],
155 'validate' => [ $this, 'validate_time_between_counts' ]
156 ],
157 'reset_counts' => [
158 'tab' => 'general',
159 'title' => __( 'Reset Data Interval', 'post-views-counter' ),
160 'section' => 'post_views_counter_general_settings',
161 'type' => 'custom',
162 'description' => '',
163 'min' => 0,
164 'max' => 999999,
165 'options' => $time_types,
166 'callback' => [ $this, 'setting_reset_counts' ],
167 'validate' => [ $this, 'validate_reset_counts' ]
168 ],
169 'flush_interval' => [
170 'tab' => 'general',
171 'title' => __( 'Flush Object Cache Interval', 'post-views-counter' ),
172 'section' => 'post_views_counter_general_settings',
173 'type' => 'custom',
174 'description' => '',
175 'min' => 0,
176 'max' => 999999,
177 'options' => $time_types,
178 'callback' => [ $this, 'setting_flush_interval' ],
179 'validate' => [ $this, 'validate_flush_interval' ]
180 ],
181 'exclude' => [
182 'tab' => 'general',
183 'title' => __( 'Exclude Visitors', 'post-views-counter' ),
184 'section' => 'post_views_counter_general_settings',
185 'type' => 'custom',
186 'description' => '',
187 'options' => [
188 'groups' => $groups,
189 'roles' => $user_roles
190 ],
191 'callback' => [ $this, 'setting_exclude' ],
192 'validate' => [ $this, 'validate_exclude' ]
193 ],
194 'exclude_ips' => [
195 'tab' => 'general',
196 'title' => __( 'Exclude IPs', 'post-views-counter' ),
197 'section' => 'post_views_counter_general_settings',
198 'type' => 'custom',
199 'description' => '',
200 'callback' => [ $this, 'setting_exclude_ips' ],
201 'validate' => [ $this, 'validate_exclude_ips' ]
202 ],
203 'strict_counts' => [
204 'tab' => 'general',
205 'title' => __( 'Strict counts', 'post-views-counter' ),
206 'section' => 'post_views_counter_general_settings',
207 'type' => 'boolean',
208 'description' => '',
209 'label' => __( 'Enable to prevent bypassing the counts interval (for e.g. using incognito browser window or by clearing cookies).', 'post-views-counter' )
210 ],
211 'wp_postviews' => [
212 'tab' => 'general',
213 'title' => __( 'Tools', 'post-views-counter' ),
214 'section' => 'post_views_counter_general_settings',
215 'type' => 'custom',
216 'description' => '',
217 'skip_saving' => true,
218 'callback' => [ $this, 'setting_wp_postviews' ]
219 ],
220 'deactivation_delete' => [
221 'tab' => 'general',
222 'title' => __( 'Deactivation', 'post-views-counter' ),
223 'section' => 'post_views_counter_general_settings',
224 'type' => 'boolean',
225 'description' => '',
226 'label' => __( 'Enable to delete all plugin data on deactivation.', 'post-views-counter' )
227 ],
228 'label' => [
229 'tab' => 'display',
230 'title' => __( 'Post Views Label', 'post-views-counter' ),
231 'section' => 'post_views_counter_display_settings',
232 'type' => 'input',
233 'description' => __( 'Enter the label for the post views counter field.', 'post-views-counter' ),
234 'subclass' => 'regular-text',
235 'validate' => [ $this, 'validate_label' ],
236 'reset' => [ $this, 'reset_label' ]
237 ],
238 'post_types_display' => [
239 'tab' => 'display',
240 'title' => __( 'Post Type', 'post-views-counter' ),
241 'section' => 'post_views_counter_display_settings',
242 'type' => 'checkbox',
243 'display_type' => 'horizontal',
244 'description' => __( 'Select post types for which the views count will be displayed.', 'post-views-counter' ),
245 'options' => $post_types
246 ],
247 'page_types_display' => [
248 'tab' => 'display',
249 'title' => __( 'Page Type', 'post-views-counter' ),
250 'section' => 'post_views_counter_display_settings',
251 'type' => 'checkbox',
252 'display_type' => 'horizontal',
253 'description' => __( 'Select page types where the views count will be displayed.', 'post-views-counter' ),
254 'options' => apply_filters(
255 'pvc_page_types_display_options',
256 [
257 'home' => __( 'Home', 'post-views-counter' ),
258 'archive' => __( 'Archives', 'post-views-counter' ),
259 'singular' => __( 'Single pages', 'post-views-counter' ),
260 'search' => __( 'Search results', 'post-views-counter' ),
261 ]
262 )
263 ],
264 'restrict_display' => [
265 'tab' => 'display',
266 'title' => __( 'User Type', 'post-views-counter' ),
267 'section' => 'post_views_counter_display_settings',
268 'type' => 'custom',
269 'description' => '',
270 'options' => [
271 'groups' => $groups,
272 'roles' => $user_roles
273 ],
274 'callback' => [ $this, 'setting_restrict_display' ],
275 'validate' => [ $this, 'validate_restrict_display' ]
276 ],
277 'position' => [
278 'tab' => 'display',
279 'title' => __( 'Position', 'post-views-counter' ),
280 'section' => 'post_views_counter_display_settings',
281 'type' => 'select',
282 '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>' ),
283 'options' => [
284 'before' => __( 'before the content', 'post-views-counter' ),
285 'after' => __( 'after the content', 'post-views-counter' ),
286 'manual' => __( 'manual', 'post-views-counter' )
287 ]
288 ],
289 'display_style' => [
290 'tab' => 'display',
291 'title' => __( 'Display Style', 'post-views-counter' ),
292 'section' => 'post_views_counter_display_settings',
293 'type' => 'custom',
294 'description' => __( 'Choose how to display the post views counter.', 'post-views-counter' ),
295 'callback' => [ $this, 'setting_display_style' ],
296 'validate' => [ $this, 'validate_display_style' ],
297 'options' => [
298 'icon' => __( 'icon', 'post-views-counter' ),
299 'text' => __( 'label', 'post-views-counter' )
300 ]
301 ],
302 'icon_class' => [
303 'tab' => 'display',
304 'title' => __( 'Icon Class', 'post-views-counter' ),
305 'section' => 'post_views_counter_display_settings',
306 'type' => 'input',
307 '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/' ),
308 'subclass' => 'regular-text'
309 ],
310 'toolbar_statistics' => [
311 'tab' => 'display',
312 'title' => __( 'Toolbar Chart', 'post-views-counter' ),
313 'section' => 'post_views_counter_display_settings',
314 'type' => 'boolean',
315 'description' => __( 'The post views chart will be displayed for the post types that are being counted.', 'post-views-counter' ),
316 'label' => __( 'Enable to display the post views chart at the toolbar.', 'post-views-counter' )
317 ]
318 ]
319 ];
320
321 return $settings;
322 }
323
324 /**
325 * Add settings page.
326 *
327 * @param array $pages
328 * @return array
329 */
330 public function settings_page( $pages ) {
331 $pages['post-views-counter'] = [
332 'type' => 'settings_page',
333 'menu_slug' => 'post-views-counter',
334 'page_title' => __( 'Post Views Counter Settings', 'post-views-counter' ),
335 'menu_title' => __( 'Post Views Counter', 'post-views-counter' ),
336 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
337 'callback' => null,
338 'tabs' => [
339 'general' => [
340 'label' => __( 'General', 'post-views-counter' ),
341 'option_name' => 'post_views_counter_settings_general'
342 ],
343 'display' => [
344 'label' => __( 'Display', 'post-views-counter' ),
345 'option_name' => 'post_views_counter_settings_display'
346 ]
347 ]
348 ];
349
350 return $pages;
351 }
352
353 /**
354 * Validate options.
355 *
356 * @param array $input Settings data
357 * @return array
358 */
359 public function validate_settings( $input ) {
360 // check capability
361 if ( ! current_user_can( 'manage_options' ) )
362 return $input;
363
364 // get main instance
365 $pvc = Post_Views_Counter();
366
367 // use internal settings API to validate settings first
368 $input = $pvc->settings_api->validate_settings( $input );
369
370 // import post views data from another plugin
371 if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
372 // make sure we do not change anything in the settings
373 $input = $pvc->options['general'];
374
375 global $wpdb;
376
377 // get views key
378 $meta_key = esc_attr( apply_filters( 'pvc_import_meta_key', 'views' ) );
379
380 // get views
381 $views = $wpdb->get_results( "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = '" . $meta_key . "'", ARRAY_A, 0 );
382
383 // any views?
384 if ( ! empty( $views ) ) {
385 $sql = [];
386
387 foreach ( $views as $view ) {
388 $sql[] = "(" . $view['post_id'] . ", 4, 'total', " . ( (int) $view['meta_value'] ) . ")";
389 }
390
391 $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)" );
392
393 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported succesfully.', 'post-views-counter' ), 'updated' );
394 } else
395 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
396 // delete all post views data
397 } elseif ( isset( $_POST['post_views_counter_reset_views'] ) ) {
398 // make sure we do not change anything in the settings
399 $input = $pvc->options['general'];
400
401 global $wpdb;
402
403 if ( $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . 'post_views' ) )
404 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'All existing data deleted succesfully.', 'post-views-counter' ), 'updated' );
405 else
406 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'Error occurred. All existing data were not deleted.', 'post-views-counter' ), 'error' );
407 // save general settings
408 } elseif ( isset( $_POST['save_post_views_counter_settings_general'] ) ) {
409 $input['update_version'] = $pvc->options['general']['update_version'];
410 $input['update_notice'] = $pvc->options['general']['update_notice'];
411 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
412 // reset general settings
413 } elseif ( isset( $_POST['reset_post_views_counter_settings_general'] ) ) {
414 $input['update_version'] = $pvc->options['general']['update_version'];
415 $input['update_notice'] = $pvc->options['general']['update_notice'];
416 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
417 }
418
419 return $input;
420 }
421
422 /**
423 * Validate label.
424 *
425 * @param array $input Input POST data
426 * @param array $field Field options
427 * @return array
428 */
429 public function validate_label( $input, $field ) {
430 // get main instance
431 $pvc = Post_Views_Counter();
432
433 if ( ! isset( $input ) )
434 $input = $pvc->defaults['display']['label'];
435
436 // use internal settings API to validate settings first
437 $input = $pvc->settings_api->validate_field( $input, 'input', $field );
438
439 if ( function_exists( 'icl_register_string' ) )
440 icl_register_string( 'Post Views Counter', 'Post Views Label', $input );
441
442 return $input;
443 }
444
445 /**
446 * Restore post views label to default value.
447 *
448 * @param array $default Default value
449 * @param array $field Field options
450 * @return array
451 */
452 public function reset_label( $default, $field ) {
453 if ( function_exists( 'icl_register_string' ) )
454 icl_register_string( 'Post Views Counter', 'Post Views Label', $default );
455
456 return $default;
457 }
458
459 /**
460 * Setting: display style.
461 *
462 * @param array $field Field options
463 * @return string
464 */
465 public function setting_display_style( $field ) {
466 // get main instance
467 $pvc = Post_Views_Counter();
468
469 $html = '
470 <input type="hidden" name="post_views_counter_settings_display[display_style]" value="empty" />';
471
472 foreach ( $field['options'] as $key => $label ) {
473 $html .= '
474 <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> ';
475 }
476
477 return $html;
478 }
479
480 /**
481 * Validate display style.
482 *
483 * @param array $input Input POST data
484 * @param array $field Field options
485 * @return array
486 */
487 public function validate_display_style( $input, $field ) {
488 // get main instance
489 $pvc = Post_Views_Counter();
490
491 $data = [];
492
493 foreach ( $field['options'] as $value => $label ) {
494 $data[$value] = false;
495 }
496
497 // any data?
498 if ( ! empty( $input['display_style'] && $input['display_style'] !== 'empty' && is_array( $input['display_style'] ) ) ) {
499 foreach ( $input['display_style'] as $value ) {
500 if ( array_key_exists( $value, $field['options'] ) )
501 $data[$value] = true;
502 }
503 }
504
505 $input['display_style'] = $data;
506
507 return $input;
508 }
509
510 /**
511 * Setting: count interval.
512 *
513 * @param array $field Field options
514 * @return string
515 */
516 public function setting_time_between_counts( $field ) {
517 // get main instance
518 $pvc = Post_Views_Counter();
519
520 $html = '
521 <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'] ) . '" />
522 <select class="pvc-chosen-short" name="post_views_counter_settings_general[time_between_counts][type]">';
523
524 foreach ( $field['options'] as $type => $type_name ) {
525 $html .= '
526 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['time_between_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
527 }
528
529 $html .= '
530 </select>
531 <p class="description">' . __( 'Enter the time between single user visit count.', 'post-views-counter' ) . '</p>';
532
533 return $html;
534 }
535
536 /**
537 * Validate count interval.
538 *
539 * @param array $input Input POST data
540 * @param array $field Field options
541 * @return array
542 */
543 public function validate_time_between_counts( $input, $field ) {
544 // get main instance
545 $pvc = Post_Views_Counter();
546
547 // number
548 $input['time_between_counts']['number'] = isset( $input['time_between_counts']['number'] ) ? (int) $input['time_between_counts']['number'] : $pvc->defaults['general']['time_between_counts']['number'];
549
550 if ( $input['time_between_counts']['number'] < $field['min'] || $input['time_between_counts']['number'] > $field['max'] )
551 $input['time_between_counts']['number'] = $pvc->defaults['general']['time_between_counts']['number'];
552
553 // type
554 $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'];
555
556 return $input;
557 }
558
559 /**
560 * Setting: reset data interval.
561 *
562 * @param array $field Field options
563 * @return string
564 */
565 public function setting_reset_counts( $field ) {
566 // get main instance
567 $pvc = Post_Views_Counter();
568
569 $html = '
570 <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'] ) . '" />
571 <select class="pvc-chosen-short" name="post_views_counter_settings_general[reset_counts][type]">';
572
573 foreach ( array_slice( $field['options'], 2, null, true ) as $type => $type_name ) {
574 $html .= '
575 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['reset_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
576 }
577
578 $html .= '
579 </select>
580 <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>';
581
582 return $html;
583 }
584
585 /**
586 * Validate reset data interval.
587 *
588 * @param array $input Input POST data
589 * @param array $field Field options
590 * @return array
591 */
592 public function validate_reset_counts( $input, $field ) {
593 // get main instance
594 $pvc = Post_Views_Counter();
595
596 // number
597 $input['reset_counts']['number'] = isset( $input['reset_counts']['number'] ) ? (int) $input['reset_counts']['number'] : $pvc->defaults['general']['reset_counts']['number'];
598
599 if ( $input['reset_counts']['number'] < $field['min'] || $input['reset_counts']['number'] > $field['max'] )
600 $input['reset_counts']['number'] = $pvc->defaults['general']['reset_counts']['number'];
601
602 // type
603 $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'];
604
605 // run cron on next visit?
606 $input['cron_run'] = ( $input['reset_counts']['number'] > 0 );
607
608 // cron update?
609 $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'] ) );
610
611 return $input;
612 }
613
614 /**
615 * Setting: flush object cache interval.
616 *
617 * @param array $field Field options
618 * @return string
619 */
620 public function setting_flush_interval( $field ) {
621 // get main instance
622 $pvc = Post_Views_Counter();
623
624 $html = '
625 <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'] ) . '" />
626 <select class="pvc-chosen-short" name="post_views_counter_settings_general[flush_interval][type]">';
627
628 foreach ( $field['options'] as $type => $type_name ) {
629 $html .= '
630 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['flush_interval']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
631 }
632
633 $html .= '
634 </select>
635 <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>';
636
637 return $html;
638 }
639
640 /**
641 * Validate flush object cache interval.
642 *
643 * @param array $input Input POST data
644 * @param array $field Field options
645 * @return array
646 */
647 public function validate_flush_interval( $input, $field ) {
648 // get main instance
649 $pvc = Post_Views_Counter();
650
651 // number
652 $input['flush_interval']['number'] = isset( $input['flush_interval']['number'] ) ? (int) $input['flush_interval']['number'] : $pvc->defaults['general']['flush_interval']['number'];
653
654 if ( $input['flush_interval']['number'] < $field['min'] || $input['flush_interval']['number'] > $field['max'] )
655 $input['flush_interval']['number'] = $pvc->defaults['general']['flush_interval']['number'];
656
657 // type
658 $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'];
659
660 // Since the settings are about to be saved and cache flush interval could've changed,
661 // we want to make sure that any changes done on the settings page are in effect immediately
662 // (instead of having to wait for the previous schedule to occur).
663 // We achieve that by making sure to clear any previous cache flush schedules and
664 // schedule the new one if the specified interval is > 0
665 $pvc->remove_cache_flush();
666
667 if ( $input['flush_interval']['number'] > 0 )
668 $pvc->schedule_cache_flush();
669
670 return $input;
671 }
672
673 /**
674 * Setting: exclude visitors.
675 *
676 * @param array $field Field options
677 * @return string
678 */
679 public function setting_exclude( $field ) {
680 // get main instance
681 $pvc = Post_Views_Counter();
682
683 $html = '';
684
685 foreach ( $field['options']['groups'] as $type => $type_name ) {
686 $html .= '
687 <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>';
688 }
689
690 $html .= '
691 <p class="description">' . __( 'Use it exclude specific user groups from post views count.', 'post-views-counter' ) . '</p>
692 <div class="pvc_user_roles"' . ( in_array( 'roles', $pvc->options['general']['exclude']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
693
694 foreach ( $field['options']['roles'] as $role => $role_name ) {
695 $html .= '
696 <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>';
697 }
698
699 $html .= '
700 <p class="description">' . __( 'Use it exclude specific user roles from post views count.', 'post-views-counter' ) . '</p>
701 </div>';
702
703 return $html;
704 }
705
706 /**
707 * Validate exclude visitors.
708 *
709 * @param array $input Input POST data
710 * @param array $field Field options
711 * @return array
712 */
713 public function validate_exclude( $input, $field ) {
714 // get main instance
715 $pvc = Post_Views_Counter();
716
717 // any groups?
718 if ( isset( $input['exclude']['groups'] ) ) {
719 $groups = [];
720
721 foreach ( $input['exclude']['groups'] as $group => $set ) {
722 if ( isset( $field['options']['groups'][$group] ) )
723 $groups[] = $group;
724 }
725
726 $input['exclude']['groups'] = array_unique( $groups );
727 } else
728 $input['exclude']['groups'] = [];
729
730 // any roles?
731 if ( in_array( 'roles', $input['exclude']['groups'], true ) && isset( $input['exclude']['roles'] ) ) {
732 $roles = [];
733
734 foreach ( $input['exclude']['roles'] as $role => $set ) {
735 if ( isset( $field['options']['roles'][$role] ) )
736 $roles[] = $role;
737 }
738
739 $input['exclude']['roles'] = array_unique( $roles );
740 } else
741 $input['exclude']['roles'] = [];
742
743 return $input;
744 }
745
746 /**
747 * Setting: exclude IP addresses.
748 *
749 * @return string
750 */
751 public function setting_exclude_ips() {
752 $ips = Post_Views_Counter()->options['general']['exclude_ips'];
753
754 $html = '';
755
756 if ( ! empty( $ips ) ) {
757 foreach ( $ips as $key => $ip ) {
758 $html .= '
759 <div class="ip-box">
760 <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>
761 </div>';
762 }
763 } else {
764 $html .= '
765 <div class="ip-box">
766 <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>
767 </div>';
768 }
769
770 $html .= '
771 <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>
772 <p class="description">' . esc_html__( 'Enter the IP addresses to be excluded from post views count.', 'post-views-counter' ) . '</p>';
773
774 return $html;
775 }
776
777 /**
778 * Validate exclude IP addresses.
779 *
780 * @param array $input Input POST data
781 * @param array $field Field options
782 * @return array
783 */
784 public function validate_exclude_ips( $input, $field ) {
785 // get main instance
786 $pvc = Post_Views_Counter();
787
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 // get main instance
868 $pvc = Post_Views_Counter();
869
870 // any groups?
871 if ( isset( $input['restrict_display']['groups'] ) ) {
872 $groups = [];
873
874 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
875 if ( $group === 'robots' )
876 continue;
877
878 if ( isset( $field['options']['groups'][$group] ) )
879 $groups[] = $group;
880 }
881
882 $input['restrict_display']['groups'] = array_unique( $groups );
883 } else
884 $input['restrict_display']['groups'] = [];
885
886 // any roles?
887 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
888 $roles = [];
889
890 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
891 if ( isset( $field['options']['roles'][$role] ) )
892 $roles[] = $role;
893 }
894
895 $input['restrict_display']['roles'] = array_unique( $roles );
896 } else
897 $input['restrict_display']['roles'] = [];
898
899 return $input;
900 }
901 }