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-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
856 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 | * Add menu bar item to view admin as one of the user roles |
| 124 | * |
| 125 | * @param $wp_admin_bar The WP_Admin_Bar instance |
| 126 | * @link https://developer.wordpress.org/reference/hooks/admin_bar_menu/ |
| 127 | * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/ |
| 128 | * @since 1.8.0 |
| 129 | */ |
| 130 | public function view_admin_as_admin_bar_menu( $wp_admin_bar ) { |
| 131 | |
| 132 | // Get which role slug is currently set to "View as" |
| 133 | $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true ); |
| 134 | |
| 135 | if ( empty( $viewing_admin_as ) ) { |
| 136 | update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' ); |
| 137 | } |
| 138 | |
| 139 | // Get the role name, translated if available, from the role slug |
| 140 | $wp_roles = wp_roles()->roles; |
| 141 | |
| 142 | foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) { |
| 143 | |
| 144 | if ( $wp_role_slug == $viewing_admin_as ) { |
| 145 | |
| 146 | $viewing_admin_as_role_name = $wp_role_info['name']; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | if ( ! isset( $viewing_admin_as_role_name ) ) { |
| 153 | |
| 154 | $viewing_admin_as_role_name = $viewing_admin_as; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | $translated_name_for_viewing_admin_as = ucfirst( $viewing_admin_as_role_name ); |
| 159 | |
| 160 | // Add parent menu basd on the role being set to "View as" |
| 161 | |
| 162 | if ( 'administrator' == $viewing_admin_as ) { |
| 163 | |
| 164 | // Add parent menu |
| 165 | $wp_admin_bar->add_menu( array( |
| 166 | 'id' => 'asenha-view-admin-as-role', |
| 167 | 'parent' => 'top-secondary', |
| 168 | 'title' => 'View as <span style="font-size:0.8125em;">▼</span>', |
| 169 | 'href' => '#', |
| 170 | 'meta' => array( |
| 171 | 'title' => 'View admin pages and the site (logged-in) as one of the following user roles.' |
| 172 | ), |
| 173 | ) ); |
| 174 | |
| 175 | } else { |
| 176 | |
| 177 | // Add parent menu |
| 178 | $wp_admin_bar->add_menu( array( |
| 179 | 'id' => 'asenha-view-admin-as-role', |
| 180 | 'parent' => 'top-secondary', |
| 181 | 'title' => 'Viewing as ' . $translated_name_for_viewing_admin_as . ' <span style="font-size:0.8125em;">▼</span>', |
| 182 | 'href' => '#', |
| 183 | ) ); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | // Get available role(s) to switch to |
| 188 | $roles_to_switch_to = $this->get_roles_to_switch_to(); |
| 189 | |
| 190 | // Add role(s) to switch to as sub-menu |
| 191 | |
| 192 | if ( 'administrator' == $viewing_admin_as ) { |
| 193 | |
| 194 | // Add submenu for each role other than Administrator |
| 195 | |
| 196 | $i = 1; |
| 197 | |
| 198 | foreach ( $roles_to_switch_to as $role_slug => $data ) { |
| 199 | |
| 200 | $wp_admin_bar->add_menu( array( |
| 201 | 'id' => 'role' . $i . '_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager |
| 202 | 'parent' => 'asenha-view-admin-as-role', |
| 203 | 'title' => $data['role_name'], // role name, e.g. Editor, Shop Manager |
| 204 | 'href' => $data['nonce_url'], // nonce URL for each role |
| 205 | ) ); |
| 206 | |
| 207 | $i++; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | } else { |
| 212 | |
| 213 | // Add submenu to switch back to Administrator role |
| 214 | |
| 215 | foreach ( $roles_to_switch_to as $role_slug => $data ) { |
| 216 | |
| 217 | $wp_admin_bar->add_menu( array( |
| 218 | 'id' => 'role_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager |
| 219 | 'parent' => 'asenha-view-admin-as-role', |
| 220 | 'title' => 'Switch back to ' . $data['role_name'], // role name, e.g. Editor, Shop Manager |
| 221 | 'href' => $data['nonce_url'], // nonce URL for each role |
| 222 | |
| 223 | ) ); |
| 224 | |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get roles availble to switch to |
| 233 | * |
| 234 | * @since 1.8.0 |
| 235 | */ |
| 236 | private function get_roles_to_switch_to() { |
| 237 | |
| 238 | $current_user = wp_get_current_user(); |
| 239 | $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s) |
| 240 | |
| 241 | // Get full list of roles defined in WordPress |
| 242 | $wp_roles = wp_roles()->roles; |
| 243 | |
| 244 | $roles_to_switch_to = array(); |
| 245 | |
| 246 | // Get which role slug is currently active for viewing |
| 247 | $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true ); |
| 248 | |
| 249 | if ( 'administrator' == $viewing_admin_as ) { |
| 250 | |
| 251 | // Exclude 'Administrator' from the "View as" menu |
| 252 | |
| 253 | foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) { |
| 254 | |
| 255 | if ( ! in_array( $wp_role_slug,$current_user_role_slugs ) ) { |
| 256 | |
| 257 | $roles_to_switch_to[$wp_role_slug] = array( |
| 258 | 'role_name' => $wp_role_info['name'], // role name, e.g. Editor, Shop Manager |
| 259 | 'nonce_url' => wp_nonce_url( |
| 260 | add_query_arg( array( |
| 261 | 'action' => 'switch_role_to', |
| 262 | 'role' => $wp_role_slug, |
| 263 | ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action |
| 264 | 'asenha_view_admin_as_' . $wp_role_slug, // the nonce $action name |
| 265 | 'nonce', // the nonce url parameter name |
| 266 | ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df |
| 267 | ); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | } |
| 272 | |
| 273 | } else { |
| 274 | |
| 275 | // Only show switch back to Administrator in the "View as" menu |
| 276 | |
| 277 | $roles_to_switch_to['administrator'] = array( |
| 278 | 'role_name' => 'Administrator', // role name, e.g. Editor, Shop Manager |
| 279 | 'nonce_url' => wp_nonce_url( |
| 280 | add_query_arg( array( |
| 281 | 'action' => 'switch_back_to_administrator', |
| 282 | 'role' => 'administrator', |
| 283 | ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action |
| 284 | 'asenha_view_admin_as_administrator', // the nonce $action name |
| 285 | 'nonce', // the nonce url parameter name |
| 286 | ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | return $roles_to_switch_to; // array of $role_slug => $nonce_url |
| 291 | |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Switch user role to view admin and site |
| 296 | * |
| 297 | * @since 1.8.0 |
| 298 | */ |
| 299 | public function role_switcher_to_view_admin_as() { |
| 300 | |
| 301 | $current_user = wp_get_current_user(); |
| 302 | $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s) |
| 303 | |
| 304 | if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['role'] ) && isset( $_REQUEST['nonce'] ) ) { |
| 305 | |
| 306 | $action = sanitize_text_field( $_REQUEST['action'] ); |
| 307 | $new_role = sanitize_text_field( $_REQUEST['role'] ); |
| 308 | $nonce = sanitize_text_field( $_REQUEST['nonce'] ); |
| 309 | |
| 310 | if ( 'switch_role_to' === $action ) { |
| 311 | |
| 312 | // Check nonce validity and role existence |
| 313 | |
| 314 | $wp_roles = array_keys( wp_roles()->roles ); // indexed array of all WP roles |
| 315 | |
| 316 | if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_' . $new_role ) || ! in_array( $new_role, $wp_roles ) ) { |
| 317 | return; // cancel role switching |
| 318 | } |
| 319 | |
| 320 | // Get original roles (before role switching) of the current user |
| 321 | $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); |
| 322 | |
| 323 | // Store original user role(s) before switching it to another role |
| 324 | |
| 325 | if ( empty( $original_role_slugs ) ) { |
| 326 | |
| 327 | update_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', $current_user_role_slugs ); |
| 328 | |
| 329 | } |
| 330 | |
| 331 | // Remove all current roles from current user. |
| 332 | foreach ( $current_user_role_slugs as $current_user_role_slug ) { |
| 333 | |
| 334 | $current_user->remove_role( $current_user_role_slug ); |
| 335 | |
| 336 | } |
| 337 | |
| 338 | // Add new role to current user |
| 339 | $current_user->add_role( $new_role ); |
| 340 | |
| 341 | // Mark that the user has switched to a non-administrator role |
| 342 | update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', $new_role ); |
| 343 | |
| 344 | // Redirect to admin dashboard |
| 345 | wp_safe_redirect( get_admin_url() ); |
| 346 | exit; |
| 347 | |
| 348 | } |
| 349 | |
| 350 | if ( 'switch_back_to_administrator' === $action ) { |
| 351 | |
| 352 | // Check nonce validity |
| 353 | |
| 354 | if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_administrator' ) || ( $new_role != 'administrator' ) ) { |
| 355 | return; // cancel role switching |
| 356 | } |
| 357 | |
| 358 | // Remove all current roles from current user. |
| 359 | foreach ( $current_user_role_slugs as $current_role_slug ) { |
| 360 | |
| 361 | $current_user->remove_role( $current_role_slug ); |
| 362 | |
| 363 | } |
| 364 | |
| 365 | // Get original roles (before role switching) of the current user |
| 366 | $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); |
| 367 | |
| 368 | // Add the original roles to the current user |
| 369 | foreach ( $original_role_slugs as $original_role_slug ) { |
| 370 | |
| 371 | $current_user->add_role( $original_role_slug ); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | // Mark that the user has switched back to an administrator role |
| 376 | update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' ); |
| 377 | |
| 378 | } |
| 379 | |
| 380 | } elseif ( isset( $_REQUEST['reset-view-as'] ) ) { |
| 381 | |
| 382 | $reset_view_as = sanitize_text_field( $_REQUEST['reset-view-as'] ); |
| 383 | |
| 384 | if ( $reset_view_as == 'yes' ) { |
| 385 | |
| 386 | // Remove all current roles from current user. |
| 387 | foreach ( $current_user_role_slugs as $current_role_slug ) { |
| 388 | |
| 389 | $current_user->remove_role( $current_role_slug ); |
| 390 | |
| 391 | } |
| 392 | |
| 393 | // Get original roles (before role switching) of the current user |
| 394 | $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); |
| 395 | |
| 396 | // Add the original roles to the current user |
| 397 | foreach ( $original_role_slugs as $original_role_slug ) { |
| 398 | |
| 399 | $current_user->add_role( $original_role_slug ); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | // Mark that the user has switched back to an administrator role |
| 404 | update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' ); |
| 405 | |
| 406 | // Redirect to admin dashboard |
| 407 | wp_safe_redirect( get_admin_url() ); |
| 408 | exit; |
| 409 | |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Show custom error page on switch failure, which causes inability to view admin dashboard/pages |
| 418 | * |
| 419 | * @since 1.8.0 |
| 420 | */ |
| 421 | public function custom_error_page_on_switch_failure( $callback ) { |
| 422 | |
| 423 | ?> |
| 424 | <!DOCTYPE html> |
| 425 | <html> |
| 426 | <head> |
| 427 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8; ?>" /> |
| 428 | <meta name="viewport" content="width=device-width"> |
| 429 | <title>WordPress Error</title> |
| 430 | <style type="text/css"> |
| 431 | html { |
| 432 | background: #f1f1f1; |
| 433 | } |
| 434 | body { |
| 435 | background: #fff; |
| 436 | border: 1px solid #ccd0d4; |
| 437 | color: #444; |
| 438 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
| 439 | margin: 2em auto; |
| 440 | padding: 1em 2em; |
| 441 | max-width: 700px; |
| 442 | -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04); |
| 443 | box-shadow: 0 1px 1px rgba(0, 0, 0, .04); |
| 444 | } |
| 445 | h1 { |
| 446 | border-bottom: 1px solid #dadada; |
| 447 | clear: both; |
| 448 | color: #666; |
| 449 | font-size: 24px; |
| 450 | margin: 30px 0 0 0; |
| 451 | padding: 0; |
| 452 | padding-bottom: 7px; |
| 453 | } |
| 454 | #error-page { |
| 455 | margin-top: 50px; |
| 456 | } |
| 457 | #error-page p, |
| 458 | #error-page .wp-die-message { |
| 459 | font-size: 14px; |
| 460 | line-height: 1.5; |
| 461 | margin: 20px 0; |
| 462 | } |
| 463 | #error-page .wp-die-message:last-of-type { |
| 464 | display: none; |
| 465 | } |
| 466 | #error-page code { |
| 467 | font-family: Consolas, Monaco, monospace; |
| 468 | } |
| 469 | a { |
| 470 | color: #0073aa; |
| 471 | } |
| 472 | a:hover, |
| 473 | a:active { |
| 474 | color: #006799; |
| 475 | } |
| 476 | a:focus { |
| 477 | color: #124964; |
| 478 | -webkit-box-shadow: |
| 479 | 0 0 0 1px #5b9dd9, |
| 480 | 0 0 2px 1px rgba(30, 140, 190, 0.8); |
| 481 | box-shadow: |
| 482 | 0 0 0 1px #5b9dd9, |
| 483 | 0 0 2px 1px rgba(30, 140, 190, 0.8); |
| 484 | outline: none; |
| 485 | } |
| 486 | </style> |
| 487 | </head> |
| 488 | <body id="error-page"> |
| 489 | <div class="wp-die-message">Something went wrong. <a href="<?php echo home_url( '?reset-view-as=yes' ); ?>">Click here</a> to go back to the admin dashboard.</div> |
| 490 | </body> |
| 491 | </html> |
| 492 | <?php |
| 493 | |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Modify admin bar menu for Admin Interface >> Hide or Modify Elements feature |
| 498 | * |
| 499 | * @param $wp_admin_bar object The admin bar. |
| 500 | * @since 1.9.0 |
| 501 | */ |
| 502 | public function modify_admin_bar_menu( $wp_admin_bar ) { |
| 503 | |
| 504 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 505 | |
| 506 | // Hide WP Logo Menu |
| 507 | if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) { |
| 508 | remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); // priority needs to match default value. Use QM to reference. |
| 509 | } |
| 510 | |
| 511 | // Hide Customize Menu |
| 512 | if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) { |
| 513 | remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); // priority needs to match default value. Use QM to reference. |
| 514 | } |
| 515 | |
| 516 | // Hide Updates Counter/Link |
| 517 | if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) { |
| 518 | remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); // priority needs to match default value. Use QM to reference. |
| 519 | } |
| 520 | |
| 521 | // Hide Comments Counter/Link |
| 522 | if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) { |
| 523 | remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); // priority needs to match default value. Use QM to reference. |
| 524 | } |
| 525 | |
| 526 | // Hide New Content Menu |
| 527 | if ( array_key_exists( 'hide_ab_new_content_menu', $options ) && $options['hide_ab_new_content_menu'] ) { |
| 528 | remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); // priority needs to match default value. Use QM to reference. |
| 529 | } |
| 530 | |
| 531 | // Hide 'Howdy' text |
| 532 | if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) { |
| 533 | |
| 534 | // Remove the whole my account sectino and later rebuild it |
| 535 | remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); |
| 536 | |
| 537 | $current_user = wp_get_current_user(); |
| 538 | $user_id = get_current_user_id(); |
| 539 | $profile_url = get_edit_profile_url( $user_id ); |
| 540 | |
| 541 | $avatar = get_avatar( $user_id, 26 ); // size 26x26 pixels |
| 542 | $display_name = $current_user->display_name; |
| 543 | $class = 'with-avatar'; |
| 544 | |
| 545 | $wp_admin_bar->add_menu( array( |
| 546 | 'id' => 'my-account', |
| 547 | 'parent' => 'top-secondary', |
| 548 | 'title' => $display_name . $avatar, |
| 549 | 'href' => $profile_url, |
| 550 | 'meta' => array( |
| 551 | 'class' => $class, |
| 552 | ), |
| 553 | ) ); |
| 554 | |
| 555 | } |
| 556 | |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Save custom menu order into an option |
| 561 | * |
| 562 | * @since 2.0.0 |
| 563 | */ |
| 564 | public function save_custom_menu_order() { |
| 565 | |
| 566 | if ( isset( $_REQUEST ) && ( 'save_custom_menu_order' == $_REQUEST['action'] ) ) { |
| 567 | |
| 568 | $options = get_option( ASENHA_SLUG_U ); |
| 569 | |
| 570 | // Empty option value first |
| 571 | // $options['custom_menu_order'] = ''; |
| 572 | // update_option( ASENHA_SLUG_U, $options ); |
| 573 | |
| 574 | // Save new value |
| 575 | // $options = get_option( ASENHA_SLUG_U ); |
| 576 | $options['custom_menu_order'] = sanitize_text_field( $_REQUEST['menu_order'] ); |
| 577 | $success = update_option( ASENHA_SLUG_U, $options ); // true or false |
| 578 | |
| 579 | if ( $success ) { |
| 580 | |
| 581 | $data = array( |
| 582 | 'status' => 'success', |
| 583 | 'message' => 'Custom menu order was successfully saved.', |
| 584 | ); |
| 585 | |
| 586 | } else { |
| 587 | |
| 588 | $data = array( |
| 589 | 'status' => 'error', |
| 590 | 'message' => 'Custom menu order was not saved.', |
| 591 | ); |
| 592 | |
| 593 | } |
| 594 | |
| 595 | echo json_encode( $data ); |
| 596 | |
| 597 | } |
| 598 | |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Render custom menu order |
| 603 | * |
| 604 | * @param $menu_order array an ordered array of menu items |
| 605 | * @link https://developer.wordpress.org/reference/hooks/menu_order/ |
| 606 | * @since 2.0.0 |
| 607 | */ |
| 608 | public function render_custom_menu_order( $menu_order ) { |
| 609 | |
| 610 | global $menu; |
| 611 | |
| 612 | $options = get_option( ASENHA_SLUG_U ); |
| 613 | |
| 614 | // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values. |
| 615 | |
| 616 | $current_menu_order = array(); |
| 617 | |
| 618 | foreach ( $menu as $menu_key => $menu_info ) { |
| 619 | |
| 620 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 621 | $menu_item_id = $menu_info[2]; |
| 622 | } else { |
| 623 | $menu_item_id = $menu_info[5]; |
| 624 | } |
| 625 | |
| 626 | $current_menu_order[] = array( $menu_item_id, $menu_info[2] ); |
| 627 | |
| 628 | } |
| 629 | |
| 630 | // Get custom menu order |
| 631 | $custom_menu_order = $options['custom_menu_order']; // comma separated |
| 632 | $custom_menu_order = explode( ",", $custom_menu_order ); // array of menu ID, e.g. menu-dashboard |
| 633 | |
| 634 | // Return menu order for rendering |
| 635 | |
| 636 | $rendered_menu_order = array(); |
| 637 | |
| 638 | // Render menu based on items saved in custom menu order |
| 639 | |
| 640 | foreach ( $custom_menu_order as $custom_menu_item_id ) { |
| 641 | |
| 642 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 643 | |
| 644 | if ( $custom_menu_item_id == $current_menu_item[0] ) { |
| 645 | |
| 646 | $rendered_menu_order[] = $current_menu_item[1]; |
| 647 | |
| 648 | } |
| 649 | |
| 650 | } |
| 651 | |
| 652 | } |
| 653 | |
| 654 | // do_action( 'inspect', [ 'rendered_menu_order_1', $rendered_menu_order ] ); |
| 655 | |
| 656 | // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item |
| 657 | |
| 658 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 659 | |
| 660 | if ( ! in_array( $current_menu_item[0], $custom_menu_order ) ) { |
| 661 | |
| 662 | $rendered_menu_order[] = $current_menu_item[1]; |
| 663 | |
| 664 | } |
| 665 | |
| 666 | } |
| 667 | |
| 668 | // do_action( 'inspect', [ 'rendered_menu_order_2', $rendered_menu_order ] ); |
| 669 | |
| 670 | return $rendered_menu_order; |
| 671 | |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Save menu items that was chosen to be hidden |
| 676 | * |
| 677 | * @since 2.0.0 |
| 678 | */ |
| 679 | public function save_hidden_menu_items() { |
| 680 | |
| 681 | if ( isset( $_REQUEST ) && ( 'save_hidden_menu_items' == $_REQUEST['action'] ) ) { |
| 682 | |
| 683 | $options = get_option( ASENHA_SLUG_U ); |
| 684 | |
| 685 | // Empty option value first |
| 686 | // $options['custom_menu_order'] = ''; |
| 687 | // update_option( ASENHA_SLUG_U, $options ); |
| 688 | |
| 689 | // Save new value |
| 690 | // $options = get_option( ASENHA_SLUG_U ); |
| 691 | $options['custom_menu_hidden'] = sanitize_text_field( $_REQUEST['hidden_menu_items'] ); |
| 692 | $success = update_option( ASENHA_SLUG_U, $options ); // true or false |
| 693 | |
| 694 | if ( $success ) { |
| 695 | |
| 696 | $data = array( |
| 697 | 'status' => 'success', |
| 698 | 'message' => 'Hidden menu items was successfully saved.', |
| 699 | ); |
| 700 | |
| 701 | } else { |
| 702 | |
| 703 | $data = array( |
| 704 | 'status' => 'error', |
| 705 | 'message' => 'Hidden menu items was not saved.', |
| 706 | ); |
| 707 | |
| 708 | } |
| 709 | |
| 710 | echo json_encode( $data ); |
| 711 | |
| 712 | } |
| 713 | |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Apply custom menu item titles |
| 718 | * |
| 719 | * @since 2.9.0 |
| 720 | */ |
| 721 | public function apply_custom_menu_item_titles() { |
| 722 | |
| 723 | global $menu; |
| 724 | |
| 725 | $options = get_option( ASENHA_SLUG_U ); |
| 726 | |
| 727 | // Get custom menu item titles |
| 728 | $custom_menu_titles = $options['custom_menu_titles']; |
| 729 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 730 | |
| 731 | foreach ( $menu as $menu_key => $menu_info ) { |
| 732 | |
| 733 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 734 | $menu_item_id = $menu_info[2]; |
| 735 | } else { |
| 736 | $menu_item_id = $menu_info[5]; |
| 737 | } |
| 738 | |
| 739 | // Get defaul/custom menu item title |
| 740 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 741 | |
| 742 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 743 | |
| 744 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 745 | |
| 746 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 747 | $menu_item_title = $custom_menu_title[1]; // e.g. Code Snippets |
| 748 | break; // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 749 | } else { |
| 750 | $menu_item_title = $menu_info[0]; |
| 751 | } |
| 752 | |
| 753 | } |
| 754 | |
| 755 | $menu[$menu_key][0] = $menu_item_title; |
| 756 | |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Hide menu items by adding 'hidden' class (part of WP Core's common.css) |
| 762 | * |
| 763 | * @since 2.0.0 |
| 764 | */ |
| 765 | public function hide_menu_items() { |
| 766 | |
| 767 | global $menu; |
| 768 | |
| 769 | $options = get_option( ASENHA_SLUG_U ); |
| 770 | |
| 771 | // Get hidden menu items |
| 772 | $hidden_menu = $options['custom_menu_hidden']; |
| 773 | $hidden_menu = explode( ',', $hidden_menu ); |
| 774 | |
| 775 | foreach ( $menu as $menu_key => $menu_info ) { |
| 776 | |
| 777 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 778 | $menu_item_id = $menu_info[2]; |
| 779 | } else { |
| 780 | $menu_item_id = $menu_info[5]; |
| 781 | } |
| 782 | |
| 783 | if ( in_array( $menu_item_id, $hidden_menu ) ) { |
| 784 | |
| 785 | $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu'; |
| 786 | |
| 787 | } |
| 788 | |
| 789 | } |
| 790 | |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Add toggle to show hidden menu items |
| 795 | * |
| 796 | * @since 2.0.0 |
| 797 | */ |
| 798 | public function add_hidden_menu_toggle() { |
| 799 | |
| 800 | $options = get_option( ASENHA_SLUG_U ); |
| 801 | |
| 802 | // Get hidden menu items |
| 803 | $hidden_menu = $options['custom_menu_hidden']; |
| 804 | |
| 805 | if ( ! empty( $hidden_menu ) ) { |
| 806 | |
| 807 | add_menu_page( |
| 808 | 'Show All', |
| 809 | 'Show All', |
| 810 | 'manage_options', |
| 811 | 'asenha_show_hidden_menu', |
| 812 | function () { return false; }, |
| 813 | "dashicons-arrow-down-alt2", |
| 814 | 300 // position |
| 815 | ); |
| 816 | |
| 817 | add_menu_page( |
| 818 | 'Show Less', |
| 819 | 'Show Less', |
| 820 | 'manage_options', |
| 821 | 'asenha_hide_hidden_menu', |
| 822 | function () { return false; }, |
| 823 | "dashicons-arrow-up-alt2", |
| 824 | 301 // position |
| 825 | ); |
| 826 | } |
| 827 | |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Script to toggle hidden menu itesm |
| 832 | * |
| 833 | * @since 2.0.0 |
| 834 | */ |
| 835 | public function enqueue_toggle_hidden_menu_script() { |
| 836 | |
| 837 | $options = get_option( ASENHA_SLUG_U ); |
| 838 | |
| 839 | // Get hidden menu items |
| 840 | |
| 841 | if ( array_key_exists( 'custom_menu_hidden', $options ) ) { |
| 842 | $hidden_menu = $options['custom_menu_hidden']; |
| 843 | } else { |
| 844 | $hidden_menu = ''; |
| 845 | } |
| 846 | |
| 847 | if ( ! empty( $hidden_menu ) ) { |
| 848 | |
| 849 | // Script to set behaviour and actions of the sortable menu |
| 850 | wp_enqueue_script( 'asenha-toggle-hidden-menu', ASENHA_URL . 'assets/js/toggle-hidden-menu.js', array(), ASENHA_VERSION, false ); |
| 851 | |
| 852 | } |
| 853 | |
| 854 | } |
| 855 | |
| 856 | } |