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