-
Notifications
You must be signed in to change notification settings - Fork 291
/
Settings.php
256 lines (234 loc) · 7.21 KB
/
Settings.php
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
<?php
/**
* Class Google\Site_Kit\Modules\AdSense\Settings
*
* @package Google\Site_Kit\Modules\AdSense
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Modules\AdSense;
use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Storage\Setting_With_Legacy_Keys_Trait;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface;
/**
* Class for AdSense settings.
*
* @since 1.2.0
* @access private
* @ignore
*/
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface, Setting_With_ViewOnly_Keys_Interface {
use Setting_With_Legacy_Keys_Trait;
use Setting_With_Owned_Keys_Trait;
const OPTION = 'googlesitekit_adsense_settings';
/**
* Various ad blocking recovery setup statuses.
*/
const AD_BLOCKING_RECOVERY_SETUP_STATUS_TAG_PLACED = 'tag-placed';
const AD_BLOCKING_RECOVERY_SETUP_STATUS_SETUP_CONFIRMED = 'setup-confirmed';
/**
* Legacy account statuses to be migrated on-the-fly.
*
* @since 1.9.0
* @var array
*/
protected $legacy_account_statuses = array(
'account-connected' => array(
'accountStatus' => 'approved',
'siteStatus' => 'added',
),
'account-connected-nonmatching' => array(
'accountStatus' => 'approved',
'siteStatus' => 'added',
),
'account-connected-no-data' => array(
'accountStatus' => 'approved',
'siteStatus' => 'added',
),
'account-pending-review' => array(
'accountStatus' => 'approved',
'siteStatus' => 'none',
),
'account-required-action' => array(
'accountStatus' => 'no-client',
),
'disapproved-account-afc' => array(
'accountStatus' => 'no-client',
),
'ads-display-pending' => array(
'accountStatus' => 'pending',
),
'disapproved-account' => array(
'accountStatus' => 'disapproved',
),
'no-account' => array(
'accountStatus' => 'none',
),
'no-account-tag-found' => array(
'accountStatus' => 'none',
),
);
/**
* Registers the setting in WordPress.
*
* @since 1.2.0
*/
public function register() {
parent::register();
$this->register_legacy_keys_migration(
array(
'account_id' => 'accountID',
'accountId' => 'accountID',
'account_status' => 'accountStatus',
'adsenseTagEnabled' => 'useSnippet',
'client_id' => 'clientID',
'clientId' => 'clientID',
'setup_complete' => 'setupComplete',
)
);
$this->register_owned_keys();
add_filter(
'option_' . self::OPTION,
function ( $option ) {
/**
* Filters the AdSense account ID to use.
*
* @since 1.0.0
*
* @param string $account_id Empty by default, will fall back to the option value if not set.
*/
$account_id = apply_filters( 'googlesitekit_adsense_account_id', '' );
if ( $account_id ) {
$option['accountID'] = $account_id;
}
// Migrate legacy account statuses (now split into account status and site status).
if ( ! empty( $option['accountStatus'] ) && isset( $this->legacy_account_statuses[ $option['accountStatus'] ] ) ) {
foreach ( $this->legacy_account_statuses[ $option['accountStatus'] ] as $key => $value ) {
$option[ $key ] = $value;
}
}
// Migration of legacy setting.
if ( ! empty( $option['setupComplete'] ) ) {
$option['accountSetupComplete'] = $option['setupComplete'];
$option['siteSetupComplete'] = $option['setupComplete'];
}
unset( $option['setupComplete'] );
return $option;
}
);
add_filter(
'pre_update_option_' . self::OPTION,
function ( $value, $old_value ) {
if ( isset( $old_value['setupCompletedTimestamp'] ) ) {
return $value;
}
if ( ! empty( $old_value['accountStatus'] ) && ! empty( $old_value['siteStatus'] ) && 'ready' === $old_value['accountStatus'] && 'ready' === $old_value['siteStatus'] ) {
$value['setupCompletedTimestamp'] = strtotime( '-1 month' );
} elseif ( ! empty( $value['accountStatus'] ) && ! empty( $value['siteStatus'] ) && 'ready' === $value['accountStatus'] && 'ready' === $value['siteStatus'] ) {
$value['setupCompletedTimestamp'] = time();
}
return $value;
},
10,
2
);
}
/**
* Returns keys for owned settings.
*
* @since 1.16.0
*
* @return array An array of keys for owned settings.
*/
public function get_owned_keys() {
return array(
'accountID',
'clientID',
);
}
/**
* Returns keys for view-only settings.
*
* @since 1.122.0
*
* @return array An array of keys for view-only settings.
*/
public function get_view_only_keys() {
return array( 'accountID' );
}
/**
* Gets the default value.
*
* @since 1.2.0
* @since 1.102.0 Added settings for the Ad Blocking Recovery feature.
*
* @return array
*/
protected function get_default() {
return array(
'ownerID' => 0,
'accountID' => '',
'autoAdsDisabled' => array(),
'clientID' => '',
'accountStatus' => '',
'siteStatus' => '',
'accountSetupComplete' => false,
'siteSetupComplete' => false,
'useSnippet' => true,
'webStoriesAdUnit' => '',
'setupCompletedTimestamp' => null,
'useAdBlockingRecoverySnippet' => false,
'useAdBlockingRecoveryErrorSnippet' => false,
'adBlockingRecoverySetupStatus' => '',
);
}
/**
* Gets the callback for sanitizing the setting's value before saving.
*
* @since 1.6.0
*
* @return callable|null
*/
protected function get_sanitize_callback() {
return function ( $option ) {
if ( is_array( $option ) ) {
if ( isset( $option['accountSetupComplete'] ) ) {
$option['accountSetupComplete'] = (bool) $option['accountSetupComplete'];
}
if ( isset( $option['siteStatusComplete'] ) ) {
$option['siteStatusComplete'] = (bool) $option['siteStatusComplete'];
}
if ( isset( $option['useSnippet'] ) ) {
$option['useSnippet'] = (bool) $option['useSnippet'];
}
if ( isset( $option['autoAdsDisabled'] ) ) {
$option['autoAdsDisabled'] = (array) $option['autoAdsDisabled'];
}
if ( isset( $option['useAdBlockingRecoverySnippet'] ) ) {
$option['useAdBlockingRecoverySnippet'] = (bool) $option['useAdBlockingRecoverySnippet'];
}
if ( isset( $option['useAdBlockingRecoveryErrorSnippet'] ) ) {
$option['useAdBlockingRecoveryErrorSnippet'] = (bool) $option['useAdBlockingRecoveryErrorSnippet'];
}
if (
isset( $option['adBlockingRecoverySetupStatus'] ) &&
! in_array(
$option['adBlockingRecoverySetupStatus'],
array(
'',
self::AD_BLOCKING_RECOVERY_SETUP_STATUS_TAG_PLACED,
self::AD_BLOCKING_RECOVERY_SETUP_STATUS_SETUP_CONFIRMED,
),
true
)
) {
$option['adBlockingRecoverySetupStatus'] = $this->get()['adBlockingRecoverySetupStatus'];
}
}
return $option;
};
}
}