Yoast_Dashboard_Widget

Class to change or add WordPress dashboard widgets.

Defined (1)

The class is defined in the following location(s).

/admin/class-yoast-dashboard-widget.php  
  1. class Yoast_Dashboard_Widget { 
  2.  
  3. const CACHE_TRANSIENT_KEY = 'wpseo-dashboard-totals'; 
  4.  
  5. /** 
  6. * @var WPSEO_Statistics 
  7. */ 
  8. protected $statistics; 
  9.  
  10. /** 
  11. * @param WPSEO_Statistics $statistics The statistics class to retrieve statistics from. 
  12. */ 
  13. public function __construct( WPSEO_Statistics $statistics = null ) { 
  14. if ( null === $statistics ) { 
  15. $statistics = new WPSEO_Statistics(); 
  16.  
  17. $this->statistics = $statistics; 
  18.  
  19. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dashboard_stylesheet' ) ); 
  20. add_action( 'wp_insert_post', array( $this, 'clear_cache' ) ); 
  21. add_action( 'delete_post', array( $this, 'clear_cache' ) ); 
  22.  
  23. if ( $this->show_widget() ) { 
  24. add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) ); 
  25.  
  26. /** 
  27. * Adds dashboard widget to WordPress 
  28. */ 
  29. public function add_dashboard_widget() { 
  30. add_filter( 'postbox_classes_dashboard_wpseo-dashboard-overview', array( $this, 'wpseo_dashboard_overview_class' ) ); 
  31. wp_add_dashboard_widget( 
  32. 'wpseo-dashboard-overview',  
  33. /** translators: %s is the plugin name */ 
  34. sprintf( __( '%s Posts Overview', 'wordpress-seo' ), 'Yoast SEO' ),  
  35. array( $this, 'display_dashboard_widget' ) 
  36. ); 
  37.  
  38. /** 
  39. * Adds CSS classes to the dashboard widget. 
  40. * @param array $classes An array of postbox CSS classes. 
  41. * @return array 
  42. */ 
  43. public function wpseo_dashboard_overview_class( $classes ) { 
  44. $classes[] = 'yoast wpseo-dashboard-overview'; 
  45. return $classes; 
  46.  
  47. /** 
  48. * Display the dashboard widget 
  49. */ 
  50. public function display_dashboard_widget() { 
  51. $statistics = $this->statistic_items(); 
  52.  
  53. $onpage_option = new WPSEO_OnPage_Option(); 
  54. $onpage = false; 
  55. if ( $onpage_option->is_enabled() ) { 
  56. $onpage = array( 
  57. 'indexable' => $onpage_option->get_status(),  
  58. 'can_fetch' => $onpage_option->should_be_fetched(),  
  59. ); 
  60.  
  61. include WPSEO_PATH . '/admin/views/dashboard-widget.php'; 
  62.  
  63. /** 
  64. * Enqueue's stylesheet for the dashboard if the current page is the dashboard 
  65. */ 
  66. public function enqueue_dashboard_stylesheet() { 
  67. $current_screen = get_current_screen(); 
  68.  
  69. if ( $current_screen instanceof WP_Screen && 'dashboard' === $current_screen->id ) { 
  70. $asset_manager = new WPSEO_Admin_Asset_Manager(); 
  71. $asset_manager->enqueue_style( 'wp-dashboard' ); 
  72.  
  73. /** 
  74. * Clears the dashboard widget items cache 
  75. */ 
  76. public function clear_cache() { 
  77. delete_transient( self::CACHE_TRANSIENT_KEY ); 
  78.  
  79. /** 
  80. * An array representing items to be added to the At a Glance dashboard widget 
  81. * @return array 
  82. */ 
  83. private function statistic_items() { 
  84. $transient = get_transient( self::CACHE_TRANSIENT_KEY ); 
  85. $user_id = get_current_user_id(); 
  86.  
  87. if ( isset( $transient[ $user_id ] ) ) { 
  88. return $transient[ $user_id ]; 
  89.  
  90. return $this->set_statistic_items_for_this_user( $transient ); 
  91.  
  92. /** 
  93. * Set the cache for a specific user 
  94. * @param array|boolean $transient The current stored transient with the cached data. 
  95. * @return mixed 
  96. */ 
  97. private function set_statistic_items_for_this_user( $transient ) { 
  98. if ( $transient === false ) { 
  99. $transient = array(); 
  100.  
  101. $user_id = get_current_user_id(); 
  102. $transient[ $user_id ] = array_filter( $this->get_seo_scores_with_post_count(), array( $this, 'filter_items' ) ); 
  103.  
  104. set_transient( self::CACHE_TRANSIENT_KEY, $transient, DAY_IN_SECONDS ); 
  105.  
  106. return $transient[ $user_id ]; 
  107.  
  108. /** 
  109. * Set the SEO scores belonging to their SEO score result 
  110. * @return array 
  111. */ 
  112. private function get_seo_scores_with_post_count() { 
  113. $ranks = WPSEO_Rank::get_all_ranks(); 
  114.  
  115. return array_map( array( $this, 'map_rank_to_widget' ), $ranks ); 
  116.  
  117. /** 
  118. * Converts a rank to data usable in the dashboard widget 
  119. * @param WPSEO_Rank $rank The rank to map. 
  120. * @return array 
  121. */ 
  122. private function map_rank_to_widget( WPSEO_Rank $rank ) { 
  123. return array( 
  124. 'seo_rank' => $rank->get_rank(),  
  125. 'title' => $this->get_title_for_rank( $rank ),  
  126. 'class' => 'wpseo-glance-' . $rank->get_css_class(),  
  127. 'icon_class' => $rank->get_css_class(),  
  128. 'count' => $this->statistics->get_post_count( $rank ),  
  129. ); 
  130.  
  131. /** 
  132. * Returns a dashboard widget label to use for a certain rank 
  133. * @param WPSEO_Rank $rank The rank to return a label for. 
  134. * @return string 
  135. */ 
  136. private function get_title_for_rank( WPSEO_Rank $rank ) { 
  137. $labels = array( 
  138. WPSEO_Rank::NO_FOCUS => __( 'Posts without focus keyword', 'wordpress-seo' ),  
  139. WPSEO_Rank::BAD => __( 'Posts with bad SEO score', 'wordpress-seo' ),  
  140. WPSEO_Rank::OK => __( 'Posts with OK SEO score', 'wordpress-seo' ),  
  141. WPSEO_Rank::GOOD => __( 'Posts with good SEO score', 'wordpress-seo' ),  
  142. /** translators: %s expands to <span lang="en">noindex</span> */ 
  143. WPSEO_Rank::NO_INDEX => sprintf( __( 'Posts that are set to “%s”', 'wordpress-seo' ), '<span lang="en">noindex</span>' ),  
  144. ); 
  145.  
  146. return $labels[ $rank->get_rank() ]; 
  147.  
  148. /** 
  149. * Filter items if they have a count of zero 
  150. * @param array $item Data array. 
  151. * @return bool 
  152. */ 
  153. private function filter_items( $item ) { 
  154. return 0 !== $item['count']; 
  155.  
  156. /** 
  157. * Returns true when the dashboard widget should be shown. 
  158. * @return bool 
  159. */ 
  160. private function show_widget() { 
  161. $analysis_seo = new WPSEO_Metabox_Analysis_SEO(); 
  162.  
  163. return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );