first-time-configuration-action.php
8.49 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
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
<?php
namespace Yoast\WP\SEO\Actions\Configuration;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Helpers\Social_Profiles_Helper;
/**
* Class First_Time_Configuration_Action.
*/
class First_Time_Configuration_Action {
/**
* The fields for the site representation payload.
*/
const SITE_REPRESENTATION_FIELDS = [
'company_or_person',
'company_name',
'website_name',
'company_logo',
'company_logo_id',
'person_logo',
'person_logo_id',
'company_or_person_user_id',
'description',
];
/**
* The Options_Helper instance.
*
* @var Options_Helper
*/
protected $options_helper;
/**
* The Social_Profiles_Helper instance.
*
* @var Social_Profiles_Helper
*/
protected $social_profiles_helper;
/**
* First_Time_Configuration_Action constructor.
*
* @param Options_Helper $options_helper The WPSEO options helper.
* @param Social_Profiles_Helper $social_profiles_helper The social profiles helper.
*/
public function __construct( Options_Helper $options_helper, Social_Profiles_Helper $social_profiles_helper ) {
$this->options_helper = $options_helper;
$this->social_profiles_helper = $social_profiles_helper;
}
/**
* Stores the values for the site representation.
*
* @param array $params The values to store.
*
* @return object The response object.
*/
public function set_site_representation( $params ) {
$failures = [];
$old_values = $this->get_old_values( self::SITE_REPRESENTATION_FIELDS );
foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) {
if ( isset( $params[ $field_name ] ) ) {
$result = $this->options_helper->set( $field_name, $params[ $field_name ] );
if ( ! $result ) {
$failures[] = $field_name;
}
}
}
// Delete cached logos in the db.
$this->options_helper->set( 'company_logo_meta', false );
$this->options_helper->set( 'person_logo_meta', false );
/**
* Action: 'wpseo_post_update_site_representation' - Allows for Hiive event tracking.
*
* @param array The new values of the options.
* @param array The old values of the options.
* @param array The options that failed to be saved.
*
* @internal
*/
\do_action( 'wpseo_ftc_post_update_site_representation', $params, $old_values, $failures );
if ( \count( $failures ) === 0 ) {
return (object) [
'success' => true,
'status' => 200,
];
}
return (object) [
'success' => false,
'status' => 500,
'error' => 'Could not save some options in the database',
'failures' => $failures,
];
}
/**
* Stores the values for the social profiles.
*
* @param array $params The values to store.
*
* @return object The response object.
*/
public function set_social_profiles( $params ) {
$old_values = $this->get_old_values( \array_keys( $this->social_profiles_helper->get_organization_social_profile_fields() ) );
$failures = $this->social_profiles_helper->set_organization_social_profiles( $params );
/**
* Action: 'wpseo_post_update_social_profiles' - Allows for Hiive event tracking.
*
* @param array The new values of the options.
* @param array The old values of the options.
* @param array The options that failed to be saved.
*
* @internal
*/
\do_action( 'wpseo_ftc_post_update_social_profiles', $params, $old_values, $failures );
if ( empty( $failures ) ) {
return (object) [
'success' => true,
'status' => 200,
];
}
return (object) [
'success' => false,
'status' => 200,
'error' => 'Could not save some options in the database',
'failures' => $failures,
];
}
/**
* Stores the values for the social profiles.
*
* @param array $params The values to store.
*
* @return object The response object.
*/
public function set_person_social_profiles( $params ) {
$social_profiles = \array_filter(
$params,
static function ( $key ) {
return $key !== 'user_id';
},
\ARRAY_FILTER_USE_KEY
);
$failures = $this->social_profiles_helper->set_person_social_profiles( $params['user_id'], $social_profiles );
if ( \count( $failures ) === 0 ) {
return (object) [
'success' => true,
'status' => 200,
];
}
return (object) [
'success' => false,
'status' => 200,
'error' => 'Could not save some options in the database',
'failures' => $failures,
];
}
/**
* Gets the values for the social profiles.
*
* @param int $user_id The person ID.
*
* @return object The response object.
*/
public function get_person_social_profiles( $user_id ) {
return (object) [
'success' => true,
'status' => 200,
'social_profiles' => $this->social_profiles_helper->get_person_social_profiles( $user_id ),
];
}
/**
* Stores the values to enable/disable tracking.
*
* @param array $params The values to store.
*
* @return object The response object.
*/
public function set_enable_tracking( $params ) {
$success = true;
$option_value = $this->options_helper->get( 'tracking' );
if ( $option_value !== $params['tracking'] ) {
$this->options_helper->set( 'toggled_tracking', true );
$success = $this->options_helper->set( 'tracking', $params['tracking'] );
}
/**
* Action: 'wpseo_post_update_enable_tracking' - Allows for Hiive event tracking.
*
* @param array The new value.
* @param array The old value.
* @param bool Whether the option failed to be stored.
*
* @internal
*/
// $success is negated to be aligned with the other two actions which pass $failures.
\do_action( 'wpseo_ftc_post_update_enable_tracking', $params['tracking'], $option_value, ! $success );
if ( $success ) {
return (object) [
'success' => true,
'status' => 200,
];
}
return (object) [
'success' => false,
'status' => 500,
'error' => 'Could not save the option in the database',
];
}
/**
* Checks if the current user has the capability a specific user.
*
* @param int $user_id The id of the user to be edited.
*
* @return object The response object.
*/
public function check_capability( $user_id ) {
if ( $this->can_edit_profile( $user_id ) ) {
return (object) [
'success' => true,
'status' => 200,
];
}
return (object) [
'success' => false,
'status' => 403,
];
}
/**
* Stores the first time configuration state.
*
* @param array $params The values to store.
*
* @return object The response object.
*/
public function save_configuration_state( $params ) {
// If the finishedSteps param is not present in the REST request, it's a malformed request.
if ( ! isset( $params['finishedSteps'] ) ) {
return (object) [
'success' => false,
'status' => 400,
'error' => 'Bad request',
];
}
// Sanitize input.
$finished_steps = \array_map( '\sanitize_text_field', \wp_unslash( $params['finishedSteps'] ) );
$success = $this->options_helper->set( 'configuration_finished_steps', $finished_steps );
if ( ! $success ) {
return (object) [
'success' => false,
'status' => 500,
'error' => 'Could not save the option in the database',
];
}
// If all the five steps of the configuration have been completed, set first_time_install option to false.
if ( \count( $params['finishedSteps'] ) === 3 ) {
$this->options_helper->set( 'first_time_install', false );
}
return (object) [
'success' => true,
'status' => 200,
];
}
/**
* Gets the first time configuration state.
*
* @return object The response object.
*/
public function get_configuration_state() {
$configuration_option = $this->options_helper->get( 'configuration_finished_steps' );
if ( ! \is_null( $configuration_option ) ) {
return (object) [
'success' => true,
'status' => 200,
'data' => $configuration_option,
];
}
return (object) [
'success' => false,
'status' => 500,
'error' => 'Could not get data from the database',
];
}
/**
* Checks if the current user has the capability to edit a specific user.
*
* @param int $person_id The id of the person to edit.
*
* @return bool
*/
private function can_edit_profile( $person_id ) {
return \current_user_can( 'edit_user', $person_id );
}
/**
* Gets the old values for the given fields.
*
* @param array $fields_names The fields to get the old values for.
*
* @return array The old values.
*/
private function get_old_values( array $fields_names ) : array {
$old_values = [];
foreach ( $fields_names as $field_name ) {
$old_values[ $field_name ] = $this->options_helper->get( $field_name );
}
return $old_values;
}
}