PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.5.4
Post Views Counter v1.5.4
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-settings.php
post-views-counter / includes Last commit date
class-admin.php 1 year ago class-columns.php 1 year ago class-counter.php 1 year ago class-crawler-detect.php 1 year ago class-cron.php 1 year ago class-dashboard.php 1 year ago class-frontend.php 1 year ago class-functions.php 1 year ago class-query.php 1 year ago class-settings-api.php 1 year ago class-settings.php 1 year ago class-update.php 1 year ago class-widgets.php 1 year ago functions.php 1 year ago
class-settings.php
1498 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' ], 12 );
21 add_action( 'pvc_settings_sidebar', [ $this, 'settings_sidebar' ], 12 );
22 add_action( 'pvc_settings_form', [ $this, 'settings_form' ], 10, 4 );
23
24 // filters
25 add_filter( 'post_views_counter_settings_data', [ $this, 'settings_data' ] );
26 add_filter( 'post_views_counter_settings_pages', [ $this, 'settings_page' ] );
27 add_filter( 'post_views_counter_settings_page_class', [ $this, 'settings_page_class' ] );
28 }
29
30 /**
31 * Add hidden inputs to redirect to valid page after changing menu position.
32 *
33 * @param string $setting
34 * @param string $page_type
35 * @param string $url_page
36 * @param string $tab_key
37 *
38 * @return void
39 */
40 public function settings_form( $setting, $page_type, $url_page, $tab_key ) {
41 // get main instance
42 $pvc = Post_Views_Counter();
43
44 // topmenu referer
45 $topmenu = '<input type="hidden" name="_wp_http_referer" data-pvc-menu="topmenu" value="' .esc_url( admin_url( 'admin.php?page=post-views-counter' . ( $tab_key !== '' ? '&tab=' . $tab_key : '' ) ) ) . '" />';
46
47 // submenu referer
48 $submenu = '<input type="hidden" name="_wp_http_referer" data-pvc-menu="submenu" value="' .esc_url( admin_url( 'options-general.php?page=post-views-counter' . ( $tab_key !== '' ? '&tab=' . $tab_key : '' ) ) ) . '" />';
49
50 if ( $pvc->options['other']['menu_position'] === 'sub' )
51 echo $topmenu . $submenu;
52 else
53 echo $submenu . $topmenu;
54 }
55
56 /**
57 * Display settings sidebar.
58 *
59 * @return void
60 */
61 public function settings_sidebar() {
62 // get main instance
63 $pvc = Post_Views_Counter();
64
65 if ( ! class_exists( 'Post_Views_Counter_Pro' ) ) {
66 echo '
67 <div class="post-views-sidebar">
68 <div class="post-views-credits">
69 <div class="inside">
70 <div class="inner">
71 <div class="pvc-sidebar-info">
72 <div class="pvc-sidebar-head">
73 <p>' . esc_html__( "You're using", 'post-views-counter' ) . '</p>
74 <h2>Post Views Counter</h2>
75 <h2>Lite</h2>
76 </div>
77 <div class="pvc-sidebar-body">
78 <p><span class="pvc-icon pvc-icon-arrow-right"></span>' . __( 'Get <b>more accurate information</b> about the number of views of your site, regardless of what the user is visiting.', 'post-views-counter' ) . '</p>
79 <p><span class="pvc-icon pvc-icon-arrow-right"></span>' . __( 'Unlock <b>optimization features</b> and speed up view count tracking.', 'post-views-counter' ) . '</p>
80 <p><span class="pvc-icon pvc-icon-arrow-right"></span>' . __( 'Take your insights to the next level with dedicated, <b>customizable reporting</b>.', 'post-views-counter' ) . '</p>
81 </div>
82 <div class="pvc-pricing-footer">
83 <a href="https://postviewscounter.com/upgrade/?utm_source=post-views-counter-lite&utm_medium=button&utm_campaign=upgrade-to-pro" class="button button-secondary button-hero pvc-button" target="_blank">' . esc_html__( 'Upgrade to Pro', 'post-views-counter' ) . '</a>
84 </div>
85 </div>
86 </div>
87 </div>
88 </div>
89 </div>';
90 }
91 }
92
93 /**
94 * Update counter mode.
95 *
96 * @return void
97 */
98 public function update_counter_mode() {
99 // get main instance
100 $pvc = Post_Views_Counter();
101
102 // get settings
103 $settings = $pvc->settings_api->get_settings();
104
105 // fast ajax as active but not available counter mode?
106 if ( $pvc->options['general']['counter_mode'] === 'ajax' && in_array( 'ajax', $settings['post-views-counter']['fields']['counter_mode']['disabled'], true ) ) {
107 // set standard javascript ajax calls
108 $pvc->options['general']['counter_mode'] = 'js';
109
110 // update database options
111 update_option( 'post_views_counter_settings_general', $pvc->options['general'] );
112 }
113 }
114
115 /**
116 * Get available counter modes.
117 *
118 * @return array
119 */
120 public function get_counter_modes() {
121 // counter modes
122 $modes = [
123 'php' => __( 'PHP', 'post-views-counter' ),
124 'js' => __( 'JavaScript', 'post-views-counter' ),
125 'rest_api' => __( 'REST API', 'post-views-counter' ),
126 'ajax' => __( 'Fast AJAX', 'post-views-counter' )
127 ];
128
129 return apply_filters( 'pvc_get_counter_modes', $modes );
130 }
131
132 /**
133 * Add settings data.
134 *
135 * @param array $settings
136 *
137 * @return array
138 */
139 public function settings_data( $settings ) {
140 // get main instance
141 $pvc = Post_Views_Counter();
142
143 // time types
144 $time_types = [
145 'minutes' => __( 'minutes', 'post-views-counter' ),
146 'hours' => __( 'hours', 'post-views-counter' ),
147 'days' => __( 'days', 'post-views-counter' ),
148 'weeks' => __( 'weeks', 'post-views-counter' ),
149 'months' => __( 'months', 'post-views-counter' ),
150 'years' => __( 'years', 'post-views-counter' )
151 ];
152
153 // user groups
154 $groups = [
155 'robots' => __( 'crawlers', 'post-views-counter' ),
156 'ai_bots' => __( 'AI bots', 'post-views-counter' ),
157 'users' => __( 'logged in users', 'post-views-counter' ),
158 'guests' => __( 'guests', 'post-views-counter' ),
159 'roles' => __( 'selected user roles', 'post-views-counter' )
160 ];
161
162 // get user roles
163 $user_roles = $pvc->functions->get_user_roles();
164
165 // get post types
166 $post_types = $pvc->functions->get_post_types();
167
168 // check object cache
169 $wp_using_ext_object_cache = wp_using_ext_object_cache();
170
171 // add settings
172 $settings['post-views-counter'] = [
173 'label' => __( 'Post Views Counter Settings', 'post-views-counter' ),
174 'form' => [
175 'reports' => [
176 'buttons' => false
177 ]
178 ],
179 'option_name' => [
180 'general' => 'post_views_counter_settings_general',
181 'display' => 'post_views_counter_settings_display',
182 'reports' => 'post_views_counter_settings_reports',
183 'other' => 'post_views_counter_settings_other'
184 ],
185 'validate' => [ $this, 'validate_settings' ],
186 'sections' => [
187 'post_views_counter_general_settings' => [
188 'tab' => 'general'
189 ],
190 'post_views_counter_display_settings' => [
191 'tab' => 'display'
192 ],
193 'post_views_counter_reports_settings' => [
194 'tab' => 'reports',
195 'callback' => [ $this, 'section_reports_placeholder' ]
196 ],
197 'post_views_counter_other_settings' => [
198 'tab' => 'other'
199 ]
200 ],
201 'fields' => [
202 'post_types_count' => [
203 'tab' => 'general',
204 'title' => __( 'Post Types Count', 'post-views-counter' ),
205 'section' => 'post_views_counter_general_settings',
206 'type' => 'checkbox',
207 'display_type' => 'horizontal',
208 'description' => __( 'Select post types for which post views will be counted.', 'post-views-counter' ),
209 'options' => $post_types
210 ],
211 'taxonomies_count' => [
212 'tab' => 'general',
213 'title' => __( 'Taxonomies Count', 'post-views-counter' ),
214 'section' => 'post_views_counter_general_settings',
215 'type' => 'custom',
216 'label' => __( 'Enable to count taxonomy terms visits.', 'post-views-counter' ),
217 'class' => 'pvc-pro',
218 'skip_saving' => true,
219 'callback' => [ $this, 'setting_taxonomies_count' ]
220 ],
221 'users_count' => [
222 'tab' => 'general',
223 'title' => __( 'Authors Count', 'post-views-counter' ),
224 'section' => 'post_views_counter_general_settings',
225 'type' => 'custom',
226 'label' => __( 'Enable to count authors archive visits.', 'post-views-counter' ),
227 'class' => 'pvc-pro',
228 'skip_saving' => true,
229 'callback' => [ $this, 'setting_users_count' ]
230 ],
231 'other_count' => [
232 'tab' => 'general',
233 'title' => __( 'Other Count', 'post-views-counter' ),
234 'section' => 'post_views_counter_general_settings',
235 'type' => 'boolean',
236 'label' => __( 'Enable to count visits of front page, post type and date archives, 404 and search pages.', 'post-views-counter' ),
237 'class' => 'pvc-pro',
238 'disabled' => true,
239 'skip_saving' => true,
240 'value' => false
241 ],
242 'counter_mode' => [
243 'tab' => 'general',
244 'title' => __( 'Counter Mode', 'post-views-counter' ),
245 'section' => 'post_views_counter_general_settings',
246 'type' => 'radio',
247 'description' => __( 'Select the method of collecting post views data. If you are using any of the caching plugins select JavaScript, REST API or Fast AJAX (up to <code>10+</code> times faster!).', 'post-views-counter' ),
248 'class' => 'pvc-pro-extended',
249 'options' => $this->get_counter_modes(),
250 'disabled' => [ 'ajax' ]
251 ],
252 'data_storage' => [
253 'tab' => 'general',
254 'title' => __( 'Data Storage', 'post-views-counter' ),
255 'section' => 'post_views_counter_general_settings',
256 'type' => 'radio',
257 'class' => 'pvc-pro',
258 'skip_saving' => true,
259 'description' => __( "Choose how to store the content views data in the user's browser - with or without cookies.", 'post-views-counter' ),
260 'options' => [
261 'cookies' => __( 'Cookies', 'post-views-counter' ),
262 'cookieless' => __( 'Cookieless', 'post-views-counter' )
263 ],
264 'disabled' => [ 'cookies', 'cookieless' ],
265 'value' => 'cookies'
266 ],
267 'amp_support' => [
268 'tab' => 'general',
269 'title' => __( 'AMP Support', 'post-views-counter' ),
270 'section' => 'post_views_counter_general_settings',
271 'type' => 'boolean',
272 'class' => 'pvc-pro',
273 'disabled' => true,
274 'skip_saving' => true,
275 'value' => false,
276 'label' => __( 'Enable to support Google AMP.', 'post-views-counter' ),
277 'description' => __( 'This feature requires official WordPress Google AMP plugin to be installed and activated.', 'post-views-counter' )
278 ],
279 'time_between_counts' => [
280 'tab' => 'general',
281 'title' => __( 'Count Interval', 'post-views-counter' ),
282 'section' => 'post_views_counter_general_settings',
283 'type' => 'custom',
284 'description' => '',
285 'min' => 0,
286 'max' => 999999,
287 'options' => $time_types,
288 'callback' => [ $this, 'setting_time_between_counts' ],
289 'validate' => [ $this, 'validate_time_between_counts' ]
290 ],
291 'strict_counts' => [
292 'tab' => 'general',
293 'title' => __( 'Strict counts', 'post-views-counter' ),
294 'section' => 'post_views_counter_general_settings',
295 'type' => 'boolean',
296 'class' => 'pvc-pro',
297 'disabled' => true,
298 'skip_saving' => true,
299 'value' => false,
300 'description' => '',
301 'label' => __( 'Enable to prevent bypassing the counts interval (for e.g. using incognito browser window or by clearing cookies).', 'post-views-counter' )
302 ],
303 'reset_counts' => [
304 'tab' => 'general',
305 'title' => __( 'Cleanup Interval', 'post-views-counter' ),
306 'section' => 'post_views_counter_general_settings',
307 'type' => 'custom',
308 'description' => sprintf( __( 'Delete single day post views data older than specified above. Enter %s if you want to preserve your daily views data regardless of its age.', 'post-views-counter' ), '<code>0</code>' ),
309 'min' => 0,
310 'max' => 999999,
311 'options' => $time_types,
312 'callback' => [ $this, 'setting_reset_counts' ],
313 'validate' => [ $this, 'validate_reset_counts' ]
314 ],
315 'caching_compatibility' => [
316 'tab' => 'general',
317 'title' => __( 'Caching Compatibility', 'post-views-counter' ),
318 'section' => 'post_views_counter_general_settings',
319 'type' => 'boolean',
320 'class' => 'pvc-pro',
321 'disabled' => true,
322 'value' => false,
323 'skip_saving' => true,
324 'label' => __( 'Enable to apply changes improving compatibility with caching plugins.', 'post-views-counter' ),
325 'description' => $this->get_caching_compatibility_description()
326 ],
327 'object_cache' => [
328 'tab' => 'general',
329 'title' => __( 'Object Cache Support', 'post-views-counter' ),
330 'section' => 'post_views_counter_general_settings',
331 'type' => 'boolean',
332 'class' => 'pvc-pro',
333 'disabled' => true,
334 'value' => false,
335 'skip_saving' => true,
336 'label' => sprintf( __( 'Enable to use object cache optimization.', 'post-views-counter' ), '<code>Redis</code>', '<code>Memcached</code>' ),
337 'description' => sprintf( __( 'This feature requires a persistent object cache like %s or %s to be installed and activated.', 'post-views-counter' ), '<code>Redis</code>', '<code>Memcached</code>' ) . '<br />' . __( 'Current status', 'post-views-counter' ) . ': <span class="' . ( $wp_using_ext_object_cache ? '' : 'un' ) . 'available">' . ( $wp_using_ext_object_cache ? __( 'available', 'post-views-counter' ) : __( 'unavailable', 'post-views-counter' ) ) . '</span>.'
338 ],
339 'exclude' => [
340 'tab' => 'general',
341 'title' => __( 'Exclude Visitors', 'post-views-counter' ),
342 'section' => 'post_views_counter_general_settings',
343 'type' => 'custom',
344 'description' => '',
345 'class' => 'pvc-pro-extended',
346 'options' => [
347 'groups' => $groups,
348 'roles' => $user_roles
349 ],
350 'disabled' => [
351 'groups' => [ 'ai_bots' ]
352 ],
353 'callback' => [ $this, 'setting_exclude' ],
354 'validate' => [ $this, 'validate_exclude' ]
355 ],
356 'exclude_ips' => [
357 'tab' => 'general',
358 'title' => __( 'Exclude IPs', 'post-views-counter' ),
359 'section' => 'post_views_counter_general_settings',
360 'type' => 'custom',
361 'description' => '',
362 'callback' => [ $this, 'setting_exclude_ips' ],
363 'validate' => [ $this, 'validate_exclude_ips' ]
364 ],
365 'label' => [
366 'tab' => 'display',
367 'title' => __( 'Views Label', 'post-views-counter' ),
368 'section' => 'post_views_counter_display_settings',
369 'type' => 'input',
370 'description' => __( 'Enter the label for the post views counter field.', 'post-views-counter' ),
371 'subclass' => 'regular-text',
372 'validate' => [ $this, 'validate_label' ],
373 'reset' => [ $this, 'reset_label' ]
374 ],
375 'display_period' => [
376 'tab' => 'display',
377 'title' => __( 'Views Period', 'post-views-counter' ),
378 'section' => 'post_views_counter_display_settings',
379 'type' => 'select',
380 'class' => 'pvc-pro',
381 'disabled' => true,
382 'skip_saving' => true,
383 'description' => __( 'Select the time period to be included when displaying the number of views. The default display is the total number of views of the post.', 'post-views-counter' ),
384 'options' => [
385 'total' => __( 'Total Views', 'post-views-counter' )
386 ]
387 ],
388 'display_style' => [
389 'tab' => 'display',
390 'title' => __( 'Display Style', 'post-views-counter' ),
391 'section' => 'post_views_counter_display_settings',
392 'type' => 'custom',
393 'description' => __( 'Choose how to display the post views counter.', 'post-views-counter' ),
394 'callback' => [ $this, 'setting_display_style' ],
395 'validate' => [ $this, 'validate_display_style' ],
396 'options' => [
397 'icon' => __( 'icon', 'post-views-counter' ),
398 'text' => __( 'label', 'post-views-counter' )
399 ]
400 ],
401 'icon_class' => [
402 'tab' => 'display',
403 'title' => __( 'Icon Class', 'post-views-counter' ),
404 'section' => 'post_views_counter_display_settings',
405 'type' => 'class',
406 'default' => '',
407 '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/' ),
408 'subclass' => 'regular-text'
409 ],
410 'position' => [
411 'tab' => 'display',
412 'title' => __( 'Position', 'post-views-counter' ),
413 'section' => 'post_views_counter_display_settings',
414 'type' => 'select',
415 '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>' ),
416 'options' => [
417 'before' => __( 'before the content', 'post-views-counter' ),
418 'after' => __( 'after the content', 'post-views-counter' ),
419 'manual' => __( 'manual', 'post-views-counter' )
420 ]
421 ],
422 'post_views_column' => [
423 'tab' => 'display',
424 'title' => __( 'Admin Column', 'post-views-counter' ),
425 'section' => 'post_views_counter_display_settings',
426 'type' => 'boolean',
427 'description' => '',
428 'label' => __( 'Enable to display post views count admin column for each counted content type.', 'post-views-counter' )
429 ],
430 'restrict_edit_views' => [
431 'tab' => 'display',
432 'title' => __( 'Admin Edit', 'post-views-counter' ),
433 'section' => 'post_views_counter_display_settings',
434 'type' => 'boolean',
435 'description' => '',
436 'label' => __( 'Enable to restrict post views editing to admins only.', 'post-views-counter' )
437 ],
438 'dynamic_loading' => [
439 'tab' => 'display',
440 'title' => __( 'Dynamic Loading', 'post-views-counter' ),
441 'section' => 'post_views_counter_display_settings',
442 'type' => 'boolean',
443 'class' => 'pvc-pro',
444 'disabled' => true,
445 'skip_saving' => true,
446 'value' => false,
447 'label' => __( 'Enable dynamic loading and prevent caching of the displayed views count.', 'post-views-counter' )
448 ],
449 'use_format' => [
450 'tab' => 'display',
451 'title' => __( 'Format Number', 'post-views-counter' ),
452 'section' => 'post_views_counter_display_settings',
453 'type' => 'boolean',
454 'label' => __( 'Enable to display the views number formatted based on the locale (using the WP number_format_i18n function).', 'post-views-counter' )
455 ],
456 'taxonomies_display' => [
457 'tab' => 'display',
458 'title' => __( 'Taxonomies', 'post-views-counter' ),
459 'section' => 'post_views_counter_display_settings',
460 'type' => 'custom',
461 'class' => 'pvc-pro',
462 'skip_saving' => true,
463 'options' => $pvc->functions->get_taxonomies( 'labels' ),
464 'callback' => [ $this, 'setting_taxonomies_display' ]
465 ],
466 'user_display' => [
467 'tab' => 'display',
468 'title' => __( 'Authors', 'post-views-counter' ),
469 'section' => 'post_views_counter_display_settings',
470 'type' => 'boolean',
471 'class' => 'pvc-pro',
472 'disabled' => true,
473 'skip_saving' => true,
474 'value' => false,
475 'label' => __( 'Display number of views on authors archive pages.', 'post-views-counter' )
476 ],
477 'post_types_display' => [
478 'tab' => 'display',
479 'title' => __( 'Post Type', 'post-views-counter' ),
480 'section' => 'post_views_counter_display_settings',
481 'type' => 'checkbox',
482 'display_type' => 'horizontal',
483 'description' => __( 'Select post types for which the views count will be displayed.', 'post-views-counter' ),
484 'options' => $post_types
485 ],
486 'page_types_display' => [
487 'tab' => 'display',
488 'title' => __( 'Page Type', 'post-views-counter' ),
489 'section' => 'post_views_counter_display_settings',
490 'type' => 'checkbox',
491 'display_type' => 'horizontal',
492 'description' => __( 'Select page types where the views count will be displayed.', 'post-views-counter' ),
493 'options' => apply_filters(
494 'pvc_page_types_display_options',
495 [
496 'home' => __( 'Home', 'post-views-counter' ),
497 'archive' => __( 'Archives', 'post-views-counter' ),
498 'singular' => __( 'Single pages', 'post-views-counter' ),
499 'search' => __( 'Search results', 'post-views-counter' ),
500 ]
501 )
502 ],
503 'restrict_display' => [
504 'tab' => 'display',
505 'title' => __( 'User Type', 'post-views-counter' ),
506 'section' => 'post_views_counter_display_settings',
507 'type' => 'custom',
508 'description' => '',
509 'options' => [
510 'groups' => $groups,
511 'roles' => $user_roles
512 ],
513 'callback' => [ $this, 'setting_restrict_display' ],
514 'validate' => [ $this, 'validate_restrict_display' ]
515 ],
516 'toolbar_statistics' => [
517 'tab' => 'display',
518 'title' => __( 'Toolbar Chart', 'post-views-counter' ),
519 'section' => 'post_views_counter_display_settings',
520 'type' => 'boolean',
521 'description' => __( 'The post views chart will be displayed for the post types that are being counted.', 'post-views-counter' ),
522 'label' => __( 'Enable to display the post views chart at the toolbar.', 'post-views-counter' )
523 ],
524 'license' => [
525 'tab' => 'other',
526 'title' => __( 'License', 'post-views-counter' ),
527 'section' => 'post_views_counter_other_settings',
528 'disabled' => true,
529 'value' => $pvc->options['other']['license'],
530 'type' => 'input',
531 'description' => __( 'Enter your Post Views Counter Pro license key (requires Pro version to be installed and active).', 'post-views-counter' ),
532 'subclass' => 'regular-text',
533 'validate' => [ $this, 'validate_license' ],
534 'append' => '<span class="pvc-icon license-icon"></span>'
535 ],
536 'menu_position' => [
537 'tab' => 'other',
538 'title' => __( 'Menu Position', 'post-views-counter' ),
539 'section' => 'post_views_counter_other_settings',
540 'type' => 'radio',
541 'options' => [
542 'top' => __( 'Top menu', 'post-views-counter' ),
543 'sub' => __( 'Settings submenu', 'post-views-counter' )
544 ],
545 'description' => __( "Choose where to display the plugin's menu.", 'post-views-counter' ),
546 ],
547 'import_views' => [
548 'tab' => 'other',
549 'title' => __( 'Import Views', 'post-views-counter' ),
550 'section' => 'post_views_counter_other_settings',
551 'type' => 'custom',
552 'description' => '',
553 'skip_saving' => true,
554 'callback' => [ $this, 'setting_import_views' ]
555 ],
556 'delete_views' => [
557 'tab' => 'other',
558 'title' => __( 'Delete Views', 'post-views-counter' ),
559 'section' => 'post_views_counter_other_settings',
560 'type' => 'custom',
561 'description' => '',
562 'skip_saving' => true,
563 'callback' => [ $this, 'setting_delete_views' ]
564 ],
565 'deactivation_delete' => [
566 'tab' => 'other',
567 'title' => __( 'Deactivation', 'post-views-counter' ),
568 'section' => 'post_views_counter_other_settings',
569 'type' => 'boolean',
570 'description' => __( 'If you deactivate the plugin with this option enabled all plugin data will be deleted along with the number of post views.', 'post-views-counter' ),
571 'label' => __( 'Enable to delete all plugin data on deactivation.', 'post-views-counter' )
572 ]
573 ]
574 ];
575
576 return $settings;
577 }
578
579 /**
580 * Add settings page.
581 *
582 * @param array $pages
583 *
584 * @return array
585 */
586 public function settings_page( $pages ) {
587 // get main instance
588 $pvc = Post_Views_Counter();
589
590 // default page
591 $pages['post-views-counter'] = [
592 'menu_slug' => 'post-views-counter',
593 'page_title' => __( 'Post Views Counter Settings', 'post-views-counter' ),
594 'menu_title' => $pvc->options['other']['menu_position'] === 'sub' ? __( 'Post Views Counter', 'post-views-counter' ) : __( 'Post Views', 'post-views-counter' ),
595 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
596 'callback' => null,
597 'tabs' => [
598 'general' => [
599 'label' => __( 'Counting', 'post-views-counter' ),
600 'option_name' => 'post_views_counter_settings_general'
601 ],
602 'display' => [
603 'label' => __( 'Display', 'post-views-counter' ),
604 'option_name' => 'post_views_counter_settings_display'
605 ],
606 'reports' => [
607 'label' => __( 'Reports', 'post-views-counter' ),
608 'option_name' => 'post_views_counter_settings_reports'
609 ],
610 'other' => [
611 'label' => __( 'Other', 'post-views-counter' ),
612 'option_name' => 'post_views_counter_settings_other'
613 ]
614 ]
615 ];
616
617 // submenu?
618 if ( $pvc->options['other']['menu_position'] === 'sub' ) {
619 $pages['post-views-counter']['type'] = 'settings_page';
620 // topmenu?
621 } else {
622 // highlight submenus
623 add_filter( 'submenu_file', [ $this, 'submenu_file' ], 10, 2 );
624
625 // add parameters
626 $pages['post-views-counter']['type'] = 'page';
627 $pages['post-views-counter']['icon'] = 'dashicons-chart-bar';
628 $pages['post-views-counter']['position'] = '99.301';
629
630 // add subpages
631 $pages['post-views-counter-general'] = [
632 'menu_slug' => 'post-views-counter',
633 'parent_slug' => 'post-views-counter',
634 'type' => 'subpage',
635 'page_title' => __( 'Counting', 'post-views-counter' ),
636 'menu_title' => __( 'Counting', 'post-views-counter' ),
637 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
638 'callback' => null
639 ];
640
641 $pages['post-views-counter-display'] = [
642 'menu_slug' => 'post-views-counter&tab=display',
643 'parent_slug' => 'post-views-counter',
644 'type' => 'subpage',
645 'page_title' => __( 'Display', 'post-views-counter' ),
646 'menu_title' => __( 'Display', 'post-views-counter' ),
647 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
648 'callback' => null
649 ];
650
651 $pages['post-views-counter-reports'] = [
652 'menu_slug' => 'post-views-counter&tab=reports',
653 'parent_slug' => 'post-views-counter',
654 'type' => 'subpage',
655 'page_title' => __( 'Reports', 'post-views-counter' ),
656 'menu_title' => __( 'Reports', 'post-views-counter' ),
657 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
658 'callback' => null
659 ];
660
661 $pages['post-views-counter-other'] = [
662 'menu_slug' => 'post-views-counter&tab=other',
663 'parent_slug' => 'post-views-counter',
664 'type' => 'subpage',
665 'page_title' => __( 'Other', 'post-views-counter' ),
666 'menu_title' => __( 'Other', 'post-views-counter' ),
667 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
668 'callback' => null
669 ];
670 }
671
672 return $pages;
673 }
674
675 /**
676 * Settings page CSS class(es).
677 *
678 * @param array $class
679 * @return array
680 */
681 public function settings_page_class( $class ) {
682 $is_pro = class_exists( 'Post_Views_Counter_Pro' );
683
684 if ( ! $is_pro )
685 $class[] = 'has-sidebar';
686
687 return $class;
688 }
689
690 /**
691 * Highlight submenu items.
692 *
693 * @param string|null $submenu_file
694 * @param string $parent_file
695 * @return string|null
696 */
697 public function submenu_file( $submenu_file, $parent_file ) {
698 if ( $parent_file === 'post-views-counter' ) {
699 $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general';
700
701 if ( $tab !== 'general' )
702 return 'post-views-counter&tab=' . $tab;
703 }
704
705 return $submenu_file;
706 }
707
708 /**
709 * Validate options.
710 *
711 * @global object $wpdb
712 *
713 * @param array $input
714 * @return array
715 */
716 public function validate_settings( $input ) {
717 // check capability
718 if ( ! current_user_can( 'manage_options' ) )
719 return $input;
720
721 global $wpdb;
722
723 // get main instance
724 $pvc = Post_Views_Counter();
725
726 // use internal settings api to validate settings first
727 $input = $pvc->settings_api->validate_settings( $input );
728
729 // update meta key on save changes
730 if ( isset( $_POST['post_views_counter_import_meta_key'] ) && isset( $_POST['save_post_views_counter_settings_other'] ) )
731 $input['import_meta_key'] = sanitize_key( $_POST['post_views_counter_import_meta_key'] );
732
733 // import post views data from another plugin
734 if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
735 // make sure we do not change anything in the settings
736 $input = $pvc->options['other'];
737
738 // get views key
739 $meta_key = sanitize_key( apply_filters( 'pvc_import_meta_key', ( isset( $_POST['post_views_counter_import_meta_key'] ) ? $_POST['post_views_counter_import_meta_key'] : $pvc->options['other']['import_meta_key'] ) ) );
740
741 // set meta_key option
742 $input['import_meta_key'] = $meta_key;
743
744 // get views
745 $views = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = %s", $meta_key ), ARRAY_A, 0 );
746
747 // any views?
748 if ( ! empty( $views ) ) {
749 $sql = [];
750
751 foreach ( $views as $view ) {
752 $sql[] = $wpdb->prepare( "(%d, 4, 'total', %d)", (int) $view['post_id'], (int) $view['meta_value'] );
753 }
754
755 $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)" );
756
757 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported successfully.', 'post-views-counter' ), 'updated' );
758 } else
759 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
760 // delete all post views data
761 } elseif ( isset( $_POST['post_views_counter_reset_views'] ) ) {
762 // make sure we do not change anything in the settings
763 $input = $pvc->options['other'];
764
765 if ( $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . 'post_views' ) )
766 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'All existing data deleted successfully.', 'post-views-counter' ), 'updated' );
767 else
768 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'Error occurred. All existing data were not deleted.', 'post-views-counter' ), 'error' );
769 // save general settings
770 } elseif ( isset( $_POST['save_post_views_counter_settings_general'] ) ) {
771 $input['update_version'] = $pvc->options['general']['update_version'];
772 $input['update_notice'] = $pvc->options['general']['update_notice'];
773 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
774 // reset general settings
775 } elseif ( isset( $_POST['reset_post_views_counter_settings_general'] ) ) {
776 $input['update_version'] = $pvc->options['general']['update_version'];
777 $input['update_notice'] = $pvc->options['general']['update_notice'];
778 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
779 }
780
781 return $input;
782 }
783
784 /**
785 * Get caching compatibility description.
786 *
787 * @return array
788 */
789 public function get_caching_compatibility_description() {
790 // caching compatibility description
791 $caching_compatibility_desc = '';
792
793 // get active caching plugins
794 $active_plugins = $this->get_active_caching_plugins();
795
796 if ( ! empty( $active_plugins ) ) {
797 $empty_active_caching_plugins = false;
798 $active_plugins_html = [];
799
800 $caching_compatibility_desc .= esc_html__( 'Currently detected active caching plugins', 'cookie-notice' ) . ': ';
801
802 foreach ( $active_plugins as $plugin ) {
803 $active_plugins_html[] = '<code>' . esc_html( $plugin ) . '</code>';
804 }
805
806 $caching_compatibility_desc .= implode( ', ', $active_plugins_html ) . '.';
807 } else {
808 $empty_active_caching_plugins = true;
809
810 $caching_compatibility_desc .= esc_html__( 'No compatible caching plugins found.', 'cookie-notice' );
811 }
812
813 return $caching_compatibility_desc . '<br />' . __( 'Current status', 'post-views-counter' ) . ': <span class="' . ( ! $empty_active_caching_plugins ? '' : 'un' ) . 'available">' . ( ! $empty_active_caching_plugins ? __( 'available', 'post-views-counter' ) : __( 'unavailable', 'post-views-counter' ) ) . '</span>.';
814 }
815
816 /**
817 * Get active caching plugins.
818 *
819 * @return array
820 */
821 public function get_active_caching_plugins() {
822 $active_plugins = [];
823
824 // autoptimize
825 if ( $this->is_plugin_active( 'autoptimize' ) )
826 $active_plugins[] = 'Autoptimize';
827
828 // breeze
829 // if ( $this->is_plugin_active( 'breeze' ) )
830 // $active_plugins[] = 'Breeze';
831
832 // hummingbird
833 if ( $this->is_plugin_active( 'hummingbird' ) )
834 $active_plugins[] = 'Hummingbird';
835
836 // litespeed
837 if ( $this->is_plugin_active( 'litespeed' ) )
838 $active_plugins[] = 'LiteSpeed Cache';
839
840 // speed optimizer
841 if ( $this->is_plugin_active( 'speedoptimizer' ) )
842 $active_plugins[] = 'Speed Optimizer';
843
844 // speedycache
845 if ( $this->is_plugin_active( 'speedycache' ) )
846 $active_plugins[] = 'SpeedyCache';
847
848 // wp fastest cache
849 if ( $this->is_plugin_active( 'wpfastestcache' ) )
850 $active_plugins[] = 'WP Fastest Cache';
851
852 // wp-optimize
853 if ( $this->is_plugin_active( 'wpoptimize' ) )
854 $active_plugins[] = 'WP-Optimize';
855
856 // wp rocket
857 if ( $this->is_plugin_active( 'wprocket' ) )
858 $active_plugins[] = 'WP Rocket';
859
860 // wp super cache
861 // if ( $this->is_plugin_active( 'wpsupercache' ) )
862 // $active_plugins[] = 'WP Super Cache';
863
864 return apply_filters( 'pvc_active_caching_plugins', $active_plugins );
865 }
866
867 /**
868 * Check whether specified plugin is active.
869 *
870 * @global object $siteground_optimizer_loader
871 * @global int $wpsc_version
872 *
873 * @param string $plugin
874 *
875 * @return bool
876 */
877 public function is_plugin_active( $plugin = '' ) {
878 // set default flag
879 $is_plugin_active = false;
880
881 switch ( $plugin ) {
882 // autoptimize
883 case 'autoptimize':
884 if ( function_exists( 'autoptimize' ) && defined( 'AUTOPTIMIZE_PLUGIN_VERSION' ) && version_compare( AUTOPTIMIZE_PLUGIN_VERSION, '2.4', '>=' ) )
885 $is_plugin_active = true;
886 break;
887
888 // breeze
889 // case 'breeze':
890 // if ( class_exists( 'Breeze_PurgeCache' ) && class_exists( 'Breeze_Options_Reader' ) && function_exists( 'breeze_get_option' ) && function_exists( 'breeze_update_option' ) && defined( 'BREEZE_VERSION' ) && version_compare( BREEZE_VERSION, '1.1.0', '>=' ) )
891 // $is_plugin_active = true;
892 // break;
893
894 // hummingbird
895 case 'hummingbird':
896 if ( class_exists( 'Hummingbird\\WP_Hummingbird' ) && defined( 'WPHB_VERSION' ) && version_compare( WPHB_VERSION, '2.1.0', '>=' ) )
897 $is_plugin_active = true;
898 break;
899
900 // litespeed
901 case 'litespeed':
902 if ( class_exists( 'LiteSpeed\Core' ) && defined( 'LSCWP_CUR_V' ) && version_compare( LSCWP_CUR_V, '3.0', '>=' ) )
903 $is_plugin_active = true;
904 break;
905
906 // speed optimizer
907 case 'speedoptimizer':
908 global $siteground_optimizer_loader;
909
910 if ( ! empty( $siteground_optimizer_loader ) && is_object( $siteground_optimizer_loader ) && is_a( $siteground_optimizer_loader, 'SiteGround_Optimizer\Loader\Loader' ) && defined( '\SiteGround_Optimizer\VERSION' ) && version_compare( \SiteGround_Optimizer\VERSION, '5.5', '>=' ) )
911 $is_plugin_active = true;
912 break;
913
914 // speedycache
915 case 'speedycache':
916 if ( class_exists( 'SpeedyCache' ) && defined( 'SPEEDYCACHE_VERSION' ) && function_exists( 'speedycache_delete_cache' ) && version_compare( SPEEDYCACHE_VERSION, '1.0.0', '>=' ) )
917 $is_plugin_active = true;
918 break;
919
920 // wp fastest cache
921 case 'wpfastestcache':
922 if ( function_exists( 'wpfc_clear_all_cache' ) )
923 $is_plugin_active = true;
924 break;
925
926 // wp-optimize
927 case 'wpoptimize':
928 if ( function_exists( 'WP_Optimize' ) && defined( 'WPO_VERSION' ) && version_compare( WPO_VERSION, '3.0.12', '>=' ) )
929 $is_plugin_active = true;
930 break;
931
932 // wp rocket
933 case 'wprocket':
934 if ( function_exists( 'rocket_init' ) && defined( 'WP_ROCKET_VERSION' ) && version_compare( WP_ROCKET_VERSION, '3.8', '>=' ) )
935 $is_plugin_active = true;
936 break;
937
938 // wp super cache
939 // case 'wpsupercache':
940 // global $wpsc_version;
941
942 // if ( ( ! empty( $wpsc_version ) && $wpsc_version >= 169 ) || ( defined( 'WPSC_VERSION' ) && version_compare( WPSC_VERSION, '1.6.9', '>=' ) ) )
943 // $is_plugin_active = true;
944 // break;
945
946 // other caching plugin
947 default:
948 $is_plugin_active = apply_filters( 'pvc_is_plugin_active', false, $plugin );
949 }
950
951 return $is_plugin_active;
952 }
953
954 /**
955 * Setting: taxonomies count.
956 *
957 * @param array $field
958 * @return string
959 */
960 public function setting_taxonomies_count( $field ) {
961 $html = '
962 <label><input id="post_views_counter_general_taxonomies_count" type="checkbox" name="" value="" disabled />' . esc_html( $field['label'] ) . '</label>';
963
964 return $html;
965 }
966
967 /**
968 * Setting: users count.
969 *
970 * @param array $field
971 * @return string
972 */
973 public function setting_users_count( $field ) {
974 // get base instance
975 $pvc = Post_Views_Counter();
976
977 $html = '
978 <label><input id="post_views_counter_general_users_count" type="checkbox" name="" value="" disabled />' . esc_html( $field['label'] ) . '</label>';
979
980 return $html;
981 }
982
983 /**
984 * Setting: taxonomies count.
985 *
986 * @param array $field
987 * @return string
988 */
989 public function setting_taxonomies_display( $field ) {
990 // get base instance
991 $pvc = Post_Views_Counter();
992
993 $html = '';
994
995 foreach ( $field['options'] as $taxonomy => $label ) {
996 $html .= '
997 <label><input type="checkbox" name="" value="" disabled />' . esc_html( $label ) . '</label>';
998 }
999
1000 $html .= '
1001 <p class="description">' . esc_html__( 'Select taxonomies for which the views count will be displayed.', 'post-views-counter' ) . '</p>';
1002
1003 return $html;
1004 }
1005
1006 /**
1007 * Validate label.
1008 *
1009 * @param array $input
1010 * @param array $field
1011 * @return array
1012 */
1013 public function validate_label( $input, $field ) {
1014 // get main instance
1015 $pvc = Post_Views_Counter();
1016
1017 if ( ! isset( $input ) )
1018 $input = $pvc->defaults['display']['label'];
1019
1020 // use internal settings API to validate settings first
1021 $input = $pvc->settings_api->validate_field( $input, 'input', $field );
1022
1023 if ( function_exists( 'icl_register_string' ) )
1024 icl_register_string( 'Post Views Counter', 'Post Views Label', $input );
1025
1026 return $input;
1027 }
1028
1029 /**
1030 * Restore post views label to default value.
1031 *
1032 * @param array $default
1033 * @param array $field
1034 * @return array
1035 */
1036 public function reset_label( $default, $field ) {
1037 if ( function_exists( 'icl_register_string' ) )
1038 icl_register_string( 'Post Views Counter', 'Post Views Label', $default );
1039
1040 return $default;
1041 }
1042
1043 /**
1044 * Setting: display style.
1045 *
1046 * @param array $field
1047 * @return string
1048 */
1049 public function setting_display_style( $field ) {
1050 // get main instance
1051 $pvc = Post_Views_Counter();
1052
1053 $html = '
1054 <input type="hidden" name="post_views_counter_settings_display[display_style]" value="empty" />';
1055
1056 foreach ( $field['options'] as $key => $label ) {
1057 $html .= '
1058 <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> ';
1059 }
1060
1061 return $html;
1062 }
1063
1064 /**
1065 * Validate display style.
1066 *
1067 * @param array $input
1068 * @param array $field
1069 * @return array
1070 */
1071 public function validate_display_style( $input, $field ) {
1072 // get main instance
1073 $pvc = Post_Views_Counter();
1074
1075 $data = [];
1076
1077 foreach ( $field['options'] as $value => $label ) {
1078 $data[$value] = false;
1079 }
1080
1081 // any data?
1082 if ( ! empty( $input['display_style'] && $input['display_style'] !== 'empty' && is_array( $input['display_style'] ) ) ) {
1083 foreach ( $input['display_style'] as $value ) {
1084 if ( array_key_exists( $value, $field['options'] ) )
1085 $data[$value] = true;
1086 }
1087 }
1088
1089 $input['display_style'] = $data;
1090
1091 return $input;
1092 }
1093
1094 /**
1095 * Setting: count interval.
1096 *
1097 * @param array $field
1098 * @return string
1099 */
1100 public function setting_time_between_counts( $field ) {
1101 // get main instance
1102 $pvc = Post_Views_Counter();
1103
1104 $html = '
1105 <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'] ) . '" />
1106 <select name="post_views_counter_settings_general[time_between_counts][type]">';
1107
1108 foreach ( $field['options'] as $type => $type_name ) {
1109 $html .= '
1110 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['time_between_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
1111 }
1112
1113 $html .= '
1114 </select>
1115 <p class="description">' . sprintf( __( 'Enter the time between single user visit count. Enter %s if you want to count every page view.', 'post-views-counter' ), '<code>0</code>' ) . '</p>';
1116
1117 return $html;
1118 }
1119
1120 /**
1121 * Validate count interval.
1122 *
1123 * @param array $input
1124 * @param array $field
1125 * @return array
1126 */
1127 public function validate_time_between_counts( $input, $field ) {
1128 // get main instance
1129 $pvc = Post_Views_Counter();
1130
1131 // number
1132 $input['time_between_counts']['number'] = isset( $input['time_between_counts']['number'] ) ? (int) $input['time_between_counts']['number'] : $pvc->defaults['general']['time_between_counts']['number'];
1133
1134 if ( $input['time_between_counts']['number'] < $field['min'] || $input['time_between_counts']['number'] > $field['max'] )
1135 $input['time_between_counts']['number'] = $pvc->defaults['general']['time_between_counts']['number'];
1136
1137 // type
1138 $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'];
1139
1140 return $input;
1141 }
1142
1143 /**
1144 * Setting: reset data interval.
1145 *
1146 * @param array $field
1147 * @return string
1148 */
1149 public function setting_reset_counts( $field ) {
1150 // get main instance
1151 $pvc = Post_Views_Counter();
1152
1153 $html = '
1154 <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'] ) . '" />
1155 <select name="post_views_counter_settings_general[reset_counts][type]">';
1156
1157 foreach ( array_slice( $field['options'], 2, null, true ) as $type => $type_name ) {
1158 $html .= '
1159 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['reset_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
1160 }
1161
1162 $html .= '
1163 </select>';
1164
1165 return $html;
1166 }
1167
1168 /**
1169 * Validate reset data interval.
1170 *
1171 * @param array $input
1172 * @param array $field
1173 * @return array
1174 */
1175 public function validate_reset_counts( $input, $field ) {
1176 // get main instance
1177 $pvc = Post_Views_Counter();
1178
1179 // number
1180 $input['reset_counts']['number'] = isset( $input['reset_counts']['number'] ) ? (int) $input['reset_counts']['number'] : $pvc->defaults['general']['reset_counts']['number'];
1181
1182 if ( $input['reset_counts']['number'] < $field['min'] || $input['reset_counts']['number'] > $field['max'] )
1183 $input['reset_counts']['number'] = $pvc->defaults['general']['reset_counts']['number'];
1184
1185 // type
1186 $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'];
1187
1188 // run cron on next visit?
1189 $input['cron_run'] = ( $input['reset_counts']['number'] > 0 );
1190
1191 // cron update?
1192 $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'] ) );
1193
1194 return $input;
1195 }
1196
1197 /**
1198 * Setting: object cache.
1199 *
1200 * @param array $field
1201 * @return string
1202 */
1203 public function setting_object_cache( $field ) {
1204 // get main instance
1205 $pvc = Post_Views_Counter();
1206
1207 // check object cache
1208 $wp_using_ext_object_cache = wp_using_ext_object_cache();
1209
1210 $html = '
1211 <input size="4" type="number" min="' . ( (int) $field['min'] ) . '" max="' . ( (int) $field['max'] ) . '" name="" value="0" disabled /> <span>' . __( 'minutes', 'post-views-counter' ) . '</span>
1212 <p class="">' . __( 'Persistent Object Cache', 'post-views-counter' ) . ': <span class="' . ( $wp_using_ext_object_cache ? '' : 'un' ) . 'available">' . ( $wp_using_ext_object_cache ? __( 'available', 'post-views-counter' ) : __( 'unavailable', 'post-views-counter' ) ) . '</span></p>
1213 <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 like %s or %s 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. The maximum value is %s which means 24 hours.%sNotice:%s Potential data loss may occur if the object cache is cleared/unavailable for the duration of the interval.', 'post-views-counter' ), '<code>Redis</code>', '<code>Memcached</code>', '<code>0</code>', '<code>1440</code>', '<br /><strong> ', '</strong>' ) . '</p>';
1214
1215 return $html;
1216 }
1217
1218 /**
1219 * Setting: exclude visitors.
1220 *
1221 * @param array $field
1222 * @return string
1223 */
1224 public function setting_exclude( $field ) {
1225 // get main instance
1226 $pvc = Post_Views_Counter();
1227
1228 $html = '';
1229
1230 foreach ( $field['options']['groups'] as $type => $type_name ) {
1231 $is_disabled = ! empty( $field['disabled']['groups'] ) && in_array( $type, $field['disabled']['groups'], true );
1232
1233 $html .= '
1234 <label for="' . esc_attr( 'pvc_exclude-' . $type ) . '"><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 ) && ! $is_disabled, true, false ) . ' ' . disabled( $is_disabled, true, false ) . ' />' . esc_html( $type_name ) . '</label>';
1235 }
1236
1237 $html .= '
1238 <p class="description">' . __( 'Use it exclude specific user groups from post views count.', 'post-views-counter' ) . '</p>
1239 <div class="pvc_user_roles pvc_subfield"' . ( in_array( 'roles', $pvc->options['general']['exclude']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
1240
1241 foreach ( $field['options']['roles'] as $role => $role_name ) {
1242 $html .= '
1243 <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>';
1244 }
1245
1246 $html .= '
1247 <p class="description">' . __( 'Use it to exclude specific user roles from post views count.', 'post-views-counter' ) . '</p>
1248 </div>';
1249
1250 return $html;
1251 }
1252
1253 /**
1254 * Validate exclude visitors.
1255 *
1256 * @param array $input
1257 * @param array $field
1258 * @return array
1259 */
1260 public function validate_exclude( $input, $field ) {
1261 // any groups?
1262 if ( isset( $input['exclude']['groups'] ) ) {
1263 $groups = [];
1264
1265 foreach ( $input['exclude']['groups'] as $group => $set ) {
1266 // disallow disabled checkboxes
1267 if ( ! empty( $field['disabled']['groups'] ) && in_array( $group, $field['disabled']['groups'], true ) )
1268 continue;
1269
1270 if ( isset( $field['options']['groups'][$group] ) )
1271 $groups[] = $group;
1272 }
1273
1274 $input['exclude']['groups'] = array_unique( $groups );
1275 } else
1276 $input['exclude']['groups'] = [];
1277
1278 // any roles?
1279 if ( in_array( 'roles', $input['exclude']['groups'], true ) && isset( $input['exclude']['roles'] ) ) {
1280 $roles = [];
1281
1282 foreach ( $input['exclude']['roles'] as $role => $set ) {
1283 if ( isset( $field['options']['roles'][$role] ) )
1284 $roles[] = $role;
1285 }
1286
1287 $input['exclude']['roles'] = array_unique( $roles );
1288 } else
1289 $input['exclude']['roles'] = [];
1290
1291 return $input;
1292 }
1293
1294 /**
1295 * Setting: exclude IP addresses.
1296 *
1297 * @return string
1298 */
1299 public function setting_exclude_ips() {
1300 // get ip addresses
1301 $ips = Post_Views_Counter()->options['general']['exclude_ips'];
1302
1303 $html = '';
1304
1305 // any ip addresses?
1306 if ( ! empty( $ips ) ) {
1307 foreach ( $ips as $key => $ip ) {
1308 $html .= '
1309 <div class="ip-box">
1310 <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>
1311 </div>';
1312 }
1313 } else {
1314 $html .= '
1315 <div class="ip-box">
1316 <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' ) . '" style="display: none">' . esc_html__( 'Remove', 'post-views-counter' ) . '</a>
1317 </div>';
1318 }
1319
1320 $html .= '
1321 <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>
1322 <p class="description">' . esc_html__( 'Enter the IP addresses to be excluded from post views count.', 'post-views-counter' ) . '</p>';
1323
1324 return $html;
1325 }
1326
1327 /**
1328 * Validate exclude IP addresses.
1329 *
1330 * @param array $input
1331 * @param array $field
1332 * @return array
1333 */
1334 public function validate_exclude_ips( $input, $field ) {
1335 // any ip addresses?
1336 if ( isset( $input['exclude_ips'] ) ) {
1337 $ips = [];
1338
1339 foreach ( $input['exclude_ips'] as $ip ) {
1340 if ( strpos( $ip, '*' ) !== false ) {
1341 $new_ip = str_replace( '*', '0', $ip );
1342
1343 if ( filter_var( $new_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
1344 $ips[] = $ip;
1345 } elseif ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
1346 $ips[] = $ip;
1347 }
1348
1349 $input['exclude_ips'] = array_unique( $ips );
1350 }
1351
1352 return $input;
1353 }
1354
1355 /**
1356 * Setting: tools.
1357 *
1358 * @return string
1359 */
1360 public function setting_import_views() {
1361 $html = '
1362 <div>
1363 <input type="text" class="regular-text" name="post_views_counter_import_meta_key" value="' . esc_attr( Post_Views_Counter()->options['other']['import_meta_key'] ) . '"/>
1364 <p class="description">' . esc_html__( 'Enter the meta key from which the views data is to be retrieved during import.', 'post-views-counter' ) . '</p>
1365 </div>
1366 <div class="pvc-subfield">
1367 <input type="submit" class="button button-secondary" name="post_views_counter_import_wp_postviews" value="' . esc_attr__( 'Import Views', 'post-views-counter' ) . '"/> <label><input id="pvc-wp-postviews" type="checkbox" name="post_views_counter_import_wp_postviews_override" value="1" />' . esc_html__( 'Override existing views data during import.', 'post-views-counter' ) . '</label>
1368 <p class="description">' . esc_html__( 'Click Import Views to start importing the views data.', 'post-views-counter' ) . '</p>
1369 </div>';
1370
1371 return $html;
1372 }
1373
1374 /**
1375 * Setting: delete views.
1376 *
1377 * @return string
1378 */
1379 public function setting_delete_views() {
1380 $html = '
1381 <input type="submit" class="button button-secondary" name="post_views_counter_reset_views" value="' . esc_attr__( 'Delete Views', 'post-views-counter' ) . '" />
1382 <p class="description">' . esc_html__( 'Delete ALL the existing post views data. Note that this is an irreversible process!', 'post-views-counter' ) . '</p>';
1383
1384 return $html;
1385 }
1386
1387 /**
1388 * Setting: user type.
1389 *
1390 * @param array $field
1391 *
1392 * @return string
1393 */
1394 public function setting_restrict_display( $field ) {
1395 // get main instance
1396 $pvc = Post_Views_Counter();
1397
1398 $html = '';
1399
1400 foreach ( $field['options']['groups'] as $type => $type_name ) {
1401 if ( $type === 'robots' || $type === 'ai_bots' )
1402 continue;
1403
1404 $html .= '
1405 <label><input id="pvc_restrict_display-' . esc_attr( $type ) . '" type="checkbox" name="post_views_counter_settings_display[restrict_display][groups][' . esc_attr( $type ) . ']" value="1" ' . checked( in_array( $type, $pvc->options['display']['restrict_display']['groups'], true ), true, false ) . ' />' . esc_html( $type_name ) . '</label>';
1406 }
1407
1408 $html .= '
1409 <p class="description">' . __( 'Use it to hide the post views counter from selected type of visitors.', 'post-views-counter' ) . '</p>
1410 <div class="pvc_user_roles pvc-subfield"' . ( in_array( 'roles', $pvc->options['display']['restrict_display']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
1411
1412 foreach ( $field['options']['roles'] as $role => $role_name ) {
1413 $html .= '
1414 <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>';
1415 }
1416
1417 $html .= '
1418 <p class="description">' . __( 'Use it to hide the post views counter from selected user roles.', 'post-views-counter' ) . '</p>
1419 </div>';
1420
1421 return $html;
1422 }
1423
1424 /**
1425 * Validate user type.
1426 *
1427 * @param array $input
1428 * @param array $field
1429 *
1430 * @return array
1431 */
1432 public function validate_restrict_display( $input, $field ) {
1433 // any groups?
1434 if ( isset( $input['restrict_display']['groups'] ) ) {
1435 $groups = [];
1436
1437 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
1438 if ( $group === 'robots' || $group === 'ai_bots' )
1439 continue;
1440
1441 if ( isset( $field['options']['groups'][$group] ) )
1442 $groups[] = $group;
1443 }
1444
1445 $input['restrict_display']['groups'] = array_unique( $groups );
1446 } else
1447 $input['restrict_display']['groups'] = [];
1448
1449 // any roles?
1450 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
1451 $roles = [];
1452
1453 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
1454 if ( isset( $field['options']['roles'][$role] ) )
1455 $roles[] = $role;
1456 }
1457
1458 $input['restrict_display']['roles'] = array_unique( $roles );
1459 } else
1460 $input['restrict_display']['roles'] = [];
1461
1462 return $input;
1463 }
1464
1465 /**
1466 * Reports page placeholder.
1467 */
1468 public function section_reports_placeholder() {
1469 echo '
1470 <form action="#">
1471 <div id="pvc-reports-placeholder">
1472 <img id="pvc-reports-bg" src="' . esc_url( POST_VIEWS_COUNTER_URL ) . '/css/page-reports.png" alt="Post Views Counter - Reports" />
1473 <div id="pvc-reports-upgrade">
1474 <div id="pvc-reports-modal">
1475 <h2>' . esc_html__( 'Display Reports and Export Views to CSV/XML', 'post-views-counter' ) . '</h2>
1476 <p>' . esc_html__( 'View detailed stats about the popularity of your content.', 'post-views-counter' ) . '</p>
1477 <p>' . esc_html__( 'Generate views reports in any date range you need.', 'post-views-counter' ) . '</p>
1478 <p>' . esc_html__( 'Export, download and share your website views data.', 'post-views-counter' ) . '</p>
1479 <p><a href="https://postviewscounter.com/upgrade/?utm_source=post-views-counter-lite&utm_medium=button&utm_campaign=upgrade-to-pro" class="button button-secondary button-hero pvc-button" target="_blank">' . esc_html__( 'Upgrade to Pro', 'post-views-counter' ) . '</a></p>
1480 </div>
1481 </div>
1482 </div>
1483 </form>';
1484 }
1485
1486 /**
1487 * Validate license.
1488 *
1489 * @param array $input
1490 * @param array $field
1491 * @return array
1492 */
1493 public function validate_license( $input, $field ) {
1494 // save value from database
1495 return $field['value'];
1496 }
1497 }
1498