classes->get( Indexable_Repository::class ); } if ( ! $score_icon_helper ) { $score_icon_helper = YoastSEO()->helpers->score_icon; } if ( ! $product_helper ) { $product_helper = YoastSEO()->helpers->product; } if ( ! $shortlinker ) { $shortlinker = new WPSEO_Shortlinker(); } $this->product_helper = $product_helper; $this->asset_manager = $asset_manager; $this->indexable_repository = $indexable_repository; $this->score_icon_helper = $score_icon_helper; $this->shortlinker = $shortlinker; } /** * Gets whether SEO score is enabled, with cache applied. * * @return bool True if SEO score is enabled, false otherwise. */ protected function get_is_seo_enabled() { if ( is_null( $this->is_seo_enabled ) ) { $this->is_seo_enabled = ( new WPSEO_Metabox_Analysis_SEO() )->is_enabled(); } return $this->is_seo_enabled; } /** * Gets whether readability is enabled, with cache applied. * * @return bool True if readability is enabled, false otherwise. */ protected function get_is_readability_enabled() { if ( is_null( $this->is_readability_enabled ) ) { $this->is_readability_enabled = ( new WPSEO_Metabox_Analysis_Readability() )->is_enabled(); } return $this->is_readability_enabled; } /** * Returns the indexable for the current WordPress page, with cache applied. * * @return bool|Indexable The indexable, false if none could be found. */ protected function get_current_indexable() { if ( is_null( $this->current_indexable ) ) { $this->current_indexable = $this->indexable_repository->for_current_page(); } return $this->current_indexable; } /** * 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 ) { // On block editor pages, the admin bar only shows on mobile, where having this menu icon is not very helpful. if ( is_admin() ) { $screen = get_current_screen(); if ( isset( $screen ) && $screen->is_block_editor() ) { 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->add_root_menu( $wp_admin_bar ); /** * Adds a submenu item in the top of the adminbar. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * @param string $menu_identifier The menu identifier. */ do_action( 'wpseo_add_adminbar_submenu', $wp_admin_bar, self::MENU_IDENTIFIER ); if ( ! is_admin() ) { if ( is_singular() || is_tag() || is_tax() || is_category() ) { $is_seo_enabled = $this->get_is_seo_enabled(); $is_readability_enabled = $this->get_is_readability_enabled(); $indexable = $this->get_current_indexable(); if ( $is_seo_enabled ) { $focus_keyword = ( ! is_a( $indexable, 'Yoast\WP\SEO\Models\Indexable' ) || is_null( $indexable->primary_focus_keyword ) ) ? __( 'not set', 'wordpress-seo' ) : $indexable->primary_focus_keyword; $wp_admin_bar->add_menu( [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-seo-focus-keyword', 'title' => __( 'Focus keyphrase: ', 'wordpress-seo' ) . '' . $focus_keyword . '', 'meta' => [ 'tabindex' => '0' ], ] ); $wp_admin_bar->add_menu( [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-seo-score', 'title' => __( 'SEO score', 'wordpress-seo' ) . ': ' . $this->score_icon_helper->for_seo( $indexable, 'adminbar-sub-menu-score' )->present(), 'meta' => [ 'tabindex' => '0' ], ] ); } if ( $is_readability_enabled ) { $wp_admin_bar->add_menu( [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-readability-score', 'title' => __( 'Readability', 'wordpress-seo' ) . ': ' . $this->score_icon_helper->for_readability( $indexable->readability_score, 'adminbar-sub-menu-score' )->present(), 'meta' => [ 'tabindex' => '0' ], ] ); } if ( ! $this->product_helper->is_premium() ) { $wp_admin_bar->add_menu( [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-frontend-inspector', 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-frontend-inspector' ), 'title' => __( 'Front-end SEO inspector', 'wordpress-seo' ) . new Premium_Badge_Presenter( 'wpseo-frontend-inspector-badge' ), 'meta' => [ 'tabindex' => '0', 'target' => '_blank', ], ] ); } } $this->add_analysis_submenu( $wp_admin_bar ); $this->add_seo_tools_submenu( $wp_admin_bar ); $this->add_how_to_submenu( $wp_admin_bar ); $this->add_get_help_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 ); } if ( ! $this->product_helper->is_premium() ) { $this->add_premium_link( $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', [ $this, 'add_menu' ], 95 ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); add_action( 'admin_enqueue_scripts', [ $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 = ''; $notification_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(); $notification_popup = $this->get_notification_popup(); } $admin_bar_menu_args = [ 'id' => self::MENU_IDENTIFIER, 'title' => $title . $score . $counter . $notification_popup, 'href' => $settings_url, 'meta' => [ 'tabindex' => ! empty( $settings_url ) ? false : '0' ], ]; $wp_admin_bar->add_menu( $admin_bar_menu_args ); if ( ! empty( $counter ) ) { $admin_bar_menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-notifications', 'title' => __( 'Notifications', 'wordpress-seo' ) . $counter, 'href' => $settings_url, 'meta' => [ 'tabindex' => ! empty( $settings_url ) ? false : '0' ], ]; $wp_admin_bar->add_menu( $admin_bar_menu_args ); } } /** * 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 ) { try { $url = YoastSEO()->meta->for_current_page()->canonical; } catch ( Exception $e ) { // This is not the type of error we can handle here. return; } if ( ! $url ) { return; } $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => self::ANALYSIS_SUBMENU_IDENTIFIER, 'title' => __( 'Analyze this page', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; $wp_admin_bar->add_menu( $menu_args ); $encoded_url = rawurlencode( $url ); $submenu_items = [ [ 'id' => 'wpseo-inlinks', 'title' => __( 'Check links to this URL', 'wordpress-seo' ), 'href' => 'https://search.google.com/search-console/links/drilldown?resource_id=' . rawurlencode( get_option( 'siteurl' ) ) . '&type=EXTERNAL&target=' . $encoded_url . '&domain=', ], [ 'id' => 'wpseo-structureddata', 'title' => __( 'Google Rich Results Test', 'wordpress-seo' ), 'href' => 'https://search.google.com/test/rich-results?url=' . $encoded_url, ], [ 'id' => 'wpseo-facebookdebug', 'title' => __( 'Facebook Debugger', 'wordpress-seo' ), 'href' => '//developers.facebook.com/tools/debug/?q=' . $encoded_url, ], [ 'id' => 'wpseo-pagespeed', 'title' => __( 'Google Page Speed Test', 'wordpress-seo' ), 'href' => '//developers.google.com/speed/pagespeed/insights/?url=' . $encoded_url, ], [ 'id' => 'wpseo-google-mobile-friendly', 'title' => __( 'Mobile-Friendly Test', 'wordpress-seo' ), 'href' => 'https://www.google.com/webmasters/tools/mobile-friendly/?url=' . $encoded_url, ], ]; $this->add_submenu_items( $submenu_items, $wp_admin_bar, self::ANALYSIS_SUBMENU_IDENTIFIER ); } /** * Adds the admin bar tools submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_seo_tools_submenu( WP_Admin_Bar $wp_admin_bar ) { $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-sub-tools', 'title' => __( 'SEO Tools', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; $wp_admin_bar->add_menu( $menu_args ); $submenu_items = [ [ 'id' => 'wpseo-semrush', 'title' => 'Semrush', 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-semrush' ), ], [ 'id' => 'wpseo-wincher', 'title' => 'Wincher', 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-wincher' ), ], [ 'id' => 'wpseo-google-trends', 'title' => 'Google trends', 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-gtrends' ), ], ]; $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-tools' ); } /** * Adds the admin bar How To submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_how_to_submenu( WP_Admin_Bar $wp_admin_bar ) { $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-sub-howto', 'title' => __( 'How to', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; $wp_admin_bar->add_menu( $menu_args ); $submenu_items = [ [ 'id' => 'wpseo-learn-seo', 'title' => __( 'Learn more SEO', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-learn-more-seo' ), ], [ 'id' => 'wpseo-improve-blogpost', 'title' => __( 'Improve your blog post', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-improve-blog-post' ), ], [ 'id' => 'wpseo-write-better-content', 'title' => __( 'Write better content', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-write-better' ), ], ]; $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-howto' ); } /** * Adds the admin bar How To submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_get_help_submenu( WP_Admin_Bar $wp_admin_bar ) { $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-sub-get-help', 'title' => __( 'Help', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; if ( current_user_can( Support_Integration::CAPABILITY ) ) { $menu_args['href'] = admin_url( 'admin.php?page=' . Support_Integration::PAGE ); $wp_admin_bar->add_menu( $menu_args ); return; } $wp_admin_bar->add_menu( $menu_args ); $submenu_items = [ [ 'id' => 'wpseo-yoast-help', 'title' => __( 'Yoast.com help section', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-yoast-help' ), ], [ 'id' => 'wpseo-premium-support', 'title' => __( 'Yoast Premium support', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-premium-support' ), ], [ 'id' => 'wpseo-wp-support-forums', 'title' => __( 'WordPress.org support forums', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-wp-support-forums' ), ], [ 'id' => 'wpseo-learn-seo-2', 'title' => __( 'Learn more SEO', 'wordpress-seo' ), 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-learn-more-seo-help' ), ], ]; $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-get-help' ); } /** * Adds the admin bar How To submenu. * * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to. * * @return void */ protected function add_premium_link( WP_Admin_Bar $wp_admin_bar ) { $wp_admin_bar->add_menu( [ 'parent' => self::MENU_IDENTIFIER, 'id' => 'wpseo-get-premium', // Circumvent an issue in the WP admin bar API in order to pass `data` attributes. See https://core.trac.wordpress.org/ticket/38636. 'title' => sprintf( '%2$s »', $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-get-premium' ), __( 'Get Yoast SEO Premium', 'wordpress-seo' ) ), 'meta' => [ 'tabindex' => '0', ], ] ); } /** * 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(); $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => self::SETTINGS_SUBMENU_IDENTIFIER, 'title' => __( 'SEO Settings', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; $wp_admin_bar->add_menu( $menu_args ); foreach ( $submenu_pages as $submenu_page ) { if ( ! current_user_can( $submenu_page[3] ) ) { continue; } // Don't add the Google Search Console menu item. if ( $submenu_page[4] === 'wpseo_search_console' ) { continue; } $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) ); if ( $id === 'wpseo-dashboard' ) { $id = 'wpseo-general'; } $menu_args = [ 'parent' => self::SETTINGS_SUBMENU_IDENTIFIER, 'id' => $id, 'title' => $submenu_page[2], 'href' => admin_url( 'admin.php?page=' . rawurlencode( $submenu_page[4] ) ), ]; $wp_admin_bar->add_menu( $menu_args ); } } /** * 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(); $menu_args = [ 'parent' => self::MENU_IDENTIFIER, 'id' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER, 'title' => __( 'SEO Settings', 'wordpress-seo' ), 'meta' => [ 'tabindex' => '0' ], ]; $wp_admin_bar->add_menu( $menu_args ); 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'; } $menu_args = [ 'parent' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER, 'id' => $id, 'title' => $submenu_page[2], 'href' => network_admin_url( 'admin.php?page=' . rawurlencode( $submenu_page[4] ) ), ]; $wp_admin_bar->add_menu( $menu_args ); } } /** * Gets the menu title markup. * * @return string Admin bar title markup. */ protected function get_title() { return '