ajax.php
7 years ago
class-admin.php
4 years ago
class-columns.php
4 years ago
class-counter.php
4 years ago
class-crawler-detect.php
4 years ago
class-cron.php
4 years ago
class-dashboard.php
4 years ago
class-frontend.php
4 years ago
class-functions.php
4 years ago
class-query.php
4 years ago
class-settings-api.php
4 years ago
class-settings.php
4 years ago
class-update.php
4 years ago
class-widgets.php
4 years ago
columns.php
6 years ago
counter.php
6 years ago
crawler-detect.php
6 years ago
cron.php
6 years ago
dashboard.php
6 years ago
frontend.php
6 years ago
functions.php
4 years ago
query.php
6 years ago
settings.php
6 years ago
update.php
6 years ago
widgets.php
6 years ago
columns.php
449 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 | public function __construct() { |
| 14 | // actions |
| 15 | add_action( 'admin_init', array( $this, 'register_new_column' ) ); |
| 16 | add_action( 'post_submitbox_misc_actions', array( $this, 'submitbox_views' ) ); |
| 17 | add_action( 'attachment_submitbox_misc_actions', array( $this, 'submitbox_views' ) ); |
| 18 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
| 19 | add_action( 'edit_attachment', array( $this, 'save_post' ), 10 ); |
| 20 | add_action( 'bulk_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 ); |
| 21 | add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 ); |
| 22 | add_action( 'wp_ajax_save_bulk_post_views', array( $this, 'save_bulk_post_views' ) ); |
| 23 | |
| 24 | // gutenberg |
| 25 | add_action( 'plugins_loaded', array( $this, 'init_gutemberg' ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Init Gutenberg |
| 30 | */ |
| 31 | public function init_gutemberg() { |
| 32 | $block_editor = has_action( 'enqueue_block_assets' ); |
| 33 | $gutenberg = function_exists( 'gutenberg_can_edit_post_type' ); |
| 34 | |
| 35 | if ( ! $block_editor && ! $gutenberg ) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | add_action( 'add_meta_boxes', array( $this, 'gutenberg_add_meta_box' ) ); |
| 40 | add_action( 'rest_api_init', array( $this, 'gutenberg_rest_api_init' ) ); |
| 41 | add_action( 'enqueue_block_editor_assets', array( $this, 'gutenberg_enqueue_scripts' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register Gutenberg Metabox. |
| 46 | */ |
| 47 | public function gutenberg_add_meta_box() { |
| 48 | add_meta_box( 'post_views_meta_box', __( 'Post Views', 'post-views-counter' ), '', 'post', '', '', array( |
| 49 | '__back_compat_meta_box' => false, |
| 50 | '__block_editor_compatible_meta_box' => true |
| 51 | ) ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register REST API Gutenberg endpoints. |
| 56 | */ |
| 57 | public function gutenberg_rest_api_init() { |
| 58 | // get views route |
| 59 | register_rest_route( |
| 60 | 'post-views-counter', |
| 61 | '/update-post-views/', |
| 62 | array( |
| 63 | 'methods' => array( 'POST' ), |
| 64 | 'callback' => array( $this, 'gutenberg_update_callback' ), |
| 65 | 'args' => array( |
| 66 | 'id' => array( |
| 67 | 'sanitize_callback' => 'absint', |
| 68 | ) |
| 69 | ) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * REST API Callback for Gutenberg endpoint. |
| 76 | * |
| 77 | * @param array $data |
| 78 | * @return array|int |
| 79 | */ |
| 80 | public function gutenberg_update_callback( $data ) { |
| 81 | $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0; |
| 82 | $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0; |
| 83 | |
| 84 | // get countable post types |
| 85 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 86 | |
| 87 | // check if post exists |
| 88 | $post = get_post( $post_id ); |
| 89 | |
| 90 | // whether to count this post type or not |
| 91 | if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) ) |
| 92 | return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) ); |
| 93 | |
| 94 | // break if current user can't edit this post |
| 95 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 96 | return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 97 | |
| 98 | // break if views editing is restricted |
| 99 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 100 | |
| 101 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 102 | return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) ); |
| 103 | |
| 104 | global $wpdb; |
| 105 | |
| 106 | pvc_update_post_views( $post_id, $post_views ); |
| 107 | |
| 108 | do_action( 'pvc_after_update_post_views_count', $post_id ); |
| 109 | |
| 110 | return $post_id; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Enqueue front end and editor JavaScript and CSS |
| 115 | */ |
| 116 | public function gutenberg_enqueue_scripts() { |
| 117 | // enqueue the bundled block JS file |
| 118 | wp_enqueue_script( |
| 119 | 'pvc-gutenberg', |
| 120 | POST_VIEWS_COUNTER_URL . '/js/gutenberg.min.js', |
| 121 | array( 'wp-i18n', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-api' ), |
| 122 | Post_Views_Counter()->defaults['version'] |
| 123 | ); |
| 124 | |
| 125 | // restrict editing |
| 126 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 127 | $can_edit = $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ); |
| 128 | |
| 129 | $js_args = array( |
| 130 | 'postID' => get_the_ID(), |
| 131 | 'postViews' => pvc_get_post_views( get_the_ID() ), |
| 132 | 'canEdit' => $can_edit, |
| 133 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
| 134 | 'textPostViews' => __( 'Post Views', 'post-views-counter' ), |
| 135 | 'textHelp' => __( 'Adjust the views count for this post.', 'post-views-counter' ), |
| 136 | 'textCancel' => __( 'Cancel', 'post-views-counter' ) |
| 137 | ); |
| 138 | |
| 139 | wp_localize_script( |
| 140 | 'pvc-gutenberg', |
| 141 | 'pvcEditorArgs', |
| 142 | $js_args |
| 143 | ); |
| 144 | |
| 145 | // enqueue frontend and editor block styles |
| 146 | wp_enqueue_style( |
| 147 | 'pvc-gutenberg', |
| 148 | POST_VIEWS_COUNTER_URL . '/css/gutenberg.min.css', '', |
| 149 | Post_Views_Counter()->defaults['version'] |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Output post views for single post. |
| 155 | * |
| 156 | * @global object $post |
| 157 | * @return mixed |
| 158 | */ |
| 159 | public function submitbox_views() { |
| 160 | global $post; |
| 161 | |
| 162 | if ( ! in_array( $post->post_type, (array) Post_Views_Counter()->options['general']['post_types_count'] ) ) |
| 163 | return; |
| 164 | |
| 165 | // break if current user can't edit this post |
| 166 | if ( ! current_user_can( 'edit_post', $post->ID ) ) |
| 167 | return; |
| 168 | |
| 169 | // get total post views |
| 170 | $count = (int) pvc_get_post_views( $post->ID ); ?> |
| 171 | |
| 172 | <div class="misc-pub-section" id="post-views"> |
| 173 | |
| 174 | <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?> |
| 175 | |
| 176 | <span id="post-views-display"> |
| 177 | <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?> |
| 178 | </span> |
| 179 | |
| 180 | <?php |
| 181 | // restrict editing |
| 182 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 183 | |
| 184 | if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) { |
| 185 | ?> |
| 186 | <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a> |
| 187 | |
| 188 | <div id="post-views-input-container" class="hide-if-js"> |
| 189 | |
| 190 | <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p> |
| 191 | <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo $count; ?>" /> |
| 192 | <input type="text" name="post_views" id="post-views-input" value="<?php echo $count; ?>"/><br /> |
| 193 | <p> |
| 194 | <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a> |
| 195 | <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a> |
| 196 | </p> |
| 197 | |
| 198 | </div> |
| 199 | <?php |
| 200 | } |
| 201 | ?> |
| 202 | |
| 203 | </div> |
| 204 | <?php |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Save post views data. |
| 209 | * |
| 210 | * @param int $post_id |
| 211 | * @param object $post |
| 212 | */ |
| 213 | public function save_post( $post_id, $post = null ) { |
| 214 | if ( is_null( $post ) ) |
| 215 | $post_type = get_post_type( $post_id ); |
| 216 | else |
| 217 | $post_type = $post->post_type; |
| 218 | |
| 219 | // break if doing autosave |
| 220 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 221 | return $post_id; |
| 222 | |
| 223 | // break if current user can't edit this post |
| 224 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 225 | return $post_id; |
| 226 | |
| 227 | // is post views set |
| 228 | if ( ! isset( $_POST['post_views'] ) ) |
| 229 | return $post_id; |
| 230 | |
| 231 | // cast numeric post views |
| 232 | $post_views = (int) $_POST['post_views']; |
| 233 | |
| 234 | // unchanged post views value? |
| 235 | if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] ) |
| 236 | return $post_id; |
| 237 | |
| 238 | // break if post views in not one of the selected |
| 239 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 240 | |
| 241 | if ( ! in_array( $post_type, (array) $post_types ) ) |
| 242 | return $post_id; |
| 243 | |
| 244 | // break if views editing is restricted |
| 245 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 246 | |
| 247 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 248 | return $post_id; |
| 249 | |
| 250 | // validate data |
| 251 | if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) ) |
| 252 | return $post_id; |
| 253 | |
| 254 | pvc_update_post_views( $post_id, $post_views ); |
| 255 | |
| 256 | do_action( 'pvc_after_update_post_views_count', $post_id ); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Register post views column for specific post types |
| 261 | */ |
| 262 | public function register_new_column() { |
| 263 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 264 | |
| 265 | if ( ! empty( $post_types ) ) { |
| 266 | foreach ( $post_types as $post_type ) { |
| 267 | if ( $post_type === 'attachment' ) { |
| 268 | // actions |
| 269 | add_action( 'manage_media_custom_column', array( $this, 'add_new_column_content' ), 10, 2 ); |
| 270 | |
| 271 | // filters |
| 272 | add_filter( 'manage_media_columns', array( $this, 'add_new_column' ) ); |
| 273 | add_filter( 'manage_upload_sortable_columns', array( $this, 'register_sortable_custom_column' ) ); |
| 274 | } else { |
| 275 | // actions |
| 276 | add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 ); |
| 277 | |
| 278 | // filters |
| 279 | add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) ); |
| 280 | add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) ); |
| 281 | |
| 282 | if ( class_exists( 'bbPress' ) ) { |
| 283 | if ( $post_type === 'forum' ) |
| 284 | add_filter( 'bbp_admin_forums_column_headers', array( $this, 'add_new_column' ) ); |
| 285 | elseif ( $post_type === 'topic' ) |
| 286 | add_filter( 'bbp_admin_topics_column_headers', array( $this, 'add_new_column' ) ); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Register sortable post views column. |
| 295 | * |
| 296 | * @param array $columns |
| 297 | * @return array |
| 298 | */ |
| 299 | public function register_sortable_custom_column( $columns ) { |
| 300 | // add new sortable column |
| 301 | $columns['post_views'] = 'post_views'; |
| 302 | |
| 303 | return $columns; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Add post views column. |
| 308 | * |
| 309 | * @param array $columns |
| 310 | * @return array |
| 311 | */ |
| 312 | public function add_new_column( $columns ) { |
| 313 | $offset = 0; |
| 314 | |
| 315 | if ( isset( $columns['date'] ) ) |
| 316 | $offset++; |
| 317 | |
| 318 | if ( isset( $columns['comments'] ) ) |
| 319 | $offset++; |
| 320 | |
| 321 | if ( $offset > 0 ) { |
| 322 | $date = array_slice( $columns, -$offset, $offset, true ); |
| 323 | |
| 324 | foreach ( $date as $column => $name ) { |
| 325 | unset( $columns[$column] ); |
| 326 | } |
| 327 | |
| 328 | $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>'; |
| 329 | |
| 330 | foreach ( $date as $column => $name ) { |
| 331 | $columns[$column] = $name; |
| 332 | } |
| 333 | } else |
| 334 | $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>'; |
| 335 | |
| 336 | return $columns; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Add post views column content. |
| 341 | * |
| 342 | * @param string $column_name |
| 343 | * @param int $id |
| 344 | * @return muxed |
| 345 | */ |
| 346 | public function add_new_column_content( $column_name, $id ) { |
| 347 | if ( $column_name === 'post_views' ) { |
| 348 | // get total post views |
| 349 | $count = pvc_get_post_views( $id ); |
| 350 | |
| 351 | echo $count; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Handle quick edit. |
| 357 | * |
| 358 | * @global string $pagenow |
| 359 | * @param string $column_name |
| 360 | * @return mixed |
| 361 | */ |
| 362 | function quick_edit_custom_box( $column_name, $post_type ) { |
| 363 | global $pagenow, $post; |
| 364 | |
| 365 | if ( $pagenow !== 'edit.php' ) |
| 366 | return; |
| 367 | |
| 368 | if ( $column_name !== 'post_views' ) |
| 369 | return; |
| 370 | |
| 371 | if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) ) |
| 372 | return; |
| 373 | |
| 374 | // break if views editing is restricted |
| 375 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 376 | |
| 377 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 378 | return; |
| 379 | |
| 380 | ?> |
| 381 | <fieldset class="inline-edit-col-left"> |
| 382 | <div id="inline-edit-post_views" class="inline-edit-col"> |
| 383 | <label class="inline-edit-group"> |
| 384 | <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span> |
| 385 | <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span> |
| 386 | <input type="hidden" name="current_post_views" value="" /> |
| 387 | <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?> |
| 388 | </label> |
| 389 | </div> |
| 390 | </fieldset> |
| 391 | <?php |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Bulk save post views. |
| 396 | * |
| 397 | * @global object $wpdb; |
| 398 | * @return type |
| 399 | */ |
| 400 | function save_bulk_post_views() { |
| 401 | if ( ! isset( $_POST['post_views'] ) ) |
| 402 | $count = null; |
| 403 | else { |
| 404 | $count = trim( $_POST['post_views'] ); |
| 405 | |
| 406 | if ( is_numeric( $_POST['post_views'] ) ) { |
| 407 | $count = (int) $_POST['post_views']; |
| 408 | |
| 409 | if ( $count < 0 ) |
| 410 | $count = 0; |
| 411 | } else |
| 412 | $count = null; |
| 413 | } |
| 414 | |
| 415 | $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : array(); |
| 416 | |
| 417 | if ( is_null( $count ) ) |
| 418 | exit; |
| 419 | |
| 420 | // break if views editing is restricted |
| 421 | $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views']; |
| 422 | |
| 423 | if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) |
| 424 | exit; |
| 425 | |
| 426 | if ( ! empty( $post_ids ) ) { |
| 427 | foreach ( $post_ids as $post_id ) { |
| 428 | |
| 429 | // break if current user can't edit this post |
| 430 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 431 | continue; |
| 432 | |
| 433 | global $wpdb; |
| 434 | |
| 435 | // insert or update db post views count |
| 436 | $wpdb->query( |
| 437 | $wpdb->prepare( " |
| 438 | INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) |
| 439 | VALUES (%d, %d, %s, %d) |
| 440 | ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count |
| 441 | ) |
| 442 | ); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | exit; |
| 447 | } |
| 448 | } |
| 449 |