PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-wpseo-admin-bar-menu.php

class-wpseo-admin-bar-menu.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at inc/class-wpseo-admin-bar-menu.php

979 lines 28.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO
6 */
7
8 use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
9 use Yoast\WP\SEO\Helpers\Product_Helper;
10 use Yoast\WP\SEO\Helpers\Score_Icon_Helper;
11 use Yoast\WP\SEO\Integrations\Admin\Brand_Insights_Page;
12 use Yoast\WP\SEO\Integrations\Support_Integration;
13 use Yoast\WP\SEO\Models\Indexable;
14 use Yoast\WP\SEO\Presenters\Admin\Premium_Badge_Presenter;
15 use Yoast\WP\SEO\Promotions\Application\Promotion_Manager;
16 use Yoast\WP\SEO\Repositories\Indexable_Repository;
17
18 /**
19 * Class for the Yoast SEO admin bar menu.
20 */
21 class WPSEO_Admin_Bar_Menu implements WPSEO_WordPress_Integration {
22
23 /**
24 * The identifier used for the menu.
25 *
26 * @var string
27 */
28 public const MENU_IDENTIFIER = 'wpseo-menu';
29
30 /**
31 * The identifier used for the Keyword Research submenu.
32 *
33 * @var string
34 */
35 public const KEYWORD_RESEARCH_SUBMENU_IDENTIFIER = 'wpseo-kwresearch';
36
37 /**
38 * The identifier used for the Analysis submenu.
39 *
40 * @var string
41 */
42 public const ANALYSIS_SUBMENU_IDENTIFIER = 'wpseo-analysis';
43
44 /**
45 * The identifier used for the Settings submenu.
46 *
47 * @var string
48 */
49 public const SETTINGS_SUBMENU_IDENTIFIER = 'wpseo-settings';
50
51 /**
52 * The identifier used for the Network Settings submenu.
53 *
54 * @var string
55 */
56 public const NETWORK_SETTINGS_SUBMENU_IDENTIFIER = 'wpseo-network-settings';
57
58 /**
59 * Asset manager instance.
60 *
61 * @var WPSEO_Admin_Asset_Manager
62 */
63 protected $asset_manager;
64
65 /**
66 * Holds the Score_Icon_Helper instance.
67 *
68 * @var Score_Icon_Helper
69 */
70 protected $indexable_repository;
71
72 /**
73 * Holds the Score_Icon_Helper instance.
74 *
75 * @var Score_Icon_Helper
76 */
77 protected $score_icon_helper;
78
79 /**
80 * Holds the Product_Helper instance.
81 *
82 * @var Product_Helper
83 */
84 protected $product_helper;
85
86 /**
87 * Holds the shortlinker instance.
88 *
89 * @var WPSEO_Shortlinker
90 */
91 protected $shortlinker;
92
93 /**
94 * Whether SEO Score is enabled.
95 *
96 * @var bool|null
97 */
98 protected $is_seo_enabled = null;
99
100 /**
101 * Whether readability is enabled.
102 *
103 * @var bool|null
104 */
105 protected $is_readability_enabled = null;
106
107 /**
108 * The indexable for the current WordPress page, if found.
109 *
110 * @var Indexable|bool|null
111 */
112 protected $current_indexable = null;
113
114 /**
115 * Constructs the WPSEO_Admin_Bar_Menu.
116 *
117 * @param WPSEO_Admin_Asset_Manager|null $asset_manager Optional. Asset manager to use.
118 * @param Indexable_Repository|null $indexable_repository Optional. The Indexable_Repository.
119 * @param Score_Icon_Helper|null $score_icon_helper Optional. The Score_Icon_Helper.
120 * @param Product_Helper|null $product_helper Optional. The product helper.
121 * @param WPSEO_Shortlinker|null $shortlinker The shortlinker.
122 */
123 public function __construct(
124 ?WPSEO_Admin_Asset_Manager $asset_manager = null,
125 ?Indexable_Repository $indexable_repository = null,
126 ?Score_Icon_Helper $score_icon_helper = null,
127 ?Product_Helper $product_helper = null,
128 ?WPSEO_Shortlinker $shortlinker = null
129 ) {
130 if ( ! $asset_manager ) {
131 $asset_manager = new WPSEO_Admin_Asset_Manager();
132 }
133 if ( ! $indexable_repository ) {
134 $indexable_repository = YoastSEO()->classes->get( Indexable_Repository::class );
135 }
136 if ( ! $score_icon_helper ) {
137 $score_icon_helper = YoastSEO()->helpers->score_icon;
138 }
139 if ( ! $product_helper ) {
140 $product_helper = YoastSEO()->helpers->product;
141 }
142 if ( ! $shortlinker ) {
143 $shortlinker = new WPSEO_Shortlinker();
144 }
145
146 $this->product_helper = $product_helper;
147 $this->asset_manager = $asset_manager;
148 $this->indexable_repository = $indexable_repository;
149 $this->score_icon_helper = $score_icon_helper;
150 $this->shortlinker = $shortlinker;
151 }
152
153 /**
154 * Gets whether SEO score is enabled, with cache applied.
155 *
156 * @return bool True if SEO score is enabled, false otherwise.
157 */
158 protected function get_is_seo_enabled() {
159 $this->is_seo_enabled ??= ( new WPSEO_Metabox_Analysis_SEO() )->is_enabled();
160
161 return $this->is_seo_enabled;
162 }
163
164 /**
165 * Gets whether readability is enabled, with cache applied.
166 *
167 * @return bool True if readability is enabled, false otherwise.
168 */
169 protected function get_is_readability_enabled() {
170 $this->is_readability_enabled ??= ( new WPSEO_Metabox_Analysis_Readability() )->is_enabled();
171
172 return $this->is_readability_enabled;
173 }
174
175 /**
176 * Returns the indexable for the current WordPress page, with cache applied.
177 *
178 * @return bool|Indexable The indexable, false if none could be found.
179 */
180 protected function get_current_indexable() {
181 $this->current_indexable ??= $this->indexable_repository->for_current_page();
182
183 return $this->current_indexable;
184 }
185
186 /**
187 * Adds the admin bar menu.
188 *
189 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
190 *
191 * @return void
192 */
193 public function add_menu( WP_Admin_Bar $wp_admin_bar ) {
194 // On block editor pages, the admin bar only shows on mobile, where having this menu icon is not very helpful.
195 if ( is_admin() ) {
196 $screen = get_current_screen();
197 if ( isset( $screen ) && $screen->is_block_editor() ) {
198 return;
199 }
200 }
201
202 // If the current user can't write posts, this is all of no use, so let's not output an admin menu.
203 if ( ! current_user_can( 'edit_posts' ) ) {
204 return;
205 }
206
207 $this->add_root_menu( $wp_admin_bar );
208
209 /**
210 * Adds a submenu item in the top of the adminbar.
211 *
212 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
213 * @param string $menu_identifier The menu identifier.
214 */
215 do_action( 'wpseo_add_adminbar_submenu', $wp_admin_bar, self::MENU_IDENTIFIER );
216
217 if ( ! is_admin() ) {
218
219 if ( is_singular() || is_tag() || is_tax() || is_category() ) {
220 $is_seo_enabled = $this->get_is_seo_enabled();
221 $is_readability_enabled = $this->get_is_readability_enabled();
222
223 $indexable = $this->get_current_indexable();
224
225 if ( $is_seo_enabled ) {
226 $focus_keyword = ( ! is_a( $indexable, 'Yoast\WP\SEO\Models\Indexable' ) || $indexable->primary_focus_keyword === null ) ? __( 'not set', 'wordpress-seo' ) : $indexable->primary_focus_keyword;
227
228 $wp_admin_bar->add_menu(
229 [
230 'parent' => self::MENU_IDENTIFIER,
231 'id' => 'wpseo-seo-focus-keyword',
232 'title' => __( 'Focus keyphrase: ', 'wordpress-seo' ) . '<span class="wpseo-focus-keyword">' . $focus_keyword . '</span>',
233 'meta' => [ 'tabindex' => '0' ],
234 ],
235 );
236 $wp_admin_bar->add_menu(
237 [
238 'parent' => self::MENU_IDENTIFIER,
239 'id' => 'wpseo-seo-score',
240 'title' => __( 'SEO score', 'wordpress-seo' ) . ': ' . $this->score_icon_helper->for_seo( $indexable, 'adminbar-sub-menu-score' )
241 ->present(),
242 'meta' => [ 'tabindex' => '0' ],
243 ],
244 );
245 }
246
247 if ( $is_readability_enabled ) {
248 $wp_admin_bar->add_menu(
249 [
250 'parent' => self::MENU_IDENTIFIER,
251 'id' => 'wpseo-readability-score',
252 'title' => __( 'Readability', 'wordpress-seo' ) . ': ' . $this->score_icon_helper->for_readability( $indexable->readability_score, 'adminbar-sub-menu-score' )
253 ->present(),
254 'meta' => [ 'tabindex' => '0' ],
255 ],
256 );
257 }
258
259 if ( ! $this->product_helper->is_premium() ) {
260 $wp_admin_bar->add_menu(
261 [
262 'parent' => self::MENU_IDENTIFIER,
263 'id' => 'wpseo-frontend-inspector',
264 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-frontend-inspector' ),
265 'title' => __( 'Front-end SEO inspector', 'wordpress-seo' ) . new Premium_Badge_Presenter( 'wpseo-frontend-inspector-badge' ),
266 'meta' => [
267 'tabindex' => '0',
268 'target' => '_blank',
269 ],
270 ],
271 );
272 }
273 }
274 $this->add_analysis_submenu( $wp_admin_bar );
275 $this->add_seo_tools_submenu( $wp_admin_bar );
276 $this->add_how_to_submenu( $wp_admin_bar );
277 $this->add_get_help_submenu( $wp_admin_bar );
278 }
279
280 if ( ! is_admin() || is_blog_admin() ) {
281 $this->add_settings_submenu( $wp_admin_bar );
282 }
283 elseif ( is_network_admin() ) {
284 $this->add_network_settings_submenu( $wp_admin_bar );
285 }
286
287 $this->add_premium_link( $wp_admin_bar );
288 $this->add_brand_insights_link( $wp_admin_bar );
289 }
290
291 /**
292 * Enqueues admin bar assets.
293 *
294 * @return void
295 */
296 public function enqueue_assets() {
297 if ( ! is_admin_bar_showing() ) {
298 return;
299 }
300
301 // If the current user can't write posts, this is all of no use, so let's not output an admin menu.
302 if ( ! current_user_can( 'edit_posts' ) ) {
303 return;
304 }
305
306 $this->asset_manager->register_assets();
307 $this->asset_manager->enqueue_style( 'adminbar' );
308 }
309
310 /**
311 * Registers the hooks.
312 *
313 * @return void
314 */
315 public function register_hooks() {
316 if ( ! $this->meets_requirements() ) {
317 return;
318 }
319
320 add_action( 'admin_bar_menu', [ $this, 'add_menu' ], 95 );
321
322 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
323 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
324 }
325
326 /**
327 * Checks whether the requirements to use this class are met.
328 *
329 * @return bool True if requirements are met, false otherwise.
330 */
331 public function meets_requirements() {
332 if ( is_network_admin() ) {
333 return WPSEO_Utils::is_plugin_network_active();
334 }
335
336 if ( WPSEO_Options::get( 'enable_admin_bar_menu' ) !== true ) {
337 return false;
338 }
339
340 return ! is_admin() || is_blog_admin();
341 }
342
343 /**
344 * Adds the admin bar root menu.
345 *
346 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
347 *
348 * @return void
349 */
350 protected function add_root_menu( WP_Admin_Bar $wp_admin_bar ) {
351 $title = $this->get_title();
352
353 $score = '';
354 $settings_url = '';
355 $counter = '';
356 $notification_popup = '';
357 $notification_count = 0;
358
359 $post = $this->get_singular_post();
360 if ( $post ) {
361 $score = $this->get_post_score( $post );
362 }
363
364 $term = $this->get_singular_term();
365 if ( $term ) {
366 $score = $this->get_term_score( $term );
367 }
368
369 $can_manage_options = $this->can_manage_options();
370
371 if ( $can_manage_options ) {
372 $settings_url = $this->get_settings_page_url();
373 }
374
375 if ( empty( $score ) && ! is_network_admin() && $can_manage_options ) {
376 $notification_center = Yoast_Notification_Center::get();
377 $notification_count = $notification_center->get_notification_count();
378
379 $counter = $this->get_notification_counter( $notification_count );
380 $notification_popup = $this->get_notification_popup();
381 }
382
383 $admin_bar_menu_args = [
384 'id' => self::MENU_IDENTIFIER,
385 'title' => $title . $score . $counter . $notification_popup,
386 'href' => $settings_url,
387 'meta' => [ 'tabindex' => ! empty( $settings_url ) ? false : '0' ],
388 ];
389 $wp_admin_bar->add_menu( $admin_bar_menu_args );
390
391 if ( $notification_count > 0 ) {
392 $admin_bar_menu_args = [
393 'parent' => self::MENU_IDENTIFIER,
394 'id' => 'wpseo-notifications',
395 'title' => __( 'Notifications', 'wordpress-seo' ) . $counter,
396 'href' => empty( $settings_url ) ? '' : $settings_url . '#/alert-center',
397 'meta' => [ 'tabindex' => ! empty( $settings_url ) ? false : '0' ],
398 ];
399 $wp_admin_bar->add_menu( $admin_bar_menu_args );
400 }
401 }
402
403 /**
404 * Adds the admin bar analysis submenu.
405 *
406 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
407 *
408 * @return void
409 */
410 protected function add_analysis_submenu( WP_Admin_Bar $wp_admin_bar ) {
411 try {
412 $url = YoastSEO()->meta->for_current_page()->canonical;
413 } catch ( Exception $e ) {
414 // This is not the type of error we can handle here.
415 return;
416 }
417
418 if ( ! $url ) {
419 return;
420 }
421
422 $menu_args = [
423 'parent' => self::MENU_IDENTIFIER,
424 'id' => self::ANALYSIS_SUBMENU_IDENTIFIER,
425 'title' => __( 'Analyze this page', 'wordpress-seo' ),
426 'meta' => [ 'tabindex' => '0' ],
427 ];
428 $wp_admin_bar->add_menu( $menu_args );
429
430 $encoded_url = rawurlencode( $url );
431 $submenu_items = [
432 [
433 'id' => 'wpseo-inlinks',
434 'title' => __( 'Check links to this URL', 'wordpress-seo' ),
435 'href' => 'https://search.google.com/search-console/links/drilldown?resource_id=' . rawurlencode( get_option( 'siteurl' ) ) . '&type=EXTERNAL&target=' . $encoded_url . '&domain=',
436 ],
437 [
438 'id' => 'wpseo-structureddata',
439 'title' => __( 'Google Rich Results Test', 'wordpress-seo' ),
440 'href' => 'https://search.google.com/test/rich-results?url=' . $encoded_url,
441 ],
442 [
443 'id' => 'wpseo-facebookdebug',
444 'title' => __( 'Facebook Debugger', 'wordpress-seo' ),
445 'href' => '//developers.facebook.com/tools/debug/?q=' . $encoded_url,
446 ],
447 [
448 'id' => 'wpseo-pagespeed',
449 'title' => __( 'Google Page Speed Test', 'wordpress-seo' ),
450 'href' => '//developers.google.com/speed/pagespeed/insights/?url=' . $encoded_url,
451 ],
452 ];
453
454 $this->add_submenu_items( $submenu_items, $wp_admin_bar, self::ANALYSIS_SUBMENU_IDENTIFIER );
455 }
456
457 /**
458 * Adds the admin bar tools submenu.
459 *
460 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
461 *
462 * @return void
463 */
464 protected function add_seo_tools_submenu( WP_Admin_Bar $wp_admin_bar ) {
465 $menu_args = [
466 'parent' => self::MENU_IDENTIFIER,
467 'id' => 'wpseo-sub-tools',
468 'title' => __( 'SEO Tools', 'wordpress-seo' ),
469 'meta' => [ 'tabindex' => '0' ],
470 ];
471 $wp_admin_bar->add_menu( $menu_args );
472
473 $submenu_items = [
474 [
475 'id' => 'wpseo-semrush',
476 'title' => 'Semrush',
477 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-semrush' ),
478 ],
479 [
480 'id' => 'wpseo-wincher',
481 'title' => 'Wincher',
482 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-wincher' ),
483 ],
484 [
485 'id' => 'wpseo-google-trends',
486 'title' => 'Google trends',
487 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-gtrends' ),
488 ],
489 ];
490
491 $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-tools' );
492 }
493
494 /**
495 * Adds the admin bar How To submenu.
496 *
497 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
498 *
499 * @return void
500 */
501 protected function add_how_to_submenu( WP_Admin_Bar $wp_admin_bar ) {
502 $menu_args = [
503 'parent' => self::MENU_IDENTIFIER,
504 'id' => 'wpseo-sub-howto',
505 'title' => __( 'How to', 'wordpress-seo' ),
506 'meta' => [ 'tabindex' => '0' ],
507 ];
508 $wp_admin_bar->add_menu( $menu_args );
509
510 $submenu_items = [
511 [
512 'id' => 'wpseo-learn-seo',
513 'title' => __( 'Learn more SEO', 'wordpress-seo' ),
514 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-learn-more-seo' ),
515 ],
516 [
517 'id' => 'wpseo-improve-blogpost',
518 'title' => __( 'Improve your blog post', 'wordpress-seo' ),
519 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-improve-blog-post' ),
520 ],
521 [
522 'id' => 'wpseo-write-better-content',
523 'title' => __( 'Write better content', 'wordpress-seo' ),
524 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-write-better' ),
525 ],
526 ];
527
528 $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-howto' );
529 }
530
531 /**
532 * Adds the admin bar How To submenu.
533 *
534 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
535 *
536 * @return void
537 */
538 protected function add_get_help_submenu( WP_Admin_Bar $wp_admin_bar ) {
539 $menu_args = [
540 'parent' => self::MENU_IDENTIFIER,
541 'id' => 'wpseo-sub-get-help',
542 'title' => __( 'Help', 'wordpress-seo' ),
543 'meta' => [ 'tabindex' => '0' ],
544 ];
545
546 if ( current_user_can( Support_Integration::CAPABILITY ) ) {
547 $menu_args['href'] = admin_url( 'admin.php?page=' . Support_Integration::PAGE );
548 $wp_admin_bar->add_menu( $menu_args );
549
550 return;
551 }
552 $wp_admin_bar->add_menu( $menu_args );
553
554 $submenu_items = [
555 [
556 'id' => 'wpseo-yoast-help',
557 'title' => __( 'Yoast.com help section', 'wordpress-seo' ),
558 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-yoast-help' ),
559 ],
560 [
561 'id' => 'wpseo-premium-support',
562 'title' => __( 'Yoast Premium support', 'wordpress-seo' ),
563 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-premium-support' ),
564 ],
565 [
566 'id' => 'wpseo-wp-support-forums',
567 'title' => __( 'WordPress.org support forums', 'wordpress-seo' ),
568 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-wp-support-forums' ),
569 ],
570 [
571 'id' => 'wpseo-learn-seo-2',
572 'title' => __( 'Learn more SEO', 'wordpress-seo' ),
573 'href' => $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-learn-more-seo-help' ),
574 ],
575 ];
576
577 $this->add_submenu_items( $submenu_items, $wp_admin_bar, 'wpseo-sub-get-help' );
578 }
579
580 /**
581 * Adds the admin bar How To submenu.
582 *
583 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
584 *
585 * @return void
586 */
587 protected function add_premium_link( WP_Admin_Bar $wp_admin_bar ) {
588 // Don't show the Upgrade button if Yoast SEO WooCommerce addon is active.
589 $addon_manager = new WPSEO_Addon_Manager();
590 if ( $addon_manager->is_installed( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) ) {
591 return;
592 }
593
594 $has_woocommerce = ( new Woocommerce_Conditional() )->is_met();
595
596 // Don't show the Upgrade button if Premium is active without the WooCommerce plugin.
597 if ( $this->product_helper->is_premium() && ! $has_woocommerce ) {
598 return;
599 }
600
601 $link = $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-get-premium' );
602
603 if ( $has_woocommerce ) {
604 $link = $this->shortlinker->build_shortlink( 'https://yoa.st/admin-bar-get-premium-woocommerce' );
605 }
606
607 $button_label = esc_html__( 'Upgrade', 'wordpress-seo' );
608
609 if ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-promotion' ) ) {
610 $button_label = esc_html__( '30% off - BF Sale', 'wordpress-seo' );
611 }
612 $wp_admin_bar->add_menu(
613 [
614 'parent' => self::MENU_IDENTIFIER,
615 'id' => 'wpseo-get-premium',
616 // Circumvent an issue in the WP admin bar API in order to pass `data` attributes. See https://core.trac.wordpress.org/ticket/38636.
617 'title' => sprintf(
618 '<a href="%1$s" target="_blank" data-action="load-nfd-ctb" data-ctb-id="f6a84663-465f-4cb5-8ba5-f7a6d72224b2">%2$s</a>',
619 esc_url( $link ),
620 $button_label,
621 ),
622 'meta' => [
623 'tabindex' => '0',
624 ],
625 ],
626 );
627 }
628
629 /**
630 * Adds the Brand Insights link to the admin bar.
631 *
632 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
633 *
634 * @return void
635 */
636 protected function add_brand_insights_link( WP_Admin_Bar $wp_admin_bar ) {
637 $page = $this->product_helper->is_premium() ? 'wpseo_brand_insights_premium' : 'wpseo_brand_insights';
638
639 $button_content = 'AI Brand Insights';
640
641 $menu_title = '<span class="yoast-brand-insights-gradient-border">'
642 . '<span class="yoast-brand-insights-content">'
643 . $button_content
644 . Brand_Insights_Page::EXTERNAL_LINK_ICON
645 . '</span></span>';
646
647 $wp_admin_bar->add_menu(
648 [
649 'parent' => self::MENU_IDENTIFIER,
650 'id' => $page,
651 'title' => $menu_title,
652 'href' => admin_url( 'admin.php?page=' . $page ),
653 'meta' => [
654 'tabindex' => '0',
655 'target' => '_blank',
656 ],
657 ],
658 );
659 }
660
661 /**
662 * Adds the admin bar settings submenu.
663 *
664 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
665 *
666 * @return void
667 */
668 protected function add_settings_submenu( WP_Admin_Bar $wp_admin_bar ) {
669 if ( ! $this->can_manage_options() ) {
670 return;
671 }
672
673 $admin_menu = new WPSEO_Admin_Menu( new WPSEO_Menu() );
674 $submenu_pages = $admin_menu->get_submenu_pages();
675
676 $menu_args = [
677 'parent' => self::MENU_IDENTIFIER,
678 'id' => self::SETTINGS_SUBMENU_IDENTIFIER,
679 'title' => __( 'SEO Settings', 'wordpress-seo' ),
680 'meta' => [ 'tabindex' => '0' ],
681 ];
682 $wp_admin_bar->add_menu( $menu_args );
683
684 foreach ( $submenu_pages as $submenu_page ) {
685 if ( ! current_user_can( $submenu_page[3] ) ) {
686 continue;
687 }
688
689 // Don't add the Google Search Console menu item.
690 if ( $submenu_page[4] === 'wpseo_search_console' ) {
691 continue;
692 }
693
694 // Don't add the Brand Insights menu items (they're now in the main menu).
695 if ( $submenu_page[4] === 'wpseo_brand_insights' || $submenu_page[4] === 'wpseo_brand_insights_premium' ) {
696 continue;
697 }
698
699 $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) );
700 if ( $id === 'wpseo-dashboard' ) {
701 $id = 'wpseo-general';
702 }
703
704 $menu_args = [
705 'parent' => self::SETTINGS_SUBMENU_IDENTIFIER,
706 'id' => $id,
707 'title' => $submenu_page[2],
708 'href' => admin_url( 'admin.php?page=' . rawurlencode( $submenu_page[4] ) ),
709 ];
710 $wp_admin_bar->add_menu( $menu_args );
711 }
712 }
713
714 /**
715 * Adds the admin bar network settings submenu.
716 *
717 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
718 *
719 * @return void
720 */
721 protected function add_network_settings_submenu( WP_Admin_Bar $wp_admin_bar ) {
722 if ( ! $this->can_manage_options() ) {
723 return;
724 }
725
726 $network_admin_menu = new WPSEO_Network_Admin_Menu( new WPSEO_Menu() );
727 $submenu_pages = $network_admin_menu->get_submenu_pages();
728
729 $menu_args = [
730 'parent' => self::MENU_IDENTIFIER,
731 'id' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER,
732 'title' => __( 'SEO Settings', 'wordpress-seo' ),
733 'meta' => [ 'tabindex' => '0' ],
734 ];
735 $wp_admin_bar->add_menu( $menu_args );
736
737 foreach ( $submenu_pages as $submenu_page ) {
738 if ( ! current_user_can( $submenu_page[3] ) ) {
739 continue;
740 }
741
742 $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) );
743 if ( $id === 'wpseo-dashboard' ) {
744 $id = 'wpseo-general';
745 }
746
747 $menu_args = [
748 'parent' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER,
749 'id' => $id,
750 'title' => $submenu_page[2],
751 'href' => network_admin_url( 'admin.php?page=' . rawurlencode( $submenu_page[4] ) ),
752 ];
753 $wp_admin_bar->add_menu( $menu_args );
754 }
755 }
756
757 /**
758 * Gets the menu title markup.
759 *
760 * @return string Admin bar title markup.
761 */
762 protected function get_title() {
763 return '<div id="yoast-ab-icon" class="ab-item yoast-logo svg"><span class="screen-reader-text">' . __( 'SEO', 'wordpress-seo' ) . '</span></div>';
764 }
765
766 /**
767 * Gets the current post if in a singular post context.
768 *
769 * @global string $pagenow Current page identifier.
770 * @global WP_Post|null $post Current post object, or null if none available.
771 *
772 * @return WP_Post|null Post object, or null if not in singular context.
773 */
774 protected function get_singular_post() {
775 global $pagenow, $post;
776
777 if ( ! is_singular() && ( ! is_blog_admin() || ! WPSEO_Metabox::is_post_edit( $pagenow ) ) ) {
778 return null;
779 }
780
781 if ( ! isset( $post ) || ! is_object( $post ) || ! $post instanceof WP_Post ) {
782 return null;
783 }
784
785 return $post;
786 }
787
788 /**
789 * Gets the focus keyword for a given post.
790 *
791 * @param WP_Post $post Post object to get its focus keyword.
792 *
793 * @return string Focus keyword, or empty string if none available.
794 */
795 protected function get_post_focus_keyword( $post ) {
796 if ( ! is_object( $post ) || ! property_exists( $post, 'ID' ) ) {
797 return '';
798 }
799
800 /**
801 * Filter: 'wpseo_use_page_analysis' Determines if the analysis should be enabled.
802 *
803 * @param bool $enabled Determines if the analysis should be enabled.
804 */
805 if ( apply_filters( 'wpseo_use_page_analysis', true ) !== true ) {
806 return '';
807 }
808
809 return WPSEO_Meta::get_value( 'focuskw', $post->ID );
810 }
811
812 /**
813 * Gets the score for a given post.
814 *
815 * @param WP_Post $post Post object to get its score.
816 *
817 * @return string Score markup, or empty string if none available.
818 */
819 protected function get_post_score( $post ) {
820 if ( ! is_object( $post ) || ! property_exists( $post, 'ID' ) ) {
821 return '';
822 }
823
824 if ( apply_filters( 'wpseo_use_page_analysis', true ) !== true ) {
825 return '';
826 }
827
828 return $this->get_score_icon();
829 }
830
831 /**
832 * Gets the current term if in a singular term context.
833 *
834 * @global string $pagenow Current page identifier.
835 * @global WP_Query $wp_query Current query object.
836 * @global WP_Term|null $tag Current term object, or null if none available.
837 *
838 * @return WP_Term|null Term object, or null if not in singular context.
839 */
840 protected function get_singular_term() {
841 global $pagenow, $wp_query, $tag;
842
843 if ( is_category() || is_tag() || is_tax() ) {
844 return $wp_query->get_queried_object();
845 }
846
847 if ( WPSEO_Taxonomy::is_term_edit( $pagenow ) && ! WPSEO_Taxonomy::is_term_overview( $pagenow ) && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) {
848 return get_term( $tag->term_id );
849 }
850
851 return null;
852 }
853
854 /**
855 * Gets the score for a given term.
856 *
857 * @param WP_Term $term Term object to get its score.
858 *
859 * @return string Score markup, or empty string if none available.
860 */
861 protected function get_term_score( $term ) {
862 if ( ! is_object( $term ) || ! property_exists( $term, 'term_id' ) || ! property_exists( $term, 'taxonomy' ) ) {
863 return '';
864 }
865
866 return $this->get_score_icon();
867 }
868
869 /**
870 * Create the score icon.
871 *
872 * @return string The score icon, or empty string.
873 */
874 protected function get_score_icon() {
875 $is_seo_enabled = $this->get_is_seo_enabled();
876 $is_readability_enabled = $this->get_is_readability_enabled();
877
878 $indexable = $this->get_current_indexable();
879
880 if ( $is_seo_enabled ) {
881 return $this->score_icon_helper->for_seo( $indexable, 'adminbar-seo-score' )->present();
882 }
883
884 if ( $is_readability_enabled ) {
885 return $this->score_icon_helper->for_readability( $indexable->readability_score, 'adminbar-seo-score' )
886 ->present();
887 }
888
889 return '';
890 }
891
892 /**
893 * Gets the URL to the main admin settings page.
894 *
895 * @return string Admin settings page URL.
896 */
897 protected function get_settings_page_url() {
898 return self_admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER );
899 }
900
901 /**
902 * Gets the notification counter if in a valid context.
903 *
904 * @param int $notification_count Number of notifications.
905 *
906 * @return string Notification counter markup, or empty string if not available.
907 */
908 protected function get_notification_counter( $notification_count ) {
909 /* translators: Hidden accessibility text; %s: number of notifications. */
910 $counter_screen_reader_text = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
911
912 return sprintf(
913 ' <div class="wp-core-ui wp-ui-notification yoast-issue-counter%s"><span class="yoast-issues-count" aria-hidden="true">%d</span><span class="screen-reader-text">%s</span></div>',
914 ( $notification_count ) ? '' : ' wpseo-no-adminbar-notifications',
915 $notification_count,
916 $counter_screen_reader_text,
917 );
918 }
919
920 /**
921 * Gets the notification popup if in a valid context.
922 *
923 * @return string Notification popup markup, or empty string if not available.
924 */
925 protected function get_notification_popup() {
926 $notification_center = Yoast_Notification_Center::get();
927 $new_notifications = $notification_center->get_new_notifications();
928 $new_notifications_count = count( $new_notifications );
929
930 if ( ! $new_notifications_count ) {
931 return '';
932 }
933
934 $notification = sprintf(
935 _n(
936 'There is a new notification.',
937 'There are new notifications.',
938 $new_notifications_count,
939 'wordpress-seo',
940 ),
941 $new_notifications_count,
942 );
943
944 return '<div class="yoast-issue-added">' . $notification . '</div>';
945 }
946
947 /**
948 * Checks whether the current user can manage options in the current context.
949 *
950 * @return bool True if capabilities are sufficient, false otherwise.
951 */
952 protected function can_manage_options() {
953 return ( is_network_admin() && current_user_can( 'wpseo_manage_network_options' ) )
954 || ( ! is_network_admin() && WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) );
955 }
956
957 /**
958 * Add submenu items to a menu item.
959 *
960 * @param array<array{id: string, title: string, href: string}> $submenu_items Submenu items array.
961 * @param WP_Admin_Bar $wp_admin_bar Admin bar object.
962 * @param string $parent_id Parent menu item ID.
963 *
964 * @return void
965 */
966 protected function add_submenu_items( array $submenu_items, WP_Admin_Bar $wp_admin_bar, $parent_id ) {
967 foreach ( $submenu_items as $menu_item ) {
968 $menu_args = [
969 'parent' => $parent_id,
970 'id' => $menu_item['id'],
971 'title' => $menu_item['title'],
972 'href' => $menu_item['href'],
973 'meta' => [ 'target' => '_blank' ],
974 ];
975 $wp_admin_bar->add_menu( $menu_args );
976 }
977 }
978 }
979