class-wpseo-admin-bar-menu.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO
  6. */
  7. /**
  8. * Class for the Yoast SEO admin bar menu.
  9. */
  10. class WPSEO_Admin_Bar_Menu implements WPSEO_WordPress_Integration {
  11. /** The identifier used for the menu. */
  12. const MENU_IDENTIFIER = 'wpseo-menu';
  13. /** The identifier used for the Keyword Research submenu. */
  14. const KEYWORD_RESEARCH_SUBMENU_IDENTIFIER = 'wpseo-kwresearch';
  15. /** The identifier used for the Analysis submenu. */
  16. const ANALYSIS_SUBMENU_IDENTIFIER = 'wpseo-analysis';
  17. /** The identifier used for the Settings submenu. */
  18. const SETTINGS_SUBMENU_IDENTIFIER = 'wpseo-settings';
  19. /** The identifier used for the Network Settings submenu. */
  20. const NETWORK_SETTINGS_SUBMENU_IDENTIFIER = 'wpseo-network-settings';
  21. /** @var WPSEO_Admin_Asset_Manager Asset manager instance. */
  22. protected $asset_manager;
  23. /**
  24. * Constructor.
  25. *
  26. * Sets the asset manager to use.
  27. *
  28. * @param WPSEO_Admin_Asset_Manager $asset_manager Optional. Asset manager to use.
  29. */
  30. public function __construct( WPSEO_Admin_Asset_Manager $asset_manager = null ) {
  31. if ( ! $asset_manager ) {
  32. $asset_manager = new WPSEO_Admin_Asset_Manager();
  33. }
  34. $this->asset_manager = $asset_manager;
  35. }
  36. /**
  37. * Adds the admin bar menu.
  38. *
  39. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  40. *
  41. * @return void
  42. */
  43. public function add_menu( WP_Admin_Bar $wp_admin_bar ) {
  44. // If the current user can't write posts, this is all of no use, so let's not output an admin menu.
  45. if ( ! current_user_can( 'edit_posts' ) ) {
  46. return;
  47. }
  48. $this->add_root_menu( $wp_admin_bar );
  49. $this->add_keyword_research_submenu( $wp_admin_bar );
  50. if ( ! is_admin() ) {
  51. $this->add_analysis_submenu( $wp_admin_bar );
  52. }
  53. if ( ! is_admin() || is_blog_admin() ) {
  54. $this->add_settings_submenu( $wp_admin_bar );
  55. }
  56. elseif ( is_network_admin() ) {
  57. $this->add_network_settings_submenu( $wp_admin_bar );
  58. }
  59. }
  60. /**
  61. * Enqueues admin bar assets.
  62. *
  63. * @return void
  64. */
  65. public function enqueue_assets() {
  66. if ( ! is_admin_bar_showing() ) {
  67. return;
  68. }
  69. // If the current user can't write posts, this is all of no use, so let's not output an admin menu.
  70. if ( ! current_user_can( 'edit_posts' ) ) {
  71. return;
  72. }
  73. $this->asset_manager->register_assets();
  74. $this->asset_manager->enqueue_style( 'adminbar' );
  75. }
  76. /**
  77. * Registers the hooks.
  78. *
  79. * @return void
  80. */
  81. public function register_hooks() {
  82. if ( ! $this->meets_requirements() ) {
  83. return;
  84. }
  85. add_action( 'admin_bar_menu', array( $this, 'add_menu' ), 95 );
  86. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  87. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  88. }
  89. /**
  90. * Checks whether the requirements to use this class are met.
  91. *
  92. * @return bool True if requirements are met, false otherwise.
  93. */
  94. public function meets_requirements() {
  95. if ( is_network_admin() ) {
  96. return WPSEO_Utils::is_plugin_network_active();
  97. }
  98. if ( WPSEO_Options::get( 'enable_admin_bar_menu' ) !== true ) {
  99. return false;
  100. }
  101. return ! is_admin() || is_blog_admin();
  102. }
  103. /**
  104. * Adds the admin bar root menu.
  105. *
  106. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  107. *
  108. * @return void
  109. */
  110. protected function add_root_menu( WP_Admin_Bar $wp_admin_bar ) {
  111. $title = $this->get_title();
  112. $score = '';
  113. $settings_url = '';
  114. $counter = '';
  115. $alert_popup = '';
  116. $post = $this->get_singular_post();
  117. if ( $post ) {
  118. $score = $this->get_post_score( $post );
  119. }
  120. $term = $this->get_singular_term();
  121. if ( $term ) {
  122. $score = $this->get_term_score( $term );
  123. }
  124. $can_manage_options = $this->can_manage_options();
  125. if ( $can_manage_options ) {
  126. $settings_url = $this->get_settings_page_url();
  127. }
  128. if ( empty( $score ) && ! is_network_admin() && $can_manage_options ) {
  129. $counter = $this->get_notification_counter();
  130. $alert_popup = $this->get_notification_alert_popup();
  131. }
  132. $wp_admin_bar->add_menu( array(
  133. 'id' => self::MENU_IDENTIFIER,
  134. 'title' => $title . $score . $counter . $alert_popup,
  135. 'href' => $settings_url,
  136. 'meta' => array( 'tabindex' => ! empty( $settings_url ) ? false : '0' ),
  137. ) );
  138. if ( ! empty( $counter ) ) {
  139. $wp_admin_bar->add_menu( array(
  140. 'parent' => self::MENU_IDENTIFIER,
  141. 'id' => 'wpseo-notifications',
  142. 'title' => __( 'Notifications', 'wordpress-seo' ) . $counter,
  143. 'href' => $settings_url,
  144. 'meta' => array( 'tabindex' => ! empty( $settings_url ) ? false : '0' ),
  145. ) );
  146. }
  147. if ( ! is_network_admin() && $can_manage_options ) {
  148. $wp_admin_bar->add_menu( array(
  149. 'parent' => self::MENU_IDENTIFIER,
  150. 'id' => 'wpseo-configuration-wizard',
  151. 'title' => __( 'Configuration Wizard', 'wordpress-seo' ),
  152. 'href' => admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ),
  153. ) );
  154. }
  155. }
  156. /**
  157. * Adds the admin bar keyword research submenu.
  158. *
  159. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  160. *
  161. * @return void
  162. */
  163. protected function add_keyword_research_submenu( WP_Admin_Bar $wp_admin_bar ) {
  164. $adwords_url = 'https://adwords.google.com/keywordplanner';
  165. $trends_url = 'https://www.google.com/trends/explore';
  166. $seobook_url = 'http://tools.seobook.com/keyword-tools/seobook/';
  167. $post = $this->get_singular_post();
  168. if ( $post ) {
  169. $focus_keyword = $this->get_post_focus_keyword( $post );
  170. if ( ! empty( $focus_keyword ) ) {
  171. $trends_url .= '#q=' . urlencode( $focus_keyword );
  172. $seobook_url .= '?keyword=' . urlencode( $focus_keyword );
  173. }
  174. }
  175. $wp_admin_bar->add_menu( array(
  176. 'parent' => self::MENU_IDENTIFIER,
  177. 'id' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER,
  178. 'title' => __( 'Keyword Research', 'wordpress-seo' ),
  179. 'meta' => array( 'tabindex' => '0' ),
  180. ) );
  181. $wp_admin_bar->add_menu( array(
  182. 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER,
  183. 'id' => 'wpseo-kwresearchtraining',
  184. 'title' => __( 'Keyword research training', 'wordpress-seo' ),
  185. 'href' => WPSEO_Shortlinker::get( 'https://yoa.st/wp-admin-bar' ),
  186. 'meta' => array( 'target' => '_blank' ),
  187. ) );
  188. $wp_admin_bar->add_menu( array(
  189. 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER,
  190. 'id' => 'wpseo-adwordsexternal',
  191. 'title' => __( 'AdWords External', 'wordpress-seo' ),
  192. 'href' => $adwords_url,
  193. 'meta' => array( 'target' => '_blank' ),
  194. ) );
  195. $wp_admin_bar->add_menu( array(
  196. 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER,
  197. 'id' => 'wpseo-googleinsights',
  198. 'title' => __( 'Google Trends', 'wordpress-seo' ),
  199. 'href' => $trends_url,
  200. 'meta' => array( 'target' => '_blank' ),
  201. ) );
  202. $wp_admin_bar->add_menu( array(
  203. 'parent' => self::KEYWORD_RESEARCH_SUBMENU_IDENTIFIER,
  204. 'id' => 'wpseo-wordtracker',
  205. 'title' => __( 'SEO Book', 'wordpress-seo' ),
  206. 'href' => $seobook_url,
  207. 'meta' => array( 'target' => '_blank' ),
  208. ) );
  209. }
  210. /**
  211. * Adds the admin bar analysis submenu.
  212. *
  213. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  214. *
  215. * @return void
  216. */
  217. protected function add_analysis_submenu( WP_Admin_Bar $wp_admin_bar ) {
  218. $url = WPSEO_Frontend::get_instance()->canonical( false );
  219. $focus_keyword = '';
  220. if ( ! $url ) {
  221. return;
  222. }
  223. $post = $this->get_singular_post();
  224. if ( $post ) {
  225. $focus_keyword = $this->get_post_focus_keyword( $post );
  226. }
  227. $wp_admin_bar->add_menu( array(
  228. 'parent' => self::MENU_IDENTIFIER,
  229. 'id' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  230. 'title' => __( 'Analyze this page', 'wordpress-seo' ),
  231. 'meta' => array( 'tabindex' => '0' ),
  232. ) );
  233. $wp_admin_bar->add_menu( array(
  234. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  235. 'id' => 'wpseo-inlinks',
  236. 'title' => __( 'Check links to this URL', 'wordpress-seo' ),
  237. 'href' => 'https://search.google.com/search-console/links/drilldown?resource_id=' . urlencode( get_option( 'siteurl' ) ) . '&type=EXTERNAL&target=' . urlencode( $url ) . '&domain=',
  238. 'meta' => array( 'target' => '_blank' ),
  239. ) );
  240. $wp_admin_bar->add_menu( array(
  241. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  242. 'id' => 'wpseo-kwdensity',
  243. 'title' => __( 'Check Keyword Density', 'wordpress-seo' ),
  244. // HTTPS not available.
  245. 'href' => 'http://www.zippy.co.uk/keyworddensity/index.php?url=' . urlencode( $url ) . '&keyword=' . urlencode( $focus_keyword ),
  246. 'meta' => array( 'target' => '_blank' ),
  247. ) );
  248. $wp_admin_bar->add_menu( array(
  249. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  250. 'id' => 'wpseo-cache',
  251. 'title' => __( 'Check Google Cache', 'wordpress-seo' ),
  252. 'href' => '//webcache.googleusercontent.com/search?strip=1&q=cache:' . urlencode( $url ),
  253. 'meta' => array( 'target' => '_blank' ),
  254. ) );
  255. $wp_admin_bar->add_menu( array(
  256. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  257. 'id' => 'wpseo-header',
  258. 'title' => __( 'Check Headers', 'wordpress-seo' ),
  259. 'href' => '//quixapp.com/headers/?r=' . urlencode( $url ),
  260. 'meta' => array( 'target' => '_blank' ),
  261. ) );
  262. $wp_admin_bar->add_menu( array(
  263. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  264. 'id' => 'wpseo-structureddata',
  265. 'title' => __( 'Google Structured Data Test', 'wordpress-seo' ),
  266. 'href' => 'https://search.google.com/structured-data/testing-tool#url=' . urlencode( $url ),
  267. 'meta' => array( 'target' => '_blank' ),
  268. ) );
  269. $wp_admin_bar->add_menu( array(
  270. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  271. 'id' => 'wpseo-facebookdebug',
  272. 'title' => __( 'Facebook Debugger', 'wordpress-seo' ),
  273. 'href' => '//developers.facebook.com/tools/debug/og/object?q=' . urlencode( $url ),
  274. 'meta' => array( 'target' => '_blank' ),
  275. ) );
  276. $wp_admin_bar->add_menu( array(
  277. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  278. 'id' => 'wpseo-pinterestvalidator',
  279. 'title' => __( 'Pinterest Rich Pins Validator', 'wordpress-seo' ),
  280. 'href' => 'https://developers.pinterest.com/tools/url-debugger/?link=' . urlencode( $url ),
  281. 'meta' => array( 'target' => '_blank' ),
  282. ) );
  283. $wp_admin_bar->add_menu( array(
  284. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  285. 'id' => 'wpseo-htmlvalidation',
  286. 'title' => __( 'HTML Validator', 'wordpress-seo' ),
  287. 'href' => '//validator.w3.org/check?uri=' . urlencode( $url ),
  288. 'meta' => array( 'target' => '_blank' ),
  289. ) );
  290. $wp_admin_bar->add_menu( array(
  291. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  292. 'id' => 'wpseo-cssvalidation',
  293. 'title' => __( 'CSS Validator', 'wordpress-seo' ),
  294. 'href' => '//jigsaw.w3.org/css-validator/validator?uri=' . urlencode( $url ),
  295. 'meta' => array( 'target' => '_blank' ),
  296. ) );
  297. $wp_admin_bar->add_menu( array(
  298. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  299. 'id' => 'wpseo-pagespeed',
  300. 'title' => __( 'Google Page Speed Test', 'wordpress-seo' ),
  301. 'href' => '//developers.google.com/speed/pagespeed/insights/?url=' . urlencode( $url ),
  302. 'meta' => array( 'target' => '_blank' ),
  303. ) );
  304. $wp_admin_bar->add_menu( array(
  305. 'parent' => self::ANALYSIS_SUBMENU_IDENTIFIER,
  306. 'id' => 'wpseo-google-mobile-friendly',
  307. 'title' => __( 'Mobile-Friendly Test', 'wordpress-seo' ),
  308. 'href' => 'https://www.google.com/webmasters/tools/mobile-friendly/?url=' . urlencode( $url ),
  309. 'meta' => array( 'target' => '_blank' ),
  310. ) );
  311. }
  312. /**
  313. * Adds the admin bar settings submenu.
  314. *
  315. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  316. *
  317. * @return void
  318. */
  319. protected function add_settings_submenu( WP_Admin_Bar $wp_admin_bar ) {
  320. if ( ! $this->can_manage_options() ) {
  321. return;
  322. }
  323. $admin_menu = new WPSEO_Admin_Menu( new WPSEO_Menu() );
  324. $submenu_pages = $admin_menu->get_submenu_pages();
  325. $wp_admin_bar->add_menu( array(
  326. 'parent' => self::MENU_IDENTIFIER,
  327. 'id' => self::SETTINGS_SUBMENU_IDENTIFIER,
  328. 'title' => __( 'SEO Settings', 'wordpress-seo' ),
  329. 'meta' => array( 'tabindex' => '0' ),
  330. ) );
  331. foreach ( $submenu_pages as $submenu_page ) {
  332. if ( ! current_user_can( $submenu_page[3] ) ) {
  333. continue;
  334. }
  335. $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) );
  336. if ( $id === 'wpseo-dashboard' ) {
  337. $id = 'wpseo-general';
  338. }
  339. $wp_admin_bar->add_menu( array(
  340. 'parent' => self::SETTINGS_SUBMENU_IDENTIFIER,
  341. 'id' => $id,
  342. 'title' => $submenu_page[2],
  343. 'href' => admin_url( 'admin.php?page=' . urlencode( $submenu_page[4] ) ),
  344. ) );
  345. }
  346. }
  347. /**
  348. * Adds the admin bar network settings submenu.
  349. *
  350. * @param WP_Admin_Bar $wp_admin_bar Admin bar instance to add the menu to.
  351. *
  352. * @return void
  353. */
  354. protected function add_network_settings_submenu( WP_Admin_Bar $wp_admin_bar ) {
  355. if ( ! $this->can_manage_options() ) {
  356. return;
  357. }
  358. $network_admin_menu = new WPSEO_Network_Admin_Menu( new WPSEO_Menu() );
  359. $submenu_pages = $network_admin_menu->get_submenu_pages();
  360. $wp_admin_bar->add_menu( array(
  361. 'parent' => self::MENU_IDENTIFIER,
  362. 'id' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER,
  363. 'title' => __( 'SEO Settings', 'wordpress-seo' ),
  364. 'meta' => array( 'tabindex' => '0' ),
  365. ) );
  366. foreach ( $submenu_pages as $submenu_page ) {
  367. if ( ! current_user_can( $submenu_page[3] ) ) {
  368. continue;
  369. }
  370. $id = 'wpseo-' . str_replace( '_', '-', str_replace( 'wpseo_', '', $submenu_page[4] ) );
  371. if ( $id === 'wpseo-dashboard' ) {
  372. $id = 'wpseo-general';
  373. }
  374. $wp_admin_bar->add_menu( array(
  375. 'parent' => self::NETWORK_SETTINGS_SUBMENU_IDENTIFIER,
  376. 'id' => $id,
  377. 'title' => $submenu_page[2],
  378. 'href' => network_admin_url( 'admin.php?page=' . urlencode( $submenu_page[4] ) ),
  379. ) );
  380. }
  381. }
  382. /**
  383. * Gets the menu title markup.
  384. *
  385. * @return string Admin bar title markup.
  386. */
  387. protected function get_title() {
  388. return '<div id="yoast-ab-icon" class="ab-item yoast-logo svg"><span class="screen-reader-text">' . __( 'SEO', 'wordpress-seo' ) . '</span></div>';
  389. }
  390. /**
  391. * Gets the current post if in a singular post context.
  392. *
  393. * @global string $pagenow Current page identifier.
  394. * @global WP_Post|null $post Current post object, or null if none available.
  395. *
  396. * @return WP_Post|null Post object, or null if not in singular context.
  397. */
  398. protected function get_singular_post() {
  399. global $pagenow, $post;
  400. if ( ! is_singular() && ( ! is_blog_admin() || ! WPSEO_Metabox::is_post_edit( $pagenow ) ) ) {
  401. return null;
  402. }
  403. if ( ! isset( $post ) || ! is_object( $post ) ) {
  404. return null;
  405. }
  406. return $post;
  407. }
  408. /**
  409. * Gets the focus keyword for a given post.
  410. *
  411. * @param WP_Post $post Post object to get its focus keyword.
  412. *
  413. * @return string Focus keyword, or empty string if none available.
  414. */
  415. protected function get_post_focus_keyword( WP_Post $post ) {
  416. if ( apply_filters( 'wpseo_use_page_analysis', true ) !== true ) {
  417. return '';
  418. }
  419. return WPSEO_Meta::get_value( 'focuskw', $post->ID );
  420. }
  421. /**
  422. * Gets the score for a given post.
  423. *
  424. * @param WP_Post $post Post object to get its score.
  425. *
  426. * @return string Score markup, or empty string if none available.
  427. */
  428. protected function get_post_score( $post ) {
  429. if ( ! is_object( $post ) || ! property_exists( $post, 'ID' ) ) {
  430. return '';
  431. }
  432. if ( apply_filters( 'wpseo_use_page_analysis', true ) !== true ) {
  433. return '';
  434. }
  435. $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  436. $analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  437. if ( $analysis_seo->is_enabled() ) {
  438. return $this->get_score( WPSEO_Meta::get_value( 'linkdex', $post->ID ) );
  439. }
  440. if ( $analysis_readability->is_enabled() ) {
  441. return $this->get_score( WPSEO_Meta::get_value( 'content_score', $post->ID ) );
  442. }
  443. return '';
  444. }
  445. /**
  446. * Gets the current term if in a singular term context.
  447. *
  448. * @global string $pagenow Current page identifier.
  449. * @global WP_Query $wp_query Current query object.
  450. * @global WP_Term|null $tag Current term object, or null if none available.
  451. *
  452. * @return WP_Term|null Term object, or null if not in singular context.
  453. */
  454. protected function get_singular_term() {
  455. global $pagenow, $wp_query, $tag;
  456. if ( is_category() || is_tag() || is_tax() ) {
  457. return $wp_query->get_queried_object();
  458. }
  459. if ( WPSEO_Taxonomy::is_term_edit( $pagenow ) && ! WPSEO_Taxonomy::is_term_overview( $pagenow ) && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) ) {
  460. return get_term( $tag->term_id );
  461. }
  462. return null;
  463. }
  464. /**
  465. * Gets the score for a given term.
  466. *
  467. * @param WP_Term $term Term object to get its score.
  468. *
  469. * @return string Score markup, or empty string if none available.
  470. */
  471. protected function get_term_score( $term ) {
  472. if ( ! is_object( $term ) || ! property_exists( $term, 'term_id' ) || ! property_exists( $term, 'taxonomy' ) ) {
  473. return '';
  474. }
  475. $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  476. $analysis_readability = new WPSEO_Metabox_Analysis_Readability();
  477. if ( $analysis_seo->is_enabled() ) {
  478. return $this->get_score( WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'linkdex' ) );
  479. }
  480. if ( $analysis_readability->is_enabled() ) {
  481. return $this->get_score( WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'content_score' ) );
  482. }
  483. return '';
  484. }
  485. /**
  486. * Takes the SEO score and makes the score icon for the admin bar for it.
  487. *
  488. * @param int $score The 0-100 rating of the score. Can be either SEO score or content score.
  489. *
  490. * @return string Score markup.
  491. */
  492. protected function get_score( $score ) {
  493. $score = WPSEO_Utils::translate_score( $score );
  494. $score_adminbar_element = '<div class="wpseo-score-icon adminbar-seo-score ' . $score . '"><span class="adminbar-seo-score-text screen-reader-text"></span></div>';
  495. return $score_adminbar_element;
  496. }
  497. /**
  498. * Gets the URL to the main admin settings page.
  499. *
  500. * @return string Admin settings page URL.
  501. */
  502. protected function get_settings_page_url() {
  503. return self_admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER );
  504. }
  505. /**
  506. * Gets the notification counter if in a valid context.
  507. *
  508. * @return string Notification counter markup, or empty string if not available.
  509. */
  510. protected function get_notification_counter() {
  511. $notification_center = Yoast_Notification_Center::get();
  512. $notification_count = $notification_center->get_notification_count();
  513. if ( ! $notification_count ) {
  514. return '';
  515. }
  516. /* translators: %s: number of notifications */
  517. $counter_screen_reader_text = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
  518. return sprintf( ' <div class="wp-core-ui wp-ui-notification yoast-issue-counter"><span aria-hidden="true">%d</span><span class="screen-reader-text">%s</span></div>', $notification_count, $counter_screen_reader_text );
  519. }
  520. /**
  521. * Gets the notification alert popup if in a valid context.
  522. *
  523. * @return string Notification alert popup markup, or empty string if not available.
  524. */
  525. protected function get_notification_alert_popup() {
  526. $notification_center = Yoast_Notification_Center::get();
  527. $new_notifications = $notification_center->get_new_notifications();
  528. $new_notifications_count = count( $new_notifications );
  529. if ( ! $new_notifications_count ) {
  530. return '';
  531. }
  532. $notification = sprintf(
  533. _n(
  534. 'There is a new notification.',
  535. 'There are new notifications.',
  536. $new_notifications_count,
  537. 'wordpress-seo'
  538. ),
  539. $new_notifications_count
  540. );
  541. return '<div class="yoast-issue-added">' . $notification . '</div>';
  542. }
  543. /**
  544. * Checks whether the current user can manage options in the current context.
  545. *
  546. * @return bool True if capabilities are sufficient, false otherwise.
  547. */
  548. protected function can_manage_options() {
  549. return is_network_admin() && current_user_can( 'wpseo_manage_network_options' ) || ! is_network_admin() && WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' );
  550. }
  551. }