← All changes
|
src/actions/configuration/first-time-configuration-action.php
+80
-19
19.0
→
trunk
View file →
| @@ -2,11 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Actions\Configuration; |
| 4 | 4 | |
| 5 | 5 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 6 | -use Yoast\WP\SEO\Integrations\Admin\Social_Profiles_Helper; | |
| 6 | +use Yoast\WP\SEO\Helpers\Social_Profiles_Helper; | |
| 7 | 7 | |
| 8 | -// phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- First time configuration simply has a lot of words. | |
| 9 | 8 | /** |
| 10 | 9 | * Class First_Time_Configuration_Action. |
| 11 | 10 | */ |
| 12 | 11 | class First_Time_Configuration_Action { |
| @@ -13,11 +12,12 @@ | ||
| 13 | 12 | |
| 14 | 13 | /** |
| 15 | 14 | * The fields for the site representation payload. |
| 16 | 15 | */ |
| 17 | - const SITE_REPRESENTATION_FIELDS = [ | |
| 16 | + public const SITE_REPRESENTATION_FIELDS = [ | |
| 18 | 17 | 'company_or_person', |
| 19 | 18 | 'company_name', |
| 19 | + 'website_name', | |
| 20 | 20 | 'company_logo', |
| 21 | 21 | 'company_logo_id', |
| 22 | 22 | 'person_logo', |
| 23 | 23 | 'person_logo_id', |
| @@ -57,21 +57,15 @@ | ||
| 57 | 57 | * |
| 58 | 58 | * @return object The response object. |
| 59 | 59 | */ |
| 60 | 60 | public function set_site_representation( $params ) { |
| 61 | - $failures = []; | |
| 61 | + $failures = []; | |
| 62 | + $old_values = $this->get_old_values( self::SITE_REPRESENTATION_FIELDS ); | |
| 62 | 63 | |
| 63 | 64 | foreach ( self::SITE_REPRESENTATION_FIELDS as $field_name ) { |
| 64 | 65 | 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 | - } | |
| 66 | + $result = $this->options_helper->set( $field_name, $params[ $field_name ] ); | |
| 67 | + | |
| 74 | 68 | if ( ! $result ) { |
| 75 | 69 | $failures[] = $field_name; |
| 76 | 70 | } |
| 77 | 71 | } |
| @@ -80,8 +74,19 @@ | ||
| 80 | 74 | // Delete cached logos in the db. |
| 81 | 75 | $this->options_helper->set( 'company_logo_meta', false ); |
| 82 | 76 | $this->options_helper->set( 'person_logo_meta', false ); |
| 83 | 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 | + | |
| 84 | 89 | if ( \count( $failures ) === 0 ) { |
| 85 | 90 | return (object) [ |
| 86 | 91 | 'success' => true, |
| 87 | 92 | 'status' => 200, |
| @@ -86,8 +91,9 @@ | ||
| 86 | 91 | 'success' => true, |
| 87 | 92 | 'status' => 200, |
| 88 | 93 | ]; |
| 89 | 94 | } |
| 95 | + | |
| 90 | 96 | return (object) [ |
| 91 | 97 | 'success' => false, |
| 92 | 98 | 'status' => 500, |
| 93 | 99 | 'error' => 'Could not save some options in the database', |
| @@ -102,10 +108,22 @@ | ||
| 102 | 108 | * |
| 103 | 109 | * @return object The response object. |
| 104 | 110 | */ |
| 105 | 111 | public function set_social_profiles( $params ) { |
| 106 | - $failures = $this->social_profiles_helper->set_organization_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 ); | |
| 107 | 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 | + | |
| 108 | 126 | if ( empty( $failures ) ) { |
| 109 | 127 | return (object) [ |
| 110 | 128 | 'success' => true, |
| 111 | 129 | 'status' => 200, |
| @@ -129,12 +147,12 @@ | ||
| 129 | 147 | */ |
| 130 | 148 | public function set_person_social_profiles( $params ) { |
| 131 | 149 | $social_profiles = \array_filter( |
| 132 | 150 | $params, |
| 133 | - function ( $key ) { | |
| 151 | + static function ( $key ) { | |
| 134 | 152 | return $key !== 'user_id'; |
| 135 | 153 | }, |
| 136 | - ARRAY_FILTER_USE_KEY | |
| 154 | + \ARRAY_FILTER_USE_KEY, | |
| 137 | 155 | ); |
| 138 | 156 | |
| 139 | 157 | $failures = $this->social_profiles_helper->set_person_social_profiles( $params['user_id'], $social_profiles ); |
| 140 | 158 | |
| @@ -143,8 +161,9 @@ | ||
| 143 | 161 | 'success' => true, |
| 144 | 162 | 'status' => 200, |
| 145 | 163 | ]; |
| 146 | 164 | } |
| 165 | + | |
| 147 | 166 | return (object) [ |
| 148 | 167 | 'success' => false, |
| 149 | 168 | 'status' => 200, |
| 150 | 169 | 'error' => 'Could not save some options in the database', |
| @@ -154,9 +173,9 @@ | ||
| 154 | 173 | |
| 155 | 174 | /** |
| 156 | 175 | * Gets the values for the social profiles. |
| 157 | 176 | * |
| 158 | - * @param int $user_id the person id. | |
| 177 | + * @param int $user_id The person ID. | |
| 159 | 178 | * |
| 160 | 179 | * @return object The response object. |
| 161 | 180 | */ |
| 162 | 181 | public function get_person_social_profiles( $user_id ) { |
| @@ -179,11 +198,24 @@ | ||
| 179 | 198 | $success = true; |
| 180 | 199 | $option_value = $this->options_helper->get( 'tracking' ); |
| 181 | 200 | |
| 182 | 201 | if ( $option_value !== $params['tracking'] ) { |
| 202 | + $this->options_helper->set( 'toggled_tracking', true ); | |
| 183 | 203 | $success = $this->options_helper->set( 'tracking', $params['tracking'] ); |
| 184 | 204 | } |
| 185 | 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 | + | |
| 186 | 218 | if ( $success ) { |
| 187 | 219 | return (object) [ |
| 188 | 220 | 'success' => true, |
| 189 | 221 | 'status' => 200, |
| @@ -188,8 +220,9 @@ | ||
| 188 | 220 | 'success' => true, |
| 189 | 221 | 'status' => 200, |
| 190 | 222 | ]; |
| 191 | 223 | } |
| 224 | + | |
| 192 | 225 | return (object) [ |
| 193 | 226 | 'success' => false, |
| 194 | 227 | 'status' => 500, |
| 195 | 228 | 'error' => 'Could not save the option in the database', |
| @@ -203,9 +236,9 @@ | ||
| 203 | 236 | * |
| 204 | 237 | * @return object The response object. |
| 205 | 238 | */ |
| 206 | 239 | public function check_capability( $user_id ) { |
| 207 | - if ( $this->social_profiles_helper->can_edit_profile( $user_id ) ) { | |
| 240 | + if ( $this->can_edit_profile( $user_id ) ) { | |
| 208 | 241 | return (object) [ |
| 209 | 242 | 'success' => true, |
| 210 | 243 | 'status' => 200, |
| 211 | 244 | ]; |
| @@ -265,9 +298,9 @@ | ||
| 265 | 298 | */ |
| 266 | 299 | public function get_configuration_state() { |
| 267 | 300 | $configuration_option = $this->options_helper->get( 'configuration_finished_steps' ); |
| 268 | 301 | |
| 269 | - if ( ! is_null( $configuration_option ) ) { | |
| 302 | + if ( $configuration_option !== null ) { | |
| 270 | 303 | return (object) [ |
| 271 | 304 | 'success' => true, |
| 272 | 305 | 'status' => 200, |
| 273 | 306 | 'data' => $configuration_option, |
| @@ -278,6 +311,34 @@ | ||
| 278 | 311 | 'success' => false, |
| 279 | 312 | 'status' => 500, |
| 280 | 313 | 'error' => 'Could not get data from the database', |
| 281 | 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; | |
| 282 | 343 | } |
| 283 | 344 | } |