action( 'init', [$this, 'hide_admin_bar']); } /** * Hides the admin bar. * * Checks the settings for the feature and if the feature is enabled, it filters the 'show_admin_bar' hook * to disable the admin bar. If the 'disable-for-admin' option is set to true, it only disables the admin bar * for non-admin users. * * @since 1.0.0 * * @return void */ public function hide_admin_bar() { $settings = $this->get_settings(); if( $this->is_enabled( $settings ) ) { $exclude = isset( $settings['exclude'] ) ? $settings['exclude'] : []; if ( is_array( $exclude ) && ! empty( $exclude ) ) { $this->filter( 'show_admin_bar', function( $show ) use ( $exclude ) { $user = wp_get_current_user(); $user_roles = (array) $user->roles; foreach ( $user_roles as $role ) { if ( in_array( $role, $exclude ) ) { return $show; } } // For everyone else, hide admin bar return false; } ); } else { $this->filter( 'show_admin_bar', '__return_false' ); } } } /** * Get plugin settings. */ private function get_settings() { $option_name = get_tpsa_settings_option_name( $this->features_id ); return get_option( $option_name, [] ); } /** * Check if the feature is enabled. */ private function is_enabled( $settings ) { return isset( $settings['enable'] ) && (int) $settings['enable'] === 1; } }