Airplane_Mode_Core::admin_bar_toggle PHP Method

admin_bar_toggle() public method

Add our quick toggle to the admin bar to enable / disable
public admin_bar_toggle ( WP_Admin_Bar $wp_admin_bar ) : void
$wp_admin_bar WP_Admin_Bar The admin bar object.
return void if current user can't manage options and we bail early.
        public function admin_bar_toggle(WP_Admin_Bar $wp_admin_bar)
        {
            // Bail if current user doesn't have cap.
            if (!current_user_can('manage_options')) {
                return;
            }
            // Get the current status.
            $status = $this->enabled();
            // Set a title message (translatable).
            $title = $status ? __('Airplane Mode is enabled.', 'airplane-mode') : __('Airplane Mode is disabled.', 'airplane-mode');
            // Set our toggle variable parameter (in reverse since we want the opposite action).
            $toggle = $status ? 'off' : 'on';
            // Set my HTTP request count label to a blank string for now.
            $label = '';
            // Get and display the HTTP count when Query Monitor isn't active.
            if (!class_exists('QueryMonitor') || defined('QM_DISABLED') && QM_DISABLED) {
                // Pull my HTTP count.
                $count = !empty($this->http_count) ? number_format_i18n($this->http_count) : 0;
                $count_label = sprintf(_n('There was %s HTTP request.', 'There were %s HTTP requests.', $count, 'airplane-mode'), $count);
                // Build the markup for my label.
                $label = '<span class="ab-label" aria-hidden="true">' . absint($count) . '</span>';
                $label .= '<span class="screen-reader-text">' . esc_html($count_label) . '</span>';
                // Amend the tooltip title with the count.
                $title .= '&nbsp;' . $count_label;
            }
            // Get our link with the status parameter.
            $link = wp_nonce_url(add_query_arg('airplane-mode', $toggle), 'airmde_nonce', 'airmde_nonce');
            // Now add the admin bar link.
            $wp_admin_bar->add_menu(array('id' => 'airplane-mode-toggle', 'title' => '<span class="ab-icon"></span>' . $label, 'href' => esc_url($link), 'position' => 0, 'meta' => array('title' => $title)));
        }