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