3 * @package WPSEO\Admin|Google_Search_Console
7 * Class WPSEO_GSC_Count
9 class WPSEO_GSC_Count {
11 // The last checked timestamp.
12 const OPTION_CI_LAST_FETCH = 'wpseo_gsc_last_fetch';
14 // The option name where the issues counts are saved.
15 const OPTION_CI_COUNTS = 'wpseo_gsc_issues_counts';
18 * @var WPSEO_GSC_Service
23 * Holder for the fetched issues from GSC
27 private $issues = array();
32 * @param WPSEO_GSC_Service $service
34 public function __construct( WPSEO_GSC_Service $service ) {
35 $this->service = $service;
39 * Getting the counts for given platform and return them as an array
41 * @param string $platform
45 public function get_platform_counts( $platform ) {
46 $counts = $this->get_counts();
47 if ( array_key_exists( $platform, $counts ) ) {
48 return $counts[ $platform ];
55 * Return the fetched issues
59 public function get_issues() {
64 * Listing the issues an gives them back as fetched issues
66 * @param string $platform
67 * @param string $category
69 public function list_issues( $platform, $category ) {
70 $counts = $this->get_counts();
72 if ( array_key_exists( $platform, $counts ) ) {
73 $counts[ $platform ] = $this->list_category_issues( $counts[ $platform ], $platform, $category );
75 // Write the new counts value.
76 $this->set_counts( $counts );
81 * Getting the counts for given platform and category.
83 * @param string $platform
84 * @param string $category
88 public function get_issue_count( $platform, $category ) {
89 $counts = $this->get_counts();
91 if ( ! empty( $counts[ $platform ][ $category ]['count'] ) ) {
92 return $counts[ $platform ][ $category ]['count'];
99 * Update the count of the issues
101 * @param string $platform
102 * @param string $category
103 * @param integer $new_count
105 public function update_issue_count( $platform, $category, $new_count ) {
106 $counts = $this->get_counts();
108 if ( ! empty( $counts[ $platform ][ $category ] ) && is_array( $counts[ $platform ][ $category ] ) ) {
109 $counts[ $platform ][ $category ]['count'] = $new_count;
112 $this->set_counts( $counts );
116 * Fetching the counts from the GSC API
118 public function fetch_counts() {
119 if ( WPSEO_GSC_Settings::get_profile() && $this->get_last_fetch() <= strtotime( '-12 hours' ) ) {
120 // Remove the timestamp.
121 $this->remove_last_fetch();
123 // Getting the counts and parse them.
124 $counts = $this->parse_counts( $this->service->get_crawl_issue_counts() );
126 // Fetching the counts by setting an option.
127 $this->set_counts( $counts );
129 // Saving the current timestamp.
130 $this->save_last_fetch();
135 * Parsing the received counts from the API and map the keys to plugin friendly values
137 * @param array $fetched_counts
141 private function parse_counts( array $fetched_counts ) {
143 foreach ( $fetched_counts as $platform_name => $categories ) {
144 $new_platform = WPSEO_GSC_Mapper::platform_from_api( $platform_name );
146 foreach ( $categories as $category_name => $category ) {
147 $new_category = WPSEO_GSC_Mapper::category_from_api( $category_name );
148 $counts[ $new_platform ][ $new_category ] = $category;
156 * Listing the issues for current category.
158 * @param array $counts
159 * @param string $platform
160 * @param string $category
164 private function list_category_issues( array $counts, $platform, $category ) {
165 // When the issues have to be fetched.
166 if ( array_key_exists( $category, $counts ) && $counts[ $category ]['count'] > 0 && $counts[ $category ]['last_fetch'] <= strtotime( '-12 hours' ) ) {
167 if ( $issues = $this->service->fetch_category_issues( WPSEO_GSC_Mapper::platform_to_api( $platform ), WPSEO_GSC_Mapper::category_to_api( $category ) ) ) {
168 $this->issues = $issues;
171 // Be sure the total count is correct.
172 $counts[ $category ]['count'] = count( $this->issues );
175 $counts[ $category ]['last_fetch'] = time();
182 * Getting the counts from the options
186 private function get_counts() {
187 return get_option( self::OPTION_CI_COUNTS, array() );
191 * Fetching the counts from the service and store them in an option
193 * @param array $counts
195 private function set_counts( array $counts ) {
196 update_option( self::OPTION_CI_COUNTS, $counts );
200 * Store the timestamp of when crawl errors were saved the last time.
202 private function save_last_fetch() {
203 add_option( self::OPTION_CI_LAST_FETCH, time(), '', 'no' );
207 * Remove the last checked option
209 private function remove_last_fetch() {
210 delete_option( self::OPTION_CI_LAST_FETCH );
214 * Get the timestamp of when the crawl errors were last saved
218 private function get_last_fetch() {
219 return get_option( self::OPTION_CI_LAST_FETCH, 0 );