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.php
4 years ago
update.php
6 years ago
widgets.php
4 years ago
dashboard.php
606 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 | public function __construct() { |
| 14 | // actions |
| 15 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ), 1 ); |
| 16 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) ); |
| 17 | add_action( 'wp_ajax_pvc_dashboard_most_viewed', array( $this, 'dashboard_get_most_viewed' ) ); |
| 18 | add_action( 'wp_ajax_pvc_dashboard_chart', array( $this, 'dashboard_get_chart' ) ); |
| 19 | add_action( 'wp_ajax_pvc_dashboard_user_options', array( $this, 'update_dashboard_user_options' ) ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Initialize widget. |
| 24 | */ |
| 25 | public function wp_dashboard_setup() { |
| 26 | // filter user_can_see_stats |
| 27 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // add dashboard post views chart widget |
| 32 | wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views Counter', 'post-views-counter' ), array( $this, 'dashboard_widget' ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Enqueue admin scripts and styles. |
| 37 | * |
| 38 | * @param string $pagenow |
| 39 | */ |
| 40 | public function admin_scripts_styles( $pagenow ) { |
| 41 | if ( $pagenow != 'index.php' ) |
| 42 | return; |
| 43 | |
| 44 | // filter user_can_see_stats |
| 45 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) |
| 46 | return; |
| 47 | |
| 48 | wp_register_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css', array(), Post_Views_Counter()->defaults['version'] ); |
| 49 | wp_enqueue_style( 'pvc-admin-dashboard' ); |
| 50 | wp_enqueue_style( 'pvc-chartjs', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.css', array(), Post_Views_Counter()->defaults['version'] ); |
| 51 | wp_enqueue_style( 'pvc-microtip', POST_VIEWS_COUNTER_URL . '/assets/microtip/microtip.min.css', array(), Post_Views_Counter()->defaults['version'] ); |
| 52 | |
| 53 | wp_register_script( 'pvc-chartjs', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true ); |
| 54 | wp_register_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chartjs' ), Post_Views_Counter()->defaults['version'], true ); |
| 55 | |
| 56 | wp_enqueue_script( 'pvc-admin-dashboard' ); |
| 57 | |
| 58 | wp_localize_script( |
| 59 | 'pvc-admin-dashboard', |
| 60 | 'pvcArgs', |
| 61 | array( |
| 62 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 63 | 'nonce' => wp_create_nonce( 'pvc-dashboard-chart' ), |
| 64 | 'nonceUser' => wp_create_nonce( 'pvc-dashboard-user-options' ) |
| 65 | ) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Render dashboard widget. |
| 71 | * |
| 72 | * @return mixed |
| 73 | */ |
| 74 | public function dashboard_widget() { |
| 75 | // get user ID |
| 76 | $user_id = get_current_user_id(); |
| 77 | |
| 78 | // get user options |
| 79 | $user_options = get_user_meta( $user_id, 'pvc_dashboard', true ); |
| 80 | |
| 81 | // empty options? |
| 82 | if ( ! is_array( $user_options ) || empty( $user_options ) ) |
| 83 | $user_options = array(); |
| 84 | |
| 85 | // sanitize options |
| 86 | $user_options = map_deep( $user_options, 'sanitize_text_field' ); |
| 87 | $menu_items = ! empty( $user_options['menu_items'] ) ? $user_options['menu_items'] : array(); |
| 88 | |
| 89 | $months_html = $this->generate_months( current_time( 'timestamp', false ) ); |
| 90 | ?> |
| 91 | <div id="pvc-dashboard-accordion" class="pvc-accordion"> |
| 92 | <div id="pvc-post-views" class="pvc-accordion-item<?php echo in_array( 'post-views', $menu_items ) ? ' pvc-collapsed' : ''; ?>"> |
| 93 | <div class="pvc-accordion-header"> |
| 94 | <div class="pvc-accordion-toggle"><span class="pvc-accordion-title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span><span class="pvc-tooltip" aria-label="<?php _e( 'Displays the chart of most viewed post types for a selected time period.', 'post-views-counter' ); ?>" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div> |
| 95 | <?php /* |
| 96 | <div class="pvc-accordion-actions"> |
| 97 | <a href="javascript:void(0);" class="pvc-accordion-action dashicons dashicons-admin-generic"></a> |
| 98 | </div> |
| 99 | */ ?> |
| 100 | </div> |
| 101 | <div class="pvc-accordion-content"> |
| 102 | <div class="pvc-dashboard-container loading"> |
| 103 | |
| 104 | <div id="pvc-chart-container" class="pvc-data-container"> |
| 105 | <canvas id="pvc-chart" height="175"></canvas> |
| 106 | <span class="spinner"></span> |
| 107 | </div> |
| 108 | |
| 109 | <div class="pvc-months"> |
| 110 | <?php echo $months_html; ?> |
| 111 | </div> |
| 112 | |
| 113 | </div> |
| 114 | </div> |
| 115 | </div> |
| 116 | <div id="pvc-most-viewed" class="pvc-accordion-item<?php echo in_array( 'most-viewed', $menu_items ) ? ' pvc-collapsed' : ''; ?>"> |
| 117 | <div class="pvc-accordion-header"> |
| 118 | <div class="pvc-accordion-toggle"><span class="pvc-accordion-title"><?php _e( 'Top Posts', 'post-views-counter' ); ?></span><span class="pvc-tooltip" aria-label="<?php _e( 'Displays the list of most viewed posts and pages on your website.', 'post-views-counter' ); ?>" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div> |
| 119 | </div> |
| 120 | <div class="pvc-accordion-content"> |
| 121 | <div class="pvc-dashboard-container loading"> |
| 122 | |
| 123 | <div class="pvc-data-container"> |
| 124 | <div id="pvc-viewed" class="pvc-table-responsive"></div> |
| 125 | <span class="spinner"></span> |
| 126 | </div> |
| 127 | |
| 128 | <div class="pvc-months"> |
| 129 | <?php echo $months_html; ?> |
| 130 | </div> |
| 131 | |
| 132 | </div> |
| 133 | </div> |
| 134 | </div> |
| 135 | </div> |
| 136 | <?php |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Dashboard widget chart data function. |
| 141 | * |
| 142 | * @global $_wp_admin_css_colors |
| 143 | * @return void |
| 144 | */ |
| 145 | public function dashboard_get_chart() { |
| 146 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) |
| 147 | wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 148 | |
| 149 | if ( ! check_ajax_referer( 'pvc-dashboard-chart', 'nonce' ) ) |
| 150 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 151 | |
| 152 | // get period |
| 153 | $period = isset( $_POST['period'] ) ? esc_attr( $_POST['period'] ) : 'this_month'; |
| 154 | |
| 155 | // get post types |
| 156 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 157 | |
| 158 | // get stats |
| 159 | $query_args = array( |
| 160 | 'post_type' => $post_types, |
| 161 | 'posts_per_page' => -1, |
| 162 | 'paged' => false, |
| 163 | 'orderby' => 'post_views', |
| 164 | 'suppress_filters' => false, |
| 165 | 'no_found_rows' => true |
| 166 | ); |
| 167 | |
| 168 | // $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) ); |
| 169 | $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) - 2592000 ); |
| 170 | |
| 171 | // get color scheme global |
| 172 | global $_wp_admin_css_colors; |
| 173 | |
| 174 | // set default color; |
| 175 | $color = array( |
| 176 | 'r' => 105, |
| 177 | 'g' => 168, |
| 178 | 'b' => 187 |
| 179 | ); |
| 180 | |
| 181 | if ( ! empty( $_wp_admin_css_colors ) ) { |
| 182 | // get current admin color scheme name |
| 183 | $current_color_scheme = get_user_option( 'admin_color' ); |
| 184 | |
| 185 | if ( empty( $current_color_scheme ) ) |
| 186 | $current_color_scheme = 'fresh'; |
| 187 | |
| 188 | if ( isset( $_wp_admin_css_colors[$current_color_scheme] ) ) |
| 189 | $color = $this->hex2rgb( $_wp_admin_css_colors[$current_color_scheme]->colors[2] ); |
| 190 | } |
| 191 | |
| 192 | // set chart labels |
| 193 | switch ( $period ) { |
| 194 | case 'this_week': |
| 195 | $data = array( |
| 196 | 'text' => array( |
| 197 | 'xAxes' => date_i18n( 'F Y' ), |
| 198 | 'yAxes' => __( 'Post Views', 'post-views-counter' ) |
| 199 | ) |
| 200 | ); |
| 201 | |
| 202 | for ( $day = 0; $day <= 6; $day ++ ) { |
| 203 | $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' ); |
| 204 | $query = new WP_Query( wp_parse_args( $query_args, array( 'views_query' => array( 'year' => date( 'Y', $date ), 'month' => date( 'n', $date ), 'day' => date( 'd', $date ) ) ) ) ); |
| 205 | |
| 206 | $data['data']['labels'][] = date_i18n( 'j', $date ); |
| 207 | $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' ); |
| 208 | $data['data']['datasets'][0]['data'][] = $query->total_views; |
| 209 | } |
| 210 | break; |
| 211 | |
| 212 | case 'this_year': |
| 213 | $data = array( |
| 214 | 'text' => array( |
| 215 | 'xAxes' => __( 'Year', 'post-views-counter' ) . date( ' Y' ), |
| 216 | 'yAxes' => __( 'Post Views', 'post-views-counter' ), |
| 217 | ), |
| 218 | 'design' => array( |
| 219 | 'fill' => true, |
| 220 | 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)', |
| 221 | 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 222 | 'borderWidth' => 1.2, |
| 223 | 'borderDash' => array(), |
| 224 | 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 225 | 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)', |
| 226 | 'pointBorderWidth' => 1.2 |
| 227 | ) |
| 228 | ); |
| 229 | |
| 230 | $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' ); |
| 231 | $data['data']['datasets'][0]['post_type'] = '_pvc_total_views'; |
| 232 | |
| 233 | // reindex post types |
| 234 | $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) ); |
| 235 | |
| 236 | $post_type_data = array(); |
| 237 | |
| 238 | foreach ( $post_types as $id => $post_type ) { |
| 239 | $post_type_obj = get_post_type_object( $post_type ); |
| 240 | |
| 241 | $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name; |
| 242 | $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name; |
| 243 | $data['data']['datasets'][$id]['data'] = array(); |
| 244 | |
| 245 | // get month views |
| 246 | $post_type_data[$id] = array_values( |
| 247 | pvc_get_views( |
| 248 | array( |
| 249 | 'fields' => 'date=>views', |
| 250 | 'post_type' => $post_type, |
| 251 | 'views_query' => array( |
| 252 | 'year' => date( 'Y' ), |
| 253 | 'month' => '', |
| 254 | 'week' => '', |
| 255 | 'day' => '' |
| 256 | ) |
| 257 | ) |
| 258 | ) |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | $sum = array(); |
| 263 | |
| 264 | foreach ( $post_type_data as $post_type_id => $post_views ) { |
| 265 | foreach ( $post_views as $id => $views ) { |
| 266 | // generate chart data for specific post types |
| 267 | $data['data']['datasets'][$post_type_id]['data'][] = $views; |
| 268 | |
| 269 | if ( ! array_key_exists( $id, $sum ) ) |
| 270 | $sum[$id] = 0; |
| 271 | |
| 272 | $sum[$id] += $views; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // this month all days |
| 277 | for ( $i = 1; $i <= 12; $i ++ ) { |
| 278 | // generate chart data |
| 279 | $data['data']['labels'][] = $i; |
| 280 | $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) ); |
| 281 | $data['data']['datasets'][0]['data'][] = $sum[$i - 1]; |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | case 'this_month': |
| 286 | default: |
| 287 | $user_options = $this->get_dashboard_user_options( get_current_user_id(), 'post_types' ); |
| 288 | |
| 289 | if ( $period !== 'this_month' ) { |
| 290 | $date = explode( '|', $period, 2 ); |
| 291 | $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' ); |
| 292 | } else |
| 293 | $months = current_time( 'timestamp', false ); |
| 294 | |
| 295 | // get date chunks |
| 296 | $date = explode( ' ', date( "m Y t F", $months ) ); |
| 297 | |
| 298 | $data = array( |
| 299 | 'months' => $this->generate_months( $months ), |
| 300 | 'text' => array( |
| 301 | 'xAxes' => $date[3] . ' ' . $date[1], |
| 302 | 'yAxes' => __( 'Post Views', 'post-views-counter' ), |
| 303 | ), |
| 304 | 'design' => array( |
| 305 | 'fill' => true, |
| 306 | 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)', |
| 307 | 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 308 | 'borderWidth' => 1.2, |
| 309 | 'borderDash' => array(), |
| 310 | 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 311 | 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)', |
| 312 | 'pointBorderWidth' => 1.2 |
| 313 | ) |
| 314 | ); |
| 315 | |
| 316 | $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' ); |
| 317 | $data['data']['datasets'][0]['post_type'] = '_pvc_total_views'; |
| 318 | $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true ); |
| 319 | |
| 320 | // reindex post types |
| 321 | $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) ); |
| 322 | |
| 323 | $post_type_data = array(); |
| 324 | |
| 325 | foreach ( $post_types as $id => $post_type ) { |
| 326 | $post_type_obj = get_post_type_object( $post_type ); |
| 327 | |
| 328 | $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name; |
| 329 | $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name; |
| 330 | $data['data']['datasets'][$id]['hidden'] = in_array( $post_type_obj->name, $user_options, true ); |
| 331 | $data['data']['datasets'][$id]['data'] = array(); |
| 332 | |
| 333 | // get month views |
| 334 | $post_type_data[$id] = array_values( |
| 335 | pvc_get_views( |
| 336 | array( |
| 337 | 'fields' => 'date=>views', |
| 338 | 'post_type' => $post_type, |
| 339 | 'views_query' => array( |
| 340 | 'year' => $date[1], |
| 341 | 'month' => $date[0], |
| 342 | 'week' => '', |
| 343 | 'day' => '' |
| 344 | ) |
| 345 | ) |
| 346 | ) |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | $sum = array(); |
| 351 | |
| 352 | foreach ( $post_type_data as $post_type_id => $post_views ) { |
| 353 | foreach ( $post_views as $id => $views ) { |
| 354 | // generate chart data for specific post types |
| 355 | $data['data']['datasets'][$post_type_id]['data'][] = $views; |
| 356 | |
| 357 | if ( ! array_key_exists( $id, $sum ) ) |
| 358 | $sum[$id] = 0; |
| 359 | |
| 360 | $sum[$id] += $views; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // this month all days |
| 365 | for ( $i = 1; $i <= $date[2]; $i ++ ) { |
| 366 | // generate chart data |
| 367 | $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i ); |
| 368 | $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) ); |
| 369 | $data['data']['datasets'][0]['data'][] = $sum[$i - 1]; |
| 370 | } |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | echo json_encode( $data ); |
| 375 | |
| 376 | exit; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Dashboard widget chart data function. |
| 381 | * |
| 382 | * @global $_wp_admin_css_colors |
| 383 | * @return void |
| 384 | */ |
| 385 | public function dashboard_get_most_viewed() { |
| 386 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) |
| 387 | wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 388 | |
| 389 | if ( ! check_ajax_referer( 'pvc-dashboard-chart', 'nonce' ) ) |
| 390 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 391 | |
| 392 | // get period |
| 393 | $period = isset( $_POST['period'] ) ? esc_attr( $_POST['period'] ) : 'this_month'; |
| 394 | |
| 395 | // get post types |
| 396 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 397 | |
| 398 | if ( $period !== 'this_month' ) { |
| 399 | $date = explode( '|', $period, 2 ); |
| 400 | $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' ); |
| 401 | } else |
| 402 | $months = current_time( 'timestamp', false ); |
| 403 | |
| 404 | // get date chunks |
| 405 | $date = explode( ' ', date( "m Y t F", $months ) ); |
| 406 | |
| 407 | // get stats |
| 408 | $query_args = array( |
| 409 | 'post_type' => $post_types, |
| 410 | 'posts_per_page' => 10, |
| 411 | 'paged' => false, |
| 412 | 'suppress_filters' => false, |
| 413 | 'no_found_rows' => true, |
| 414 | 'views_query' => array( |
| 415 | 'year' => $date[1], |
| 416 | 'month' => $date[0], |
| 417 | ) |
| 418 | ); |
| 419 | |
| 420 | $posts = pvc_get_most_viewed_posts( $query_args ); |
| 421 | |
| 422 | $data = array( |
| 423 | 'months' => $this->generate_months( $months ), |
| 424 | 'html' => '', |
| 425 | ); |
| 426 | |
| 427 | $html = '<table id="pvc-most-viewed-table" class="pvc-table pvc-table-hover">'; |
| 428 | $html .= '<thead>'; |
| 429 | $html .= '<tr>'; |
| 430 | $html .= '<th scope="col">#</th>'; |
| 431 | $html .= '<th scope="col">' . __( 'Post', 'post-views-counter' ) . '</th>'; |
| 432 | $html .= '<th scope="col">' . __( 'Post Views', 'post-views-counter' ) . '</th>'; |
| 433 | $html .= '</tr>'; |
| 434 | $html .= '</thead>'; |
| 435 | $html .= '<tbody>'; |
| 436 | |
| 437 | if ( $posts ) { |
| 438 | foreach ( $posts as $index => $post ) { |
| 439 | setup_postdata( $post ); |
| 440 | |
| 441 | $html .= '<tr>'; |
| 442 | $html .= '<th scope="col">' . ( $index + 1 ) . '</th>'; |
| 443 | |
| 444 | if ( current_user_can( 'edit_post', $post->ID ) ) { |
| 445 | $html .= '<td><a href="' . get_edit_post_link( $post->ID ) . '">' . get_the_title( $post ) . '</a></td>'; |
| 446 | } else { |
| 447 | $html .= '<td>' . get_the_title( $post ). '</td>'; |
| 448 | } |
| 449 | |
| 450 | $html .= '<td>' . number_format_i18n( $post->post_views ) . '</td>'; |
| 451 | $html .= '</tr>'; |
| 452 | } |
| 453 | } else { |
| 454 | $html .= '<tr class="no-posts">'; |
| 455 | $html .= '<td colspan="3">' . __( 'No most viewed posts found', 'post-views-counter' ) . '</td>'; |
| 456 | $html .= '</tr>'; |
| 457 | } |
| 458 | |
| 459 | $html .= '</tbody>'; |
| 460 | $html .='</table>'; |
| 461 | |
| 462 | $data['html'] = $html; |
| 463 | |
| 464 | echo json_encode( $data ); |
| 465 | exit; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Generate months. |
| 470 | * |
| 471 | * @param string $timestamp |
| 472 | * @return string |
| 473 | */ |
| 474 | public function generate_months( $timestamp ) { |
| 475 | $dates = array( |
| 476 | explode( ' ', date( "m F Y", strtotime( "-1 months", $timestamp ) ) ), |
| 477 | explode( ' ', date( "m F Y", $timestamp ) ), |
| 478 | explode( ' ', date( "m F Y", strtotime( "+1 months", $timestamp ) ) ) |
| 479 | ); |
| 480 | |
| 481 | $current = date( "Ym", current_time( 'timestamp', false ) ); |
| 482 | |
| 483 | if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) ) |
| 484 | $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>'; |
| 485 | else |
| 486 | $next = '<a class="next" href="#" data-date="' . ( $dates[2][0] . '|' . $dates[2][2] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>'; |
| 487 | |
| 488 | $dates = array( |
| 489 | 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][0] . '|' . $dates[0][2] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>', |
| 490 | 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>', |
| 491 | 'next' => $next |
| 492 | ); |
| 493 | |
| 494 | return $dates['prev'] . $dates['current'] . $dates['next']; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Dashboard widget chart user post types. |
| 499 | * |
| 500 | * @return void |
| 501 | */ |
| 502 | public function update_dashboard_user_options() { |
| 503 | if ( ! check_ajax_referer( 'pvc-dashboard-user-options', 'nonce' ) ) |
| 504 | wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) ); |
| 505 | |
| 506 | // get allowed post types |
| 507 | $allowed_post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 508 | |
| 509 | // simulate total views as post type |
| 510 | $allowed_post_types[] = '_pvc_total_views'; |
| 511 | |
| 512 | // get allowed menu items |
| 513 | $allowed_menu_items = array( 'post-views', 'most-viewed' ); |
| 514 | |
| 515 | // valid data? |
| 516 | if ( isset( $_POST['nonce'], $_POST['options'] ) && ! empty( $_POST['options'] ) ) { |
| 517 | // get options |
| 518 | $update = map_deep( $_POST['options'], 'sanitize_text_field' ); |
| 519 | |
| 520 | // get user ID |
| 521 | $user_id = get_current_user_id(); |
| 522 | |
| 523 | // get user dashboard data |
| 524 | $user_options = get_user_meta( $user_id, 'pvc_dashboard', true ); |
| 525 | |
| 526 | // empty userdata? |
| 527 | if ( ! is_array( $user_options ) || empty( $user_options ) ) |
| 528 | $user_options = array(); |
| 529 | |
| 530 | // empty post types? |
| 531 | if ( ! array_key_exists( 'post_types', $user_options ) || ! is_array( $user_options['post_types'] ) ) |
| 532 | $user_options['post_types'] = array(); |
| 533 | |
| 534 | // hide post type? |
| 535 | if ( ! empty( $update['post_type'] ) && in_array( $update['post_type'], $allowed_post_types, true ) ) { |
| 536 | if ( isset( $update['hidden'] ) && $update['hidden'] === 'true' ) { |
| 537 | if ( ! in_array( $update['post_type'], $user_options['post_types'], true ) ) |
| 538 | $user_options['post_types'][] = $update['post_type']; |
| 539 | } else { |
| 540 | if ( ( $key = array_search( $update['post_type'], $user_options['post_types'] ) ) !== false ) |
| 541 | unset( $user_options['post_types'][$key] ); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // hide menu item? |
| 546 | $user_options['menu_items'] = array(); |
| 547 | |
| 548 | if ( ! empty( $update['menu_items'] ) && is_array( $update['menu_items'] ) ) { |
| 549 | $update['menu_items'] = map_deep( $update['menu_items'], 'sanitize_text_field' ); |
| 550 | |
| 551 | foreach ( $update['menu_items'] as $menu_item => $hidden ) { |
| 552 | if ( in_array( $menu_item, $allowed_menu_items ) && $hidden === 'true' ) |
| 553 | $user_options['menu_items'][] = $menu_item; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // update userdata |
| 558 | update_user_meta( $user_id, 'pvc_dashboard', $user_options ); |
| 559 | } |
| 560 | |
| 561 | exit; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Get user dashboard data. |
| 566 | * |
| 567 | * @param string $data_type |
| 568 | * @return array |
| 569 | */ |
| 570 | public function get_dashboard_user_options( $user_id, $data_type ) { |
| 571 | $user_options = get_user_meta( $user_id, 'pvc_dashboard', true ); |
| 572 | |
| 573 | if ( ! is_array( $user_options ) || empty( $user_options ) ) |
| 574 | $user_options = array(); |
| 575 | |
| 576 | if ( ! array_key_exists( $data_type, $user_options ) || ! is_array( $user_options[$data_type] ) ) |
| 577 | $user_options[$data_type] = array(); |
| 578 | |
| 579 | return $user_options[$data_type]; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Convert hex to rgb color. |
| 584 | * |
| 585 | * @param type $color |
| 586 | * @return boolean |
| 587 | */ |
| 588 | public function hex2rgb( $color ) { |
| 589 | if ( $color[0] == '#' ) { |
| 590 | $color = substr( $color, 1 ); |
| 591 | } |
| 592 | if ( strlen( $color ) == 6 ) { |
| 593 | list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); |
| 594 | } elseif ( strlen( $color ) == 3 ) { |
| 595 | list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); |
| 596 | } else { |
| 597 | return false; |
| 598 | } |
| 599 | $r = hexdec( $r ); |
| 600 | $g = hexdec( $g ); |
| 601 | $b = hexdec( $b ); |
| 602 | return array( 'r' => $r, 'g' => $g, 'b' => $b ); |
| 603 | } |
| 604 | |
| 605 | } |
| 606 |