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