class-yoast-dashboard-widget.php 4.5 KB

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