PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / configuration / first-time-configuration-action.php

first-time-configuration-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/actions/configuration/first-time-configuration-action.php

345 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Actions\Configuration;
4
5 use Yoast\WP\SEO\Helpers\Options_Helper;
6 use Yoast\WP\SEO\Helpers\Social_Profiles_Helper;
7
8 /**
9 * Class First_Time_Configuration_Action.
10 */
11 class First_Time_Configuration_Action {
12
13 /**
14 * The fields for the site representation payload.
15 */
16 public const SITE_REPRESENTATION_FIELDS = [
17 'company_or_person',
18 'company_name',
19 'website_name',
20 'company_logo',
21 'company_logo_id',
22 'person_logo',
23 'person_logo_id',
24 'company_or_person_user_id',
25 'description',
26 ];
27
28 /**
29 * The Options_Helper instance.
30 *
31 * @var Options_Helper
32 */
33 protected $options_helper;
34
35 /**
36 * The Social_Profiles_Helper instance.
37 *
38 * @var Social_Profiles_Helper
39 */
40 protected $social_profiles_helper;
41
42 /**
43 * First_Time_Configuration_Action constructor.
44 *
45 * @param Options_Helper $options_helper The WPSEO options helper.
46 * @param Social_Profiles_Helper $social_profiles_helper The social profiles helper.
47 */
48 public function __construct( Options_Helper $options_helper, Social_Profiles_Helper $social_profiles_helper ) {
49 $this->options_helper = $options_helper;
50 $this->social_profiles_helper = $social_profiles_helper;
51 }
52
53 /**
54 * Stores the values for the site representation.
55 *
56 * @param array $params The values to store.
57 *
58 * @return object The response object.
59 */
60 public function set_site_representation( $params ) {
61 $failures = [];
62 $old_values = $this->get_old_values( self::SITE_REPRESENTATION_FIELDS );
63
64 foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) {
65 if ( isset( $params[ $field_name ] ) ) {
66 $result = $this->options_helper->set( $field_name, $params[ $field_name ] );
67
68 if ( ! $result ) {
69 $failures[] = $field_name;
70 }
71 }
72 }
73
74 // Delete cached logos in the db.
75 $this->options_helper->set( 'company_logo_meta', false );
76 $this->options_helper->set( 'person_logo_meta', false );
77
78 /**
79 * Action: 'wpseo_post_update_site_representation' - Allows for Hiive event tracking.
80 *
81 * @param array $params The new values of the options.
82 * @param array $old_values The old values of the options.
83 * @param array $failures The options that failed to be saved.
84 *
85 * @internal
86 */
87 \do_action( 'wpseo_ftc_post_update_site_representation', $params, $old_values, $failures );
88
89 if ( \count( $failures ) === 0 ) {
90 return (object) [
91 'success' => true,
92 'status' => 200,
93 ];
94 }
95
96 return (object) [
97 'success' => false,
98 'status' => 500,
99 'error' => 'Could not save some options in the database',
100 'failures' => $failures,
101 ];
102 }
103
104 /**
105 * Stores the values for the social profiles.
106 *
107 * @param array $params The values to store.
108 *
109 * @return object The response object.
110 */
111 public function set_social_profiles( $params ) {
112 $old_values = $this->get_old_values( \array_keys( $this->social_profiles_helper->get_organization_social_profile_fields() ) );
113 $failures = $this->social_profiles_helper->set_organization_social_profiles( $params );
114
115 /**
116 * Action: 'wpseo_post_update_social_profiles' - Allows for Hiive event tracking.
117 *
118 * @param array $params The new values of the options.
119 * @param array $old_values The old values of the options.
120 * @param array $failures The options that failed to be saved.
121 *
122 * @internal
123 */
124 \do_action( 'wpseo_ftc_post_update_social_profiles', $params, $old_values, $failures );
125
126 if ( empty( $failures ) ) {
127 return (object) [
128 'success' => true,
129 'status' => 200,
130 ];
131 }
132
133 return (object) [
134 'success' => false,
135 'status' => 200,
136 'error' => 'Could not save some options in the database',
137 'failures' => $failures,
138 ];
139 }
140
141 /**
142 * Stores the values for the social profiles.
143 *
144 * @param array $params The values to store.
145 *
146 * @return object The response object.
147 */
148 public function set_person_social_profiles( $params ) {
149 $social_profiles = \array_filter(
150 $params,
151 static function ( $key ) {
152 return $key !== 'user_id';
153 },
154 \ARRAY_FILTER_USE_KEY,
155 );
156
157 $failures = $this->social_profiles_helper->set_person_social_profiles( $params['user_id'], $social_profiles );
158
159 if ( \count( $failures ) === 0 ) {
160 return (object) [
161 'success' => true,
162 'status' => 200,
163 ];
164 }
165
166 return (object) [
167 'success' => false,
168 'status' => 200,
169 'error' => 'Could not save some options in the database',
170 'failures' => $failures,
171 ];
172 }
173
174 /**
175 * Gets the values for the social profiles.
176 *
177 * @param int $user_id The person ID.
178 *
179 * @return object The response object.
180 */
181 public function get_person_social_profiles( $user_id ) {
182
183 return (object) [
184 'success' => true,
185 'status' => 200,
186 'social_profiles' => $this->social_profiles_helper->get_person_social_profiles( $user_id ),
187 ];
188 }
189
190 /**
191 * Stores the values to enable/disable tracking.
192 *
193 * @param array $params The values to store.
194 *
195 * @return object The response object.
196 */
197 public function set_enable_tracking( $params ) {
198 $success = true;
199 $option_value = $this->options_helper->get( 'tracking' );
200
201 if ( $option_value !== $params['tracking'] ) {
202 $this->options_helper->set( 'toggled_tracking', true );
203 $success = $this->options_helper->set( 'tracking', $params['tracking'] );
204 }
205
206 /**
207 * Action: 'wpseo_post_update_enable_tracking' - Allows for Hiive event tracking.
208 *
209 * @param array $new_value The new value.
210 * @param array $old_value The old value.
211 * @param bool $failure Whether the option failed to be stored.
212 *
213 * @internal
214 */
215 // $success is negated to be aligned with the other two actions which pass $failures.
216 \do_action( 'wpseo_ftc_post_update_enable_tracking', $params['tracking'], $option_value, ! $success );
217
218 if ( $success ) {
219 return (object) [
220 'success' => true,
221 'status' => 200,
222 ];
223 }
224
225 return (object) [
226 'success' => false,
227 'status' => 500,
228 'error' => 'Could not save the option in the database',
229 ];
230 }
231
232 /**
233 * Checks if the current user has the capability a specific user.
234 *
235 * @param int $user_id The id of the user to be edited.
236 *
237 * @return object The response object.
238 */
239 public function check_capability( $user_id ) {
240 if ( $this->can_edit_profile( $user_id ) ) {
241 return (object) [
242 'success' => true,
243 'status' => 200,
244 ];
245 }
246
247 return (object) [
248 'success' => false,
249 'status' => 403,
250 ];
251 }
252
253 /**
254 * Stores the first time configuration state.
255 *
256 * @param array $params The values to store.
257 *
258 * @return object The response object.
259 */
260 public function save_configuration_state( $params ) {
261 // If the finishedSteps param is not present in the REST request, it's a malformed request.
262 if ( ! isset( $params['finishedSteps'] ) ) {
263 return (object) [
264 'success' => false,
265 'status' => 400,
266 'error' => 'Bad request',
267 ];
268 }
269
270 // Sanitize input.
271 $finished_steps = \array_map( '\sanitize_text_field', \wp_unslash( $params['finishedSteps'] ) );
272
273 $success = $this->options_helper->set( 'configuration_finished_steps', $finished_steps );
274
275 if ( ! $success ) {
276 return (object) [
277 'success' => false,
278 'status' => 500,
279 'error' => 'Could not save the option in the database',
280 ];
281 }
282
283 // If all the five steps of the configuration have been completed, set first_time_install option to false.
284 if ( \count( $params['finishedSteps'] ) === 3 ) {
285 $this->options_helper->set( 'first_time_install', false );
286 }
287
288 return (object) [
289 'success' => true,
290 'status' => 200,
291 ];
292 }
293
294 /**
295 * Gets the first time configuration state.
296 *
297 * @return object The response object.
298 */
299 public function get_configuration_state() {
300 $configuration_option = $this->options_helper->get( 'configuration_finished_steps' );
301
302 if ( $configuration_option !== null ) {
303 return (object) [
304 'success' => true,
305 'status' => 200,
306 'data' => $configuration_option,
307 ];
308 }
309
310 return (object) [
311 'success' => false,
312 'status' => 500,
313 'error' => 'Could not get data from the database',
314 ];
315 }
316
317 /**
318 * Checks if the current user has the capability to edit a specific user.
319 *
320 * @param int $person_id The id of the person to edit.
321 *
322 * @return bool
323 */
324 private function can_edit_profile( $person_id ) {
325 return \current_user_can( 'edit_user', $person_id );
326 }
327
328 /**
329 * Gets the old values for the given fields.
330 *
331 * @param array $fields_names The fields to get the old values for.
332 *
333 * @return array The old values.
334 */
335 private function get_old_values( array $fields_names ): array {
336 $old_values = [];
337
338 foreach ( $fields_names as $field_name ) {
339 $old_values[ $field_name ] = $this->options_helper->get( $field_name );
340 }
341
342 return $old_values;
343 }
344 }
345