| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Routes; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
use WP_REST_Response; |
| 7 |
use Yoast\WP\SEO\Actions\Configuration\First_Time_Configuration_Action; |
| 8 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 9 |
use Yoast\WP\SEO\Main; |
| 10 |
|
| 11 |
// phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- First time configuration simply has a lot of words. |
| 12 |
/** |
| 13 |
* First_Time_Configuration_Route class. |
| 14 |
*/ |
| 15 |
class First_Time_Configuration_Route implements Route_Interface { |
| 16 |
|
| 17 |
use No_Conditionals; |
| 18 |
|
| 19 |
/** |
| 20 |
* Represents the first time configuration route. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
const CONFIGURATION_ROUTE = '/configuration'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Represents a site representation route. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
const SITE_REPRESENTATION_ROUTE = '/site_representation'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Represents a social profiles route. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
const SOCIAL_PROFILES_ROUTE = '/social_profiles'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Represents a person's social profiles route. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
const PERSON_SOCIAL_PROFILES_ROUTE = '/person_social_profiles'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Represents a route to enable/disable tracking. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
const ENABLE_TRACKING_ROUTE = '/enable_tracking'; |
| 53 |
|
| 54 |
/** |
| 55 |
* Represents a route to check if current user has the correct capabilities to edit another user's profile. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
const CHECK_CAPABILITY_ROUTE = '/check_capability'; |
| 60 |
|
| 61 |
/** |
| 62 |
* Represents a route to save the first time configuration state. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
const SAVE_CONFIGURATION_STATE_ROUTE = '/save_configuration_state'; |
| 67 |
|
| 68 |
/** |
| 69 |
* Represents a route to save the first time configuration state. |
| 70 |
* |
| 71 |
* @var string |
| 72 |
*/ |
| 73 |
const GET_CONFIGURATION_STATE_ROUTE = '/get_configuration_state'; |
| 74 |
|
| 75 |
/** |
| 76 |
* The first tinme configuration action. |
| 77 |
* |
| 78 |
* @var First_Time_Configuration_Action |
| 79 |
*/ |
| 80 |
private $first_time_configuration_action; |
| 81 |
|
| 82 |
/** |
| 83 |
* First_Time_Configuration_Route constructor. |
| 84 |
* |
| 85 |
* @param First_Time_Configuration_Action $first_time_configuration_action The first-time configuration action. |
| 86 |
*/ |
| 87 |
public function __construct( |
| 88 |
First_Time_Configuration_Action $first_time_configuration_action |
| 89 |
) { |
| 90 |
$this->first_time_configuration_action = $first_time_configuration_action; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Registers routes with WordPress. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function register_routes() { |
| 99 |
$site_representation_route = [ |
| 100 |
'methods' => 'POST', |
| 101 |
'callback' => [ $this, 'set_site_representation' ], |
| 102 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 103 |
'args' => [ |
| 104 |
'company_or_person' => [ |
| 105 |
'type' => 'string', |
| 106 |
'enum' => [ |
| 107 |
'company', |
| 108 |
'person', |
| 109 |
], |
| 110 |
'required' => true, |
| 111 |
], |
| 112 |
'company_name' => [ |
| 113 |
'type' => 'string', |
| 114 |
], |
| 115 |
'company_logo' => [ |
| 116 |
'type' => 'string', |
| 117 |
], |
| 118 |
'company_logo_id' => [ |
| 119 |
'type' => 'integer', |
| 120 |
], |
| 121 |
'person_logo' => [ |
| 122 |
'type' => 'string', |
| 123 |
], |
| 124 |
'person_logo_id' => [ |
| 125 |
'type' => 'integer', |
| 126 |
], |
| 127 |
'company_or_person_user_id' => [ |
| 128 |
'type' => 'integer', |
| 129 |
], |
| 130 |
'description' => [ |
| 131 |
'type' => 'string', |
| 132 |
], |
| 133 |
], |
| 134 |
]; |
| 135 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SITE_REPRESENTATION_ROUTE, $site_representation_route ); |
| 136 |
|
| 137 |
$social_profiles_route = [ |
| 138 |
'methods' => 'POST', |
| 139 |
'callback' => [ $this, 'set_social_profiles' ], |
| 140 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 141 |
'args' => [ |
| 142 |
'facebook_site' => [ |
| 143 |
'type' => 'string', |
| 144 |
], |
| 145 |
'twitter_site' => [ |
| 146 |
'type' => 'string', |
| 147 |
], |
| 148 |
'other_social_urls' => [ |
| 149 |
'type' => 'array', |
| 150 |
], |
| 151 |
], |
| 152 |
]; |
| 153 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SOCIAL_PROFILES_ROUTE, $social_profiles_route ); |
| 154 |
|
| 155 |
$person_social_profiles_route = [ |
| 156 |
[ |
| 157 |
'methods' => 'GET', |
| 158 |
'callback' => [ $this, 'get_person_social_profiles' ], |
| 159 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 160 |
'args' => [ |
| 161 |
'user_id' => [ |
| 162 |
'required' => true, |
| 163 |
], |
| 164 |
], |
| 165 |
], |
| 166 |
[ |
| 167 |
'methods' => 'POST', |
| 168 |
'callback' => [ $this, 'set_person_social_profiles' ], |
| 169 |
'permission_callback' => [ $this, 'can_edit_user' ], |
| 170 |
'args' => [ |
| 171 |
'user_id' => [ |
| 172 |
'type' => 'integer', |
| 173 |
], |
| 174 |
'facebook' => [ |
| 175 |
'type' => 'string', |
| 176 |
], |
| 177 |
'instagram' => [ |
| 178 |
'type' => 'string', |
| 179 |
], |
| 180 |
'linkedin' => [ |
| 181 |
'type' => 'string', |
| 182 |
], |
| 183 |
'myspace' => [ |
| 184 |
'type' => 'string', |
| 185 |
], |
| 186 |
'pinterest' => [ |
| 187 |
'type' => 'string', |
| 188 |
], |
| 189 |
'soundcloud' => [ |
| 190 |
'type' => 'string', |
| 191 |
], |
| 192 |
'tumblr' => [ |
| 193 |
'type' => 'string', |
| 194 |
], |
| 195 |
'twitter' => [ |
| 196 |
'type' => 'string', |
| 197 |
], |
| 198 |
'youtube' => [ |
| 199 |
'type' => 'string', |
| 200 |
], |
| 201 |
'wikipedia' => [ |
| 202 |
'type' => 'string', |
| 203 |
], |
| 204 |
], |
| 205 |
], |
| 206 |
]; |
| 207 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::PERSON_SOCIAL_PROFILES_ROUTE, $person_social_profiles_route ); |
| 208 |
|
| 209 |
$check_capability_route = [ |
| 210 |
'methods' => 'GET', |
| 211 |
'callback' => [ $this, 'check_capability' ], |
| 212 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 213 |
'args' => [ |
| 214 |
'user_id' => [ |
| 215 |
'required' => true, |
| 216 |
], |
| 217 |
], |
| 218 |
]; |
| 219 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::CHECK_CAPABILITY_ROUTE, $check_capability_route ); |
| 220 |
|
| 221 |
$enable_tracking_route = [ |
| 222 |
'methods' => 'POST', |
| 223 |
'callback' => [ $this, 'set_enable_tracking' ], |
| 224 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 225 |
'args' => [ |
| 226 |
'tracking' => [ |
| 227 |
'type' => 'boolean', |
| 228 |
'required' => true, |
| 229 |
], |
| 230 |
], |
| 231 |
]; |
| 232 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::ENABLE_TRACKING_ROUTE, $enable_tracking_route ); |
| 233 |
|
| 234 |
$save_configuration_state_route = [ |
| 235 |
'methods' => 'POST', |
| 236 |
'callback' => [ $this, 'save_configuration_state' ], |
| 237 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 238 |
'args' => [ |
| 239 |
'finishedSteps' => [ |
| 240 |
'type' => 'array', |
| 241 |
'required' => true, |
| 242 |
], |
| 243 |
], |
| 244 |
]; |
| 245 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SAVE_CONFIGURATION_STATE_ROUTE, $save_configuration_state_route ); |
| 246 |
|
| 247 |
$get_configuration_state_route = [ |
| 248 |
[ |
| 249 |
'methods' => 'GET', |
| 250 |
'callback' => [ $this, 'get_configuration_state' ], |
| 251 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 252 |
], |
| 253 |
]; |
| 254 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::GET_CONFIGURATION_STATE_ROUTE, $get_configuration_state_route ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Sets the site representation values. |
| 259 |
* |
| 260 |
* @param WP_REST_Request $request The request. |
| 261 |
* |
| 262 |
* @return WP_REST_Response |
| 263 |
*/ |
| 264 |
public function set_site_representation( WP_REST_Request $request ) { |
| 265 |
$data = $this |
| 266 |
->first_time_configuration_action |
| 267 |
->set_site_representation( $request->get_json_params() ); |
| 268 |
|
| 269 |
return new WP_REST_Response( $data, $data->status ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Sets the social profiles values. |
| 274 |
* |
| 275 |
* @param WP_REST_Request $request The request. |
| 276 |
* |
| 277 |
* @return WP_REST_Response |
| 278 |
*/ |
| 279 |
public function set_social_profiles( WP_REST_Request $request ) { |
| 280 |
$data = $this |
| 281 |
->first_time_configuration_action |
| 282 |
->set_social_profiles( $request->get_json_params() ); |
| 283 |
|
| 284 |
return new WP_REST_Response( |
| 285 |
[ 'json' => $data ] |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Gets a person's social profiles values. |
| 291 |
* |
| 292 |
* @param WP_REST_Request $request The request. |
| 293 |
* |
| 294 |
* @return WP_REST_Response |
| 295 |
*/ |
| 296 |
public function get_person_social_profiles( WP_REST_Request $request ) { |
| 297 |
$data = $this |
| 298 |
->first_time_configuration_action |
| 299 |
->get_person_social_profiles( $request->get_param( 'user_id' ) ); |
| 300 |
|
| 301 |
return new WP_REST_Response( $data, $data->status ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Sets a person's social profiles values. |
| 306 |
* |
| 307 |
* @param WP_REST_Request $request The request. |
| 308 |
* |
| 309 |
* @return WP_REST_Response |
| 310 |
*/ |
| 311 |
public function set_person_social_profiles( WP_REST_Request $request ) { |
| 312 |
$data = $this |
| 313 |
->first_time_configuration_action |
| 314 |
->set_person_social_profiles( $request->get_json_params() ); |
| 315 |
|
| 316 |
return new WP_REST_Response( |
| 317 |
[ 'json' => $data ] |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Checks if the current user has the correct capability to edit a specific user. |
| 323 |
* |
| 324 |
* @param WP_REST_Request $request The request. |
| 325 |
* |
| 326 |
* @return WP_REST_Response |
| 327 |
*/ |
| 328 |
public function check_capability( WP_REST_Request $request ) { |
| 329 |
$data = $this |
| 330 |
->first_time_configuration_action |
| 331 |
->check_capability( $request->get_param( 'user_id' ) ); |
| 332 |
|
| 333 |
return new WP_REST_Response( $data ); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Enables or disables tracking. |
| 338 |
* |
| 339 |
* @param WP_REST_Request $request The request. |
| 340 |
* |
| 341 |
* @return WP_REST_Response |
| 342 |
*/ |
| 343 |
public function set_enable_tracking( WP_REST_Request $request ) { |
| 344 |
$data = $this |
| 345 |
->first_time_configuration_action |
| 346 |
->set_enable_tracking( $request->get_json_params() ); |
| 347 |
|
| 348 |
return new WP_REST_Response( $data, $data->status ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Checks if the current user has the right capability. |
| 353 |
* |
| 354 |
* @return bool |
| 355 |
*/ |
| 356 |
public function can_manage_options() { |
| 357 |
return \current_user_can( 'wpseo_manage_options' ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Checks if the current user has the capability to edit a specific user. |
| 362 |
* |
| 363 |
* @param WP_REST_Request $request The request. |
| 364 |
* |
| 365 |
* @return bool |
| 366 |
*/ |
| 367 |
public function can_edit_user( WP_REST_Request $request ) { |
| 368 |
$response = $this->first_time_configuration_action->check_capability( $request->get_param( 'user_id' ) ); |
| 369 |
return $response->success; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Checks if the current user has the capability to edit posts of other users. |
| 374 |
* |
| 375 |
* @return bool |
| 376 |
*/ |
| 377 |
public function can_edit_other_posts() { |
| 378 |
return \current_user_can( 'edit_others_posts' ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Saves the first time configuration state. |
| 383 |
* |
| 384 |
* @param WP_REST_Request $request The request. |
| 385 |
* |
| 386 |
* @return WP_REST_Response |
| 387 |
*/ |
| 388 |
public function save_configuration_state( WP_REST_Request $request ) { |
| 389 |
$data = $this |
| 390 |
->first_time_configuration_action |
| 391 |
->save_configuration_state( $request->get_json_params() ); |
| 392 |
|
| 393 |
return new WP_REST_Response( $data, $data->status ); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Returns the first time configuration state. |
| 398 |
* |
| 399 |
* @return WP_REST_Response the configuration of the workouts. |
| 400 |
*/ |
| 401 |
public function get_configuration_state() { |
| 402 |
$data = $this |
| 403 |
->first_time_configuration_action |
| 404 |
->get_configuration_state(); |
| 405 |
|
| 406 |
return new WP_REST_Response( $data, $data->status ); |
| 407 |
} |
| 408 |
} |
| 409 |
|