class-gsc-count.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Count
  9. */
  10. class WPSEO_GSC_Count {
  11. /**
  12. * @var string The name of the option containing the last checked timestamp.
  13. */
  14. const OPTION_CI_LAST_FETCH = 'wpseo_gsc_last_fetch';
  15. /**
  16. * @var string The option name where the issues counts are saved.
  17. */
  18. const OPTION_CI_COUNTS = 'wpseo_gsc_issues_counts';
  19. /**
  20. * @var WPSEO_GSC_Service
  21. */
  22. private $service;
  23. /**
  24. * Holder for the fetched issues from GSC
  25. *
  26. * @var array
  27. */
  28. private $issues = array();
  29. /**
  30. * Fetching the counts
  31. *
  32. * @param WPSEO_GSC_Service $service Service class instance.
  33. */
  34. public function __construct( WPSEO_GSC_Service $service ) {
  35. $this->service = $service;
  36. }
  37. /**
  38. * Getting the counts for given platform and return them as an array
  39. *
  40. * @param string $platform Platform (desktop, mobile, feature phone).
  41. *
  42. * @return array
  43. */
  44. public function get_platform_counts( $platform ) {
  45. $counts = $this->get_counts();
  46. if ( array_key_exists( $platform, $counts ) ) {
  47. return $counts[ $platform ];
  48. }
  49. return array();
  50. }
  51. /**
  52. * Return the fetched issues
  53. *
  54. * @return array
  55. */
  56. public function get_issues() {
  57. return $this->issues;
  58. }
  59. /**
  60. * Listing the issues an gives them back as fetched issues
  61. *
  62. * @param string $platform Platform (desktop, mobile, feature phone).
  63. * @param string $category Issue category.
  64. */
  65. public function list_issues( $platform, $category ) {
  66. $counts = $this->get_counts();
  67. if ( array_key_exists( $platform, $counts ) ) {
  68. $counts[ $platform ] = $this->list_category_issues( $counts[ $platform ], $platform, $category );
  69. // Write the new counts value.
  70. $this->set_counts( $counts );
  71. }
  72. }
  73. /**
  74. * Getting the counts for given platform and category.
  75. *
  76. * @param string $platform Platform (desktop, mobile, feature phone).
  77. * @param string $category Issue type.
  78. *
  79. * @return integer
  80. */
  81. public function get_issue_count( $platform, $category ) {
  82. $counts = $this->get_counts();
  83. if ( ! empty( $counts[ $platform ][ $category ]['count'] ) ) {
  84. return $counts[ $platform ][ $category ]['count'];
  85. }
  86. return 0;
  87. }
  88. /**
  89. * Update the count of the issues
  90. *
  91. * @param string $platform Platform (desktop, mobile, feature phone).
  92. * @param string $category Issue type.
  93. * @param integer $new_count Updated count.
  94. */
  95. public function update_issue_count( $platform, $category, $new_count ) {
  96. $counts = $this->get_counts();
  97. if ( ! empty( $counts[ $platform ][ $category ] ) && is_array( $counts[ $platform ][ $category ] ) ) {
  98. $counts[ $platform ][ $category ]['count'] = $new_count;
  99. }
  100. $this->set_counts( $counts );
  101. }
  102. /**
  103. * Fetching the counts from the GSC API
  104. */
  105. public function fetch_counts() {
  106. if ( WPSEO_GSC_Settings::get_profile() && $this->get_last_fetch() <= strtotime( '-12 hours' ) ) {
  107. // Remove the timestamp.
  108. $this->remove_last_fetch();
  109. // Getting the counts and parse them.
  110. $counts = $this->parse_counts( $this->service->get_crawl_issue_counts() );
  111. // Fetching the counts by setting an option.
  112. $this->set_counts( $counts );
  113. // Saving the current timestamp.
  114. $this->save_last_fetch();
  115. }
  116. }
  117. /**
  118. * Parsing the received counts from the API and map the keys to plugin friendly values
  119. *
  120. * @param array $fetched_counts Set of retrieved counts.
  121. *
  122. * @return array
  123. */
  124. private function parse_counts( array $fetched_counts ) {
  125. $counts = array();
  126. foreach ( $fetched_counts as $platform_name => $categories ) {
  127. $new_platform = WPSEO_GSC_Mapper::platform_from_api( $platform_name );
  128. foreach ( $categories as $category_name => $category ) {
  129. $new_category = WPSEO_GSC_Mapper::category_from_api( $category_name );
  130. $counts[ $new_platform ][ $new_category ] = $category;
  131. }
  132. }
  133. return $counts;
  134. }
  135. /**
  136. * Listing the issues for current category.
  137. *
  138. * @param array $counts Set of counts.
  139. * @param string $platform Platform (desktop, mobile, feature phone).
  140. * @param string $category Issue type.
  141. *
  142. * @return array
  143. */
  144. private function list_category_issues( array $counts, $platform, $category ) {
  145. // When the issues have to be fetched.
  146. if ( array_key_exists( $category, $counts ) && $counts[ $category ]['count'] > 0 && $counts[ $category ]['last_fetch'] <= strtotime( '-12 hours' ) ) {
  147. $issues = $this->service->fetch_category_issues( WPSEO_GSC_Mapper::platform_to_api( $platform ), WPSEO_GSC_Mapper::category_to_api( $category ) );
  148. if ( ! empty( $issues ) ) {
  149. $this->issues = $issues;
  150. }
  151. // Be sure the total count is correct.
  152. $counts[ $category ]['count'] = count( $this->issues );
  153. // Set last fetch.
  154. $counts[ $category ]['last_fetch'] = time();
  155. }
  156. return $counts;
  157. }
  158. /**
  159. * Getting the counts from the options
  160. *
  161. * @return array
  162. */
  163. private function get_counts() {
  164. return get_option( self::OPTION_CI_COUNTS, array() );
  165. }
  166. /**
  167. * Fetching the counts from the service and store them in an option
  168. *
  169. * @param array $counts Set of counts.
  170. */
  171. private function set_counts( array $counts ) {
  172. update_option( self::OPTION_CI_COUNTS, $counts );
  173. }
  174. /**
  175. * Store the timestamp of when crawl errors were saved the last time.
  176. */
  177. private function save_last_fetch() {
  178. add_option( self::OPTION_CI_LAST_FETCH, time(), '', 'no' );
  179. }
  180. /**
  181. * Remove the last checked option
  182. */
  183. private function remove_last_fetch() {
  184. delete_option( self::OPTION_CI_LAST_FETCH );
  185. }
  186. /**
  187. * Get the timestamp of when the crawl errors were last saved
  188. *
  189. * @return int
  190. */
  191. private function get_last_fetch() {
  192. return get_option( self::OPTION_CI_LAST_FETCH, 0 );
  193. }
  194. }