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