class-admin.php
2 years ago
class-columns.php
2 years ago
class-counter.php
2 years ago
class-crawler-detect.php
2 years ago
class-cron.php
2 years ago
class-dashboard.php
2 years ago
class-frontend.php
2 years ago
class-functions.php
2 years ago
class-query.php
2 years ago
class-settings-api.php
2 years ago
class-settings.php
2 years ago
class-update.php
2 years ago
class-widgets.php
2 years ago
functions.php
2 years ago
class-columns.php
548 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Columns class. |
| 8 | * |
| 9 | * @class Post_Views_Counter_Columns |
| 10 | */ |
| 11 | class Post_Views_Counter_Columns { |
| 12 | |
| 13 | /** |
| 14 | * Class constructor. |
| 15 | * |
| 16 | * @return void |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | // actions |
| 20 | add_action( 'admin_init', [ $this, 'register_new_column' ] ); |
| 21 | add_action( 'post_submitbox_misc_actions', [ $this, 'submitbox_views' ] ); |
| 22 | add_action( 'attachment_submitbox_misc_actions', [ $this, 'submitbox_views' ] ); |
| 23 | add_action( 'save_post', [ $this, 'save_post' ], 10, 2 ); |
| 24 | add_action( 'edit_attachment', [ $this, 'save_post' ], 10 ); |
| 25 | add_action( 'bulk_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 ); |
| 26 | add_action( 'quick_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 ); |
| 27 | add_action( 'wp_ajax_save_bulk_post_views', [ $this, 'save_bulk_post_views' ] ); |
| 28 | add_action( 'wp_loaded', [ $this, 'maybe_load_admin_bar_menu' ] ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Output post views for single post. |
| 33 | * |
| 34 | * @global object $post |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function submitbox_views() { |
| 39 | global $post; |
| 40 | |
| 41 | // get main instance |
| 42 | $pvc = Post_Views_Counter(); |
| 43 | |
| 44 | // incorrect post type? |
| 45 | if ( ! in_array( $post->post_type, (array) $pvc->options['general']['post_types_count'] ) ) |
| 46 | return; |
| 47 | |
| 48 | // break if current user can't edit this post |
| 49 | if ( ! current_user_can( 'edit_post', $post->ID ) ) |
| 50 | return; |
| 51 | |
| 52 | // get total post views |
| 53 | $count = (int) pvc_get_post_views( $post->ID ); ?> |
| 54 | |
| 55 | <div class="misc-pub-section" id="post-views"> |
| 56 | |
| 57 | <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?> |
| 58 | |
| 59 | <span id="post-views-display"> |
| 60 | <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?> |
| 61 | </span> |
| 62 | |
| 63 | <?php |
| 64 | // restrict editing |
| 65 | $restrict = (bool) $pvc->options['general']['restrict_edit_views']; |
| 66 | |
| 67 | if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) { |
| 68 | ?> |
| 69 | <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a> |
| 70 | |
| 71 | <div id="post-views-input-container" class="hide-if-js"> |
| 72 | |
| 73 | <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p> |
| 74 | <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo esc_attr( $count ); ?>" /> |
| 75 | <input type="text" name="post_views" id="post-views-input" value="<?php echo esc_attr( $count ); ?>"/><br /> |
| 76 | <p> |
| 77 | <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a> |
| 78 | <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a> |
| 79 | </p> |
| 80 | |
| 81 | </div> |
| 82 | <?php |
| 83 | } |
| 84 | ?> |
| 85 | |
| 86 | </div> |
| 87 | <?php |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Save post views data. |
| 92 | * |
| 93 | * @param int $post_id |
| 94 | * @param object $post |
| 95 | * @return void |
| 96 | */ |
| 97 | public function save_post( $post_id, $post = null ) { |
| 98 | // break if doing autosave |
| 99 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 100 | return; |
| 101 | |
| 102 | // break if current user can't edit this post |
| 103 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 104 | return; |
| 105 | |
| 106 | // is post views set |
| 107 | if ( ! isset( $_POST['post_views'] ) ) |
| 108 | return; |
| 109 | |
| 110 | // cast numeric post views |
| 111 | $post_views = (int) $_POST['post_views']; |
| 112 | |
| 113 | // unchanged post views value? |
| 114 | if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] ) |
| 115 | return; |
| 116 | |
| 117 | // get main instance |
| 118 | $pvc = Post_Views_Counter(); |
| 119 | |
| 120 | // break if post views in not one of the selected |
| 121 | $post_types = (array) $pvc->options['general']['post_types_count']; |
| 122 | |
| 123 | // get post type |
| 124 | if ( is_null( $post ) ) |
| 125 | $post_type = get_post_type( $post_id ); |
| 126 | else |
| 127 | $post_type = $post->post_type; |
| 128 | |
| 129 | // invalid post type? |
| 130 | if ( ! in_array( $post_type, $post_types, true ) ) |
| 131 | return; |
| 132 | |
| 133 | // break if views editing is restricted |
| 134 | if ( (bool) $pvc->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 135 | return; |
| 136 | |
| 137 | // validate data |
| 138 | if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) ) |
| 139 | return; |
| 140 | |
| 141 | // update post views |
| 142 | pvc_update_post_views( $post_id, $post_views ); |
| 143 | |
| 144 | do_action( 'pvc_after_update_post_views_count', $post_id ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Register post views column for specific post types. |
| 149 | * |
| 150 | * @return void |
| 151 | */ |
| 152 | public function register_new_column() { |
| 153 | // get main instance |
| 154 | $pvc = Post_Views_Counter(); |
| 155 | |
| 156 | // is posts views column active? |
| 157 | if ( ! $pvc->options['general']['post_views_column'] ) |
| 158 | return; |
| 159 | |
| 160 | // get post types |
| 161 | $post_types = $pvc->options['general']['post_types_count']; |
| 162 | |
| 163 | // any post types? |
| 164 | if ( ! empty( $post_types ) ) { |
| 165 | foreach ( $post_types as $post_type ) { |
| 166 | if ( $post_type === 'attachment' ) { |
| 167 | // actions |
| 168 | add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 ); |
| 169 | |
| 170 | // filters |
| 171 | add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] ); |
| 172 | add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] ); |
| 173 | } else { |
| 174 | // actions |
| 175 | add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 ); |
| 176 | |
| 177 | // filters |
| 178 | add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] ); |
| 179 | add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] ); |
| 180 | |
| 181 | // bbPress? |
| 182 | if ( class_exists( 'bbPress' ) ) { |
| 183 | if ( $post_type === 'forum' ) |
| 184 | add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] ); |
| 185 | elseif ( $post_type === 'topic' ) |
| 186 | add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] ); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Register sortable post views column. |
| 195 | * |
| 196 | * @param array $columns |
| 197 | * @return array |
| 198 | */ |
| 199 | public function register_sortable_custom_column( $columns ) { |
| 200 | // add new sortable column |
| 201 | $columns['post_views'] = 'post_views'; |
| 202 | |
| 203 | return $columns; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Add post views column. |
| 208 | * |
| 209 | * @param array $columns |
| 210 | * @return array |
| 211 | */ |
| 212 | public function add_new_column( $columns ) { |
| 213 | // date column exists? |
| 214 | if ( isset( $columns['date'] ) ) { |
| 215 | // store date column |
| 216 | $date = $columns['date']; |
| 217 | |
| 218 | // unset date column |
| 219 | unset( $columns['date'] ); |
| 220 | } |
| 221 | |
| 222 | // comments column exists? |
| 223 | if ( isset( $columns['comments'] ) ) { |
| 224 | // store comments column |
| 225 | $comments = $columns['comments']; |
| 226 | |
| 227 | // unset comments column |
| 228 | unset( $columns['comments'] ); |
| 229 | } |
| 230 | |
| 231 | // add post views column |
| 232 | $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . esc_attr__( 'Post Views', 'post-views-counter' ) . '"><span class="screen-reader-text">' . esc_attr__( 'Post Views', 'post-views-counter' ) . '</span></span>'; |
| 233 | |
| 234 | // restore date column |
| 235 | if ( isset( $date ) ) |
| 236 | $columns['date'] = $date; |
| 237 | |
| 238 | // restore comments column |
| 239 | if ( isset( $comments ) ) |
| 240 | $columns['comments'] = $comments; |
| 241 | |
| 242 | return $columns; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Add post views column content. |
| 247 | * |
| 248 | * @param string $column_name |
| 249 | * @param int $id |
| 250 | * @return void |
| 251 | */ |
| 252 | public function add_new_column_content( $column_name, $id ) { |
| 253 | if ( $column_name === 'post_views' ) { |
| 254 | // get total post views |
| 255 | $count = pvc_get_post_views( $id ); |
| 256 | |
| 257 | echo esc_html( $count ); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Handle quick edit. |
| 263 | * |
| 264 | * @global string $pagenow |
| 265 | * |
| 266 | * @param string $column_name |
| 267 | * @param string $post_type |
| 268 | * @return void |
| 269 | */ |
| 270 | function quick_edit_custom_box( $column_name, $post_type ) { |
| 271 | global $pagenow; |
| 272 | |
| 273 | if ( $pagenow !== 'edit.php' ) |
| 274 | return; |
| 275 | |
| 276 | if ( $column_name !== 'post_views' ) |
| 277 | return; |
| 278 | |
| 279 | // get main instance |
| 280 | $pvc = Post_Views_Counter(); |
| 281 | |
| 282 | if ( ! $pvc->options['general']['post_views_column'] || ! in_array( $post_type, $pvc->options['general']['post_types_count'] ) ) |
| 283 | return; |
| 284 | |
| 285 | // break if views editing is restricted |
| 286 | $restrict = (bool) $pvc->options['general']['restrict_edit_views']; |
| 287 | |
| 288 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 289 | return; |
| 290 | |
| 291 | ?> |
| 292 | <fieldset class="inline-edit-col-left"> |
| 293 | <div id="inline-edit-post_views" class="inline-edit-col"> |
| 294 | <label class="inline-edit-group"> |
| 295 | <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span> |
| 296 | <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span> |
| 297 | <input type="hidden" name="current_post_views" value="" /> |
| 298 | <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?> |
| 299 | </label> |
| 300 | </div> |
| 301 | </fieldset> |
| 302 | <?php |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Bulk save post views. |
| 307 | * |
| 308 | * @global object $wpdb |
| 309 | * |
| 310 | * @return void |
| 311 | */ |
| 312 | function save_bulk_post_views() { |
| 313 | global $wpdb; |
| 314 | |
| 315 | // check nonce |
| 316 | if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'pvc_save_bulk_post_views' ) ) |
| 317 | exit; |
| 318 | |
| 319 | $count = null; |
| 320 | |
| 321 | if ( isset( $_POST['post_views'] ) && is_numeric( trim( $_POST['post_views'] ) ) ) { |
| 322 | $count = (int) $_POST['post_views']; |
| 323 | |
| 324 | if ( $count < 0 ) |
| 325 | $count = 0; |
| 326 | } |
| 327 | |
| 328 | // check post ids |
| 329 | $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : []; |
| 330 | |
| 331 | if ( is_null( $count ) ) |
| 332 | exit; |
| 333 | |
| 334 | // break if views editing is restricted |
| 335 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 336 | |
| 337 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 338 | exit; |
| 339 | |
| 340 | // any post ids? |
| 341 | if ( ! empty( $post_ids ) ) { |
| 342 | foreach ( $post_ids as $post_id ) { |
| 343 | // break if current user can't edit this post |
| 344 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 345 | continue; |
| 346 | |
| 347 | // insert or update db post views count |
| 348 | $wpdb->query( $wpdb->prepare( "INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) VALUES (%d, %d, %s, %d) ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count ) ); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | exit; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Add admin bar stats to a post. |
| 357 | * |
| 358 | * @return void |
| 359 | */ |
| 360 | public function maybe_load_admin_bar_menu() { |
| 361 | // get main instance |
| 362 | $pvc = Post_Views_Counter(); |
| 363 | |
| 364 | // statistics disabled? |
| 365 | if ( ! apply_filters( 'pvc_display_toolbar_statistics', $pvc->options['display']['toolbar_statistics'] ) ) |
| 366 | return; |
| 367 | |
| 368 | // skip for not logged in users |
| 369 | if ( ! is_user_logged_in() ) |
| 370 | return; |
| 371 | |
| 372 | // skip users with turned off admin bar at frontend |
| 373 | if ( ! is_admin() && get_user_option( 'show_admin_bar_front' ) !== 'true' ) |
| 374 | return; |
| 375 | |
| 376 | if ( is_admin() ) |
| 377 | add_action( 'admin_init', [ $this, 'admin_bar_maybe_add_style' ] ); |
| 378 | else |
| 379 | add_action( 'wp', [ $this, 'admin_bar_maybe_add_style' ] ); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Add admin bar stats to a post. |
| 384 | * |
| 385 | * @global string $pagenow |
| 386 | * @global string $post |
| 387 | * |
| 388 | * @param object $admin_bar |
| 389 | * @return void |
| 390 | */ |
| 391 | public function admin_bar_menu( $admin_bar ) { |
| 392 | // get main instance |
| 393 | $pvc = Post_Views_Counter(); |
| 394 | |
| 395 | // set empty post |
| 396 | $post = null; |
| 397 | |
| 398 | // admin? |
| 399 | if ( is_admin() && ! wp_doing_ajax() ) { |
| 400 | global $pagenow; |
| 401 | |
| 402 | $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post; |
| 403 | // frontend? |
| 404 | } elseif ( is_singular() ) |
| 405 | global $post; |
| 406 | |
| 407 | // get countable post types |
| 408 | $post_types = $pvc->options['general']['post_types_count']; |
| 409 | |
| 410 | // whether to allow this post type or not |
| 411 | if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) ) |
| 412 | return; |
| 413 | |
| 414 | $dt = new DateTime(); |
| 415 | |
| 416 | // get post views |
| 417 | $views = pvc_get_views( |
| 418 | [ |
| 419 | 'post_id' => $post->ID, |
| 420 | 'post_type' => $post->post_type, |
| 421 | 'fields' => 'date=>views', |
| 422 | 'views_query' => [ |
| 423 | 'year' => $dt->format( 'Y' ), |
| 424 | 'month' => $dt->format( 'm' ) |
| 425 | ] |
| 426 | ] |
| 427 | ); |
| 428 | |
| 429 | $graph = ''; |
| 430 | |
| 431 | // get highest value |
| 432 | $views_copy = $views; |
| 433 | |
| 434 | arsort( $views_copy, SORT_NUMERIC ); |
| 435 | |
| 436 | $highest = reset( $views_copy ); |
| 437 | |
| 438 | // find the multiplier |
| 439 | $multiplier = $highest * 0.05; |
| 440 | |
| 441 | // generate ranges |
| 442 | $ranges = []; |
| 443 | |
| 444 | for ( $i = 1; $i <= 20; $i ++ ) { |
| 445 | $ranges[$i] = round( $multiplier * $i ); |
| 446 | } |
| 447 | |
| 448 | // create graph |
| 449 | foreach ( $views as $date => $count ) { |
| 450 | $count_class = 0; |
| 451 | |
| 452 | if ( $count > 0 ) { |
| 453 | foreach ( $ranges as $index => $range ) { |
| 454 | if ( $count <= $range ) { |
| 455 | $count_class = $index; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | $graph .= '<span class="pvc-line-graph pvc-line-graph-' . $count_class . '" title="' . sprintf( _n( '%s post view', '%s post views', $count, 'post-views-counter' ), number_format_i18n( $count ) ) . '"></span>'; |
| 462 | } |
| 463 | |
| 464 | $admin_bar->add_menu( |
| 465 | [ |
| 466 | 'id' => 'pvc-post-views', |
| 467 | 'title' => '<span class="pvc-graph-container">' . $graph . '</span>', |
| 468 | 'href' => false, |
| 469 | 'meta' => [ |
| 470 | 'title' => false |
| 471 | ] |
| 472 | ] |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Maybe add admin CSS. |
| 478 | * |
| 479 | * @global string $pagenow |
| 480 | * @global string $post |
| 481 | * |
| 482 | * @return void |
| 483 | */ |
| 484 | public function admin_bar_maybe_add_style() { |
| 485 | // get main instance |
| 486 | $pvc = Post_Views_Counter(); |
| 487 | |
| 488 | // set empty post |
| 489 | $post = null; |
| 490 | |
| 491 | // admin? |
| 492 | if ( is_admin() && ! wp_doing_ajax() ) { |
| 493 | global $pagenow; |
| 494 | |
| 495 | $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post; |
| 496 | // frontend? |
| 497 | } elseif ( is_singular() ) |
| 498 | global $post; |
| 499 | |
| 500 | // get countable post types |
| 501 | $post_types = $pvc->options['general']['post_types_count']; |
| 502 | |
| 503 | // whether to allow this post type or not |
| 504 | if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) ) |
| 505 | return; |
| 506 | |
| 507 | // add admin bar |
| 508 | add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 ); |
| 509 | |
| 510 | // backend |
| 511 | if ( current_action() === 'admin_init' ) |
| 512 | add_action( 'admin_head', [ $this, 'admin_bar_css' ] ); |
| 513 | // frontend |
| 514 | elseif ( current_action() === 'wp' ) |
| 515 | add_action( 'wp_head', [ $this, 'admin_bar_css' ] ); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Add admin CSS. |
| 520 | * |
| 521 | * @return void |
| 522 | */ |
| 523 | public function admin_bar_css() { |
| 524 | $html = ' |
| 525 | <style type="text/css"> |
| 526 | #wp-admin-bar-pvc-post-views .pvc-graph-container { padding-top: 6px; padding-bottom: 6px; position: relative; display: block; height: 100%; box-sizing: border-box; } |
| 527 | #wp-admin-bar-pvc-post-views .pvc-line-graph { |
| 528 | display: inline-block; |
| 529 | width: 1px; |
| 530 | margin-right: 1px; |
| 531 | background-color: #ccc; |
| 532 | vertical-align: baseline; |
| 533 | } |
| 534 | #wp-admin-bar-pvc-post-views .pvc-line-graph:hover { background-color: #eee; } |
| 535 | #wp-admin-bar-pvc-post-views .pvc-line-graph-0 { height: 1% }'; |
| 536 | |
| 537 | for ( $i = 1; $i <= 20; $i ++ ) { |
| 538 | $html .= ' |
| 539 | #wp-admin-bar-pvc-post-views .pvc-line-graph-' . $i . ' { height: ' . $i * 5 . '% }'; |
| 540 | } |
| 541 | |
| 542 | $html .= ' |
| 543 | </style>'; |
| 544 | |
| 545 | echo wp_kses( $html, [ 'style' => [] ] ); |
| 546 | } |
| 547 | } |
| 548 |