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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
<?php /** * WPSEO plugin file. * * @package WPSEO\admin\google_search_console */
/** * Class WPSEO_GSC. */ class WPSEO_GSC implements WPSEO_WordPress_Integration {
/** * The option where data will be stored. * * @var string */ const OPTION_WPSEO_GSC = 'wpseo-gsc';
/** * @var WPSEO_GSC_Service */ private $service;
/** * @var WPSEO_GSC_Category_Filters */ protected $category_filter;
/** * @var WPSEO_GSC_Issues */ protected $issue_fetch;
/** * Current platform. * * @var string */ private $platform;
/** * Current category. * * @var string */ private $category;
/** * Registers the hooks. * * @return void */ public function register_hooks() { // Setting the screen option. if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_search_console' ) {
if ( filter_input( INPUT_GET, 'tab' ) !== 'settings' && ! $this->has_profile() ) { wp_redirect( add_query_arg( 'tab', 'settings' ) ); exit; }
add_action( 'admin_enqueue_scripts', array( $this, 'page_scripts' ) ); add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 11, 3 );
$this->set_dependencies(); $this->request_handler(); }
add_action( 'admin_init', array( $this, 'register_gsc_notification' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); }
/** * Handles the dashboard notification. * * If the Google Search Console has no credentials, show a notification * for the user to give them a heads up. This message is dismissable. * * @return void */ public function register_gsc_notification() { $notification = $this->get_profile_notification(); $notification_center = Yoast_Notification_Center::get(); $notification_center->remove_notification( $notification ); }
/** * Builds the notification used when GSC is not connected to a profile. * * @return Yoast_Notification The notification. */ private function get_profile_notification() { return new Yoast_Notification( sprintf( /* translators: 1: link open tag; 2: link close tag. */ __( 'Don\'t miss your crawl errors: %1$sconnect with Google Search Console here%2$s.', 'wordpress-seo' ), '<a href="' . admin_url( 'admin.php?page=wpseo_search_console&tab=settings' ) . '">', '</a>' ), array( 'type' => Yoast_Notification::WARNING, 'id' => 'wpseo-dismiss-gsc', 'capabilities' => 'wpseo_manage_options', ) ); }
/** * Makes sure the settings will be registered, so data can be stored. * * @codeCoverageIgnore * * @return void */ public function register_settings() { register_setting( 'yoast_wpseo_gsc_options', self::OPTION_WPSEO_GSC ); }
/** * Outputs the HTML for the redirect page. * * @return void */ public function display() { require_once WPSEO_PATH . 'admin/google_search_console/views/gsc-display.php'; }
/** * Displays the table. * * @return void */ public function display_table() { // The list table. $list_table = new WPSEO_GSC_Table( $this->platform, $this->category, $this->issue_fetch->get_issues() );
// Adding filter to display the category filters. add_filter( 'views_' . $list_table->get_screen_id(), array( $this->category_filter, 'as_array' ) );
// Preparing and displaying the table. $list_table->prepare_items(); $list_table->search_box( __( 'Search', 'wordpress-seo' ), 'wpseo-crawl-issues-search' ); $list_table->display(); }
/** * Loads the admin redirects scripts. * * @return void */ public function page_scripts() { $asset_manager = new WPSEO_Admin_Asset_Manager(); $asset_manager->enqueue_script( 'admin-gsc' ); $asset_manager->enqueue_style( 'metabox-css' );
wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-gsc', 'wpseoAdminL10n', WPSEO_Utils::get_admin_l10n() );
$screen_options = array( 'label' => __( 'Crawl errors per page', 'wordpress-seo' ), 'default' => 50, 'option' => 'errors_per_page', ); add_screen_option( 'per_page', $screen_options ); }
/** * Sets the screen options. * * @param string $status Status string. * @param string $option Option key. * @param string $value Value to return. * * @return mixed The screen option value. False when not errors_per_page. */ public function set_screen_option( $status, $option, $value ) { if ( 'errors_per_page' === $option ) { return $value; }
return false; }
/** * Handles the POST and GET requests. * * @return void */ private function request_handler() {
// List the table search post to a get. $this->list_table_search_post_to_get();
// Catch the authorization code POST. $this->catch_authentication_post();
// Is there a reset post than we will remove the posts and data. if ( filter_input( INPUT_GET, 'gsc_reset' ) ) { // Clear the google data. WPSEO_GSC_Settings::clear_data( $this->service );
// Adding notification to the notification center. /* Translators: %1$s: expands to Google Search Console. */ $this->add_notification( sprintf( __( 'The %1$s data has been removed. You will have to reauthenticate if you want to retrieve the data again.', 'wordpress-seo' ), 'Google Search Console' ), Yoast_Notification::UPDATED );
// Directly output the notifications. wp_redirect( remove_query_arg( 'gsc_reset' ) ); exit; }
// Reloads al the issues. if ( wp_verify_nonce( filter_input( INPUT_POST, 'reload-crawl-issues-nonce' ), 'reload-crawl-issues' ) && filter_input( INPUT_POST, 'reload-crawl-issues' ) ) { // Reloading all the issues. WPSEO_GSC_Settings::reload_issues();
// Adding the notification. $this->add_notification( __( 'The issues have been successfully reloaded!', 'wordpress-seo' ), Yoast_Notification::UPDATED );
// Directly output the notifications. Yoast_Notification_Center::get()->display_notifications(); }
// Catch bulk action request. new WPSEO_GSC_Bulk_Action(); }
/** * Catches the redirects search post and redirect it to a search get. * * @return void */ private function list_table_search_post_to_get() { $search_string = filter_input( INPUT_POST, 's' );
if ( $search_string === null ) { return; }
// When there is nothing being search and there is no search param in the url, break this method. if ( $search_string === '' && filter_input( INPUT_GET, 's' ) === null ) { return; }
$url = ( $search_string !== '' ) ? add_query_arg( 's', $search_string ) : remove_query_arg( 's' );
// Do the redirect. wp_redirect( $url ); exit; }
/** * Catches the authentication post. * * @return void */ private function catch_authentication_post() { $gsc_values = filter_input( INPUT_POST, 'gsc', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
// Catch the authorization code POST. if ( ! empty( $gsc_values['authorization_code'] ) && wp_verify_nonce( $gsc_values['gsc_nonce'], 'wpseo-gsc_nonce' ) ) { if ( ! WPSEO_GSC_Settings::validate_authorization( trim( $gsc_values['authorization_code'] ), $this->service->get_client() ) ) { $this->add_notification( __( 'Incorrect Google Authorization Code.', 'wordpress-seo' ), Yoast_Notification::ERROR ); }
// Redirect user to prevent a post resubmission which causes an oauth error. wp_redirect( admin_url( 'admin.php' ) . '?page=' . esc_attr( filter_input( INPUT_GET, 'page' ) ) . '&tab=settings' ); exit; } }
/** * Adds notification to the yoast notification center. * * @param string $message Message string. * @param string $type Message type. * * @return void */ private function add_notification( $message, $type ) { Yoast_Notification_Center::get()->add_notification( new Yoast_Notification( $message, array( 'type' => $type ) ) ); }
/** * Sets the dependencies which will be used one this page. * * @return void */ private function set_dependencies() { // Setting the service object. $this->service = new WPSEO_GSC_Service( WPSEO_GSC_Settings::get_profile() );
// Setting the platform. $this->platform = WPSEO_GSC_Mapper::get_current_platform( 'tab' );
// Loading the issue counter. $issue_count = new WPSEO_GSC_Count( $this->service ); $issue_count->fetch_counts();
// Loading the category filters. $this->category_filter = new WPSEO_GSC_Category_Filters( $issue_count->get_platform_counts( $this->platform ) );
// Setting the current category. $this->category = $this->category_filter->get_category();
// Listing the issues. $issue_count->list_issues( $this->platform, $this->category );
// Fetching the issues. $this->issue_fetch = new WPSEO_GSC_Issues( $this->platform, $this->category, $issue_count->get_issues() ); }
/** * Sets the tab help on top of the screen. * * @return void */ public function set_help() { $screen = get_current_screen();
if ( $screen === null ) { return; }
$screen->add_help_tab( array( 'id' => 'basic-help', 'title' => __( 'Issue categories', 'wordpress-seo' ), 'content' => '<p><strong>' . __( 'Desktop', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that occurred when your site was crawled by Googlebot.', 'wordpress-seo' ) . '</p>' . '<p><strong>' . __( 'Smartphone', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that occurred only when your site was crawled by Googlebot-Mobile (errors didn\'t appear for desktop).', 'wordpress-seo' ) . '</p>' . '<p><strong>' . __( 'Feature phone', 'wordpress-seo' ) . '</strong><br />' . __( 'Errors that only occurred when your site was crawled by Googlebot for feature phones (errors didn\'t appear for desktop).', 'wordpress-seo' ) . '</p>', ) ); }
/** * Checks if a Google Search Console profile has been set. * * @codeCoverageIgnore * * @return bool True when profile has been set. */ protected function has_profile() { return ( WPSEO_GSC_Settings::get_profile() !== '' ); }
/** * Run init logic. * * @codeCoverageIgnore * * @deprecated 9.5 * * @return void */ public function init() { _deprecated_function( __METHOD__, 'WPSEO 9.5' ); } }
|