/admin/class-yoast-dashboard-widget.php

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