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
493 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | use WP_Admin_Bar ; |
| 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 | * Admin bar menu item for the hidden admin notices |
| 25 | * |
| 26 | * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/ |
| 27 | * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ |
| 28 | * @since 1.2.0 |
| 29 | */ |
| 30 | public function admin_notices_menu( WP_Admin_Bar $wp_admin_bar ) |
| 31 | { |
| 32 | $wp_admin_bar->add_menu( array( |
| 33 | 'id' => 'asenha-hide-admin-notices', |
| 34 | 'parent' => 'top-secondary', |
| 35 | 'grou' => null, |
| 36 | 'title' => 'Notices<span class="asenha-admin-notices-counter" style="opacity:0;">0</span>', |
| 37 | 'meta' => array( |
| 38 | 'class' => 'asenha-admin-notices-menu', |
| 39 | 'title' => 'Click to view hidden admin notices', |
| 40 | ), |
| 41 | ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Inline CSS for the admin bar notices menu |
| 46 | * |
| 47 | * @since 1.2.0 |
| 48 | */ |
| 49 | public function admin_notices_menu_inline_css() |
| 50 | { |
| 51 | wp_add_inline_style( 'admin-bar', ' |
| 52 | |
| 53 | #wpadminbar .asenha-admin-notices-counter { |
| 54 | box-sizing: border-box; |
| 55 | margin: 1px 0 -1px 6px ; |
| 56 | padding: 2px 6px 3px 5px; |
| 57 | min-width: 18px; |
| 58 | height: 18px; |
| 59 | border-radius: 50%; |
| 60 | background-color: #ca4a1f; |
| 61 | color: #fff; |
| 62 | font-size: 11px; |
| 63 | line-height: 1.6; |
| 64 | text-align: center; |
| 65 | } |
| 66 | |
| 67 | ' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Hide admin bar on the frontend for the user roles selected |
| 72 | * |
| 73 | * @since 1.3.0 |
| 74 | */ |
| 75 | public function hide_admin_bar_for_roles() |
| 76 | { |
| 77 | $options = get_option( ASENHA_SLUG_U ); |
| 78 | $hide_admin_bar = $options['hide_admin_bar']; |
| 79 | $for_roles = $options['hide_admin_bar_for']; |
| 80 | $current_user = wp_get_current_user(); |
| 81 | $current_user_roles = (array) $current_user->roles; |
| 82 | // single dimensional array of role slugs |
| 83 | // User has no role, i.e. logged-out |
| 84 | |
| 85 | if ( count( $current_user_roles ) == 0 ) { |
| 86 | return false; |
| 87 | // hide admin bar |
| 88 | } |
| 89 | |
| 90 | // User has role(s). Do further checks. |
| 91 | |
| 92 | if ( isset( $for_roles ) && count( $for_roles ) > 0 ) { |
| 93 | // Assemble single-dimensional array of roles for which admin bar would be hidden |
| 94 | $roles_admin_bar_hidden = array(); |
| 95 | foreach ( $for_roles as $role_slug => $admin_bar_hidden ) { |
| 96 | if ( $admin_bar_hidden ) { |
| 97 | $roles_admin_bar_hidden[] = $role_slug; |
| 98 | } |
| 99 | } |
| 100 | // Check if any of the current user roles is one for which admin bar should be hidden |
| 101 | foreach ( $current_user_roles as $role ) { |
| 102 | |
| 103 | if ( in_array( $role, $roles_admin_bar_hidden ) ) { |
| 104 | return false; |
| 105 | // hide admin bar |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | // show admin bar |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get dashboard widgets |
| 117 | * |
| 118 | * @since 4.2.0 |
| 119 | */ |
| 120 | public function get_dashboard_widgets() |
| 121 | { |
| 122 | global $wp_meta_boxes ; |
| 123 | $dashboard_widgets = array(); |
| 124 | |
| 125 | if ( !isset( $wp_meta_boxes['dashboard'] ) ) { |
| 126 | $extra_options = get_option( 'admin_site_enhancements_extra', array() ); |
| 127 | |
| 128 | if ( !array_key_exists( 'dashboard_widgets', $extra_options ) ) { |
| 129 | require_once ABSPATH . '/wp-admin/includes/dashboard.php'; |
| 130 | set_current_screen( 'dashboard' ); |
| 131 | wp_dashboard_setup(); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | if ( isset( $wp_meta_boxes['dashboard'] ) ) { |
| 137 | foreach ( $wp_meta_boxes['dashboard'] as $context => $priorities ) { |
| 138 | foreach ( $priorities as $priority => $widgets ) { |
| 139 | foreach ( $widgets as $widget_id => $data ) { |
| 140 | $widget_title = wp_strip_all_tags( preg_replace( '/ <span.*span>/im', '', $data['title'] ) ); |
| 141 | $dashboard_widgets[$widget_id] = array( |
| 142 | 'id' => $widget_id, |
| 143 | 'title' => $widget_title, |
| 144 | 'context' => $context, |
| 145 | 'priority' => $priority, |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | $dashboard_widgets = wp_list_sort( |
| 152 | $dashboard_widgets, |
| 153 | 'title', |
| 154 | 'ASC', |
| 155 | true |
| 156 | ); |
| 157 | return $dashboard_widgets; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Disable dashboard widgets |
| 162 | * |
| 163 | * @since 4.2.0 |
| 164 | */ |
| 165 | public function disable_dashboard_widgets() |
| 166 | { |
| 167 | global $wp_meta_boxes ; |
| 168 | // Get list of disabled widgets |
| 169 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 170 | $disabled_dashboard_widgets = $options['disabled_dashboard_widgets']; |
| 171 | // Store default widgets in extra options. This will be referenced from settings field. |
| 172 | $dashboard_widgets = $this->get_dashboard_widgets(); |
| 173 | $extra_options = get_option( 'admin_site_enhancements_extra', array() ); |
| 174 | $extra_options['dashboard_widgets'] = $dashboard_widgets; |
| 175 | update_option( 'admin_site_enhancements_extra', $extra_options ); |
| 176 | // Disable widgets |
| 177 | foreach ( $disabled_dashboard_widgets as $disabled_widget_id_context_priority => $is_disabled ) { |
| 178 | // e.g. dashboard_activity__normal__core => true/false |
| 179 | |
| 180 | if ( $is_disabled ) { |
| 181 | $disabled_widget = explode( '__', $disabled_widget_id_context_priority ); |
| 182 | $widget_id = $disabled_widget[0]; |
| 183 | $widget_context = $disabled_widget[1]; |
| 184 | $widget_priority = $disabled_widget[2]; |
| 185 | // remove_meta_box( $widget_id, get_current_screen()->base, $widget_context ); |
| 186 | unset( $wp_meta_boxes['dashboard'][$widget_context][$widget_priority][$widget_id] ); |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Modify admin bar menu for Admin Interface >> Hide or Modify Elements feature |
| 194 | * |
| 195 | * @param $wp_admin_bar object The admin bar. |
| 196 | * @since 1.9.0 |
| 197 | */ |
| 198 | public function modify_admin_bar_menu( $wp_admin_bar ) |
| 199 | { |
| 200 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 201 | // Hide WP Logo Menu |
| 202 | |
| 203 | if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) { |
| 204 | remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); |
| 205 | // priority needs to match default value. Use QM to reference. |
| 206 | } |
| 207 | |
| 208 | // Hide Customize Menu |
| 209 | |
| 210 | if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) { |
| 211 | remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); |
| 212 | // priority needs to match default value. Use QM to reference. |
| 213 | } |
| 214 | |
| 215 | // Hide Updates Counter/Link |
| 216 | |
| 217 | if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) { |
| 218 | remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); |
| 219 | // priority needs to match default value. Use QM to reference. |
| 220 | } |
| 221 | |
| 222 | // Hide Comments Counter/Link |
| 223 | |
| 224 | if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) { |
| 225 | remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); |
| 226 | // priority needs to match default value. Use QM to reference. |
| 227 | } |
| 228 | |
| 229 | // Hide New Content Menu |
| 230 | |
| 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 ); |
| 233 | // priority needs to match default value. Use QM to reference. |
| 234 | } |
| 235 | |
| 236 | // Hide 'Howdy' text |
| 237 | |
| 238 | if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) { |
| 239 | // Remove the whole my account sectino and later rebuild it |
| 240 | remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); |
| 241 | $current_user = wp_get_current_user(); |
| 242 | $user_id = get_current_user_id(); |
| 243 | $profile_url = get_edit_profile_url( $user_id ); |
| 244 | $avatar = get_avatar( $user_id, 26 ); |
| 245 | // size 26x26 pixels |
| 246 | $display_name = $current_user->display_name; |
| 247 | $class = 'with-avatar'; |
| 248 | $wp_admin_bar->add_menu( array( |
| 249 | 'id' => 'my-account', |
| 250 | 'parent' => 'top-secondary', |
| 251 | 'title' => $display_name . $avatar, |
| 252 | 'href' => $profile_url, |
| 253 | 'meta' => array( |
| 254 | 'class' => $class, |
| 255 | ), |
| 256 | ) ); |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Set custom admin menu width |
| 263 | * |
| 264 | * @since 5.2.0 |
| 265 | */ |
| 266 | public function set_custom_menu_width() |
| 267 | { |
| 268 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 269 | $custom_width = $options['admin_menu_width']; |
| 270 | $wp_version = get_bloginfo( 'version' ); |
| 271 | |
| 272 | if ( version_compare( $wp_version, '5', '>' ) ) { |
| 273 | require_once ASENHA_PATH . 'includes/admin-menu-width/wp-v5-greater.php'; |
| 274 | } elseif ( version_compare( $wp_version, '4', '>=' ) ) { |
| 275 | require_once ASENHA_PATH . 'includes/admin-menu-width/wp-v4-greater.php'; |
| 276 | } else { |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Render custom menu order |
| 283 | * |
| 284 | * @param $menu_order array an ordered array of menu items |
| 285 | * @link https://developer.wordpress.org/reference/hooks/menu_order/ |
| 286 | * @since 2.0.0 |
| 287 | */ |
| 288 | public function render_custom_menu_order( $menu_order ) |
| 289 | { |
| 290 | global $menu ; |
| 291 | $options = get_option( ASENHA_SLUG_U ); |
| 292 | // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values. |
| 293 | $current_menu_order = array(); |
| 294 | foreach ( $menu as $menu_key => $menu_info ) { |
| 295 | |
| 296 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 297 | $menu_item_id = $menu_info[2]; |
| 298 | } else { |
| 299 | $menu_item_id = $menu_info[5]; |
| 300 | } |
| 301 | |
| 302 | $current_menu_order[] = array( $menu_item_id, $menu_info[2] ); |
| 303 | } |
| 304 | // Get custom menu order |
| 305 | $custom_menu_order = $options['custom_menu_order']; |
| 306 | // comma separated |
| 307 | $custom_menu_order = explode( ",", $custom_menu_order ); |
| 308 | // array of menu ID, e.g. menu-dashboard |
| 309 | // Return menu order for rendering |
| 310 | $rendered_menu_order = array(); |
| 311 | // Render menu based on items saved in custom menu order |
| 312 | foreach ( $custom_menu_order as $custom_menu_item_id ) { |
| 313 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 314 | if ( $custom_menu_item_id == $current_menu_item[0] ) { |
| 315 | $rendered_menu_order[] = $current_menu_item[1]; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item |
| 320 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 321 | if ( !in_array( $current_menu_item[0], $custom_menu_order ) ) { |
| 322 | $rendered_menu_order[] = $current_menu_item[1]; |
| 323 | } |
| 324 | } |
| 325 | return $rendered_menu_order; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Apply custom menu item titles |
| 330 | * |
| 331 | * @since 2.9.0 |
| 332 | */ |
| 333 | public function apply_custom_menu_item_titles() |
| 334 | { |
| 335 | global $menu ; |
| 336 | $options = get_option( ASENHA_SLUG_U ); |
| 337 | // Get custom menu item titles |
| 338 | $custom_menu_titles = $options['custom_menu_titles']; |
| 339 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 340 | foreach ( $menu as $menu_key => $menu_info ) { |
| 341 | |
| 342 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 343 | $menu_item_id = $menu_info[2]; |
| 344 | } else { |
| 345 | $menu_item_id = $menu_info[5]; |
| 346 | } |
| 347 | |
| 348 | // Get defaul/custom menu item title |
| 349 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 350 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 351 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 352 | |
| 353 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 354 | $menu_item_title = $custom_menu_title[1]; |
| 355 | // e.g. Code Snippets |
| 356 | break; |
| 357 | // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 358 | } else { |
| 359 | $menu_item_title = $menu_info[0]; |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | $menu[$menu_key][0] = $menu_item_title; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Hide menu items by adding a class to hide them (part of WP Core's common.css) |
| 369 | * |
| 370 | * @since 2.0.0 |
| 371 | */ |
| 372 | public function hide_menu_items() |
| 373 | { |
| 374 | global $menu ; |
| 375 | $common_methods = new Common_Methods(); |
| 376 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 377 | // indexed array |
| 378 | foreach ( $menu as $menu_key => $menu_info ) { |
| 379 | |
| 380 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 381 | $menu_item_id = $menu_info[2]; |
| 382 | } else { |
| 383 | $menu_item_id = $menu_info[5]; |
| 384 | } |
| 385 | |
| 386 | // Append 'hidden' class to hide menu item until toggled |
| 387 | if ( in_array( $menu_item_id, $menu_hidden_by_toggle ) ) { |
| 388 | $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu'; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Add toggle to show hidden menu items |
| 395 | * |
| 396 | * @since 2.0.0 |
| 397 | */ |
| 398 | public function add_hidden_menu_toggle() |
| 399 | { |
| 400 | global $current_user ; |
| 401 | // Get menu items hidden by toggle |
| 402 | $common_methods = new Common_Methods(); |
| 403 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 404 | // Get user capabilities the "Show All/Less" toggle should be shown for |
| 405 | $user_capabilities_to_show_menu_toggle_for = $common_methods->get_user_capabilities_to_show_menu_toggle_for(); |
| 406 | // Get current user's capabilities from the user's role(s) |
| 407 | $current_user_capabilities = ''; |
| 408 | $current_user_roles = $current_user->roles; |
| 409 | // indexed array of role slugs |
| 410 | foreach ( $current_user_roles as $current_user_role ) { |
| 411 | $current_user_role_capabilities = get_role( $current_user_role )->capabilities; |
| 412 | $current_user_role_capabilities = array_keys( $current_user_role_capabilities ); |
| 413 | // indexed array |
| 414 | $current_user_role_capabilities = implode( ",", $current_user_role_capabilities ); |
| 415 | $current_user_capabilities .= $current_user_role_capabilities; |
| 416 | } |
| 417 | $current_user_capabilities = array_unique( explode( ",", $current_user_capabilities ) ); |
| 418 | // Maybe show "Show All/Less" toggle |
| 419 | $show_toggle_menu = false; |
| 420 | foreach ( $user_capabilities_to_show_menu_toggle_for as $user_capability_to_show_menu_toggle_for ) { |
| 421 | |
| 422 | if ( in_array( $user_capability_to_show_menu_toggle_for, $current_user_capabilities ) ) { |
| 423 | $show_toggle_menu = true; |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | if ( !empty($menu_hidden_by_toggle) && $show_toggle_menu ) { |
| 430 | add_menu_page( |
| 431 | 'Show All', |
| 432 | 'Show All', |
| 433 | 'read', |
| 434 | 'asenha_show_hidden_menu', |
| 435 | function () { |
| 436 | return false; |
| 437 | }, |
| 438 | "dashicons-arrow-down-alt2", |
| 439 | 300 |
| 440 | ); |
| 441 | add_menu_page( |
| 442 | 'Show Less', |
| 443 | 'Show Less', |
| 444 | 'read', |
| 445 | 'asenha_hide_hidden_menu', |
| 446 | function () { |
| 447 | return false; |
| 448 | }, |
| 449 | "dashicons-arrow-up-alt2", |
| 450 | 301 |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Script to toggle hidden menu itesm |
| 458 | * |
| 459 | * @since 2.0.0 |
| 460 | */ |
| 461 | public function enqueue_toggle_hidden_menu_script() |
| 462 | { |
| 463 | // Get menu items hidden by toggle |
| 464 | $common_methods = new Common_Methods(); |
| 465 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 466 | if ( !empty($menu_hidden_by_toggle) ) { |
| 467 | // Script to set behaviour and actions of the sortable menu |
| 468 | wp_enqueue_script( |
| 469 | 'asenha-toggle-hidden-menu', |
| 470 | ASENHA_URL . 'assets/js/toggle-hidden-menu.js', |
| 471 | array(), |
| 472 | ASENHA_VERSION, |
| 473 | false |
| 474 | ); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Hide the Help tab and drawer |
| 480 | * |
| 481 | * @since 4.5.0 |
| 482 | */ |
| 483 | public function hide_help_drawer() |
| 484 | { |
| 485 | |
| 486 | if ( is_admin() ) { |
| 487 | $screen = get_current_screen(); |
| 488 | $screen->remove_help_tabs(); |
| 489 | } |
| 490 | |
| 491 | } |
| 492 | |
| 493 | } |