PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.12
Post Views Counter v1.3.12
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-dashboard.php
post-views-counter / includes Last commit date
class-admin.php 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-dashboard.php
777 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 Counter', '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 wp_localize_script(
81 'pvc-admin-dashboard',
82 'pvcArgs',
83 [
84 'ajaxURL' => admin_url( 'admin-ajax.php' ),
85 'nonce' => wp_create_nonce( 'pvc-dashboard-widget' ),
86 'nonceUser' => wp_create_nonce( 'pvc-dashboard-user-options' )
87 ]
88 );
89 }
90
91 /**
92 * Setup dashboard widget items.
93 *
94 * @return void
95 */
96 private function setup_widget_items() {
97 // standard items
98 $items = [
99 [
100 'id' => 'post-views',
101 'title' => __( 'Post Views', 'post-views-counter' ),
102 'description' => __( 'Displays the chart of most viewed post types for a selected time period.', 'post-views-counter' ),
103 'content' => '<canvas id="pvc-post-views-chart" height="' . $this->calculate_canvas_size( Post_Views_Counter()->options['general']['post_types_count'] ) . '"></canvas>'
104 ],
105 [
106 'id' => 'post-most-viewed',
107 'title' => __( 'Top Posts', 'post-views-counter' ),
108 'description' => __( 'Displays the list of most viewed posts and pages on your website.', 'post-views-counter' ),
109 'content' => '<div id="pvc-post-most-viewed-content" class="pvc-table-responsive"></div>'
110 ]
111 ];
112
113 // filter items, do not allow to remove main items
114 $new_items = apply_filters( 'pvc_dashboard_widget_items', [] );
115
116 // any new items?
117 if ( is_array( $new_items ) && ! empty( $new_items ) ) {
118 foreach ( $new_items as $item ) {
119 // add new item
120 array_push( $items, $item );
121 }
122 }
123
124 // set widget items
125 $this->widget_items = $items;
126 }
127
128 /**
129 * Calculate canvas height based on number of legend items.
130 *
131 * @param array $data
132 * @param bool $expression
133 * @return int
134 */
135 public function calculate_canvas_size( $data, $expression = true ) {
136 if ( $expression && ! empty( $data ) ) {
137 // treat every 4 legend items as 1 line - 23 pixels
138 $height = 23 * ( (int) ceil( count( $data ) / 4 ) - 1 );
139 } else
140 $height = 0;
141
142 return (int) ( 170 + $height );
143 }
144
145 /**
146 * Render dashboard widget.
147 *
148 * @return void
149 */
150 public function dashboard_widget() {
151 // get user options
152 $user_options = get_user_meta( get_current_user_id(), 'pvc_dashboard', true );
153
154 // empty options?
155 if ( empty( $user_options ) || ! is_array( $user_options ) )
156 $user_options = [];
157
158 // sanitize options
159 $user_options = map_deep( $user_options, 'sanitize_text_field' );
160
161 // get menu items
162 $menu_items = ! empty( $user_options['menu_items'] ) ? $user_options['menu_items'] : [];
163
164 // generate months
165 $months_html = wp_kses_post( $this->generate_months( current_time( 'timestamp', false ) ) );
166
167 // get widget items
168 $items = $this->widget_items;
169
170 $html = '
171 <div id="pvc-dashboard-accordion" class="pvc-accordion">';
172
173 foreach ( $items as $item ) {
174 $html .= $this->generate_dashboard_widget_item( $item, $menu_items, $months_html );
175 }
176
177 $html .= '
178 </div>';
179
180 echo $html;
181 }
182
183 /**
184 * Generate dashboard widget item HTML.
185 *
186 * @param array $item
187 * @param array $menu_items
188 * @param string $esc_months_html
189 * @return string
190 */
191 public function generate_dashboard_widget_item( $item, $menu_items, $esc_months_html ) {
192 // get allowed html tags
193 $allowed_html = wp_kses_allowed_html( 'post' );
194 $allowed_html['canvas'] = [
195 'id' => [],
196 'height' => []
197 ];
198
199 return '
200 <div id="pvc-' . esc_attr( $item['id'] ) . '" class="pvc-accordion-item' . ( in_array( $item['id'], $menu_items, true ) ? ' pvc-collapsed' : '' ) . '">
201 <div class="pvc-accordion-header">
202 <div class="pvc-accordion-toggle"><span class="pvc-accordion-title">' . esc_html( $item['title'] ) . '</span><span class="pvc-tooltip" aria-label="' . esc_html( $item['description'] ) . '" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div>
203 <!--
204 <div class="pvc-accordion-actions">
205 <a href="javascript:void(0);" class="pvc-accordion-action dashicons dashicons-admin-generic"></a>
206 </div>
207 -->
208 </div>
209 <div class="pvc-accordion-content">
210 <div class="pvc-dashboard-container loading">
211 <div class="pvc-data-container">
212 ' . wp_kses( $item['content'], $allowed_html ) . '
213 <span class="spinner"></span>
214 </div>
215 <div class="pvc-months">' . $esc_months_html . '</div>
216 </div>
217 </div>
218 </div>';
219 }
220
221 /**
222 * Render dashboard widget with post views.
223 *
224 * @return void
225 */
226 public function dashboard_post_views_chart() {
227 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
228 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
229
230 if ( ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) )
231 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
232
233 // get period
234 $period = isset( $_POST['period'] ) ? sanitize_text_field( $_POST['period'] ) : 'this_month';
235
236 // get post types
237 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
238
239 // get dashboard user options
240 $user_options = $this->get_dashboard_user_options( get_current_user_id(), 'post_types' );
241
242 // get current date
243 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
244
245 // get colors
246 $color = $this->get_colors();
247
248 // set chart labels
249 switch ( $period ) {
250 case 'this_week':
251 //@TODO
252 $data = [
253 'design' => [
254 'fill' => true,
255 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
256 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
257 'borderWidth' => 1.2,
258 'borderDash' => [],
259 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
260 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
261 'pointBorderWidth' => 1.2
262 ]
263 ];
264
265 $data['data']['datasets'][0]['label'] = __( 'Post Views', 'post-views-counter' );
266 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
267
268 for ( $day = 0; $day <= 6; $day++ ) {
269 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
270 $query = new WP_Query(
271 wp_parse_args(
272 [
273 'post_type' => $post_types,
274 'posts_per_page' => -1,
275 'paged' => false,
276 'orderby' => 'post_views',
277 'suppress_filters' => false,
278 'no_found_rows' => true
279 ],
280 [
281 'views_query' => [
282 'year' => date( 'Y', $date ),
283 'month' => date( 'n', $date ),
284 'day' => date( 'd', $date )
285 ]
286 ]
287 )
288 );
289
290 $data['data']['labels'][] = date_i18n( 'j', $date );
291 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), $date );
292 $data['data']['datasets'][0]['data'][] = $query->total_views;
293 $data['data']['datasets'][$day]['data'][] = $query->total_views;
294 }
295 break;
296
297 case 'this_year':
298 $data = [
299 'design' => [
300 'fill' => true,
301 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
302 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
303 'borderWidth' => 1.2,
304 'borderDash' => [],
305 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
306 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
307 'pointBorderWidth' => 1.2
308 ]
309 ];
310
311 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
312 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
313 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true );
314 $data['data']['datasets'][0]['data'] = [];
315
316 $sum = [];
317
318 // any post types?
319 if ( ! empty( $post_types ) ) {
320 // reindex post types
321 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
322
323 $post_type_data = [];
324
325 foreach ( $post_types as $id => $post_type ) {
326 $post_type_obj = get_post_type_object( $post_type );
327
328 // unrecognized post type? (mainly from deactivated plugins)
329 if ( empty( $post_type_obj ) )
330 $label = $post_type;
331 else
332 $label = $post_type_obj->labels->name;
333
334 $data['data']['datasets'][$id]['label'] = $label;
335 $data['data']['datasets'][$id]['post_type'] = $post_type;
336 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true );
337 $data['data']['datasets'][$id]['data'] = [];
338
339 // get month views
340 $post_type_data[$id] = array_values(
341 pvc_get_views(
342 [
343 'fields' => 'date=>views',
344 'post_type' => $post_type,
345 'views_query' => [
346 'year' => date( 'Y' ),
347 'month' => '',
348 'week' => '',
349 'day' => ''
350 ]
351 ]
352 )
353 );
354 }
355
356 foreach ( $post_type_data as $post_type_id => $post_views ) {
357 foreach ( $post_views as $id => $views ) {
358 // generate chart data for specific post types
359 $data['data']['datasets'][$post_type_id]['data'][] = $views;
360
361 if ( ! array_key_exists( $id, $sum ) )
362 $sum[$id] = 0;
363
364 $sum[$id] += $views;
365 }
366 }
367 }
368
369 // this month all days
370 for ( $i = 1; $i <= 12; $i ++ ) {
371 // generate chart data
372 $data['data']['labels'][] = $i;
373 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
374 $data['data']['datasets'][0]['data'][] = ( array_key_exists( $i - 1, $sum ) ? $sum[$i - 1] : 0 );
375 }
376 break;
377
378 case 'this_month':
379 default:
380 // convert period
381 $time = $this->period2timestamp( $period );
382
383 // get date chunks
384 $date = explode( ' ', date( "m Y t F", $time ) );
385
386 $data = [
387 'months' => $this->generate_months( $time ),
388 'design' => [
389 'fill' => true,
390 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
391 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
392 'borderWidth' => 1.2,
393 'borderDash' => [],
394 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
395 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
396 'pointBorderWidth' => 1.2
397 ]
398 ];
399
400 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
401 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
402 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true );
403 $data['data']['datasets'][0]['data'] = [];
404
405 $sum = [];
406
407 // any post types?
408 if ( ! empty( $post_types ) ) {
409 // reindex post types
410 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
411
412 $post_type_data = [];
413
414 foreach ( $post_types as $id => $post_type ) {
415 $post_type_obj = get_post_type_object( $post_type );
416
417 // unrecognized post type? (mainly from deactivated plugins)
418 if ( empty( $post_type_obj ) )
419 $label = $post_type;
420 else
421 $label = $post_type_obj->labels->name;
422
423 $data['data']['datasets'][$id]['label'] = $label;
424 $data['data']['datasets'][$id]['post_type'] = $post_type;
425 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true );
426 $data['data']['datasets'][$id]['data'] = [];
427
428 // get month views
429 $post_type_data[$id] = array_values(
430 pvc_get_views(
431 [
432 'fields' => 'date=>views',
433 'post_type' => $post_type,
434 'views_query' => [
435 'year' => $date[1],
436 'month' => $date[0],
437 'week' => '',
438 'day' => '',
439 'hide_empty' => true
440 ]
441 ]
442 )
443 );
444 }
445
446 foreach ( $post_type_data as $post_type_id => $post_views ) {
447 foreach ( $post_views as $id => $views ) {
448 // generate chart data for specific post types
449 $data['data']['datasets'][$post_type_id]['data'][] = $views;
450
451 if ( ! array_key_exists( $id, $sum ) )
452 $sum[$id] = 0;
453
454 $sum[$id] += $views;
455 }
456 }
457 }
458
459 // this month all days
460 for ( $i = 1; $i <= $date[2]; $i ++ ) {
461 // generate chart data
462 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
463 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
464 $data['data']['datasets'][0]['data'][] = ( array_key_exists( $i - 1, $sum ) ? $sum[$i - 1] : 0 );
465 }
466 break;
467 }
468
469 echo json_encode( $data );
470
471 exit;
472 }
473
474 /**
475 * Render dashboard widget with most viewed posts.
476 *
477 * @return void
478 */
479 public function dashboard_post_most_viewed() {
480 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
481 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
482
483 if ( ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) )
484 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
485
486 // get main instance
487 $pvc = Post_Views_Counter();
488
489 // get post types
490 $post_types = $pvc->options['general']['post_types_count'];
491
492 // get period
493 $period = isset( $_POST['period'] ) ? sanitize_text_field( $_POST['period'] ) : 'this_month';
494
495 // convert period
496 $time = $this->period2timestamp( $period );
497
498 // get date chunks
499 $date = explode( ' ', date( "m Y t F", $time ) );
500
501 // get stats
502 $query_args = [
503 'post_type' => $post_types,
504 'posts_per_page' => 10,
505 'paged' => false,
506 'suppress_filters' => false,
507 'no_found_rows' => true,
508 'views_query' => [
509 'year' => $date[1],
510 'month' => $date[0],
511 'hide_empty' => true
512 ]
513 ];
514
515 $posts = pvc_get_most_viewed_posts( $query_args );
516
517 $data = [
518 'months' => $this->generate_months( $time ),
519 'html' => ''
520 ];
521
522 $html = '
523 <table id="pvc-post-most-viewed-table" class="pvc-table pvc-table-hover">
524 <thead>
525 <tr>
526 <th scope="col">#</th>
527 <th scope="col">' . esc_html__( 'Post', 'post-views-counter' ) . '</th>
528 <th scope="col">' . esc_html__( 'Post Views', 'post-views-counter' ) . '</th>
529 </tr>
530 </thead>
531 <tbody>';
532
533 if ( $posts ) {
534 // active post types
535 $active_post_types = [];
536
537 foreach ( $posts as $index => $post ) {
538 setup_postdata( $post );
539
540 $html .= '
541 <tr>
542 <th scope="col">' . ( $index + 1 ) . '</th>';
543
544 // check post type existence
545 if ( array_key_exists( $post->post_type, $active_post_types ) )
546 $post_type_exists = $active_post_types[$post->post_type];
547 else
548 $post_type_exists = $active_post_types[$post->post_type] = post_type_exists( $post->post_type );
549
550 // edit post link
551 if ( $post_type_exists && current_user_can( 'edit_post', $post->ID ) ) {
552 $html .= '
553 <td><a href="' . esc_url( get_edit_post_link( $post->ID ) ) . '">' . esc_html( get_the_title( $post ) ) . '</a></td>';
554 } else {
555 $html .= '
556 <td>' . esc_html( get_the_title( $post ) ). '</td>';
557 }
558
559 $html .= '
560 <td>' . number_format_i18n( $post->post_views ) . '</td>
561 </tr>';
562 }
563 } else {
564 $html .= '
565 <tr class="no-posts">
566 <td colspan="3">' . esc_html__( 'No most viewed posts found', 'post-views-counter' ) . '</td>
567 </tr>';
568 }
569
570 $html .= '
571 </tbody>
572 </table>';
573
574 $data['html'] = $html;
575
576 echo json_encode( $data );
577
578 exit;
579 }
580
581 /**
582 * Generate dashboard widget months HTML.
583 *
584 * @param int $timestamp
585 * @return string
586 */
587 public function generate_months( $timestamp ) {
588 $dates = [
589 explode( ' ', date( "m F Y", strtotime( "-1 months", $timestamp ) ) ),
590 explode( ' ', date( "m F Y", $timestamp ) ),
591 explode( ' ', date( "m F Y", strtotime( "+1 months", $timestamp ) ) )
592 ];
593
594 $current = date( "Ym", current_time( 'timestamp', false ) );
595
596 if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) )
597 $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>';
598 else
599 $next = '<a class="next" href="#" data-date="' . ( $dates[2][0] . '|' . $dates[2][2] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>';
600
601 $dates = [
602 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][0] . '|' . $dates[0][2] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>',
603 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>',
604 'next' => $next
605 ];
606
607 return $dates['prev'] . $dates['current'] . $dates['next'];
608 }
609
610 /**
611 * Update dashboard widget user options.
612 *
613 * @return void
614 */
615 public function update_dashboard_user_options() {
616 if ( ! check_ajax_referer( 'pvc-dashboard-user-options', 'nonce' ) )
617 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
618
619 // valid data?
620 if ( isset( $_POST['nonce'], $_POST['options'] ) && ! empty( $_POST['options'] ) ) {
621 // get sanitized options
622 $update = map_deep( $_POST['options'], 'sanitize_text_field' );
623
624 // get user ID
625 $user_id = get_current_user_id();
626
627 // get user dashboard data
628 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
629
630 // empty userdata?
631 if ( ! is_array( $user_options ) || empty( $user_options ) )
632 $user_options = [];
633
634 // empty post types?
635 if ( ! array_key_exists( 'post_types', $user_options ) || ! is_array( $user_options['post_types'] ) )
636 $user_options['post_types'] = [];
637
638 // hide post type?
639 if ( ! empty( $update['post_type'] ) ) {
640 // get allowed post types
641 $allowed_post_types = Post_Views_Counter()->options['general']['post_types_count'];
642
643 // simulate total post views as post type
644 $allowed_post_types[] = '_pvc_total_views';
645
646 if ( in_array( $update['post_type'], $allowed_post_types, true ) ) {
647 if ( isset( $update['hidden'] ) && $update['hidden'] === 'true' ) {
648 if ( ! in_array( $update['post_type'], $user_options['post_types'], true ) )
649 $user_options['post_types'][] = $update['post_type'];
650 } else {
651 if ( ( $key = array_search( $update['post_type'], $user_options['post_types'] ) ) !== false )
652 unset( $user_options['post_types'][$key] );
653 }
654 }
655 }
656
657 // empty menu items?
658 if ( ! array_key_exists( 'menu_items', $user_options ) || ! is_array( $user_options['menu_items'] ) )
659 $user_options['menu_items'] = [];
660
661 if ( ! empty( $update['menu_items'] ) && is_array( $update['menu_items'] ) ) {
662 $user_options['menu_items'] = [];
663
664 // get allowed menu items
665 $allowed_menu_items = array_column( $this->widget_items, 'id' );
666
667 foreach ( $update['menu_items'] as $menu_item => $hidden ) {
668 if ( in_array( $menu_item, $allowed_menu_items, true ) && $hidden === 'true' )
669 $user_options['menu_items'][] = $menu_item;
670 }
671 }
672
673 // filter user options
674 $user_options = apply_filters( 'pvc_update_dashboard_user_options', $user_options, $update, $user_id );
675
676 // update userdata
677 update_user_meta( $user_id, 'pvc_dashboard', $user_options );
678 }
679
680 exit;
681 }
682
683 /**
684 * Get user dashboard data.
685 *
686 * @param int $user_id
687 * @param string $data_type
688 * @return array
689 */
690 public function get_dashboard_user_options( $user_id, $data_type ) {
691 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
692
693 if ( ! is_array( $user_options ) || empty( $user_options ) )
694 $user_options = [];
695
696 if ( ! array_key_exists( $data_type, $user_options ) || ! is_array( $user_options[$data_type] ) )
697 $user_options[$data_type] = [];
698
699 return $user_options[$data_type];
700 }
701
702 /**
703 * Convert period to timestamp.
704 *
705 * @param string $period
706 * @return int
707 */
708 public function period2timestamp( $period ) {
709 // whitelisted period?
710 if ( in_array( $period, [ 'this_week', 'this_year', 'this_month' ], true ) ) {
711 $timestamp = current_time( 'timestamp', false );
712 } else {
713 if ( preg_match( '/^([0-9]{2}\|[0-9]{4})$/', $period ) === 1 ) {
714 // month|year
715 $date = explode( '|', $period, 2 );
716
717 // get timestamp
718 $timestamp = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
719 } else
720 $timestamp = current_time( 'timestamp', false );
721 }
722
723 return $timestamp;
724 }
725
726 /**
727 * Get default color for charts.
728 *
729 * @global array $_wp_admin_css_colors
730 *
731 * @return array
732 */
733 public function get_colors() {
734 // get global color scheme
735 global $_wp_admin_css_colors;
736
737 // set default colors
738 $colors = [
739 'r' => 105,
740 'g' => 168,
741 'b' => 187
742 ];
743
744 if ( ! empty( $_wp_admin_css_colors ) ) {
745 // get current admin color scheme name
746 $current_color_scheme = get_user_option( 'admin_color' );
747
748 if ( empty( $current_color_scheme ) )
749 $current_color_scheme = 'fresh';
750
751 if ( isset( $_wp_admin_css_colors[$current_color_scheme] ) )
752 $colors = $this->hex2rgb( $_wp_admin_css_colors[$current_color_scheme]->colors[2] );
753 }
754
755 return $colors;
756 }
757
758 /**
759 * Convert HEX to RGB color.
760 *
761 * @param string $color
762 * @return bool|array
763 */
764 public function hex2rgb( $color ) {
765 if ( $color[0] === '#' )
766 $color = substr( $color, 1 );
767
768 if ( strlen( $color ) == 6 )
769 list( $r, $g, $b ) = [ $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ];
770 elseif ( strlen( $color ) == 3 )
771 list( $r, $g, $b ) = [ $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ];
772 else
773 return false;
774
775 return [ 'r' => hexdec( $r ), 'g' => hexdec( $g ), 'b' => hexdec( $b ) ];
776 }
777 }