]> _ Git - couzy.git/blob
4e60a5e54dbc7884dda94505a01ccaa19f8aa412
[couzy.git] /
1 <?php
2 /**
3  * @package WPSEO\Admin|Google_Search_Console
4  */
5
6 /**
7  * Class WPSEO_GSC_Service
8  */
9 class WPSEO_GSC_Service {
10
11         /**
12          * @var Yoast_Api_Google_Client
13          */
14         private $client;
15
16         /**
17          * @var string
18          */
19         private $profile;
20
21         /**
22          * Constructor
23          *
24          * @param string $profile
25          */
26         public function __construct( $profile = '' ) {
27                 $this->profile = $profile;
28
29                 $this->set_client();
30         }
31
32         /**
33          * Returns the client
34          *
35          * @return Yoast_Api_Google_Client
36          */
37         public function get_client() {
38                 return $this->client;
39         }
40
41         /**
42          * Removes the option and calls the clients clear_data method to clear that one as well
43          */
44         public function clear_data() {
45                 // Clear client data.
46                 $this->client->clear_data();
47         }
48
49         /**
50          * Get all sites that are registered in the GSC panel
51          *
52          * @return array
53          */
54         public function get_sites() {
55                 $sites = array();
56
57                 $response_json = $this->client->do_request( 'sites', true );
58
59                 // Do list sites request.
60                 if ( ! empty( $response_json->siteEntry ) ) {
61                         foreach ( $response_json->siteEntry as $entry ) {
62                                 $sites[ str_ireplace( 'sites/', '', (string) $entry->siteUrl ) ] = (string) $entry->siteUrl;
63                         }
64
65                         // Sorting the retrieved sites.
66                         asort( $sites );
67                 }
68
69                 return $sites;
70         }
71
72         /**
73          * Get crawl issues
74          *
75          * @return array
76          */
77         public function get_crawl_issue_counts() {
78                 // Setup crawl error list.
79                 $crawl_error_counts = $this->get_crawl_error_counts( $this->profile );
80
81                 $return = array();
82                 if ( ! empty( $crawl_error_counts->countPerTypes ) ) {
83                         foreach ( $crawl_error_counts->countPerTypes as $category ) {
84                                 $return[ $category->platform ][ $category->category ] = array(
85                                         'count'      => $category->entries[0]->count,
86                                         'last_fetch' => null,
87                                 );
88                         }
89                 }
90
91                 return $return;
92         }
93
94         /**
95          * Sending request to mark issue as fixed
96          *
97          * @param string $url
98          * @param string $platform
99          * @param string $category
100          *
101          * @return bool
102          */
103         public function mark_as_fixed( $url, $platform, $category ) {
104                 $response = $this->client->do_request( 'sites/' .  urlencode( $this->profile ) .  '/urlCrawlErrorsSamples/' . urlencode( ltrim( $url, '/' ) ) . '?category=' . WPSEO_GSC_Mapper::category_to_api( $category ) . '&platform=' . WPSEO_GSC_Mapper::platform_to_api( $platform ) . '', false, 'DELETE' );
105                 return ( $response->getResponseHttpCode() === 204 );
106         }
107
108         /**
109          * Fetching the issues from the GSC API
110          *
111          * @param string $platform
112          * @param string $category
113          *
114          * @return mixed
115          */
116         public function fetch_category_issues( $platform, $category ) {
117                 $issues = $this->client->do_request(
118                         'sites/' . urlencode( $this->profile ) . '/urlCrawlErrorsSamples?category=' . $category . '&platform=' . $platform,
119                         true
120                 );
121
122                 if ( ! empty( $issues->urlCrawlErrorSample ) ) {
123                         return $issues->urlCrawlErrorSample;
124                 }
125         }
126
127         /**
128          * Setting the GSC client
129          */
130         private function set_client() {
131                 try {
132                         new Yoast_Api_Libs( '2.0' );
133                 }
134                 catch ( Exception $exception ) {
135                         if ( $exception->getMessage() === 'required_version' ) {
136                                 $this->incompatible_api_libs(
137                                         __( 'Yoast plugins share some code between them to make your site faster. As a result of that, we need all Yoast plugins to be up to date. We\'ve detected this isn\'t the case, so please update the Yoast plugins that aren\'t up to date yet.', 'wordpress-seo' )
138                                 );
139                         }
140                 }
141
142                 if ( class_exists( 'Yoast_Api_Google_Client' ) === false ) {
143                         $this->incompatible_api_libs(
144                                 /* translators: %1$s expands to Yoast SEO, %2$s expands to Google Analytics by Yoast */
145                                 sprintf(
146                                         __(
147                                                 '%1$s detected you’re using a version of %2$s which is not compatible with %1$s. Please update %2$s to the latest version to use this feature.',
148                                                 'wordpress-seo'
149                                         ),
150                                         'Yoast SEO',
151                                         'Google Analytics by Yoast'
152                                 )
153                         );
154
155                         wp_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
156                         exit;
157                 }
158
159                 $this->client = new Yoast_Api_Google_Client( WPSEO_GSC_Config::$gsc, 'wpseo-gsc', 'https://www.googleapis.com/webmasters/v3/' );
160         }
161
162         /**
163          * Adding notice that the api libs has the wrong version
164          *
165          * @param string $notice
166          */
167         private function incompatible_api_libs( $notice ) {
168                 Yoast_Notification_Center::get()->add_notification(
169                         new Yoast_Notification( $notice, array( 'type' => 'error' ) )
170                 );
171         }
172
173         /**
174          * Getting the crawl error counts
175          *
176          * @param string $profile
177          *
178          * @return object|bool
179          */
180         private function get_crawl_error_counts( $profile ) {
181                 $crawl_error_counts = $this->client->do_request(
182                         'sites/' . urlencode( $profile ) . '/urlCrawlErrorsCounts/query',
183                         true
184                 );
185
186                 if ( ! empty( $crawl_error_counts ) ) {
187                         return $crawl_error_counts;
188                 }
189
190                 return false;
191         }
192
193 }