asset_manager = $asset_manager; } /** * Adds the admin bar menu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ public function add_menu( WP_Admin_Bar $wp_admin_bar ) { // If the current user can't write posts, this is all of no use, so let's not output an admin menu. if ( ! current_user_can( 'edit_posts' ) ) { return; } $this->add_root_menu( $wp_admin_bar ); $this->add_keyword_research_submenu( $wp_admin_bar ); if ( ! is_admin() ) { $this->add_analysis_submenu( $wp_admin_bar ); } if ( ! is_admin() || is_blog_admin() ) { $this->add_settings_submenu( $wp_admin_bar ); } elseif ( is_network_admin() ) { $this->add_network_settings_submenu( $wp_admin_bar ); } } /** * Enqueues admin bar assets. * * @return void */ public function enqueue_assets() { if ( ! is_admin_bar_showing() ) { return; } // If the current user can't write posts, this is all of no use, so let's not output an admin menu. if ( ! current_user_can( 'edit_posts' ) ) { return; } $this->asset_manager->register_assets(); $this->asset_manager->enqueue_style( 'adminbar' ); } /** * Registers the hooks. * * @return void */ public function register_hooks() { if ( ! $this->meets_requirements() ) { return; } add_action( 'admin_bar_menu', array( $this, 'add_menu' ), 95 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Checks whether the requirements to use this class are met. * * @return bool True if requirements are met, false otherwise. */ public function meets_requirements() { if ( is_network_admin() ) { return WPSEO_Utils::is_plugin_network_active(); } if ( WPSEO_Options::get( 'enable_admin_bar_menu' ) !== true ) { return false; } return ! is_admin() || is_blog_admin(); } /** * Adds the admin bar root menu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_root_menu( WP_Admin_Bar $wp_admin_bar ) { $title = $this->get_title(); $score = ''; $settings_url = ''; $counter = ''; $alert_popup = ''; $post = $this->get_singular_post(); if ( $post ) { $score = $this->get_post_score( $post ); } $term = $this->get_singular_term(); if ( $term ) { $score = $this->get_term_score( $term ); } $can_manage_options = $this->can_manage_options(); if ( $can_manage_options ) { $settings_url = $this->get_settings_page_url(); } if ( empty( $score ) && ! is_network_admin() && $can_manage_options ) { $counter = $this->get_notification_counter(); $alert_popup = $this->get_notification_alert_popup(); } $wp_admin_bar->add_menu( array( 'id' => self::MENU_IDENTIFIER, 'title' => $title . $score . $counter . $alert_popup, 'href' => $settings_url, 'meta' => array( 'tabindex' => ! empty( $settings_url ) ? false : '0' ), ) ); if ( ! empty( $counter ) ) { $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-notifications', 'title' => __( 'Notifications', 'wordpress-seo' ) . $counter, 'href' => $settings_url, 'meta' => array( 'tabindex' => ! empty( $settings_url ) ? false : '0' ), ) ); } if ( ! is_network_admin() && $can_manage_options ) { $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-configuration-wizard', 'title' => __( 'Configuration Wizard', 'wordpress-seo' ), 'href' => admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ), ) ); } } /** * Adds the admin bar keyword research submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_keyword_research_submenu( WP_Admin_Bar $wp_admin_bar ) { $adwords_url = 'https://yoa.st/keywordplanner'; $trends_url = 'https://yoa.st/google-trends'; $post = $this->get_singular_post(); if ( $post ) { $focus_keyword = $this->get_post_focus_keyword( $post ); if ( ! empty( $focus_keyword ) ) { $trends_url .= '#q=' . urlencode( $focus_keyword ); } } $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER, 'title' => __( 'Keyword Research', 'wordpress-seo' ), 'meta' => array( 'tabindex' => '0' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER, 'id' => 'wpseo-kwresearchtraining', 'title' => __( 'Keyword research training', 'wordpress-seo' ), 'href' => WPSEO_Shortlinker::get( 'https://yoa.st/wp-admin-bar' ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER, 'id' => 'wpseo-adwordsexternal', 'title' => __( 'Google Ads', 'wordpress-seo' ), 'href' => $adwords_url, 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER, 'id' => 'wpseo-googleinsights', 'title' => __( 'Google Trends', 'wordpress-seo' ), 'href' => $trends_url, 'meta' => array( 'target' => '_blank' ), ) ); } /** * Adds the admin bar analysis submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_analysis_submenu( WP_Admin_Bar $wp_admin_bar ) { $url = WPSEO_Frontend::get_instance()->canonical( false ); $focus_keyword = ''; if ( ! $url ) { return; } $post = $this->get_singular_post(); if ( $post ) { $focus_keyword = $this->get_post_focus_keyword( $post ); } $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'title' => __( 'Analyze this page', 'wordpress-seo' ), 'meta' => array( 'tabindex' => '0' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-inlinks', 'title' => __( 'Check links to this URL', 'wordpress-seo' ), 'href' => 'https://search.google.com/search-console/links/drilldown?resource_id=' . urlencode( get_option( 'siteurl' ) ) . '&type=EXTERNAL&target=' . urlencode( $url ) . '&domain=', 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-kwdensity', 'title' => __( 'Check Keyphrase Density', 'wordpress-seo' ), // HTTPS not available. 'href' => 'http://www.zippy.co.uk/keyworddensity/index.php?url=' . urlencode( $url ) . '&keyword=' . urlencode( $focus_keyword ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-cache', 'title' => __( 'Check Google Cache', 'wordpress-seo' ), 'href' => '//webcache.googleusercontent.com/search?strip=1&q=cache:' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-header', 'title' => __( 'Check Headers', 'wordpress-seo' ), 'href' => '//quixapp.com/headers/?r=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-structureddata', 'title' => __( 'Google Structured Data Test', 'wordpress-seo' ), 'href' => 'https://search.google.com/structured-data/testing-tool#url=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-facebookdebug', 'title' => __( 'Facebook Debugger', 'wordpress-seo' ), 'href' => '//developers.facebook.com/tools/debug/og/object?q=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-pinterestvalidator', 'title' => __( 'Pinterest Rich Pins Validator', 'wordpress-seo' ), 'href' => 'https://developers.pinterest.com/tools/url-debugger/?link=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-htmlvalidation', 'title' => __( 'HTML Validator', 'wordpress-seo' ), 'href' => '//validator.w3.org/check?uri=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-cssvalidation', 'title' => __( 'CSS Validator', 'wordpress-seo' ), 'href' => '//jigsaw.w3.org/css-validator/validator?uri=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-pagespeed', 'title' => __( 'Google Page Speed Test', 'wordpress-seo' ), 'href' => '//developers.google.com/speed/pagespeed/insights/?url=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); $wp_admin_bar->add_menu( array( 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'id' => 'wpseo-google-mobile-friendly', 'title' => __( 'Mobile-Friendly Test', 'wordpress-seo' ), 'href' => 'https://www.google.com/webmasters/tools/mobile-friendly/?url=' . urlencode( $url ), 'meta' => array( 'target' => '_blank' ), ) ); } /** * Adds the admin bar settings submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_settings_submenu( WP_Admin_Bar $wp_admin_bar ) { if ( ! $this->can_manage_options() ) { return; } $admin_menu = new WPSEO_Admin_Menu( new WPSEO_Menu() ); $submenu_pages = $admin_menu->get_submenu_pages(); $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => self::SETTINGS_SUBMENU_IDENTIFIER, 'title' => __( 'SEO Settings', 'wordpress-seo' ), 'meta' => array( 'tabindex' => '0' ), ) ); foreach ( $submenu_pages as $submenu_page ) { if ( ! current_user_can( $submenu_page[3] ) ) { continue; } $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) ); if ( $id === 'wpseo-dashboard' ) { $id = 'wpseo-general'; } $wp_admin_bar->add_menu( array( 'parent' => self::SETTINGS_SUBMENU_IDENTIFIER, 'id' => $id, 'title' => $submenu_page[2], 'href' => admin_url( 'admin.php?page=' . urlencode( $submenu_page[4] ) ), ) ); } } /** * Adds the admin bar network settings submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_network_settings_submenu( WP_Admin_Bar $wp_admin_bar ) { if ( ! $this->can_manage_options() ) { return; } $network_admin_menu = new WPSEO_Network_Admin_Menu( new WPSEO_Menu() ); $submenu_pages = $network_admin_menu->get_submenu_pages(); $wp_admin_bar->add_menu( array( 'parent' => self::MENU_IDENTIFIER, 'id' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER, 'title' => __( 'SEO Settings', 'wordpress-seo' ), 'meta' => array( 'tabindex' => '0' ), ) ); foreach ( $submenu_pages as $submenu_page ) { if ( ! current_user_can( $submenu_page[3] ) ) { continue; } $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) ); if ( $id === 'wpseo-dashboard' ) { $id = 'wpseo-general'; } $wp_admin_bar->add_menu( array( 'parent' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER, 'id' => $id, 'title' => $submenu_page[2], 'href' => network_admin_url( 'admin.php?page=' . urlencode( $submenu_page[4] ) ), ) ); } } /** * Gets the menu title markup. * * @return string Admin bar title markup. */ protected function get_title() { return '