PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.5.5
Post Views Counter v1.5.5
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
1564 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 <h3 class="pvc-sidebar-title">Get Post Views Counter Pro</h3>
74 </div>
75 <div class="pvc-sidebar-body">
76 <p><span class="pvc-icon pvc-icon-check"></span>' . __( '<b>Collect more accurate data</b> about the number of views of your site, regardless of what the user is visiting.', 'post-views-counter' ) . '</p>
77 <p><span class="pvc-icon pvc-icon-check"></span>' . __( '<b>Unlock optimization features</b> and caching plugins compatibility to speed up view count tracking.', 'post-views-counter' ) . '</p>
78 <p><span class="pvc-icon pvc-icon-check"></span>' . __( '<b>Take your insights to the next level</b> with customizable Views by Date, Post and Author reporting.', 'post-views-counter' ) . '</p>
79 <p><span class="pvc-icon pvc-icon-check"></span>' . __( '<b>Order posts by views count</b> using built-in Elementor Pro, Divi Theme and GenerateBlocks integration.', 'post-views-counter' ) . '</p>
80 </div>
81 <div class="pvc-pricing-footer">
82 <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' ) . ' &rarr;</a>
83 <p>Starting from $29 per year<br />14-day money back guarantee.</p>
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 'count_time' => [
292 'tab' => 'general',
293 'title' => __( 'Count Time', 'post-views-counter' ),
294 'section' => 'post_views_counter_general_settings',
295 'type' => 'radio',
296 'class' => 'pvc-pro',
297 'skip_saving' => true,
298 'description' => __( "Whether to store the views using GMT timezone or adjust it to the GMT offset of the site.", 'post-views-counter' ),
299 'options' => [
300 'gmt' => __( 'GMT Time', 'post-views-counter' ),
301 'local' => __( 'Local Time', 'post-views-counter' )
302 ],
303 'disabled' => [ 'gmt', 'local' ],
304 'value' => 'gmt'
305 ],
306 'strict_counts' => [
307 'tab' => 'general',
308 'title' => __( 'Strict counts', 'post-views-counter' ),
309 'section' => 'post_views_counter_general_settings',
310 'type' => 'boolean',
311 'class' => 'pvc-pro',
312 'disabled' => true,
313 'skip_saving' => true,
314 'value' => false,
315 'description' => '',
316 'label' => __( 'Enable to prevent bypassing the counts interval (for e.g. using incognito browser window or by clearing cookies).', 'post-views-counter' )
317 ],
318 'reset_counts' => [
319 'tab' => 'general',
320 'title' => __( 'Cleanup Interval', 'post-views-counter' ),
321 'section' => 'post_views_counter_general_settings',
322 'type' => 'custom',
323 '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>' ),
324 'min' => 0,
325 'max' => 999999,
326 'options' => $time_types,
327 'callback' => [ $this, 'setting_reset_counts' ],
328 'validate' => [ $this, 'validate_reset_counts' ]
329 ],
330 'caching_compatibility' => [
331 'tab' => 'general',
332 'title' => __( 'Caching Compatibility', 'post-views-counter' ),
333 'section' => 'post_views_counter_general_settings',
334 'type' => 'boolean',
335 'class' => 'pvc-pro',
336 'disabled' => true,
337 'value' => false,
338 'skip_saving' => true,
339 'label' => __( 'Enable to apply changes improving compatibility with caching plugins.', 'post-views-counter' ),
340 'description' => $this->get_caching_compatibility_description()
341 ],
342 'object_cache' => [
343 'tab' => 'general',
344 'title' => __( 'Object Cache Support', 'post-views-counter' ),
345 'section' => 'post_views_counter_general_settings',
346 'type' => 'boolean',
347 'class' => 'pvc-pro',
348 'disabled' => true,
349 'value' => false,
350 'skip_saving' => true,
351 'label' => sprintf( __( 'Enable to use object cache optimization.', 'post-views-counter' ), '<code>Redis</code>', '<code>Memcached</code>' ),
352 '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>.'
353 ],
354 'exclude' => [
355 'tab' => 'general',
356 'title' => __( 'Exclude Visitors', 'post-views-counter' ),
357 'section' => 'post_views_counter_general_settings',
358 'type' => 'custom',
359 'description' => '',
360 'class' => 'pvc-pro-extended',
361 'options' => [
362 'groups' => $groups,
363 'roles' => $user_roles
364 ],
365 'disabled' => [
366 'groups' => [ 'ai_bots' ]
367 ],
368 'callback' => [ $this, 'setting_exclude' ],
369 'validate' => [ $this, 'validate_exclude' ]
370 ],
371 'exclude_ips' => [
372 'tab' => 'general',
373 'title' => __( 'Exclude IPs', 'post-views-counter' ),
374 'section' => 'post_views_counter_general_settings',
375 'type' => 'custom',
376 'description' => '',
377 'callback' => [ $this, 'setting_exclude_ips' ],
378 'validate' => [ $this, 'validate_exclude_ips' ]
379 ],
380 'label' => [
381 'tab' => 'display',
382 'title' => __( 'Views Label', 'post-views-counter' ),
383 'section' => 'post_views_counter_display_settings',
384 'type' => 'input',
385 'description' => __( 'Enter the label for the post views counter field.', 'post-views-counter' ),
386 'subclass' => 'regular-text',
387 'validate' => [ $this, 'validate_label' ],
388 'reset' => [ $this, 'reset_label' ]
389 ],
390 'display_period' => [
391 'tab' => 'display',
392 'title' => __( 'Views Period', 'post-views-counter' ),
393 'section' => 'post_views_counter_display_settings',
394 'type' => 'select',
395 'class' => 'pvc-pro',
396 'disabled' => true,
397 'skip_saving' => true,
398 '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' ),
399 'options' => [
400 'total' => __( 'Total Views', 'post-views-counter' )
401 ]
402 ],
403 'display_style' => [
404 'tab' => 'display',
405 'title' => __( 'Display Style', 'post-views-counter' ),
406 'section' => 'post_views_counter_display_settings',
407 'type' => 'custom',
408 'description' => __( 'Choose how to display the post views counter.', 'post-views-counter' ),
409 'callback' => [ $this, 'setting_display_style' ],
410 'validate' => [ $this, 'validate_display_style' ],
411 'options' => [
412 'icon' => __( 'icon', 'post-views-counter' ),
413 'text' => __( 'label', 'post-views-counter' )
414 ]
415 ],
416 'icon_class' => [
417 'tab' => 'display',
418 'title' => __( 'Icon Class', 'post-views-counter' ),
419 'section' => 'post_views_counter_display_settings',
420 'type' => 'class',
421 'default' => '',
422 '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/' ),
423 'subclass' => 'regular-text'
424 ],
425 'position' => [
426 'tab' => 'display',
427 'title' => __( 'Position', 'post-views-counter' ),
428 'section' => 'post_views_counter_display_settings',
429 'type' => 'select',
430 '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>' ),
431 'options' => [
432 'before' => __( 'before the content', 'post-views-counter' ),
433 'after' => __( 'after the content', 'post-views-counter' ),
434 'manual' => __( 'manual', 'post-views-counter' )
435 ]
436 ],
437 'post_views_column' => [
438 'tab' => 'display',
439 'title' => __( 'Admin Column', 'post-views-counter' ),
440 'section' => 'post_views_counter_display_settings',
441 'type' => 'boolean',
442 'description' => '',
443 'label' => __( 'Enable to display views count admin column.', 'post-views-counter' )
444 ],
445 'restrict_edit_views' => [
446 'tab' => 'display',
447 'title' => __( 'Admin Edit', 'post-views-counter' ),
448 'section' => 'post_views_counter_display_settings',
449 'type' => 'boolean',
450 'description' => '',
451 'label' => __( 'Enable to allow views count editing.', 'post-views-counter' )
452 ],
453 'dynamic_loading' => [
454 'tab' => 'display',
455 'title' => __( 'Dynamic Loading', 'post-views-counter' ),
456 'section' => 'post_views_counter_display_settings',
457 'type' => 'boolean',
458 'class' => 'pvc-pro',
459 'disabled' => true,
460 'skip_saving' => true,
461 'value' => false,
462 'label' => __( 'Enable dynamic loading and prevent caching of the displayed views count.', 'post-views-counter' )
463 ],
464 'use_format' => [
465 'tab' => 'display',
466 'title' => __( 'Format Number', 'post-views-counter' ),
467 'section' => 'post_views_counter_display_settings',
468 'type' => 'boolean',
469 'label' => __( 'Enable to display the views number formatted based on the locale (using the WP number_format_i18n function).', 'post-views-counter' )
470 ],
471 'taxonomies_display' => [
472 'tab' => 'display',
473 'title' => __( 'Taxonomies', 'post-views-counter' ),
474 'section' => 'post_views_counter_display_settings',
475 'type' => 'custom',
476 'class' => 'pvc-pro',
477 'skip_saving' => true,
478 'options' => $pvc->functions->get_taxonomies( 'labels' ),
479 'callback' => [ $this, 'setting_taxonomies_display' ]
480 ],
481 'user_display' => [
482 'tab' => 'display',
483 'title' => __( 'Authors', 'post-views-counter' ),
484 'section' => 'post_views_counter_display_settings',
485 'type' => 'boolean',
486 'class' => 'pvc-pro',
487 'disabled' => true,
488 'skip_saving' => true,
489 'value' => false,
490 'label' => __( 'Display number of views on authors archive pages.', 'post-views-counter' )
491 ],
492 'post_types_display' => [
493 'tab' => 'display',
494 'title' => __( 'Post Type', 'post-views-counter' ),
495 'section' => 'post_views_counter_display_settings',
496 'type' => 'checkbox',
497 'display_type' => 'horizontal',
498 'description' => __( 'Select post types for which the views count will be displayed.', 'post-views-counter' ),
499 'options' => $post_types
500 ],
501 'page_types_display' => [
502 'tab' => 'display',
503 'title' => __( 'Page Type', 'post-views-counter' ),
504 'section' => 'post_views_counter_display_settings',
505 'type' => 'checkbox',
506 'display_type' => 'horizontal',
507 'description' => __( 'Select page types where the views count will be displayed.', 'post-views-counter' ),
508 'options' => apply_filters(
509 'pvc_page_types_display_options',
510 [
511 'home' => __( 'Home', 'post-views-counter' ),
512 'archive' => __( 'Archives', 'post-views-counter' ),
513 'singular' => __( 'Single pages', 'post-views-counter' ),
514 'search' => __( 'Search results', 'post-views-counter' ),
515 ]
516 )
517 ],
518 'restrict_display' => [
519 'tab' => 'display',
520 'title' => __( 'User Type', 'post-views-counter' ),
521 'section' => 'post_views_counter_display_settings',
522 'type' => 'custom',
523 'description' => '',
524 'options' => [
525 'groups' => $groups,
526 'roles' => $user_roles
527 ],
528 'callback' => [ $this, 'setting_restrict_display' ],
529 'validate' => [ $this, 'validate_restrict_display' ]
530 ],
531 'toolbar_statistics' => [
532 'tab' => 'display',
533 'title' => __( 'Toolbar Chart', 'post-views-counter' ),
534 'section' => 'post_views_counter_display_settings',
535 'type' => 'boolean',
536 'description' => __( 'The post views chart will be displayed for the post types that are being counted.', 'post-views-counter' ),
537 'label' => __( 'Enable to display the post views chart at the toolbar.', 'post-views-counter' )
538 ],
539 'license' => [
540 'tab' => 'other',
541 'title' => __( 'License', 'post-views-counter' ),
542 'section' => 'post_views_counter_other_settings',
543 'disabled' => true,
544 'value' => $pvc->options['other']['license'],
545 'type' => 'input',
546 'description' => __( 'Enter your Post Views Counter Pro license key (requires Pro version to be installed and active).', 'post-views-counter' ),
547 'subclass' => 'regular-text',
548 'validate' => [ $this, 'validate_license' ],
549 'append' => '<span class="pvc-icon license-icon"></span>'
550 ],
551 'menu_position' => [
552 'tab' => 'other',
553 'title' => __( 'Menu Position', 'post-views-counter' ),
554 'section' => 'post_views_counter_other_settings',
555 'type' => 'radio',
556 'options' => [
557 'top' => __( 'Top menu', 'post-views-counter' ),
558 'sub' => __( 'Settings submenu', 'post-views-counter' )
559 ],
560 'description' => __( "Choose where to display the plugin's menu.", 'post-views-counter' ),
561 ],
562 'import_views' => [
563 'tab' => 'other',
564 'title' => __( 'Import Views', 'post-views-counter' ),
565 'section' => 'post_views_counter_other_settings',
566 'type' => 'custom',
567 'description' => '',
568 'skip_saving' => true,
569 'callback' => [ $this, 'setting_import_views' ]
570 ],
571 'delete_views' => [
572 'tab' => 'other',
573 'title' => __( 'Delete Views', 'post-views-counter' ),
574 'section' => 'post_views_counter_other_settings',
575 'type' => 'custom',
576 'description' => '',
577 'skip_saving' => true,
578 'callback' => [ $this, 'setting_delete_views' ]
579 ],
580 'deactivation_delete' => [
581 'tab' => 'other',
582 'title' => __( 'Deactivation', 'post-views-counter' ),
583 'section' => 'post_views_counter_other_settings',
584 'type' => 'boolean',
585 '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' ),
586 'label' => __( 'Enable to delete all plugin data on deactivation.', 'post-views-counter' )
587 ]
588 ]
589 ];
590
591 return $settings;
592 }
593
594 /**
595 * Add settings page.
596 *
597 * @param array $pages
598 *
599 * @return array
600 */
601 public function settings_page( $pages ) {
602 // get main instance
603 $pvc = Post_Views_Counter();
604
605 // default page
606 $pages['post-views-counter'] = [
607 'menu_slug' => 'post-views-counter',
608 'page_title' => __( 'Post Views Counter Settings', 'post-views-counter' ),
609 'menu_title' => $pvc->options['other']['menu_position'] === 'sub' ? __( 'Post Views Counter', 'post-views-counter' ) : __( 'Post Views', 'post-views-counter' ),
610 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
611 'callback' => null,
612 'tabs' => [
613 'general' => [
614 'label' => __( 'Counting', 'post-views-counter' ),
615 'option_name' => 'post_views_counter_settings_general'
616 ],
617 'display' => [
618 'label' => __( 'Display', 'post-views-counter' ),
619 'option_name' => 'post_views_counter_settings_display'
620 ],
621 'reports' => [
622 'label' => __( 'Reports', 'post-views-counter' ),
623 'option_name' => 'post_views_counter_settings_reports'
624 ],
625 'other' => [
626 'label' => __( 'Other', 'post-views-counter' ),
627 'option_name' => 'post_views_counter_settings_other'
628 ]
629 ]
630 ];
631
632 // update admin title
633 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
634
635 // submenu?
636 if ( $pvc->options['other']['menu_position'] === 'sub' ) {
637 $pages['post-views-counter']['type'] = 'settings_page';
638 // topmenu?
639 } else {
640 // highlight submenus
641 add_filter( 'submenu_file', [ $this, 'submenu_file' ], 10, 2 );
642
643 // add parameters
644 $pages['post-views-counter']['type'] = 'page';
645 $pages['post-views-counter']['icon'] = 'dashicons-chart-bar';
646 $pages['post-views-counter']['position'] = '99.301';
647
648 // add subpages
649 $pages['post-views-counter-general'] = [
650 'menu_slug' => 'post-views-counter',
651 'parent_slug' => 'post-views-counter',
652 'type' => 'subpage',
653 'page_title' => __( 'Counting', 'post-views-counter' ),
654 'menu_title' => __( 'Counting', 'post-views-counter' ),
655 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
656 'callback' => null
657 ];
658
659 $pages['post-views-counter-display'] = [
660 'menu_slug' => 'post-views-counter&tab=display',
661 'parent_slug' => 'post-views-counter',
662 'type' => 'subpage',
663 'page_title' => __( 'Display', 'post-views-counter' ),
664 'menu_title' => __( 'Display', 'post-views-counter' ),
665 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
666 'callback' => null
667 ];
668
669 $pages['post-views-counter-reports'] = [
670 'menu_slug' => 'post-views-counter&tab=reports',
671 'parent_slug' => 'post-views-counter',
672 'type' => 'subpage',
673 'page_title' => __( 'Reports', 'post-views-counter' ),
674 'menu_title' => __( 'Reports', 'post-views-counter' ),
675 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
676 'callback' => null
677 ];
678
679 $pages['post-views-counter-other'] = [
680 'menu_slug' => 'post-views-counter&tab=other',
681 'parent_slug' => 'post-views-counter',
682 'type' => 'subpage',
683 'page_title' => __( 'Other', 'post-views-counter' ),
684 'menu_title' => __( 'Other', 'post-views-counter' ),
685 'capability' => apply_filters( 'pvc_settings_capability', 'manage_options' ),
686 'callback' => null
687 ];
688 }
689
690 return $pages;
691 }
692
693 /**
694 * Settings page CSS class(es).
695 *
696 * @param array $class
697 * @return array
698 */
699 public function settings_page_class( $class ) {
700 $is_pro = class_exists( 'Post_Views_Counter_Pro' );
701
702 if ( ! $is_pro )
703 $class[] = 'has-sidebar';
704
705 return $class;
706 }
707
708 /**
709 * Highlight submenu items.
710 *
711 * @param string|null $submenu_file
712 * @param string $parent_file
713 *
714 * @return string|null
715 */
716 public function submenu_file( $submenu_file, $parent_file ) {
717 if ( $parent_file === 'post-views-counter' ) {
718 $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general';
719
720 if ( $tab !== 'general' )
721 return 'post-views-counter&tab=' . $tab;
722 }
723
724 return $submenu_file;
725 }
726
727 /**
728 * Update admin title.
729 *
730 * @global array $submenu
731 * @global string $pagenow
732 *
733 * @param string $admin_title
734 * @param string $title
735 *
736 * @return string
737 */
738 public function admin_title( $admin_title, $title ) {
739 global $submenu, $pagenow;
740
741 // get main instance
742 $pvc = Post_Views_Counter();
743
744 if ( isset( $_GET['page'] ) && $_GET['page'] === 'post-views-counter' ) {
745 if ( $pvc->options['other']['menu_position'] === 'sub' && $pagenow === 'options-general.php' ) {
746 // get tab
747 $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general';
748
749 // get settings pages
750 $pages = $pvc->settings_api->get_pages();
751
752 if ( array_key_exists( $tab, $pages['post-views-counter']['tabs'] ) ) {
753 // update title
754 $admin_title = preg_replace( '/' . $pages['post-views-counter']['page_title'] . '/', $pages['post-views-counter']['page_title'] . ' - ' . $pages['post-views-counter']['tabs'][$tab]['label'], $admin_title, 1 );
755 }
756 } else if ( $pvc->options['other']['menu_position'] === 'top' && get_admin_page_parent() === 'post-views-counter' && ! empty( $submenu['post-views-counter'] ) ) {
757 // get tab
758 $tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general';
759
760 // get settings pages
761 $pages = $pvc->settings_api->get_pages();
762
763 if ( array_key_exists( 'post-views-counter-' . $tab, $pages ) ) {
764 // update title
765 $admin_title = $pages['post-views-counter']['page_title'] . ' - ' . preg_replace( '/' . $title . '/', $pages['post-views-counter-' . $tab]['page_title'], $admin_title, 1 );
766 }
767 }
768 }
769
770 return $admin_title;
771 }
772
773 /**
774 * Validate options.
775 *
776 * @global object $wpdb
777 *
778 * @param array $input
779 *
780 * @return array
781 */
782 public function validate_settings( $input ) {
783 // check capability
784 if ( ! current_user_can( 'manage_options' ) )
785 return $input;
786
787 global $wpdb;
788
789 // get main instance
790 $pvc = Post_Views_Counter();
791
792 // use internal settings api to validate settings first
793 $input = $pvc->settings_api->validate_settings( $input );
794
795 // update meta key on save changes
796 if ( isset( $_POST['post_views_counter_import_meta_key'] ) && isset( $_POST['save_post_views_counter_settings_other'] ) )
797 $input['import_meta_key'] = sanitize_key( $_POST['post_views_counter_import_meta_key'] );
798
799 // import post views data from another plugin
800 if ( isset( $_POST['post_views_counter_import_wp_postviews'] ) ) {
801 // make sure we do not change anything in the settings
802 $input = $pvc->options['other'];
803
804 // get views key
805 $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'] ) ) );
806
807 // set meta_key option
808 $input['import_meta_key'] = $meta_key;
809
810 // get views
811 $views = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM " . $wpdb->postmeta . " WHERE meta_key = %s", $meta_key ), ARRAY_A, 0 );
812
813 // any views?
814 if ( ! empty( $views ) ) {
815 $sql = [];
816
817 foreach ( $views as $view ) {
818 $sql[] = $wpdb->prepare( "(%d, 4, 'total', %d)", (int) $view['post_id'], (int) $view['meta_value'] );
819 }
820
821 $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)" );
822
823 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'Post views data imported successfully.', 'post-views-counter' ), 'updated' );
824 } else
825 add_settings_error( 'wp_postviews_import', 'wp_postviews_import', __( 'There was no post views data to import.', 'post-views-counter' ), 'updated' );
826 // delete all post views data
827 } elseif ( isset( $_POST['post_views_counter_reset_views'] ) ) {
828 // make sure we do not change anything in the settings
829 $input = $pvc->options['other'];
830
831 if ( $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . 'post_views' ) )
832 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'All existing data deleted successfully.', 'post-views-counter' ), 'updated' );
833 else
834 add_settings_error( 'reset_post_views', 'reset_post_views', __( 'Error occurred. All existing data were not deleted.', 'post-views-counter' ), 'error' );
835 // save general settings
836 } elseif ( isset( $_POST['save_post_views_counter_settings_general'] ) ) {
837 $input['update_version'] = $pvc->options['general']['update_version'];
838 $input['update_notice'] = $pvc->options['general']['update_notice'];
839 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
840 // reset general settings
841 } elseif ( isset( $_POST['reset_post_views_counter_settings_general'] ) ) {
842 $input['update_version'] = $pvc->options['general']['update_version'];
843 $input['update_notice'] = $pvc->options['general']['update_notice'];
844 $input['update_delay_date'] = $pvc->options['general']['update_delay_date'];
845 }
846
847 return $input;
848 }
849
850 /**
851 * Get caching compatibility description.
852 *
853 * @return array
854 */
855 public function get_caching_compatibility_description() {
856 // caching compatibility description
857 $caching_compatibility_desc = '';
858
859 // get active caching plugins
860 $active_plugins = $this->get_active_caching_plugins();
861
862 if ( ! empty( $active_plugins ) ) {
863 $empty_active_caching_plugins = false;
864 $active_plugins_html = [];
865
866 $caching_compatibility_desc .= esc_html__( 'Currently detected active caching plugins', 'cookie-notice' ) . ': ';
867
868 foreach ( $active_plugins as $plugin ) {
869 $active_plugins_html[] = '<code>' . esc_html( $plugin ) . '</code>';
870 }
871
872 $caching_compatibility_desc .= implode( ', ', $active_plugins_html ) . '.';
873 } else {
874 $empty_active_caching_plugins = true;
875
876 $caching_compatibility_desc .= esc_html__( 'No compatible caching plugins found.', 'cookie-notice' );
877 }
878
879 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>.';
880 }
881
882 /**
883 * Get active caching plugins.
884 *
885 * @return array
886 */
887 public function get_active_caching_plugins() {
888 $active_plugins = [];
889
890 // autoptimize
891 if ( $this->is_plugin_active( 'autoptimize' ) )
892 $active_plugins[] = 'Autoptimize';
893
894 // breeze
895 // if ( $this->is_plugin_active( 'breeze' ) )
896 // $active_plugins[] = 'Breeze';
897
898 // hummingbird
899 if ( $this->is_plugin_active( 'hummingbird' ) )
900 $active_plugins[] = 'Hummingbird';
901
902 // litespeed
903 if ( $this->is_plugin_active( 'litespeed' ) )
904 $active_plugins[] = 'LiteSpeed Cache';
905
906 // speed optimizer
907 if ( $this->is_plugin_active( 'speedoptimizer' ) )
908 $active_plugins[] = 'Speed Optimizer';
909
910 // speedycache
911 if ( $this->is_plugin_active( 'speedycache' ) )
912 $active_plugins[] = 'SpeedyCache';
913
914 // wp fastest cache
915 if ( $this->is_plugin_active( 'wpfastestcache' ) )
916 $active_plugins[] = 'WP Fastest Cache';
917
918 // wp-optimize
919 if ( $this->is_plugin_active( 'wpoptimize' ) )
920 $active_plugins[] = 'WP-Optimize';
921
922 // wp rocket
923 if ( $this->is_plugin_active( 'wprocket' ) )
924 $active_plugins[] = 'WP Rocket';
925
926 // wp super cache
927 // if ( $this->is_plugin_active( 'wpsupercache' ) )
928 // $active_plugins[] = 'WP Super Cache';
929
930 return apply_filters( 'pvc_active_caching_plugins', $active_plugins );
931 }
932
933 /**
934 * Check whether specified plugin is active.
935 *
936 * @global object $siteground_optimizer_loader
937 * @global int $wpsc_version
938 *
939 * @param string $plugin
940 *
941 * @return bool
942 */
943 public function is_plugin_active( $plugin = '' ) {
944 // set default flag
945 $is_plugin_active = false;
946
947 switch ( $plugin ) {
948 // autoptimize
949 case 'autoptimize':
950 if ( function_exists( 'autoptimize' ) && defined( 'AUTOPTIMIZE_PLUGIN_VERSION' ) && version_compare( AUTOPTIMIZE_PLUGIN_VERSION, '2.4', '>=' ) )
951 $is_plugin_active = true;
952 break;
953
954 // breeze
955 // case 'breeze':
956 // 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', '>=' ) )
957 // $is_plugin_active = true;
958 // break;
959
960 // hummingbird
961 case 'hummingbird':
962 if ( class_exists( 'Hummingbird\\WP_Hummingbird' ) && defined( 'WPHB_VERSION' ) && version_compare( WPHB_VERSION, '2.1.0', '>=' ) )
963 $is_plugin_active = true;
964 break;
965
966 // litespeed
967 case 'litespeed':
968 if ( class_exists( 'LiteSpeed\Core' ) && defined( 'LSCWP_CUR_V' ) && version_compare( LSCWP_CUR_V, '3.0', '>=' ) )
969 $is_plugin_active = true;
970 break;
971
972 // speed optimizer
973 case 'speedoptimizer':
974 global $siteground_optimizer_loader;
975
976 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', '>=' ) )
977 $is_plugin_active = true;
978 break;
979
980 // speedycache
981 case 'speedycache':
982 if ( class_exists( 'SpeedyCache' ) && defined( 'SPEEDYCACHE_VERSION' ) && function_exists( 'speedycache_delete_cache' ) && version_compare( SPEEDYCACHE_VERSION, '1.0.0', '>=' ) )
983 $is_plugin_active = true;
984 break;
985
986 // wp fastest cache
987 case 'wpfastestcache':
988 if ( function_exists( 'wpfc_clear_all_cache' ) )
989 $is_plugin_active = true;
990 break;
991
992 // wp-optimize
993 case 'wpoptimize':
994 if ( function_exists( 'WP_Optimize' ) && defined( 'WPO_VERSION' ) && version_compare( WPO_VERSION, '3.0.12', '>=' ) )
995 $is_plugin_active = true;
996 break;
997
998 // wp rocket
999 case 'wprocket':
1000 if ( function_exists( 'rocket_init' ) && defined( 'WP_ROCKET_VERSION' ) && version_compare( WP_ROCKET_VERSION, '3.8', '>=' ) )
1001 $is_plugin_active = true;
1002 break;
1003
1004 // wp super cache
1005 // case 'wpsupercache':
1006 // global $wpsc_version;
1007
1008 // if ( ( ! empty( $wpsc_version ) && $wpsc_version >= 169 ) || ( defined( 'WPSC_VERSION' ) && version_compare( WPSC_VERSION, '1.6.9', '>=' ) ) )
1009 // $is_plugin_active = true;
1010 // break;
1011
1012 // other caching plugin
1013 default:
1014 $is_plugin_active = apply_filters( 'pvc_is_plugin_active', false, $plugin );
1015 }
1016
1017 return $is_plugin_active;
1018 }
1019
1020 /**
1021 * Setting: taxonomies count.
1022 *
1023 * @param array $field
1024 * @return string
1025 */
1026 public function setting_taxonomies_count( $field ) {
1027 $html = '
1028 <label><input id="post_views_counter_general_taxonomies_count" type="checkbox" name="" value="" disabled />' . esc_html( $field['label'] ) . '</label>';
1029
1030 return $html;
1031 }
1032
1033 /**
1034 * Setting: users count.
1035 *
1036 * @param array $field
1037 * @return string
1038 */
1039 public function setting_users_count( $field ) {
1040 // get base instance
1041 $pvc = Post_Views_Counter();
1042
1043 $html = '
1044 <label><input id="post_views_counter_general_users_count" type="checkbox" name="" value="" disabled />' . esc_html( $field['label'] ) . '</label>';
1045
1046 return $html;
1047 }
1048
1049 /**
1050 * Setting: taxonomies count.
1051 *
1052 * @param array $field
1053 * @return string
1054 */
1055 public function setting_taxonomies_display( $field ) {
1056 // get base instance
1057 $pvc = Post_Views_Counter();
1058
1059 $html = '';
1060
1061 foreach ( $field['options'] as $taxonomy => $label ) {
1062 $html .= '
1063 <label><input type="checkbox" name="" value="" disabled />' . esc_html( $label ) . '</label>';
1064 }
1065
1066 $html .= '
1067 <p class="description">' . esc_html__( 'Select taxonomies for which the views count will be displayed.', 'post-views-counter' ) . '</p>';
1068
1069 return $html;
1070 }
1071
1072 /**
1073 * Validate label.
1074 *
1075 * @param array $input
1076 * @param array $field
1077 * @return array
1078 */
1079 public function validate_label( $input, $field ) {
1080 // get main instance
1081 $pvc = Post_Views_Counter();
1082
1083 if ( ! isset( $input ) )
1084 $input = $pvc->defaults['display']['label'];
1085
1086 // use internal settings API to validate settings first
1087 $input = $pvc->settings_api->validate_field( $input, 'input', $field );
1088
1089 if ( function_exists( 'icl_register_string' ) )
1090 icl_register_string( 'Post Views Counter', 'Post Views Label', $input );
1091
1092 return $input;
1093 }
1094
1095 /**
1096 * Restore post views label to default value.
1097 *
1098 * @param array $default
1099 * @param array $field
1100 * @return array
1101 */
1102 public function reset_label( $default, $field ) {
1103 if ( function_exists( 'icl_register_string' ) )
1104 icl_register_string( 'Post Views Counter', 'Post Views Label', $default );
1105
1106 return $default;
1107 }
1108
1109 /**
1110 * Setting: display style.
1111 *
1112 * @param array $field
1113 * @return string
1114 */
1115 public function setting_display_style( $field ) {
1116 // get main instance
1117 $pvc = Post_Views_Counter();
1118
1119 $html = '
1120 <input type="hidden" name="post_views_counter_settings_display[display_style]" value="empty" />';
1121
1122 foreach ( $field['options'] as $key => $label ) {
1123 $html .= '
1124 <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> ';
1125 }
1126
1127 return $html;
1128 }
1129
1130 /**
1131 * Validate display style.
1132 *
1133 * @param array $input
1134 * @param array $field
1135 * @return array
1136 */
1137 public function validate_display_style( $input, $field ) {
1138 // get main instance
1139 $pvc = Post_Views_Counter();
1140
1141 $data = [];
1142
1143 foreach ( $field['options'] as $value => $label ) {
1144 $data[$value] = false;
1145 }
1146
1147 // any data?
1148 if ( ! empty( $input['display_style'] && $input['display_style'] !== 'empty' && is_array( $input['display_style'] ) ) ) {
1149 foreach ( $input['display_style'] as $value ) {
1150 if ( array_key_exists( $value, $field['options'] ) )
1151 $data[$value] = true;
1152 }
1153 }
1154
1155 $input['display_style'] = $data;
1156
1157 return $input;
1158 }
1159
1160 /**
1161 * Setting: count interval.
1162 *
1163 * @param array $field
1164 * @return string
1165 */
1166 public function setting_time_between_counts( $field ) {
1167 // get main instance
1168 $pvc = Post_Views_Counter();
1169
1170 $html = '
1171 <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'] ) . '" />
1172 <select name="post_views_counter_settings_general[time_between_counts][type]">';
1173
1174 foreach ( $field['options'] as $type => $type_name ) {
1175 $html .= '
1176 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['time_between_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
1177 }
1178
1179 $html .= '
1180 </select>
1181 <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>';
1182
1183 return $html;
1184 }
1185
1186 /**
1187 * Validate count interval.
1188 *
1189 * @param array $input
1190 * @param array $field
1191 * @return array
1192 */
1193 public function validate_time_between_counts( $input, $field ) {
1194 // get main instance
1195 $pvc = Post_Views_Counter();
1196
1197 // number
1198 $input['time_between_counts']['number'] = isset( $input['time_between_counts']['number'] ) ? (int) $input['time_between_counts']['number'] : $pvc->defaults['general']['time_between_counts']['number'];
1199
1200 if ( $input['time_between_counts']['number'] < $field['min'] || $input['time_between_counts']['number'] > $field['max'] )
1201 $input['time_between_counts']['number'] = $pvc->defaults['general']['time_between_counts']['number'];
1202
1203 // type
1204 $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'];
1205
1206 return $input;
1207 }
1208
1209 /**
1210 * Setting: reset data interval.
1211 *
1212 * @param array $field
1213 * @return string
1214 */
1215 public function setting_reset_counts( $field ) {
1216 // get main instance
1217 $pvc = Post_Views_Counter();
1218
1219 $html = '
1220 <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'] ) . '" />
1221 <select name="post_views_counter_settings_general[reset_counts][type]">';
1222
1223 foreach ( array_slice( $field['options'], 2, null, true ) as $type => $type_name ) {
1224 $html .= '
1225 <option value="' . esc_attr( $type ) . '" ' . selected( $type, $pvc->options['general']['reset_counts']['type'], false ) . '>' . esc_html( $type_name ) . '</option>';
1226 }
1227
1228 $html .= '
1229 </select>';
1230
1231 return $html;
1232 }
1233
1234 /**
1235 * Validate reset data interval.
1236 *
1237 * @param array $input
1238 * @param array $field
1239 * @return array
1240 */
1241 public function validate_reset_counts( $input, $field ) {
1242 // get main instance
1243 $pvc = Post_Views_Counter();
1244
1245 // number
1246 $input['reset_counts']['number'] = isset( $input['reset_counts']['number'] ) ? (int) $input['reset_counts']['number'] : $pvc->defaults['general']['reset_counts']['number'];
1247
1248 if ( $input['reset_counts']['number'] < $field['min'] || $input['reset_counts']['number'] > $field['max'] )
1249 $input['reset_counts']['number'] = $pvc->defaults['general']['reset_counts']['number'];
1250
1251 // type
1252 $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'];
1253
1254 // run cron on next visit?
1255 $input['cron_run'] = ( $input['reset_counts']['number'] > 0 );
1256
1257 // cron update?
1258 $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'] ) );
1259
1260 return $input;
1261 }
1262
1263 /**
1264 * Setting: object cache.
1265 *
1266 * @param array $field
1267 * @return string
1268 */
1269 public function setting_object_cache( $field ) {
1270 // get main instance
1271 $pvc = Post_Views_Counter();
1272
1273 // check object cache
1274 $wp_using_ext_object_cache = wp_using_ext_object_cache();
1275
1276 $html = '
1277 <input size="4" type="number" min="' . ( (int) $field['min'] ) . '" max="' . ( (int) $field['max'] ) . '" name="" value="0" disabled /> <span>' . __( 'minutes', 'post-views-counter' ) . '</span>
1278 <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>
1279 <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>';
1280
1281 return $html;
1282 }
1283
1284 /**
1285 * Setting: exclude visitors.
1286 *
1287 * @param array $field
1288 * @return string
1289 */
1290 public function setting_exclude( $field ) {
1291 // get main instance
1292 $pvc = Post_Views_Counter();
1293
1294 $html = '';
1295
1296 foreach ( $field['options']['groups'] as $type => $type_name ) {
1297 $is_disabled = ! empty( $field['disabled']['groups'] ) && in_array( $type, $field['disabled']['groups'], true );
1298
1299 $html .= '
1300 <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>';
1301 }
1302
1303 $html .= '
1304 <p class="description">' . __( 'Use it exclude specific user groups from post views count.', 'post-views-counter' ) . '</p>
1305 <div class="pvc_user_roles pvc_subfield"' . ( in_array( 'roles', $pvc->options['general']['exclude']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
1306
1307 foreach ( $field['options']['roles'] as $role => $role_name ) {
1308 $html .= '
1309 <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>';
1310 }
1311
1312 $html .= '
1313 <p class="description">' . __( 'Use it to exclude specific user roles from post views count.', 'post-views-counter' ) . '</p>
1314 </div>';
1315
1316 return $html;
1317 }
1318
1319 /**
1320 * Validate exclude visitors.
1321 *
1322 * @param array $input
1323 * @param array $field
1324 * @return array
1325 */
1326 public function validate_exclude( $input, $field ) {
1327 // any groups?
1328 if ( isset( $input['exclude']['groups'] ) ) {
1329 $groups = [];
1330
1331 foreach ( $input['exclude']['groups'] as $group => $set ) {
1332 // disallow disabled checkboxes
1333 if ( ! empty( $field['disabled']['groups'] ) && in_array( $group, $field['disabled']['groups'], true ) )
1334 continue;
1335
1336 if ( isset( $field['options']['groups'][$group] ) )
1337 $groups[] = $group;
1338 }
1339
1340 $input['exclude']['groups'] = array_unique( $groups );
1341 } else
1342 $input['exclude']['groups'] = [];
1343
1344 // any roles?
1345 if ( in_array( 'roles', $input['exclude']['groups'], true ) && isset( $input['exclude']['roles'] ) ) {
1346 $roles = [];
1347
1348 foreach ( $input['exclude']['roles'] as $role => $set ) {
1349 if ( isset( $field['options']['roles'][$role] ) )
1350 $roles[] = $role;
1351 }
1352
1353 $input['exclude']['roles'] = array_unique( $roles );
1354 } else
1355 $input['exclude']['roles'] = [];
1356
1357 return $input;
1358 }
1359
1360 /**
1361 * Setting: exclude IP addresses.
1362 *
1363 * @return string
1364 */
1365 public function setting_exclude_ips() {
1366 // get ip addresses
1367 $ips = Post_Views_Counter()->options['general']['exclude_ips'];
1368
1369 $html = '';
1370
1371 // any ip addresses?
1372 if ( ! empty( $ips ) ) {
1373 foreach ( $ips as $key => $ip ) {
1374 $html .= '
1375 <div class="ip-box">
1376 <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>
1377 </div>';
1378 }
1379 } else {
1380 $html .= '
1381 <div class="ip-box">
1382 <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>
1383 </div>';
1384 }
1385
1386 $html .= '
1387 <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>
1388 <p class="description">' . esc_html__( 'Enter the IP addresses to be excluded from post views count.', 'post-views-counter' ) . '</p>';
1389
1390 return $html;
1391 }
1392
1393 /**
1394 * Validate exclude IP addresses.
1395 *
1396 * @param array $input
1397 * @param array $field
1398 * @return array
1399 */
1400 public function validate_exclude_ips( $input, $field ) {
1401 // any ip addresses?
1402 if ( isset( $input['exclude_ips'] ) ) {
1403 $ips = [];
1404
1405 foreach ( $input['exclude_ips'] as $ip ) {
1406 if ( strpos( $ip, '*' ) !== false ) {
1407 $new_ip = str_replace( '*', '0', $ip );
1408
1409 if ( filter_var( $new_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
1410 $ips[] = $ip;
1411 } elseif ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) )
1412 $ips[] = $ip;
1413 }
1414
1415 $input['exclude_ips'] = array_unique( $ips );
1416 }
1417
1418 return $input;
1419 }
1420
1421 /**
1422 * Setting: tools.
1423 *
1424 * @return string
1425 */
1426 public function setting_import_views() {
1427 $html = '
1428 <div>
1429 <input type="text" class="regular-text" name="post_views_counter_import_meta_key" value="' . esc_attr( Post_Views_Counter()->options['other']['import_meta_key'] ) . '"/>
1430 <p class="description">' . esc_html__( 'Enter the meta key from which the views data is to be retrieved during import.', 'post-views-counter' ) . '</p>
1431 </div>
1432 <div class="pvc-subfield">
1433 <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>
1434 <p class="description">' . esc_html__( 'Click Import Views to start importing the views data.', 'post-views-counter' ) . '</p>
1435 </div>';
1436
1437 return $html;
1438 }
1439
1440 /**
1441 * Setting: delete views.
1442 *
1443 * @return string
1444 */
1445 public function setting_delete_views() {
1446 $html = '
1447 <input type="submit" class="button button-secondary" name="post_views_counter_reset_views" value="' . esc_attr__( 'Delete Views', 'post-views-counter' ) . '" />
1448 <p class="description">' . esc_html__( 'Delete ALL the existing post views data. Note that this is an irreversible process!', 'post-views-counter' ) . '</p>';
1449
1450 return $html;
1451 }
1452
1453 /**
1454 * Setting: user type.
1455 *
1456 * @param array $field
1457 *
1458 * @return string
1459 */
1460 public function setting_restrict_display( $field ) {
1461 // get main instance
1462 $pvc = Post_Views_Counter();
1463
1464 $html = '';
1465
1466 foreach ( $field['options']['groups'] as $type => $type_name ) {
1467 if ( $type === 'robots' || $type === 'ai_bots' )
1468 continue;
1469
1470 $html .= '
1471 <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>';
1472 }
1473
1474 $html .= '
1475 <p class="description">' . __( 'Use it to hide the post views counter from selected type of visitors.', 'post-views-counter' ) . '</p>
1476 <div class="pvc_user_roles pvc-subfield"' . ( in_array( 'roles', $pvc->options['display']['restrict_display']['groups'], true ) ? '' : ' style="display: none;"' ) . '>';
1477
1478 foreach ( $field['options']['roles'] as $role => $role_name ) {
1479 $html .= '
1480 <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>';
1481 }
1482
1483 $html .= '
1484 <p class="description">' . __( 'Use it to hide the post views counter from selected user roles.', 'post-views-counter' ) . '</p>
1485 </div>';
1486
1487 return $html;
1488 }
1489
1490 /**
1491 * Validate user type.
1492 *
1493 * @param array $input
1494 * @param array $field
1495 *
1496 * @return array
1497 */
1498 public function validate_restrict_display( $input, $field ) {
1499 // any groups?
1500 if ( isset( $input['restrict_display']['groups'] ) ) {
1501 $groups = [];
1502
1503 foreach ( $input['restrict_display']['groups'] as $group => $set ) {
1504 if ( $group === 'robots' || $group === 'ai_bots' )
1505 continue;
1506
1507 if ( isset( $field['options']['groups'][$group] ) )
1508 $groups[] = $group;
1509 }
1510
1511 $input['restrict_display']['groups'] = array_unique( $groups );
1512 } else
1513 $input['restrict_display']['groups'] = [];
1514
1515 // any roles?
1516 if ( in_array( 'roles', $input['restrict_display']['groups'], true ) && isset( $input['restrict_display']['roles'] ) ) {
1517 $roles = [];
1518
1519 foreach ( $input['restrict_display']['roles'] as $role => $set ) {
1520 if ( isset( $field['options']['roles'][$role] ) )
1521 $roles[] = $role;
1522 }
1523
1524 $input['restrict_display']['roles'] = array_unique( $roles );
1525 } else
1526 $input['restrict_display']['roles'] = [];
1527
1528 return $input;
1529 }
1530
1531 /**
1532 * Reports page placeholder.
1533 */
1534 public function section_reports_placeholder() {
1535 echo '
1536 <form action="#">
1537 <div id="pvc-reports-placeholder">
1538 <img id="pvc-reports-bg" src="' . esc_url( POST_VIEWS_COUNTER_URL ) . '/css/page-reports.png" alt="Post Views Counter - Reports" />
1539 <div id="pvc-reports-upgrade">
1540 <div id="pvc-reports-modal">
1541 <h2>' . esc_html__( 'Display Reports and Export Views to CSV/XML', 'post-views-counter' ) . '</h2>
1542 <p>' . esc_html__( 'View detailed stats about the popularity of your content.', 'post-views-counter' ) . '</p>
1543 <p>' . esc_html__( 'Generate views reports in any date range you need.', 'post-views-counter' ) . '</p>
1544 <p>' . esc_html__( 'Export, download and share your website views data.', 'post-views-counter' ) . '</p>
1545 <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>
1546 </div>
1547 </div>
1548 </div>
1549 </form>';
1550 }
1551
1552 /**
1553 * Validate license.
1554 *
1555 * @param array $input
1556 * @param array $field
1557 * @return array
1558 */
1559 public function validate_license( $input, $field ) {
1560 // save value from database
1561 return $field['value'];
1562 }
1563 }
1564