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