• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 59517635615d055f783a2be92e52a1e2637df1be

17 Feb 2025 11:08AM UTC coverage: 53.377% (-1.3%) from 54.636%
59517635615d055f783a2be92e52a1e2637df1be

Pull #22048

github

web-flow
Merge e41dbb150 into 711656c23
Pull Request #22048: Update Dashboard page description

7808 of 13867 branches covered (56.31%)

Branch coverage included in aggregate %.

4 of 5 new or added lines in 2 files covered. (80.0%)

1554 existing lines in 42 files now uncovered.

30279 of 57488 relevant lines covered (52.67%)

40022.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
/src/actions/configuration/first-time-configuration-action.php
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 ) {
2✔
49
                $this->options_helper         = $options_helper;
2✔
50
                $this->social_profiles_helper = $social_profiles_helper;
2✔
51
        }
1✔
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 ) {
6✔
61
                $failures   = [];
6✔
62
                $old_values = $this->get_old_values( self::SITE_REPRESENTATION_FIELDS );
6✔
63

64
                foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) {
6✔
65
                        if ( isset( $params[ $field_name ] ) ) {
6✔
66
                                $result = $this->options_helper->set( $field_name, $params[ $field_name ] );
6✔
67

68
                                if ( ! $result ) {
6✔
69
                                        $failures[] = $field_name;
2✔
70
                                }
71
                        }
72
                }
73

74
                // Delete cached logos in the db.
75
                $this->options_helper->set( 'company_logo_meta', false );
6✔
76
                $this->options_helper->set( 'person_logo_meta', false );
6✔
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 );
6✔
88

89
                if ( \count( $failures ) === 0 ) {
6✔
90
                        return (object) [
2✔
91
                                'success' => true,
4✔
92
                                'status'  => 200,
2✔
93
                        ];
2✔
94
                }
95

96
                return (object) [
1✔
97
                        'success'  => false,
2✔
98
                        'status'   => 500,
2✔
99
                        'error'    => 'Could not save some options in the database',
2✔
100
                        'failures' => $failures,
2✔
101
                ];
1✔
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 ) {
6✔
112
                $old_values = $this->get_old_values( \array_keys( $this->social_profiles_helper->get_organization_social_profile_fields() ) );
6✔
113
                $failures   = $this->social_profiles_helper->set_organization_social_profiles( $params );
6✔
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 );
6✔
125

126
                if ( empty( $failures ) ) {
6✔
127
                        return (object) [
1✔
128
                                'success' => true,
2✔
129
                                'status'  => 200,
1✔
130
                        ];
1✔
131
                }
132

133
                return (object) [
2✔
134
                        'success'  => false,
4✔
135
                        'status'   => 200,
4✔
136
                        'error'    => 'Could not save some options in the database',
4✔
137
                        'failures' => $failures,
4✔
138
                ];
2✔
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 ) {
UNCOV
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 ) {
10✔
198
                $success      = true;
10✔
199
                $option_value = $this->options_helper->get( 'tracking' );
200

201
                if ( $option_value !== $params['tracking'] ) {
6✔
202
                        $this->options_helper->set( 'toggled_tracking', true );
6✔
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) [
8✔
220
                                'success' => true,
4✔
221
                                'status'  => 200,
4✔
222
                        ];
4✔
223
                }
224

225
                return (object) [
2✔
226
                        'success' => false,
1✔
227
                        'status'  => 500,
1✔
228
                        'error'   => 'Could not save the option in the database',
1✔
229
                ];
1✔
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 ) {
2✔
240
                if ( $this->can_edit_profile( $user_id ) ) {
241
                        return (object) [
2✔
242
                                'success' => true,
1✔
243
                                'status'  => 200,
1✔
244
                        ];
1✔
245
                }
246

247
                return (object) [
2✔
248
                        'success' => false,
1✔
249
                        'status'  => 403,
1✔
250
                ];
1✔
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 ) {
4✔
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) [
2✔
264
                                'success' => false,
1✔
265
                                'status'  => 400,
1✔
266
                                'error'   => 'Bad request',
1✔
267
                        ];
1✔
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) [
2✔
277
                                'success' => false,
1✔
278
                                'status'  => 500,
1✔
279
                                'error'   => 'Could not save the option in the database',
1✔
280
                        ];
1✔
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 ) {
2✔
285
                        $this->options_helper->set( 'first_time_install', false );
286
                }
287

288
                return (object) [
4✔
289
                        'success' => true,
2✔
290
                        'status'  => 200,
2✔
291
                ];
2✔
292
        }
293

294
        /**
295
         * Gets the first time configuration state.
296
         *
297
         * @return object The response object.
298
         */
299
        public function get_configuration_state() {
2✔
300
                $configuration_option = $this->options_helper->get( 'configuration_finished_steps' );
301

302
                if ( ! \is_null( $configuration_option ) ) {
303
                        return (object) [
2✔
304
                                'success' => true,
2✔
305
                                'status'  => 200,
2✔
306
                                'data'    => $configuration_option,
1✔
307
                        ];
1✔
308
                }
309

310
                return (object) [
2✔
311
                        'success' => false,
1✔
312
                        'status'  => 500,
1✔
313
                        'error'   => 'Could not get data from the database',
1✔
314
                ];
1✔
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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc