WPSEO_GSC_Count

Class WPSEO_GSC_Count.

Defined (1)

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

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