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
568 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 | $widget_title = wp_strip_all_tags( preg_replace( '/ <span.*span>/im', '', $data['title'] ) ); |
| 147 | $dashboard_widgets[$widget_id] = array( |
| 148 | 'id' => $widget_id, |
| 149 | 'title' => $widget_title, |
| 150 | 'context' => $context, // 'normal' or 'side' |
| 151 | 'priority' => $priority, // 'core' |
| 152 | ); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $dashboard_widgets = wp_list_sort( $dashboard_widgets, 'title', 'ASC', true ); |
| 159 | |
| 160 | do_action( 'inspect', [ 'dashboard_widgets', $dashboard_widgets ] ); |
| 161 | |
| 162 | return $dashboard_widgets; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Disable dashboard widgets |
| 168 | * |
| 169 | * @since 4.2.0 |
| 170 | */ |
| 171 | public function disable_dashboard_widgets() { |
| 172 | |
| 173 | global $wp_meta_boxes; |
| 174 | |
| 175 | // Get list of disabled widgets |
| 176 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 177 | $disabled_dashboard_widgets = $options['disabled_dashboard_widgets']; |
| 178 | |
| 179 | // Store default widgets in extra options. This will be referenced from settings field. |
| 180 | $dashboard_widgets = $this->get_dashboard_widgets(); |
| 181 | $extra_options = get_option( 'admin_site_enhancements_extra', array() ); |
| 182 | $extra_options['dashboard_widgets'] = $dashboard_widgets; |
| 183 | update_option( 'admin_site_enhancements_extra', $extra_options ); |
| 184 | |
| 185 | // Disable widgets |
| 186 | foreach( $disabled_dashboard_widgets as $disabled_widget_id_context_priority => $is_disabled ) { |
| 187 | // e.g. dashboard_activity__normal__core => true/false |
| 188 | if ( $is_disabled ) { |
| 189 | $disabled_widget = explode('__', $disabled_widget_id_context_priority); |
| 190 | $widget_id = $disabled_widget[0]; |
| 191 | $widget_context = $disabled_widget[1]; |
| 192 | $widget_priority = $disabled_widget[2]; |
| 193 | // remove_meta_box( $widget_id, get_current_screen()->base, $widget_context ); |
| 194 | unset( $wp_meta_boxes['dashboard'][$widget_context][$widget_priority][$widget_id] ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Modify admin bar menu for Admin Interface >> Hide or Modify Elements feature |
| 202 | * |
| 203 | * @param $wp_admin_bar object The admin bar. |
| 204 | * @since 1.9.0 |
| 205 | */ |
| 206 | public function modify_admin_bar_menu( $wp_admin_bar ) { |
| 207 | |
| 208 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 209 | |
| 210 | // Hide WP Logo Menu |
| 211 | if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) { |
| 212 | remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); // priority needs to match default value. Use QM to reference. |
| 213 | } |
| 214 | |
| 215 | // Hide Customize Menu |
| 216 | if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) { |
| 217 | remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); // priority needs to match default value. Use QM to reference. |
| 218 | } |
| 219 | |
| 220 | // Hide Updates Counter/Link |
| 221 | if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) { |
| 222 | remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); // priority needs to match default value. Use QM to reference. |
| 223 | } |
| 224 | |
| 225 | // Hide Comments Counter/Link |
| 226 | if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) { |
| 227 | remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); // priority needs to match default value. Use QM to reference. |
| 228 | } |
| 229 | |
| 230 | // Hide New Content Menu |
| 231 | if ( array_key_exists( 'hide_ab_new_content_menu', $options ) && $options['hide_ab_new_content_menu'] ) { |
| 232 | remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); // priority needs to match default value. Use QM to reference. |
| 233 | } |
| 234 | |
| 235 | // Hide 'Howdy' text |
| 236 | if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) { |
| 237 | |
| 238 | // Remove the whole my account sectino and later rebuild it |
| 239 | remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); |
| 240 | |
| 241 | $current_user = wp_get_current_user(); |
| 242 | $user_id = get_current_user_id(); |
| 243 | $profile_url = get_edit_profile_url( $user_id ); |
| 244 | |
| 245 | $avatar = get_avatar( $user_id, 26 ); // size 26x26 pixels |
| 246 | $display_name = $current_user->display_name; |
| 247 | $class = 'with-avatar'; |
| 248 | |
| 249 | $wp_admin_bar->add_menu( array( |
| 250 | 'id' => 'my-account', |
| 251 | 'parent' => 'top-secondary', |
| 252 | 'title' => $display_name . $avatar, |
| 253 | 'href' => $profile_url, |
| 254 | 'meta' => array( |
| 255 | 'class' => $class, |
| 256 | ), |
| 257 | ) ); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Save custom menu order into an option |
| 265 | * |
| 266 | * @since 2.0.0 |
| 267 | */ |
| 268 | public function save_custom_menu_order() { |
| 269 | |
| 270 | if ( isset( $_REQUEST ) && ( 'save_custom_menu_order' == $_REQUEST['action'] ) ) { |
| 271 | |
| 272 | $options = get_option( ASENHA_SLUG_U ); |
| 273 | |
| 274 | // Empty option value first |
| 275 | // $options['custom_menu_order'] = ''; |
| 276 | // update_option( ASENHA_SLUG_U, $options ); |
| 277 | |
| 278 | // Save new value |
| 279 | // $options = get_option( ASENHA_SLUG_U ); |
| 280 | $options['custom_menu_order'] = sanitize_text_field( $_REQUEST['menu_order'] ); |
| 281 | $success = update_option( ASENHA_SLUG_U, $options ); // true or false |
| 282 | |
| 283 | if ( $success ) { |
| 284 | |
| 285 | $data = array( |
| 286 | 'status' => 'success', |
| 287 | 'message' => 'Custom menu order was successfully saved.', |
| 288 | ); |
| 289 | |
| 290 | } else { |
| 291 | |
| 292 | $data = array( |
| 293 | 'status' => 'error', |
| 294 | 'message' => 'Custom menu order was not saved.', |
| 295 | ); |
| 296 | |
| 297 | } |
| 298 | |
| 299 | echo json_encode( $data ); |
| 300 | |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Render custom menu order |
| 307 | * |
| 308 | * @param $menu_order array an ordered array of menu items |
| 309 | * @link https://developer.wordpress.org/reference/hooks/menu_order/ |
| 310 | * @since 2.0.0 |
| 311 | */ |
| 312 | public function render_custom_menu_order( $menu_order ) { |
| 313 | |
| 314 | global $menu; |
| 315 | |
| 316 | $options = get_option( ASENHA_SLUG_U ); |
| 317 | |
| 318 | // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values. |
| 319 | |
| 320 | $current_menu_order = array(); |
| 321 | |
| 322 | foreach ( $menu as $menu_key => $menu_info ) { |
| 323 | |
| 324 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 325 | $menu_item_id = $menu_info[2]; |
| 326 | } else { |
| 327 | $menu_item_id = $menu_info[5]; |
| 328 | } |
| 329 | |
| 330 | $current_menu_order[] = array( $menu_item_id, $menu_info[2] ); |
| 331 | |
| 332 | } |
| 333 | |
| 334 | // Get custom menu order |
| 335 | $custom_menu_order = $options['custom_menu_order']; // comma separated |
| 336 | $custom_menu_order = explode( ",", $custom_menu_order ); // array of menu ID, e.g. menu-dashboard |
| 337 | |
| 338 | // Return menu order for rendering |
| 339 | |
| 340 | $rendered_menu_order = array(); |
| 341 | |
| 342 | // Render menu based on items saved in custom menu order |
| 343 | |
| 344 | foreach ( $custom_menu_order as $custom_menu_item_id ) { |
| 345 | |
| 346 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 347 | |
| 348 | if ( $custom_menu_item_id == $current_menu_item[0] ) { |
| 349 | |
| 350 | $rendered_menu_order[] = $current_menu_item[1]; |
| 351 | |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
| 356 | } |
| 357 | |
| 358 | // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item |
| 359 | |
| 360 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 361 | |
| 362 | if ( ! in_array( $current_menu_item[0], $custom_menu_order ) ) { |
| 363 | |
| 364 | $rendered_menu_order[] = $current_menu_item[1]; |
| 365 | |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | return $rendered_menu_order; |
| 371 | |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Save menu items that was chosen to be hidden |
| 376 | * |
| 377 | * @since 2.0.0 |
| 378 | */ |
| 379 | public function save_hidden_menu_items() { |
| 380 | |
| 381 | if ( isset( $_REQUEST ) && ( 'save_hidden_menu_items' == $_REQUEST['action'] ) ) { |
| 382 | |
| 383 | $options = get_option( ASENHA_SLUG_U ); |
| 384 | |
| 385 | // Empty option value first |
| 386 | // $options['custom_menu_order'] = ''; |
| 387 | // update_option( ASENHA_SLUG_U, $options ); |
| 388 | |
| 389 | // Save new value |
| 390 | // $options = get_option( ASENHA_SLUG_U ); |
| 391 | $options['custom_menu_hidden'] = sanitize_text_field( $_REQUEST['hidden_menu_items'] ); |
| 392 | $success = update_option( ASENHA_SLUG_U, $options ); // true or false |
| 393 | |
| 394 | if ( $success ) { |
| 395 | |
| 396 | $data = array( |
| 397 | 'status' => 'success', |
| 398 | 'message' => 'Hidden menu items was successfully saved.', |
| 399 | ); |
| 400 | |
| 401 | } else { |
| 402 | |
| 403 | $data = array( |
| 404 | 'status' => 'error', |
| 405 | 'message' => 'Hidden menu items was not saved.', |
| 406 | ); |
| 407 | |
| 408 | } |
| 409 | |
| 410 | echo json_encode( $data ); |
| 411 | |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Apply custom menu item titles |
| 418 | * |
| 419 | * @since 2.9.0 |
| 420 | */ |
| 421 | public function apply_custom_menu_item_titles() { |
| 422 | |
| 423 | global $menu; |
| 424 | |
| 425 | $options = get_option( ASENHA_SLUG_U ); |
| 426 | |
| 427 | // Get custom menu item titles |
| 428 | $custom_menu_titles = $options['custom_menu_titles']; |
| 429 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 430 | |
| 431 | foreach ( $menu as $menu_key => $menu_info ) { |
| 432 | |
| 433 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 434 | $menu_item_id = $menu_info[2]; |
| 435 | } else { |
| 436 | $menu_item_id = $menu_info[5]; |
| 437 | } |
| 438 | |
| 439 | // Get defaul/custom menu item title |
| 440 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 441 | |
| 442 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 443 | |
| 444 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 445 | |
| 446 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 447 | $menu_item_title = $custom_menu_title[1]; // e.g. Code Snippets |
| 448 | break; // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 449 | } else { |
| 450 | $menu_item_title = $menu_info[0]; |
| 451 | } |
| 452 | |
| 453 | } |
| 454 | |
| 455 | $menu[$menu_key][0] = $menu_item_title; |
| 456 | |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Hide menu items by adding 'hidden' class (part of WP Core's common.css) |
| 462 | * |
| 463 | * @since 2.0.0 |
| 464 | */ |
| 465 | public function hide_menu_items() { |
| 466 | |
| 467 | global $menu; |
| 468 | |
| 469 | $options = get_option( ASENHA_SLUG_U ); |
| 470 | |
| 471 | // Get hidden menu items |
| 472 | $hidden_menu = $options['custom_menu_hidden']; |
| 473 | $hidden_menu = explode( ',', $hidden_menu ); |
| 474 | |
| 475 | foreach ( $menu as $menu_key => $menu_info ) { |
| 476 | |
| 477 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 478 | $menu_item_id = $menu_info[2]; |
| 479 | } else { |
| 480 | $menu_item_id = $menu_info[5]; |
| 481 | } |
| 482 | |
| 483 | if ( in_array( $menu_item_id, $hidden_menu ) ) { |
| 484 | |
| 485 | $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu'; |
| 486 | |
| 487 | } |
| 488 | |
| 489 | } |
| 490 | |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Add toggle to show hidden menu items |
| 495 | * |
| 496 | * @since 2.0.0 |
| 497 | */ |
| 498 | public function add_hidden_menu_toggle() { |
| 499 | |
| 500 | $options = get_option( ASENHA_SLUG_U ); |
| 501 | |
| 502 | // Get hidden menu items |
| 503 | $hidden_menu = $options['custom_menu_hidden']; |
| 504 | |
| 505 | if ( ! empty( $hidden_menu ) ) { |
| 506 | |
| 507 | add_menu_page( |
| 508 | 'Show All', |
| 509 | 'Show All', |
| 510 | 'manage_options', |
| 511 | 'asenha_show_hidden_menu', |
| 512 | function () { return false; }, |
| 513 | "dashicons-arrow-down-alt2", |
| 514 | 300 // position |
| 515 | ); |
| 516 | |
| 517 | add_menu_page( |
| 518 | 'Show Less', |
| 519 | 'Show Less', |
| 520 | 'manage_options', |
| 521 | 'asenha_hide_hidden_menu', |
| 522 | function () { return false; }, |
| 523 | "dashicons-arrow-up-alt2", |
| 524 | 301 // position |
| 525 | ); |
| 526 | } |
| 527 | |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Script to toggle hidden menu itesm |
| 532 | * |
| 533 | * @since 2.0.0 |
| 534 | */ |
| 535 | public function enqueue_toggle_hidden_menu_script() { |
| 536 | |
| 537 | $options = get_option( ASENHA_SLUG_U ); |
| 538 | |
| 539 | // Get hidden menu items |
| 540 | |
| 541 | if ( array_key_exists( 'custom_menu_hidden', $options ) ) { |
| 542 | $hidden_menu = $options['custom_menu_hidden']; |
| 543 | } else { |
| 544 | $hidden_menu = ''; |
| 545 | } |
| 546 | |
| 547 | if ( ! empty( $hidden_menu ) ) { |
| 548 | |
| 549 | // Script to set behaviour and actions of the sortable menu |
| 550 | wp_enqueue_script( 'asenha-toggle-hidden-menu', ASENHA_URL . 'assets/js/toggle-hidden-menu.js', array(), ASENHA_VERSION, false ); |
| 551 | |
| 552 | } |
| 553 | |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Hide the Help tab and drawer |
| 558 | * |
| 559 | * @since 4.5.0 |
| 560 | */ |
| 561 | public function hide_help_drawer() { |
| 562 | if ( is_admin() ) { |
| 563 | $screen = get_current_screen(); |
| 564 | $screen->remove_help_tabs(); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | } |