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 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-admin-interface.php
admin-site-enhancements / classes Last commit date
class-activation.php 3 years ago class-admin-interface.php 3 years ago class-common-methods.php 3 years ago class-content-management.php 3 years ago class-custom-code.php 3 years ago class-deactivation.php 3 years ago class-disable-components.php 3 years ago class-login-logout.php 3 years ago class-optimizations.php 3 years ago class-security.php 3 years ago class-settings-fields-render.php 3 years ago class-settings-sanitization.php 3 years ago class-settings-sections-fields.php 3 years ago class-utilities.php 3 years ago
class-admin-interface.php
569 lines
1 <?php
2
3 namespace ASENHA\Classes;
4 use WP_Admin_Bar;
5
6 /**
7 * Class related to Admin Interface features
8 *
9 * @since 1.2.0
10 */
11 class Admin_Interface {
12
13 /**
14 * Wrapper for admin notices being output on admin screens
15 *
16 * @since 1.2.0
17 */
18 public function admin_notices_wrapper() {
19
20 echo '<div class="asenha-admin-notices-drawer" style="display:none;"><h2>Admin Notices</h2></div>';
21
22 }
23
24 /**
25 * Admin bar menu item for the hidden admin notices
26 *
27 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/
28 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
29 * @since 1.2.0
30 */
31 public function admin_notices_menu( WP_Admin_Bar $wp_admin_bar ) {
32
33 $wp_admin_bar->add_menu( array(
34 'id' => 'asenha-hide-admin-notices',
35 'parent' => 'top-secondary',
36 'grou' => null,
37 'title' => 'Notices<span class="asenha-admin-notices-counter" style="opacity:0;">0</span>',
38 // 'href' => '',
39 'meta' => array(
40 'class' => 'asenha-admin-notices-menu',
41 'title' => 'Click to view hidden admin notices',
42 ),
43 ) );
44
45 }
46
47 /**
48 * Inline CSS for the admin bar notices menu
49 *
50 * @since 1.2.0
51 */
52 public function admin_notices_menu_inline_css() {
53
54 wp_add_inline_style( 'admin-bar', '
55
56 #wpadminbar .asenha-admin-notices-counter {
57 box-sizing: border-box;
58 margin: 1px 0 -1px 6px ;
59 padding: 2px 6px 3px 5px;
60 min-width: 18px;
61 height: 18px;
62 border-radius: 50%;
63 background-color: #ca4a1f;
64 color: #fff;
65 font-size: 11px;
66 line-height: 1.6;
67 text-align: center;
68 }
69
70 ' );
71
72 }
73
74 /**
75 * Hide admin bar on the frontend for the user roles selected
76 *
77 * @since 1.3.0
78 */
79 public function hide_admin_bar_for_roles() {
80
81 $options = get_option( ASENHA_SLUG_U );
82 $hide_admin_bar = $options['hide_admin_bar'];
83 $for_roles = $options['hide_admin_bar_for'];
84
85 $current_user = wp_get_current_user();
86 $current_user_roles = (array) $current_user->roles; // single dimensional array of role slugs
87
88 // User has no role, i.e. logged-out
89
90 if ( count( $current_user_roles ) == 0 ) {
91 return false; // hide admin bar
92 }
93
94 // User has role(s). Do further checks.
95
96 if ( isset( $for_roles ) && ( count( $for_roles ) > 0 ) ) {
97
98 // Assemble single-dimensional array of roles for which admin bar would be hidden
99
100 $roles_admin_bar_hidden = array();
101
102 foreach( $for_roles as $role_slug => $admin_bar_hidden ) {
103 if ( $admin_bar_hidden ) {
104 $roles_admin_bar_hidden[] = $role_slug;
105 }
106 }
107
108 // Check if any of the current user roles is one for which admin bar should be hidden
109
110 foreach ( $current_user_roles as $role ) {
111 if ( in_array( $role, $roles_admin_bar_hidden ) ) {
112 return false; // hide admin bar
113 }
114 }
115
116 }
117
118 return true; // show admin bar
119
120 }
121
122 /**
123 * Get dashboard widgets
124 *
125 * @since 4.2.0
126 */
127 public function get_dashboard_widgets() {
128
129 global $wp_meta_boxes;
130
131 $dashboard_widgets = array();
132
133 if ( ! isset( $wp_meta_boxes['dashboard'] ) ) {
134 $extra_options = get_option( 'admin_site_enhancements_extra', array() );
135 if ( ! array_key_exists( 'dashboard_widgets', $extra_options ) ) {
136 require_once ABSPATH . '/wp-admin/includes/dashboard.php';
137 set_current_screen( 'dashboard' );
138 wp_dashboard_setup();
139 }
140 }
141
142 if ( isset( $wp_meta_boxes['dashboard'] ) ) {
143 foreach( $wp_meta_boxes['dashboard'] as $context => $priorities ) {
144 foreach ( $priorities as $priority => $widgets ) {
145 foreach( $widgets as $widget_id => $data ) {
146 $dashboard_widgets[$widget_id] = array(
147 'id' => $widget_id,
148 'title' => wp_strip_all_tags( preg_replace( '/ <span.*span>/im', '', $data['title'] ) ),
149 'context' => $context, // 'normal' or 'side'
150 'priority' => $priority, // 'core'
151 );
152 }
153 }
154 }
155 }
156
157 $dashboard_widgets = wp_list_sort( $dashboard_widgets, 'title', 'ASC', true );
158
159 return $dashboard_widgets;
160
161 }
162
163 /**
164 * Disable dashboard widgets
165 *
166 * @since 4.2.0
167 */
168 public function disable_dashboard_widgets() {
169
170 global $wp_meta_boxes;
171
172 // Get list of disabled widgets
173 $options = get_option( ASENHA_SLUG_U, array() );
174 $disabled_dashboard_widgets = $options['disabled_dashboard_widgets'];
175
176 // Store default widgets in extra options. This will be referenced from settings field.
177 $dashboard_widgets = $this->get_dashboard_widgets();
178 $extra_options = get_option( 'admin_site_enhancements_extra', array() );
179 $extra_options['dashboard_widgets'] = $dashboard_widgets;
180 update_option( 'admin_site_enhancements_extra', $extra_options );
181
182 // Disable widgets
183 foreach( $disabled_dashboard_widgets as $disabled_widget_id_context_priority => $is_disabled ) {
184 // e.g. dashboard_activity__normal__core => true/false
185 if ( $is_disabled ) {
186 $disabled_widget = explode('__', $disabled_widget_id_context_priority);
187 $widget_id = $disabled_widget[0];
188 $widget_context = $disabled_widget[1];
189 $widget_priority = $disabled_widget[2];
190 // remove_meta_box( $widget_id, get_current_screen()->base, $widget_context );
191 unset( $wp_meta_boxes['dashboard'][$widget_context][$widget_priority][$widget_id] );
192 }
193 }
194
195 }
196
197 /**
198 * Modify admin bar menu for Admin Interface >> Hide or Modify Elements feature
199 *
200 * @param $wp_admin_bar object The admin bar.
201 * @since 1.9.0
202 */
203 public function modify_admin_bar_menu( $wp_admin_bar ) {
204
205 $options = get_option( ASENHA_SLUG_U, array() );
206
207 // Hide WP Logo Menu
208 if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) {
209 remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); // priority needs to match default value. Use QM to reference.
210 }
211
212 // Hide Customize Menu
213 if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) {
214 remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); // priority needs to match default value. Use QM to reference.
215 }
216
217 // Hide Updates Counter/Link
218 if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) {
219 remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); // priority needs to match default value. Use QM to reference.
220 }
221
222 // Hide Comments Counter/Link
223 if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) {
224 remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); // priority needs to match default value. Use QM to reference.
225 }
226
227 // Hide New Content Menu
228 if ( array_key_exists( 'hide_ab_new_content_menu', $options ) && $options['hide_ab_new_content_menu'] ) {
229 remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); // priority needs to match default value. Use QM to reference.
230 }
231
232 // Hide 'Howdy' text
233 if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) {
234
235 // Remove the whole my account sectino and later rebuild it
236 remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
237
238 $current_user = wp_get_current_user();
239 $user_id = get_current_user_id();
240 $profile_url = get_edit_profile_url( $user_id );
241
242 $avatar = get_avatar( $user_id, 26 ); // size 26x26 pixels
243 $display_name = $current_user->display_name;
244 $class = 'with-avatar';
245
246 $wp_admin_bar->add_menu( array(
247 'id' => 'my-account',
248 'parent' => 'top-secondary',
249 'title' => $display_name . $avatar,
250 'href' => $profile_url,
251 'meta' => array(
252 'class' => $class,
253 ),
254 ) );
255
256 }
257
258 }
259
260 /**
261 * Save custom menu order into an option
262 *
263 * @since 2.0.0
264 */
265 public function save_custom_menu_order() {
266
267 if ( isset( $_REQUEST ) && ( 'save_custom_menu_order' == $_REQUEST['action'] ) ) {
268
269 $options = get_option( ASENHA_SLUG_U );
270
271 // Empty option value first
272 // $options['custom_menu_order'] = '';
273 // update_option( ASENHA_SLUG_U, $options );
274
275 // Save new value
276 // $options = get_option( ASENHA_SLUG_U );
277 $options['custom_menu_order'] = sanitize_text_field( $_REQUEST['menu_order'] );
278 $success = update_option( ASENHA_SLUG_U, $options ); // true or false
279
280 if ( $success ) {
281
282 $data = array(
283 'status' => 'success',
284 'message' => 'Custom menu order was successfully saved.',
285 );
286
287 } else {
288
289 $data = array(
290 'status' => 'error',
291 'message' => 'Custom menu order was not saved.',
292 );
293
294 }
295
296 echo json_encode( $data );
297
298 }
299
300 }
301
302 /**
303 * Render custom menu order
304 *
305 * @param $menu_order array an ordered array of menu items
306 * @link https://developer.wordpress.org/reference/hooks/menu_order/
307 * @since 2.0.0
308 */
309 public function render_custom_menu_order( $menu_order ) {
310
311 global $menu;
312
313 $options = get_option( ASENHA_SLUG_U );
314
315 // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values.
316
317 $current_menu_order = array();
318
319 foreach ( $menu as $menu_key => $menu_info ) {
320
321 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
322 $menu_item_id = $menu_info[2];
323 } else {
324 $menu_item_id = $menu_info[5];
325 }
326
327 $current_menu_order[] = array( $menu_item_id, $menu_info[2] );
328
329 }
330
331 // Get custom menu order
332 $custom_menu_order = $options['custom_menu_order']; // comma separated
333 $custom_menu_order = explode( ",", $custom_menu_order ); // array of menu ID, e.g. menu-dashboard
334
335 // Return menu order for rendering
336
337 $rendered_menu_order = array();
338
339 // Render menu based on items saved in custom menu order
340
341 foreach ( $custom_menu_order as $custom_menu_item_id ) {
342
343 foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) {
344
345 if ( $custom_menu_item_id == $current_menu_item[0] ) {
346
347 $rendered_menu_order[] = $current_menu_item[1];
348
349 }
350
351 }
352
353 }
354
355 // do_action( 'inspect', [ 'rendered_menu_order_1', $rendered_menu_order ] );
356
357 // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item
358
359 foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) {
360
361 if ( ! in_array( $current_menu_item[0], $custom_menu_order ) ) {
362
363 $rendered_menu_order[] = $current_menu_item[1];
364
365 }
366
367 }
368
369 // do_action( 'inspect', [ 'rendered_menu_order_2', $rendered_menu_order ] );
370
371 return $rendered_menu_order;
372
373 }
374
375 /**
376 * Save menu items that was chosen to be hidden
377 *
378 * @since 2.0.0
379 */
380 public function save_hidden_menu_items() {
381
382 if ( isset( $_REQUEST ) && ( 'save_hidden_menu_items' == $_REQUEST['action'] ) ) {
383
384 $options = get_option( ASENHA_SLUG_U );
385
386 // Empty option value first
387 // $options['custom_menu_order'] = '';
388 // update_option( ASENHA_SLUG_U, $options );
389
390 // Save new value
391 // $options = get_option( ASENHA_SLUG_U );
392 $options['custom_menu_hidden'] = sanitize_text_field( $_REQUEST['hidden_menu_items'] );
393 $success = update_option( ASENHA_SLUG_U, $options ); // true or false
394
395 if ( $success ) {
396
397 $data = array(
398 'status' => 'success',
399 'message' => 'Hidden menu items was successfully saved.',
400 );
401
402 } else {
403
404 $data = array(
405 'status' => 'error',
406 'message' => 'Hidden menu items was not saved.',
407 );
408
409 }
410
411 echo json_encode( $data );
412
413 }
414
415 }
416
417 /**
418 * Apply custom menu item titles
419 *
420 * @since 2.9.0
421 */
422 public function apply_custom_menu_item_titles() {
423
424 global $menu;
425
426 $options = get_option( ASENHA_SLUG_U );
427
428 // Get custom menu item titles
429 $custom_menu_titles = $options['custom_menu_titles'];
430 $custom_menu_titles = explode( ',', $custom_menu_titles );
431
432 foreach ( $menu as $menu_key => $menu_info ) {
433
434 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
435 $menu_item_id = $menu_info[2];
436 } else {
437 $menu_item_id = $menu_info[5];
438 }
439
440 // Get defaul/custom menu item title
441 foreach ( $custom_menu_titles as $custom_menu_title ) {
442
443 // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets
444
445 $custom_menu_title = explode( '__', $custom_menu_title );
446
447 if ( $custom_menu_title[0] == $menu_item_id ) {
448 $menu_item_title = $custom_menu_title[1]; // e.g. Code Snippets
449 break; // stop foreach loop so $menu_item_title is not overwritten in the next iteration
450 } else {
451 $menu_item_title = $menu_info[0];
452 }
453
454 }
455
456 $menu[$menu_key][0] = $menu_item_title;
457
458 }
459 }
460
461 /**
462 * Hide menu items by adding 'hidden' class (part of WP Core's common.css)
463 *
464 * @since 2.0.0
465 */
466 public function hide_menu_items() {
467
468 global $menu;
469
470 $options = get_option( ASENHA_SLUG_U );
471
472 // Get hidden menu items
473 $hidden_menu = $options['custom_menu_hidden'];
474 $hidden_menu = explode( ',', $hidden_menu );
475
476 foreach ( $menu as $menu_key => $menu_info ) {
477
478 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
479 $menu_item_id = $menu_info[2];
480 } else {
481 $menu_item_id = $menu_info[5];
482 }
483
484 if ( in_array( $menu_item_id, $hidden_menu ) ) {
485
486 $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu';
487
488 }
489
490 }
491
492 }
493
494 /**
495 * Add toggle to show hidden menu items
496 *
497 * @since 2.0.0
498 */
499 public function add_hidden_menu_toggle() {
500
501 $options = get_option( ASENHA_SLUG_U );
502
503 // Get hidden menu items
504 $hidden_menu = $options['custom_menu_hidden'];
505
506 if ( ! empty( $hidden_menu ) ) {
507
508 add_menu_page(
509 'Show All',
510 'Show All',
511 'manage_options',
512 'asenha_show_hidden_menu',
513 function () { return false; },
514 "dashicons-arrow-down-alt2",
515 300 // position
516 );
517
518 add_menu_page(
519 'Show Less',
520 'Show Less',
521 'manage_options',
522 'asenha_hide_hidden_menu',
523 function () { return false; },
524 "dashicons-arrow-up-alt2",
525 301 // position
526 );
527 }
528
529 }
530
531 /**
532 * Script to toggle hidden menu itesm
533 *
534 * @since 2.0.0
535 */
536 public function enqueue_toggle_hidden_menu_script() {
537
538 $options = get_option( ASENHA_SLUG_U );
539
540 // Get hidden menu items
541
542 if ( array_key_exists( 'custom_menu_hidden', $options ) ) {
543 $hidden_menu = $options['custom_menu_hidden'];
544 } else {
545 $hidden_menu = '';
546 }
547
548 if ( ! empty( $hidden_menu ) ) {
549
550 // Script to set behaviour and actions of the sortable menu
551 wp_enqueue_script( 'asenha-toggle-hidden-menu', ASENHA_URL . 'assets/js/toggle-hidden-menu.js', array(), ASENHA_VERSION, false );
552
553 }
554
555 }
556
557 /**
558 * Hide the Help tab and drawer
559 *
560 * @since 4.5.0
561 */
562 public function hide_help_drawer() {
563 if ( is_admin() ) {
564 $screen = get_current_screen();
565 $screen->remove_help_tabs();
566 }
567 }
568
569 }