class-component-connect-google-search-console.php
3.03 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
<?php
/**
* @package WPSEO\Admin\ConfigurationUI
*/
/**
* Class WPSEO_Config_Component_Connect_Google_Search_Console
*/
class WPSEO_Config_Component_Connect_Google_Search_Console implements WPSEO_Config_Component {
const OPTION_ACCESS_TOKEN = 'wpseo-gsc-access_token';
const OPTION_REFRESH_TOKEN = 'wpseo-gsc-refresh_token';
/** @var WPSEO_GSC_Service Service to use */
protected $gsc_service;
/**
* WPSEO_Config_Component_Connect_Google_Search_Console constructor.
*/
public function __construct() {
$this->gsc_service = new WPSEO_GSC_Service( $this->get_profile() );
}
/**
* Set the Google Search Console service.
*
* @param WPSEO_GSC_Service $service Set service to use.
*/
public function set_gsc_service( WPSEO_GSC_Service $service ) {
$this->gsc_service = $service;
}
/**
* Gets the component identifier.
*
* @return string
*/
public function get_identifier() {
return 'ConnectGoogleSearchConsole';
}
/**
* Gets the field.
*
* @return WPSEO_Config_Field
*/
public function get_field() {
return new WPSEO_Config_Field_Connect_Google_Search_Console();
}
/**
* Get the data for the field.
*
* @return mixed
*/
public function get_data() {
$data = array(
'profileList' => $this->get_profilelist(),
'profile' => $this->get_profile(),
'hasAccessToken' => $this->hasAccessToken(),
);
return $data;
}
/**
* Save data
*
* @param array $data Data containing changes.
*
* @return mixed
*/
public function set_data( $data ) {
$current_data = $this->get_data();
$this->handle_profile_change( $current_data, $data );
// Save profile.
$has_saved = update_option(
WPSEO_GSC::OPTION_WPSEO_GSC,
array( 'profile' => $data['profile'] )
);
// Collect results to return to the configurator.
$results = array(
'profile' => $has_saved,
);
return $results;
}
/**
* Remove issues when the profile has changed
*
* @param array $current_data Saved data before changes.
* @param array $data Data after changes.
*/
protected function handle_profile_change( $current_data, $data ) {
// If the profile has been changed, remove issues.
if ( $current_data['profile'] === $data['profile'] ) {
return;
}
$this->reload_issues();
}
/**
* Get the current GSC profile
*
* @return string
*/
protected function get_profile() {
return WPSEO_GSC_Settings::get_profile();
}
/**
* Reload GSC issues
*/
protected function reload_issues() {
WPSEO_GSC_Settings::reload_issues();
}
/**
* Gets a list with the profiles.
*
* @return array
*/
protected function get_profilelist() {
$profiles = array();
$sites = $this->gsc_service->get_sites();
foreach ( $sites as $siteKey => $siteValue ) {
$profiles[ untrailingslashit( $siteKey ) ] = untrailingslashit( $siteValue );
}
return $profiles;
}
/**
* Checks if there is an access token. If so, there is a connection.
*
* @return bool
*/
private function hasAccessToken() {
return ( null !== $this->gsc_service->get_client()->getAccessToken() );
}
}