PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
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 18.9, at src/actions/configuration/first-time-configuration-action.php

280 lines 6.7 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\Integrations\Admin\Social_Profiles_Helper;
7
8 // phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- First time configuration simply has a lot of words.
9 /**
10 * Class First_Time_Configuration_Action.
11 */
12 class First_Time_Configuration_Action {
13
14 /**
15 * The fields for the site representation payload.
16 */
17 const SITE_REPRESENTATION_FIELDS = [
18 'company_or_person',
19 'company_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
63 foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) {
64 if ( isset( $params[ $field_name ] ) ) {
65 if ( $field_name === 'description' && \current_user_can( 'manage_options' ) ) {
66 $result = \update_option( 'blogdescription', $params['description'] );
67 if ( ! $result && $params['description'] === \get_option( 'blogdescription' ) ) {
68 $result = true;
69 }
70 }
71 else {
72 $result = $this->options_helper->set( $field_name, $params[ $field_name ] );
73 }
74 if ( ! $result ) {
75 $failures[] = $field_name;
76 }
77 }
78 }
79
80 if ( \count( $failures ) === 0 ) {
81 return (object) [
82 'success' => true,
83 'status' => 200,
84 ];
85 }
86 return (object) [
87 'success' => false,
88 'status' => 500,
89 'error' => 'Could not save some options in the database',
90 'failures' => $failures,
91 ];
92 }
93
94 /**
95 * Stores the values for the social profiles.
96 *
97 * @param array $params The values to store.
98 *
99 * @return object The response object.
100 */
101 public function set_social_profiles( $params ) {
102 $failures = $this->social_profiles_helper->set_organization_social_profiles( $params );
103
104 if ( empty( $failures ) ) {
105 return (object) [
106 'success' => true,
107 'status' => 200,
108 ];
109 }
110
111 return (object) [
112 'success' => false,
113 'status' => 200,
114 'error' => 'Could not save some options in the database',
115 'failures' => $failures,
116 ];
117 }
118
119 /**
120 * Stores the values for the social profiles.
121 *
122 * @param array $params The values to store.
123 *
124 * @return object The response object.
125 */
126 public function set_person_social_profiles( $params ) {
127 $social_profiles = \array_filter(
128 $params,
129 function ( $key ) {
130 return $key !== 'user_id';
131 },
132 ARRAY_FILTER_USE_KEY
133 );
134
135 $failures = $this->social_profiles_helper->set_person_social_profiles( $params['user_id'], $social_profiles );
136
137 if ( \count( $failures ) === 0 ) {
138 return (object) [
139 'success' => true,
140 'status' => 200,
141 ];
142 }
143 return (object) [
144 'success' => false,
145 'status' => 200,
146 'error' => 'Could not save some options in the database',
147 'failures' => $failures,
148 ];
149 }
150
151 /**
152 * Gets the values for the social profiles.
153 *
154 * @param int $user_id the person id.
155 *
156 * @return object The response object.
157 */
158 public function get_person_social_profiles( $user_id ) {
159
160 return (object) [
161 'success' => true,
162 'status' => 200,
163 'social_profiles' => $this->social_profiles_helper->get_person_social_profiles( $user_id ),
164 ];
165 }
166
167 /**
168 * Stores the values to enable/disable tracking.
169 *
170 * @param array $params The values to store.
171 *
172 * @return object The response object.
173 */
174 public function set_enable_tracking( $params ) {
175 $success = true;
176 $option_value = $this->options_helper->get( 'tracking' );
177
178 if ( $option_value !== $params['tracking'] ) {
179 $success = $this->options_helper->set( 'tracking', $params['tracking'] );
180 }
181
182 if ( $success ) {
183 return (object) [
184 'success' => true,
185 'status' => 200,
186 ];
187 }
188 return (object) [
189 'success' => false,
190 'status' => 500,
191 'error' => 'Could not save the option in the database',
192 ];
193 }
194
195 /**
196 * Checks if the current user has the capability a specific user.
197 *
198 * @param int $user_id The id of the user to be edited.
199 *
200 * @return object The response object.
201 */
202 public function check_capability( $user_id ) {
203 if ( $this->social_profiles_helper->can_edit_profile( $user_id ) ) {
204 return (object) [
205 'success' => true,
206 'status' => 200,
207 ];
208 }
209
210 return (object) [
211 'success' => false,
212 'status' => 403,
213 ];
214 }
215
216 /**
217 * Stores the first time configuration state.
218 *
219 * @param array $params The values to store.
220 *
221 * @return object The response object.
222 */
223 public function save_configuration_state( $params ) {
224 // If the finishedSteps param is not present in the REST request, it's a malformed request.
225 if ( ! isset( $params['finishedSteps'] ) ) {
226 return (object) [
227 'success' => false,
228 'status' => 400,
229 'error' => 'Bad request',
230 ];
231 }
232
233 // Sanitize input.
234 $finished_steps = \array_map( '\sanitize_text_field', \wp_unslash( $params['finishedSteps'] ) );
235
236 $success = $this->options_helper->set( 'configuration_finished_steps', $finished_steps );
237
238 if ( ! $success ) {
239 return (object) [
240 'success' => false,
241 'status' => 500,
242 'error' => 'Could not save the option in the database',
243 ];
244 }
245
246 // If all the five steps of the configuration have been completed, set first_time_install option to false.
247 if ( \count( $params['finishedSteps'] ) === 3 ) {
248 $this->options_helper->set( 'first_time_install', false );
249 }
250
251 return (object) [
252 'success' => true,
253 'status' => 200,
254 ];
255 }
256
257 /**
258 * Gets the first time configuration state.
259 *
260 * @return object The response object.
261 */
262 public function get_configuration_state() {
263 $configuration_option = $this->options_helper->get( 'configuration_finished_steps' );
264
265 if ( ! is_null( $configuration_option ) ) {
266 return (object) [
267 'success' => true,
268 'status' => 200,
269 'data' => $configuration_option,
270 ];
271 }
272
273 return (object) [
274 'success' => false,
275 'status' => 500,
276 'error' => 'Could not get data from the database',
277 ];
278 }
279 }
280