class-hide-admin-bar.php
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Hide Admin Bar module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Hide_Admin_Bar { |
| 11 | /** |
| 12 | * Hide admin bar on the frontend for the user roles selected |
| 13 | * |
| 14 | * @since 1.3.0 |
| 15 | */ |
| 16 | public function hide_admin_bar_for_roles_on_frontend() { |
| 17 | $options = get_option( ASENHA_SLUG_U ); |
| 18 | $hide_admin_bar = $options['hide_admin_bar']; |
| 19 | $for_roles_frontend = $options['hide_admin_bar_for']; |
| 20 | $current_user = wp_get_current_user(); |
| 21 | $current_user_roles = (array) $current_user->roles; |
| 22 | // single dimensional array of role slugs |
| 23 | // User has no role, i.e. logged-out |
| 24 | if ( count( $current_user_roles ) == 0 ) { |
| 25 | return false; |
| 26 | // hide admin bar |
| 27 | } |
| 28 | // User has role(s). Do further checks. |
| 29 | if ( isset( $for_roles_frontend ) && count( $for_roles_frontend ) > 0 ) { |
| 30 | // Assemble single-dimensional array of roles for which admin bar would be hidden |
| 31 | $roles_admin_bar_hidden_frontend = array(); |
| 32 | foreach ( $for_roles_frontend as $role_slug => $admin_bar_hidden ) { |
| 33 | if ( $admin_bar_hidden ) { |
| 34 | $roles_admin_bar_hidden_frontend[] = $role_slug; |
| 35 | } |
| 36 | } |
| 37 | // Check if any of the current user roles is one for which admin bar should be hidden |
| 38 | foreach ( $current_user_roles as $role ) { |
| 39 | if ( in_array( $role, $roles_admin_bar_hidden_frontend ) ) { |
| 40 | return false; |
| 41 | // hide admin bar |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return true; |
| 46 | // show admin bar |
| 47 | } |
| 48 | |
| 49 | } |
| 50 |