class-yoast-dashboard-widget.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class to change or add WordPress dashboard widgets
  9. */
  10. class Yoast_Dashboard_Widget {
  11. const CACHE_TRANSIENT_KEY = 'wpseo-dashboard-totals';
  12. /**
  13. * @var WPSEO_Admin_Asset_Manager
  14. */
  15. protected $asset_manager;
  16. /**
  17. * @var WPSEO_Statistics
  18. */
  19. protected $statistics;
  20. /**
  21. * @param WPSEO_Statistics $statistics The statistics class to retrieve statistics from.
  22. */
  23. public function __construct( WPSEO_Statistics $statistics = null ) {
  24. if ( null === $statistics ) {
  25. $statistics = new WPSEO_Statistics();
  26. }
  27. $this->statistics = $statistics;
  28. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  29. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dashboard_assets' ) );
  30. add_action( 'admin_init', array( $this, 'queue_dashboard_widget' ) );
  31. }
  32. /**
  33. * Adds the dashboard widget if it should be shown.
  34. *
  35. * @return void
  36. */
  37. public function queue_dashboard_widget() {
  38. if ( $this->show_widget() ) {
  39. add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
  40. }
  41. }
  42. /**
  43. * Adds dashboard widget to WordPress
  44. */
  45. public function add_dashboard_widget() {
  46. add_filter( 'postbox_classes_dashboard_wpseo-dashboard-overview', array( $this, 'wpseo_dashboard_overview_class' ) );
  47. wp_add_dashboard_widget(
  48. 'wpseo-dashboard-overview',
  49. /* translators: %s is the plugin name */
  50. sprintf( __( '%s Posts Overview', 'wordpress-seo' ), 'Yoast SEO' ),
  51. array( $this, 'display_dashboard_widget' )
  52. );
  53. }
  54. /**
  55. * Adds CSS classes to the dashboard widget.
  56. *
  57. * @param array $classes An array of postbox CSS classes.
  58. *
  59. * @return array
  60. */
  61. public function wpseo_dashboard_overview_class( $classes ) {
  62. $classes[] = 'yoast wpseo-dashboard-overview';
  63. return $classes;
  64. }
  65. /**
  66. * Displays the dashboard widget.
  67. */
  68. public function display_dashboard_widget() {
  69. echo '<div id="yoast-seo-dashboard-widget"></div>';
  70. }
  71. /**
  72. * Enqueues stylesheet for the dashboard if the current page is the dashboard.
  73. */
  74. public function enqueue_dashboard_stylesheets() {
  75. _deprecated_function( __METHOD__, 'WPSEO 5.5', 'This method is deprecated, please use the <code>enqueue_dashboard_assets</code> method.' );
  76. if ( ! $this->is_dashboard_screen() ) {
  77. return;
  78. }
  79. $this->asset_manager->enqueue_style( 'wp-dashboard' );
  80. }
  81. /**
  82. * Enqueues assets for the dashboard if the current page is the dashboard.
  83. */
  84. public function enqueue_dashboard_assets() {
  85. if ( ! $this->is_dashboard_screen() ) {
  86. return;
  87. }
  88. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'dashboard-widget', 'wpseoDashboardWidgetL10n', $this->localize_dashboard_script() );
  89. $yoast_components_l10n = new WPSEO_Admin_Asset_Yoast_Components_L10n();
  90. $yoast_components_l10n->localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'dashboard-widget' );
  91. $this->asset_manager->enqueue_script( 'dashboard-widget' );
  92. $this->asset_manager->enqueue_style( 'wp-dashboard' );
  93. }
  94. /**
  95. * Translates strings used in the dashboard widget.
  96. *
  97. * @return array The translated strings.
  98. */
  99. public function localize_dashboard_script() {
  100. return array(
  101. 'feed_header' => sprintf(
  102. /* translators: %1$s resolves to Yoast.com */
  103. __( 'Latest blog posts on %1$s', 'wordpress-seo' ),
  104. 'Yoast.com'
  105. ),
  106. 'feed_footer' => __( 'Read more like this on our SEO blog', 'wordpress-seo' ),
  107. 'ryte_header' => sprintf(
  108. /* translators: %1$s expands to Ryte. */
  109. __( 'Indexability check by %1$s', 'wordpress-seo' ),
  110. 'Ryte'
  111. ),
  112. 'ryte_fetch' => __( 'Fetch the current status', 'wordpress-seo' ),
  113. 'ryte_analyze' => __( 'Analyze entire site', 'wordpress-seo' ),
  114. 'ryte_fetch_url' => esc_attr( add_query_arg( 'wpseo-redo-onpage', '1' ) ) . '#wpseo-dashboard-overview',
  115. 'ryte_landing_url' => WPSEO_Shortlinker::get( 'https://yoa.st/rytelp' ),
  116. );
  117. }
  118. /**
  119. * Checks if the current screen is the dashboard screen.
  120. *
  121. * @return bool Whether or not this is the dashboard screen.
  122. */
  123. private function is_dashboard_screen() {
  124. $current_screen = get_current_screen();
  125. return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
  126. }
  127. /**
  128. * Returns true when the dashboard widget should be shown.
  129. *
  130. * @return bool
  131. */
  132. private function show_widget() {
  133. $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
  134. return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
  135. }
  136. }