/htdocs/wp-content/plugins/wordpress-seo/admin/statistics/class-statistics-service.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 258 lines · 113 code · 35 blank · 110 comment · 6 complexity · 96fd3ae33ec01164ff3da383510b24c1 MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Statistics
  6. */
  7. /**
  8. * Class WPSEO_Statistics_Service.
  9. */
  10. class WPSEO_Statistics_Service {
  11. /**
  12. * Cache transient id.
  13. *
  14. * @var string
  15. */
  16. const CACHE_TRANSIENT_KEY = 'wpseo-statistics-totals';
  17. /**
  18. * Class that generates interesting statistics about things.
  19. *
  20. * @var WPSEO_Statistics
  21. */
  22. protected $statistics;
  23. /**
  24. * Statistics labels.
  25. *
  26. * @var string[]
  27. */
  28. protected $labels;
  29. /**
  30. * WPSEO_Statistics_Service contructor.
  31. *
  32. * @param WPSEO_Statistics $statistics The statistics class to retrieve statistics from.
  33. */
  34. public function __construct( WPSEO_Statistics $statistics ) {
  35. $this->statistics = $statistics;
  36. }
  37. /**
  38. * Fetches statistics by REST request.
  39. *
  40. * @return WP_REST_Response The response object.
  41. */
  42. public function get_statistics() {
  43. // Switch to the user locale with fallback to the site locale.
  44. switch_to_locale( WPSEO_Language_Utils::get_user_locale() );
  45. $this->labels = $this->labels();
  46. $statistics = $this->statistic_items();
  47. $data = [
  48. 'header' => $this->get_header_from_statistics( $statistics ),
  49. 'seo_scores' => $statistics['scores'],
  50. ];
  51. return new WP_REST_Response( $data );
  52. }
  53. /**
  54. * Gets a header summarizing the given statistics results.
  55. *
  56. * @param array $statistics The statistics results.
  57. *
  58. * @return string The header summing up the statistics results.
  59. */
  60. private function get_header_from_statistics( array $statistics ) {
  61. // Personal interpretation to allow release, should be looked at later.
  62. if ( $statistics['division'] === false ) {
  63. return __( 'You don\'t have any published posts, your SEO scores will appear here once you make your first post!', 'wordpress-seo' );
  64. }
  65. if ( $statistics['division']['good'] > 0.66 ) {
  66. return __( 'Hey, your SEO is doing pretty well! Check out the stats:', 'wordpress-seo' );
  67. }
  68. return __( 'Below are your published posts\' SEO scores. Now is as good a time as any to start improving some of your posts!', 'wordpress-seo' );
  69. }
  70. /**
  71. * An array representing items to be added to the At a Glance dashboard widget.
  72. *
  73. * @return array The statistics for the current user.
  74. */
  75. private function statistic_items() {
  76. $transient = $this->get_transient();
  77. $user_id = get_current_user_id();
  78. if ( isset( $transient[ $user_id ] ) ) {
  79. return $transient[ $user_id ];
  80. }
  81. return $this->set_statistic_items_for_user( $transient, $user_id );
  82. }
  83. /**
  84. * Gets the statistics transient value. Returns array if transient wasn't set.
  85. *
  86. * @return array|mixed Returns the transient or an empty array if the transient doesn't exist.
  87. */
  88. private function get_transient() {
  89. $transient = get_transient( self::CACHE_TRANSIENT_KEY );
  90. if ( $transient === false ) {
  91. return [];
  92. }
  93. return $transient;
  94. }
  95. /**
  96. * Set the statistics transient cache for a specific user.
  97. *
  98. * @param array $transient The current stored transient with the cached data.
  99. * @param int $user The user's ID to assign the retrieved values to.
  100. *
  101. * @return array The statistics transient for the user.
  102. */
  103. private function set_statistic_items_for_user( $transient, $user ) {
  104. $scores = $this->get_seo_scores_with_post_count();
  105. $division = $this->get_seo_score_division( $scores );
  106. $transient[ $user ] = [
  107. // Use array_values because array_filter may return non-zero indexed arrays.
  108. 'scores' => array_values( array_filter( $scores, [ $this, 'filter_items' ] ) ),
  109. 'division' => $division,
  110. ];
  111. set_transient( self::CACHE_TRANSIENT_KEY, $transient, DAY_IN_SECONDS );
  112. return $transient[ $user ];
  113. }
  114. /**
  115. * Gets the division of SEO scores.
  116. *
  117. * @param array $scores The SEO scores.
  118. *
  119. * @return array|bool The division of SEO scores, false if there are no posts.
  120. */
  121. private function get_seo_score_division( array $scores ) {
  122. $total = 0;
  123. $division = [];
  124. foreach ( $scores as $score ) {
  125. $total += $score['count'];
  126. }
  127. if ( $total === 0 ) {
  128. return false;
  129. }
  130. foreach ( $scores as $score ) {
  131. $division[ $score['seo_rank'] ] = ( $score['count'] / $total );
  132. }
  133. return $division;
  134. }
  135. /**
  136. * Get all SEO ranks and data associated with them.
  137. *
  138. * @return array An array of SEO scores and associated data.
  139. */
  140. private function get_seo_scores_with_post_count() {
  141. $ranks = WPSEO_Rank::get_all_ranks();
  142. return array_map( [ $this, 'map_rank_to_widget' ], $ranks );
  143. }
  144. /**
  145. * Converts a rank to data usable in the dashboard widget.
  146. *
  147. * @param WPSEO_Rank $rank The rank to map.
  148. *
  149. * @return array The mapped rank.
  150. */
  151. private function map_rank_to_widget( WPSEO_Rank $rank ) {
  152. return [
  153. 'seo_rank' => $rank->get_rank(),
  154. 'label' => $this->get_label_for_rank( $rank ),
  155. 'count' => $this->statistics->get_post_count( $rank ),
  156. 'link' => $this->get_link_for_rank( $rank ),
  157. ];
  158. }
  159. /**
  160. * Returns a dashboard widget label to use for a certain rank.
  161. *
  162. * @param WPSEO_Rank $rank The rank to return a label for.
  163. *
  164. * @return string The label for the rank.
  165. */
  166. private function get_label_for_rank( WPSEO_Rank $rank ) {
  167. return $this->labels[ $rank->get_rank() ];
  168. }
  169. /**
  170. * Determines the labels for the various scoring ranks that are known within Yoast SEO.
  171. *
  172. * @return array Array containing the translatable labels.
  173. */
  174. private function labels() {
  175. return [
  176. WPSEO_Rank::NO_FOCUS => sprintf(
  177. /* translators: %1$s expands to an opening strong tag, %2$s expands to a closing strong tag */
  178. __( 'Posts %1$swithout%2$s a focus keyphrase', 'wordpress-seo' ),
  179. '<strong>',
  180. '</strong>'
  181. ),
  182. WPSEO_Rank::BAD => sprintf(
  183. /* translators: %s expands to the score */
  184. __( 'Posts with the SEO score: %s', 'wordpress-seo' ),
  185. '<strong>' . __( 'Needs improvement', 'wordpress-seo' ) . '</strong>'
  186. ),
  187. WPSEO_Rank::OK => sprintf(
  188. /* translators: %s expands to the score */
  189. __( 'Posts with the SEO score: %s', 'wordpress-seo' ),
  190. '<strong>' . __( 'OK', 'wordpress-seo' ) . '</strong>'
  191. ),
  192. WPSEO_Rank::GOOD => sprintf(
  193. /* translators: %s expands to the score */
  194. __( 'Posts with the SEO score: %s', 'wordpress-seo' ),
  195. '<strong>' . __( 'Good', 'wordpress-seo' ) . '</strong>'
  196. ),
  197. WPSEO_Rank::NO_INDEX => __( 'Posts that should not show up in search results', 'wordpress-seo' ),
  198. ];
  199. }
  200. /**
  201. * Filter items if they have a count of zero.
  202. *
  203. * @param array $item The item to potentially filter out.
  204. *
  205. * @return bool Whether or not the count is zero.
  206. */
  207. private function filter_items( $item ) {
  208. return $item['count'] !== 0;
  209. }
  210. /**
  211. * Returns a link for the overview of posts of a certain rank.
  212. *
  213. * @param WPSEO_Rank $rank The rank to return a link for.
  214. *
  215. * @return string The link that shows an overview of posts with that rank.
  216. */
  217. private function get_link_for_rank( WPSEO_Rank $rank ) {
  218. if ( current_user_can( 'edit_others_posts' ) === false ) {
  219. return esc_url( admin_url( 'edit.php?post_status=publish&post_type=post&seo_filter=' . $rank->get_rank() . '&author=' . get_current_user_id() ) );
  220. }
  221. return esc_url( admin_url( 'edit.php?post_status=publish&post_type=post&seo_filter=' . $rank->get_rank() ) );
  222. }
  223. }