class-admin.php
8 months ago
class-columns.php
8 months ago
class-counter.php
8 months ago
class-crawler-detect.php
8 months ago
class-cron.php
8 months ago
class-dashboard.php
8 months ago
class-frontend.php
8 months ago
class-functions.php
8 months ago
class-query.php
8 months ago
class-settings-api.php
8 months ago
class-settings.php
8 months ago
class-update.php
8 months ago
class-widgets.php
8 months ago
functions.php
8 months ago
class-dashboard.php
666 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Dashboard class. |
| 8 | * |
| 9 | * @class Post_Views_Counter_Dashboard |
| 10 | */ |
| 11 | class Post_Views_Counter_Dashboard { |
| 12 | |
| 13 | private $widget_items = []; |
| 14 | |
| 15 | /** |
| 16 | * Class constructor. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | // actions |
| 22 | add_action( 'admin_init', [ $this, 'init_admin_dashboard' ] ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Dashboard initialization. |
| 27 | * |
| 28 | * @global string $pagenow |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function init_admin_dashboard() { |
| 33 | global $pagenow; |
| 34 | |
| 35 | // setup widget items |
| 36 | $this->setup_widget_items(); |
| 37 | |
| 38 | // do it only on dashboard page |
| 39 | if ( $pagenow === 'index.php' ) { |
| 40 | // filter user_can_see_stats |
| 41 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) |
| 42 | return; |
| 43 | |
| 44 | add_action( 'wp_dashboard_setup', [ $this, 'wp_dashboard_setup' ], 1 ); |
| 45 | add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts_styles' ] ); |
| 46 | // ajax endpoints |
| 47 | } elseif ( $pagenow === 'admin-ajax.php' ) { |
| 48 | add_action( 'wp_ajax_pvc_dashboard_post_most_viewed', [ $this, 'dashboard_post_most_viewed' ] ); |
| 49 | add_action( 'wp_ajax_pvc_dashboard_post_views_chart', [ $this, 'dashboard_post_views_chart' ] ); |
| 50 | add_action( 'wp_ajax_pvc_dashboard_user_options', [ $this, 'update_dashboard_user_options' ] ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add dashboard widget. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function wp_dashboard_setup() { |
| 60 | // add dashboard widget |
| 61 | wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views', 'post-views-counter' ), [ $this, 'dashboard_widget' ] ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Enqueue admin scripts and styles. |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function admin_scripts_styles() { |
| 70 | // get main instance |
| 71 | $pvc = Post_Views_Counter(); |
| 72 | |
| 73 | // styles |
| 74 | wp_enqueue_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.min.css', [], $pvc->defaults['version'] ); |
| 75 | wp_enqueue_style( 'pvc-microtip', POST_VIEWS_COUNTER_URL . '/assets/microtip/microtip.min.css', [], '1.0.0' ); |
| 76 | |
| 77 | // scripts |
| 78 | wp_enqueue_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', [ 'jquery', 'pvc-chartjs' ], $pvc->defaults['version'], true ); |
| 79 | |
| 80 | // prepare script data |
| 81 | $script_data = [ |
| 82 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 83 | 'nonce' => wp_create_nonce( 'pvc-dashboard-widget' ), |
| 84 | 'nonceUser' => wp_create_nonce( 'pvc-dashboard-user-options' ) |
| 85 | ]; |
| 86 | |
| 87 | wp_add_inline_script( 'pvc-admin-dashboard', 'var pvcArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Setup dashboard widget items. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | private function setup_widget_items() { |
| 96 | // standard items |
| 97 | $items = [ |
| 98 | [ |
| 99 | 'id' => 'post-views', |
| 100 | 'title' => __( 'Post Views', 'post-views-counter' ), |
| 101 | 'description' => __( 'Displays a chart of most viewed post types.', 'post-views-counter' ), |
| 102 | 'content' => '<canvas id="pvc-post-views-chart" height="' . (int) $this->calculate_canvas_size( Post_Views_Counter()->options['general']['post_types_count'] ) . '"></canvas>', |
| 103 | 'position' => 2 |
| 104 | ], |
| 105 | [ |
| 106 | 'id' => 'post-most-viewed', |
| 107 | 'title' => __( 'Top Posts', 'post-views-counter' ), |
| 108 | 'description' => __( 'Displays a list of most viewed single posts or pages.', 'post-views-counter' ), |
| 109 | 'content' => '<div id="pvc-post-most-viewed-content" class="pvc-table-responsive"></div>', |
| 110 | 'position' => 3 |
| 111 | ] |
| 112 | ]; |
| 113 | |
| 114 | // filter items, do not allow to remove main items |
| 115 | $new_items = apply_filters( 'pvc_dashboard_widget_items', [] ); |
| 116 | |
| 117 | // any new items? |
| 118 | if ( is_array( $new_items ) && ! empty( $new_items ) ) { |
| 119 | foreach ( $new_items as $item ) { |
| 120 | // add new item |
| 121 | array_push( $items, $item ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // sort dashboard items by position |
| 126 | array_multisort( array_column( $items, 'position' ), SORT_ASC, SORT_NUMERIC, $items ); |
| 127 | |
| 128 | // set widget items |
| 129 | $this->widget_items = $items; |
| 130 | } |
| 131 | |
| 132 | public function get_widget_items() { |
| 133 | // return widget items |
| 134 | return $this->widget_items; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Calculate canvas height based on number of legend items. |
| 139 | * |
| 140 | * @param array $data |
| 141 | * @param bool $expression |
| 142 | * @return int |
| 143 | */ |
| 144 | public function calculate_canvas_size( $data, $expression = true ) { |
| 145 | if ( $expression && ! empty( $data ) ) { |
| 146 | // treat every 4 legend items as 1 line - 23 pixels |
| 147 | $height = 23 * ( (int) ceil( count( $data ) / 4 ) - 1 ); |
| 148 | } else |
| 149 | $height = 0; |
| 150 | |
| 151 | return (int) ( 170 + $height ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Render dashboard widget. |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public function dashboard_widget() { |
| 160 | // get user options |
| 161 | $user_options = get_user_meta( get_current_user_id(), 'pvc_dashboard', true ); |
| 162 | |
| 163 | // empty options? |
| 164 | if ( empty( $user_options ) || ! is_array( $user_options ) ) |
| 165 | $user_options = []; |
| 166 | |
| 167 | // sanitize options |
| 168 | $user_options = map_deep( $user_options, 'sanitize_text_field' ); |
| 169 | |
| 170 | // get menu items |
| 171 | $menu_items = ! empty( $user_options['menu_items'] ) ? $user_options['menu_items'] : []; |
| 172 | |
| 173 | // get widget items |
| 174 | $items = $this->widget_items; |
| 175 | |
| 176 | $html = ' |
| 177 | <div id="pvc-dashboard-accordion" class="pvc-accordion">'; |
| 178 | |
| 179 | foreach ( $items as $item ) { |
| 180 | $html .= $this->generate_dashboard_widget_item( $item, $menu_items ); |
| 181 | } |
| 182 | |
| 183 | $html .= ' |
| 184 | <div class="pvc-dashboard-block"><span>' . esc_html__( 'Powered by', 'post-views-counter' ) . ' <a href="https://postviewscounter.com/?utm_source=post-views-counter-lite&utm_medium=link&utm_campaign=powered-by" target="_blank">Post Views Counter</a></span></div> |
| 185 | </div>'; |
| 186 | |
| 187 | echo $html; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Generate dashboard widget item HTML. |
| 192 | * |
| 193 | * @param array $item |
| 194 | * @param array $menu_items |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | public function generate_dashboard_widget_item( $item, $menu_items ) { |
| 199 | // get allowed html tags |
| 200 | $allowed_html = wp_kses_allowed_html( 'post' ); |
| 201 | $allowed_html['canvas'] = [ |
| 202 | 'id' => [], |
| 203 | 'height' => [] |
| 204 | ]; |
| 205 | |
| 206 | ob_start(); ?> |
| 207 | |
| 208 | <div id="pvc-<?php esc_attr_e( $item['id'] ); ?>" class="pvc-accordion-item<?php echo ( in_array( $item['id'], $menu_items, true ) ? ' pvc-collapsed' : '' ); ?>"> |
| 209 | <div class="pvc-accordion-header"> |
| 210 | <div class="pvc-accordion-toggle"><span class="pvc-accordion-title"><?php esc_html_e( $item['title'] ); ?></span><span class="pvc-tooltip" aria-label="<?php esc_html_e( $item['description'] ); ?>" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div> |
| 211 | <div class="pvc-accordion-actions"> |
| 212 | <!--<a href="javascript:void(0);" class="pvc-accordion-action dashicons dashicons-admin-generic"></a>--> |
| 213 | <a href="javascript:void(0);" class="pvc-accordion-action pvc-toggle-indicator"></a> |
| 214 | </div> |
| 215 | </div> |
| 216 | <div class="pvc-accordion-content"> |
| 217 | <div class="pvc-dashboard-container loading"> |
| 218 | <div class="pvc-dashboard-content-top"> |
| 219 | <?php do_action( 'pvc_dashboard_widget_content_top', $item['id'] ); ?> |
| 220 | </div> |
| 221 | <div class="pvc-data-container"> |
| 222 | <?php echo wp_kses( $item['content'], $allowed_html ); ?> |
| 223 | <span class="spinner"></span> |
| 224 | </div> |
| 225 | <div class="pvc-dashboard-content-bottom pvc-date-nav"> |
| 226 | <div class="pvc-date-nav pvc-months"> |
| 227 | <?php |
| 228 | // generate dates |
| 229 | echo wp_kses_post( $this->generate_months( current_time( 'timestamp', false ), $item['id'] ) ); |
| 230 | ?> |
| 231 | </div> |
| 232 | <?php do_action( 'pvc_dashboard_widget_content_bottom', $item['id'] ); ?> |
| 233 | </div> |
| 234 | </div> |
| 235 | </div> |
| 236 | </div> |
| 237 | |
| 238 | <?php |
| 239 | // Output current buffer |
| 240 | return ob_get_clean(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Render dashboard widget with post views. |
| 245 | * |
| 246 | * @return void |
| 247 | */ |
| 248 | public function dashboard_post_views_chart() { |
| 249 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) || ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) ) |
| 250 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 251 | |
| 252 | // get period |
| 253 | $period = isset( $_POST['period'] ) && ! empty( $_POST['period'] ) ? preg_replace( '/[^a-z0-9_|]/', '', $_POST['period'] ) : apply_filters( 'pvc_dashboard_widget_default_period', 'this_month', 'post-views' ); |
| 254 | |
| 255 | // get post types |
| 256 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 257 | |
| 258 | // empty options? |
| 259 | if ( empty( $post_types ) || ! is_array( $post_types ) ) |
| 260 | $post_types = []; |
| 261 | |
| 262 | // sanitize post_types |
| 263 | $post_types = map_deep( $post_types, 'sanitize_text_field' ); |
| 264 | |
| 265 | // get dashboard user options |
| 266 | $user_options = $this->get_dashboard_user_options( get_current_user_id(), 'post_types' ); |
| 267 | |
| 268 | // empty options? |
| 269 | if ( empty( $user_options ) || ! is_array( $user_options ) ) |
| 270 | $user_options = []; |
| 271 | |
| 272 | // sanitize options |
| 273 | $user_options = map_deep( $user_options, 'sanitize_text_field' ); |
| 274 | |
| 275 | // get colors |
| 276 | $colors = Post_Views_Counter()->functions->get_colors(); |
| 277 | |
| 278 | // parameters to be used in filter |
| 279 | $args = [ |
| 280 | 'widget' => 'post_views', |
| 281 | 'period' => $period, |
| 282 | 'post_types' => $post_types, |
| 283 | 'user_options' => $user_options |
| 284 | ]; |
| 285 | |
| 286 | $data = [ |
| 287 | 'widget' => 'post-views', |
| 288 | 'design' => [ |
| 289 | 'fill' => true, |
| 290 | 'backgroundColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 0.2)', |
| 291 | 'borderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)', |
| 292 | 'borderWidth' => 1.2, |
| 293 | 'borderDash' => [], |
| 294 | 'pointBorderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)', |
| 295 | 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)', |
| 296 | 'pointBorderWidth' => 1.2 |
| 297 | ], |
| 298 | 'data' => [ |
| 299 | 'datasets' => [ |
| 300 | [ |
| 301 | 'label' => __( 'Total Views', 'post-views-counter' ), |
| 302 | 'post_type' => '_pvc_total_views', |
| 303 | 'hidden' => in_array( '_pvc_total_views', $user_options, true ), |
| 304 | 'data' => [] |
| 305 | ] |
| 306 | ] |
| 307 | ], |
| 308 | ]; |
| 309 | |
| 310 | // convert period |
| 311 | $time = pvc_period2timestamp( $period ); |
| 312 | |
| 313 | // get date chunks |
| 314 | $date = date( 'Y m W d t', $time ); |
| 315 | $date_chunks = explode( ' ', $date ); |
| 316 | |
| 317 | // get date |
| 318 | $year = (int) $date_chunks[0]; |
| 319 | $month = sanitize_text_field( $date_chunks[1] ); |
| 320 | $dates_number = (int) $date_chunks[4]; |
| 321 | |
| 322 | // get previous date chunks |
| 323 | $previous_time = strtotime( '-1 months', $time ); |
| 324 | $previous_date = date( 'Y m W d t', $previous_time ); |
| 325 | $previous_date_chunks = explode( ' ', $previous_date ); |
| 326 | |
| 327 | // get current date |
| 328 | $current_date = date_create( 'now', wp_timezone() )->format('Y m W d t'); |
| 329 | $current_date_chunks = explode( ' ', $current_date ); |
| 330 | |
| 331 | // generate dates |
| 332 | $data['dates'] = $this->generate_months( $time ); |
| 333 | |
| 334 | // generate chart data |
| 335 | $sum = []; |
| 336 | |
| 337 | // any post types? |
| 338 | if ( ! empty( $post_types ) ) { |
| 339 | // reindex post types |
| 340 | $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) ); |
| 341 | |
| 342 | $post_type_data = []; |
| 343 | |
| 344 | foreach ( $post_types as $id => $post_type ) { |
| 345 | $post_type_obj = get_post_type_object( $post_type ); |
| 346 | |
| 347 | // unrecognized post type? (mainly from deactivated plugins) |
| 348 | if ( empty( $post_type_obj ) ) |
| 349 | $label = $post_type; |
| 350 | else |
| 351 | $label = $post_type_obj->labels->name; |
| 352 | |
| 353 | $data['data']['datasets'][$id]['label'] = $label; |
| 354 | $data['data']['datasets'][$id]['post_type'] = $post_type; |
| 355 | $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true ); |
| 356 | $data['data']['datasets'][$id]['data'] = []; |
| 357 | |
| 358 | // get month views |
| 359 | $post_type_data[$id] = array_values( |
| 360 | pvc_get_views( apply_filters( 'pvc_dashboard_post_views_query_args', |
| 361 | [ |
| 362 | 'fields' => 'date=>views', |
| 363 | 'post_type' => $post_type, |
| 364 | 'views_query' => [ |
| 365 | 'year' => $year, |
| 366 | 'month' => $month, |
| 367 | 'hide_empty' => true |
| 368 | ] |
| 369 | ], $period |
| 370 | ) ) |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | foreach ( $post_type_data as $post_type_id => $post_views ) { |
| 375 | foreach ( $post_views as $id => $views ) { |
| 376 | // generate chart data for specific post types |
| 377 | $data['data']['datasets'][$post_type_id]['data'][] = $views; |
| 378 | |
| 379 | if ( ! array_key_exists( $id, $sum ) ) |
| 380 | $sum[$id] = 0; |
| 381 | |
| 382 | $sum[$id] += $views; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // this month all days |
| 388 | for ( $i = 1; $i <= $dates_number; $i++ ) { |
| 389 | // generate chart data |
| 390 | $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i ); |
| 391 | $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $year . '-' . $month . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) ); |
| 392 | $data['data']['datasets'][0]['data'][] = ( array_key_exists( $i - 1, $sum ) ? $sum[$i - 1] : 0 ); |
| 393 | } |
| 394 | |
| 395 | echo wp_json_encode( apply_filters( 'pvc_dashboard_post_views_data', $data, $args ) ); |
| 396 | exit; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Render dashboard widget with most viewed posts. |
| 401 | * |
| 402 | * @return void |
| 403 | */ |
| 404 | public function dashboard_post_most_viewed() { |
| 405 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) || ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) ) |
| 406 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 407 | |
| 408 | // get main instance |
| 409 | $pvc = Post_Views_Counter(); |
| 410 | |
| 411 | // get post types |
| 412 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 413 | |
| 414 | // empty options? |
| 415 | if ( empty( $post_types ) || ! is_array( $post_types ) ) |
| 416 | $post_types = []; |
| 417 | |
| 418 | // sanitize post_types |
| 419 | $post_types = map_deep( $post_types, 'sanitize_text_field' ); |
| 420 | |
| 421 | // get period |
| 422 | $period = isset( $_POST['period'] ) && ! empty( $_POST['period'] ) ? preg_replace( '/[^a-z0-9_|]/', '', $_POST['period'] ) : apply_filters( 'pvc_dashboard_widget_default_period', 'this_month', 'post-most-viewed' ); |
| 423 | |
| 424 | // parameters to be used in filter |
| 425 | $args = [ |
| 426 | 'widget' => 'post_most_viewed', |
| 427 | 'period' => $period, |
| 428 | 'post_types' => $post_types |
| 429 | ]; |
| 430 | |
| 431 | $data = [ |
| 432 | 'widget' => 'post-most-viewed', |
| 433 | 'html' => '' |
| 434 | ]; |
| 435 | |
| 436 | // convert period |
| 437 | $time = pvc_period2timestamp( $period ); |
| 438 | |
| 439 | // get date chunks |
| 440 | $date = date( 'Y m W d t', $time ); |
| 441 | $date_chunks = explode( ' ', $date ); |
| 442 | |
| 443 | // get date |
| 444 | $year = (int) $date_chunks[0]; |
| 445 | $month = sanitize_text_field( $date_chunks[1] ); |
| 446 | |
| 447 | // generate dates |
| 448 | $data['dates'] = $this->generate_months( $time ); |
| 449 | |
| 450 | // query args |
| 451 | $query_args = apply_filters( 'pvc_dashboard_post_most_viewed_query_args', [ |
| 452 | 'post_type' => $post_types, |
| 453 | 'posts_per_page' => 10, |
| 454 | 'paged' => false, |
| 455 | 'suppress_filters' => false, |
| 456 | 'no_found_rows' => true, |
| 457 | 'views_query' => [ |
| 458 | 'year' => $year, |
| 459 | 'month' => $month, |
| 460 | 'hide_empty' => true |
| 461 | ] |
| 462 | ], $period ); |
| 463 | |
| 464 | $posts = pvc_get_most_viewed_posts( $query_args ); |
| 465 | |
| 466 | $html = ' |
| 467 | <table id="pvc-post-most-viewed-table" class="pvc-table pvc-table-hover"> |
| 468 | <thead> |
| 469 | <tr> |
| 470 | <th scope="col">#</th> |
| 471 | <th scope="col">' . esc_html__( 'Post', 'post-views-counter' ) . '</th> |
| 472 | <th scope="col">' . esc_html__( 'Views', 'post-views-counter' ) . '</th> |
| 473 | </tr> |
| 474 | </thead> |
| 475 | <tbody>'; |
| 476 | |
| 477 | if ( $posts ) { |
| 478 | // active post types |
| 479 | $active_post_types = []; |
| 480 | |
| 481 | foreach ( $posts as $index => $post ) { |
| 482 | setup_postdata( $post ); |
| 483 | |
| 484 | $html .= ' |
| 485 | <tr> |
| 486 | <th scope="col">' . ( $index + 1 ) . '</th>'; |
| 487 | |
| 488 | // check post type existence |
| 489 | if ( array_key_exists( $post->post_type, $active_post_types ) ) |
| 490 | $post_type_exists = $active_post_types[$post->post_type]; |
| 491 | else |
| 492 | $post_type_exists = $active_post_types[$post->post_type] = post_type_exists( $post->post_type ); |
| 493 | |
| 494 | $title = get_the_title( $post ); |
| 495 | |
| 496 | if ( $title === '' ) |
| 497 | $title = __( '(no title)' ); |
| 498 | |
| 499 | // post link |
| 500 | $link = '<a href="' . esc_url( get_permalink( $post->ID ) ) . '" target="_blank">' . esc_html( $title ) . '</a>'; |
| 501 | |
| 502 | // edit post link |
| 503 | if ( $post_type_exists && current_user_can( 'edit_post', $post->ID ) ) { |
| 504 | $link .= ' <a href="' . esc_url( get_edit_post_link( $post->ID ) ) . '" class="cn-edit-link" target="_blank">' . esc_html__( 'Edit', 'post-views-counter' ) . '</a>'; |
| 505 | } |
| 506 | |
| 507 | $html .= ' |
| 508 | <td>' . $link . '</td>'; |
| 509 | |
| 510 | $html .= ' |
| 511 | <td>' . number_format_i18n( $post->post_views ) . '</td> |
| 512 | </tr>'; |
| 513 | } |
| 514 | } else { |
| 515 | $html .= ' |
| 516 | <tr class="no-posts"> |
| 517 | <td colspan="3">' . esc_html__( 'No most viewed posts found.', 'post-views-counter' ) . '</td> |
| 518 | </tr>'; |
| 519 | } |
| 520 | |
| 521 | $html .= ' |
| 522 | </tbody> |
| 523 | </table>'; |
| 524 | |
| 525 | $data['html'] = $html; |
| 526 | |
| 527 | echo wp_json_encode( apply_filters( 'pvc_dashboard_post_most_viewed_data', $data, $args ) ); |
| 528 | exit; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Generate dashboard widget months HTML. |
| 533 | * |
| 534 | * @param int $timestamp |
| 535 | * @return string |
| 536 | */ |
| 537 | public function generate_months( $timestamp, $item = '' ) { |
| 538 | $dates = [ |
| 539 | explode( ' ', date( 'm F Y', strtotime( '-1 months', $timestamp ) ) ), |
| 540 | explode( ' ', date( 'm F Y', $timestamp ) ), |
| 541 | explode( ' ', date( 'm F Y', strtotime( '+1 months', $timestamp ) ) ) |
| 542 | ]; |
| 543 | |
| 544 | $current = date( 'Ym', current_time( 'timestamp', false ) ); |
| 545 | |
| 546 | if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) ) |
| 547 | $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>'; |
| 548 | else |
| 549 | $next = '<a class="next" href="#" data-date="' . ( $dates[2][2] . $dates[2][0] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>'; |
| 550 | |
| 551 | $dates_formatted = apply_filters( 'pvc_dashboard_widget_generate_months_formatted', [ |
| 552 | 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][2] . $dates[0][0] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>', |
| 553 | 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>', |
| 554 | 'next' => $next |
| 555 | ], $timestamp, $item ); |
| 556 | |
| 557 | return wp_kses_post( apply_filters( 'pvc_dashboard_widget_generate_months_html', $dates_formatted['prev'] . $dates_formatted['current'] . $dates_formatted['next'], $timestamp, $item ) ); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Update dashboard widget user options. |
| 562 | * |
| 563 | * @return void |
| 564 | */ |
| 565 | public function update_dashboard_user_options() { |
| 566 | if ( ! check_ajax_referer( 'pvc-dashboard-user-options', 'nonce' ) ) |
| 567 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 568 | |
| 569 | // valid data? |
| 570 | if ( isset( $_POST['nonce'], $_POST['options'] ) && ! empty( $_POST['options'] ) ) { |
| 571 | // get sanitized options |
| 572 | $update = map_deep( $_POST['options'], 'sanitize_text_field' ); |
| 573 | |
| 574 | // get user ID |
| 575 | $user_id = get_current_user_id(); |
| 576 | |
| 577 | // get user dashboard data |
| 578 | $user_options = get_user_meta( $user_id, 'pvc_dashboard', true ); |
| 579 | |
| 580 | // empty userdata? |
| 581 | if ( ! is_array( $user_options ) || empty( $user_options ) ) |
| 582 | $user_options = []; |
| 583 | |
| 584 | // empty post types? |
| 585 | if ( ! array_key_exists( 'post_types', $user_options ) || ! is_array( $user_options['post_types'] ) ) |
| 586 | $user_options['post_types'] = []; |
| 587 | |
| 588 | // hide post type? |
| 589 | if ( ! empty( $update['post_type'] ) ) { |
| 590 | // get allowed post types |
| 591 | $allowed_post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 592 | |
| 593 | // simulate total post views as post type |
| 594 | $allowed_post_types[] = '_pvc_total_views'; |
| 595 | |
| 596 | if ( in_array( $update['post_type'], $allowed_post_types, true ) ) { |
| 597 | if ( isset( $update['hidden'] ) && $update['hidden'] === 'true' ) { |
| 598 | if ( ! in_array( $update['post_type'], $user_options['post_types'], true ) ) |
| 599 | $user_options['post_types'][] = $update['post_type']; |
| 600 | } else { |
| 601 | if ( ( $key = array_search( $update['post_type'], $user_options['post_types'] ) ) !== false ) |
| 602 | unset( $user_options['post_types'][$key] ); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // empty menu items? |
| 608 | if ( ! array_key_exists( 'menu_items', $user_options ) || ! is_array( $user_options['menu_items'] ) ) |
| 609 | $user_options['menu_items'] = []; |
| 610 | |
| 611 | if ( ! empty( $update['menu_items'] ) && is_array( $update['menu_items'] ) ) { |
| 612 | $user_options['menu_items'] = []; |
| 613 | |
| 614 | // get allowed menu items |
| 615 | $allowed_menu_items = array_column( $this->widget_items, 'id' ); |
| 616 | |
| 617 | foreach ( $update['menu_items'] as $menu_item => $hidden ) { |
| 618 | if ( in_array( $menu_item, $allowed_menu_items, true ) && $hidden === 'true' ) |
| 619 | $user_options['menu_items'][] = $menu_item; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // filter user options |
| 624 | $user_options = apply_filters( 'pvc_update_dashboard_user_options', $user_options, $update, $user_id ); |
| 625 | |
| 626 | // update userdata |
| 627 | update_user_meta( $user_id, 'pvc_dashboard', $user_options ); |
| 628 | |
| 629 | echo wp_send_json_success(); |
| 630 | } |
| 631 | |
| 632 | echo wp_send_json_error(); |
| 633 | exit; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Get user dashboard data. |
| 638 | * |
| 639 | * @param int $user_id |
| 640 | * @param string $data_type |
| 641 | * @return array |
| 642 | */ |
| 643 | public function get_dashboard_user_options( $user_id = 0, $data_type = '' ) { |
| 644 | $user_options = get_user_meta( $user_id, 'pvc_dashboard', true ); |
| 645 | |
| 646 | if ( ! is_array( $user_options ) || empty( $user_options ) ) |
| 647 | $user_options = []; |
| 648 | |
| 649 | if ( ! array_key_exists( $data_type, $user_options ) || ! is_array( $user_options[$data_type] ) ) |
| 650 | $user_options[$data_type] = []; |
| 651 | |
| 652 | return $user_options[$data_type]; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Convert period to timestamp. |
| 657 | * |
| 658 | * @deprecated |
| 659 | * @param string $period |
| 660 | * @return int |
| 661 | */ |
| 662 | public function period2timestamp( $period ) { |
| 663 | return pvc_period2timestamp( $period ); |
| 664 | } |
| 665 | } |
| 666 |