| 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 |
/** |
| 12 |
* First_Time_Configuration_Route class. |
| 13 |
*/ |
| 14 |
class First_Time_Configuration_Route implements Route_Interface { |
| 15 |
|
| 16 |
use No_Conditionals; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents the first time configuration route. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
public const CONFIGURATION_ROUTE = '/configuration'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Represents a site representation route. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public const SITE_REPRESENTATION_ROUTE = '/site_representation'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Represents a social profiles route. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
public const SOCIAL_PROFILES_ROUTE = '/social_profiles'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Represents a route to enable/disable tracking. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public const ENABLE_TRACKING_ROUTE = '/enable_tracking'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Represents a route to check if current user has the correct capabilities to edit another user's profile. |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public const CHECK_CAPABILITY_ROUTE = '/check_capability'; |
| 52 |
|
| 53 |
/** |
| 54 |
* Represents a route to save the first time configuration state. |
| 55 |
* |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
public const SAVE_CONFIGURATION_STATE_ROUTE = '/save_configuration_state'; |
| 59 |
|
| 60 |
/** |
| 61 |
* Represents a route to save the first time configuration state. |
| 62 |
* |
| 63 |
* @var string |
| 64 |
*/ |
| 65 |
public const GET_CONFIGURATION_STATE_ROUTE = '/get_configuration_state'; |
| 66 |
|
| 67 |
/** |
| 68 |
* The first tinme configuration action. |
| 69 |
* |
| 70 |
* @var First_Time_Configuration_Action |
| 71 |
*/ |
| 72 |
private $first_time_configuration_action; |
| 73 |
|
| 74 |
/** |
| 75 |
* First_Time_Configuration_Route constructor. |
| 76 |
* |
| 77 |
* @param First_Time_Configuration_Action $first_time_configuration_action The first-time configuration action. |
| 78 |
*/ |
| 79 |
public function __construct( First_Time_Configuration_Action $first_time_configuration_action ) { |
| 80 |
$this->first_time_configuration_action = $first_time_configuration_action; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Registers routes with WordPress. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function register_routes() { |
| 89 |
$site_representation_route = [ |
| 90 |
'methods' => 'POST', |
| 91 |
'callback' => [ $this, 'set_site_representation' ], |
| 92 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 93 |
'args' => [ |
| 94 |
'company_or_person' => [ |
| 95 |
'type' => 'string', |
| 96 |
'enum' => [ |
| 97 |
'company', |
| 98 |
'person', |
| 99 |
], |
| 100 |
'required' => true, |
| 101 |
], |
| 102 |
'company_name' => [ |
| 103 |
'type' => 'string', |
| 104 |
], |
| 105 |
'company_logo' => [ |
| 106 |
'type' => 'string', |
| 107 |
], |
| 108 |
'company_logo_id' => [ |
| 109 |
'type' => 'integer', |
| 110 |
], |
| 111 |
'person_logo' => [ |
| 112 |
'type' => 'string', |
| 113 |
], |
| 114 |
'person_logo_id' => [ |
| 115 |
'type' => 'integer', |
| 116 |
], |
| 117 |
'company_or_person_user_id' => [ |
| 118 |
'type' => 'integer', |
| 119 |
], |
| 120 |
'description' => [ |
| 121 |
'type' => 'string', |
| 122 |
], |
| 123 |
], |
| 124 |
]; |
| 125 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SITE_REPRESENTATION_ROUTE, $site_representation_route ); |
| 126 |
|
| 127 |
$social_profiles_route = [ |
| 128 |
'methods' => 'POST', |
| 129 |
'callback' => [ $this, 'set_social_profiles' ], |
| 130 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 131 |
'args' => [ |
| 132 |
'facebook_site' => [ |
| 133 |
'type' => 'string', |
| 134 |
], |
| 135 |
'twitter_site' => [ |
| 136 |
'type' => 'string', |
| 137 |
], |
| 138 |
'other_social_urls' => [ |
| 139 |
'type' => 'array', |
| 140 |
], |
| 141 |
], |
| 142 |
]; |
| 143 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SOCIAL_PROFILES_ROUTE, $social_profiles_route ); |
| 144 |
|
| 145 |
$check_capability_route = [ |
| 146 |
'methods' => 'GET', |
| 147 |
'callback' => [ $this, 'check_capability' ], |
| 148 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 149 |
'args' => [ |
| 150 |
'user_id' => [ |
| 151 |
'required' => true, |
| 152 |
], |
| 153 |
], |
| 154 |
]; |
| 155 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::CHECK_CAPABILITY_ROUTE, $check_capability_route ); |
| 156 |
|
| 157 |
$enable_tracking_route = [ |
| 158 |
'methods' => 'POST', |
| 159 |
'callback' => [ $this, 'set_enable_tracking' ], |
| 160 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 161 |
'args' => [ |
| 162 |
'tracking' => [ |
| 163 |
'type' => 'boolean', |
| 164 |
'required' => true, |
| 165 |
], |
| 166 |
], |
| 167 |
]; |
| 168 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::ENABLE_TRACKING_ROUTE, $enable_tracking_route ); |
| 169 |
|
| 170 |
$save_configuration_state_route = [ |
| 171 |
'methods' => 'POST', |
| 172 |
'callback' => [ $this, 'save_configuration_state' ], |
| 173 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 174 |
'args' => [ |
| 175 |
'finishedSteps' => [ |
| 176 |
'type' => 'array', |
| 177 |
'required' => true, |
| 178 |
], |
| 179 |
], |
| 180 |
]; |
| 181 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::SAVE_CONFIGURATION_STATE_ROUTE, $save_configuration_state_route ); |
| 182 |
|
| 183 |
$get_configuration_state_route = [ |
| 184 |
[ |
| 185 |
'methods' => 'GET', |
| 186 |
'callback' => [ $this, 'get_configuration_state' ], |
| 187 |
'permission_callback' => [ $this, 'can_manage_options' ], |
| 188 |
], |
| 189 |
]; |
| 190 |
\register_rest_route( Main::API_V1_NAMESPACE, self::CONFIGURATION_ROUTE . self::GET_CONFIGURATION_STATE_ROUTE, $get_configuration_state_route ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Sets the site representation values. |
| 195 |
* |
| 196 |
* @param WP_REST_Request $request The request. |
| 197 |
* |
| 198 |
* @return WP_REST_Response |
| 199 |
*/ |
| 200 |
public function set_site_representation( WP_REST_Request $request ) { |
| 201 |
$data = $this |
| 202 |
->first_time_configuration_action |
| 203 |
->set_site_representation( $request->get_json_params() ); |
| 204 |
|
| 205 |
return new WP_REST_Response( $data, $data->status ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Sets the social profiles values. |
| 210 |
* |
| 211 |
* @param WP_REST_Request $request The request. |
| 212 |
* |
| 213 |
* @return WP_REST_Response |
| 214 |
*/ |
| 215 |
public function set_social_profiles( WP_REST_Request $request ) { |
| 216 |
$data = $this |
| 217 |
->first_time_configuration_action |
| 218 |
->set_social_profiles( $request->get_json_params() ); |
| 219 |
|
| 220 |
return new WP_REST_Response( |
| 221 |
[ 'json' => $data ], |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Checks if the current user has the correct capability to edit a specific user. |
| 227 |
* |
| 228 |
* @param WP_REST_Request $request The request. |
| 229 |
* |
| 230 |
* @return WP_REST_Response |
| 231 |
*/ |
| 232 |
public function check_capability( WP_REST_Request $request ) { |
| 233 |
$data = $this |
| 234 |
->first_time_configuration_action |
| 235 |
->check_capability( $request->get_param( 'user_id' ) ); |
| 236 |
|
| 237 |
return new WP_REST_Response( $data ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Enables or disables tracking. |
| 242 |
* |
| 243 |
* @param WP_REST_Request $request The request. |
| 244 |
* |
| 245 |
* @return WP_REST_Response |
| 246 |
*/ |
| 247 |
public function set_enable_tracking( WP_REST_Request $request ) { |
| 248 |
$data = $this |
| 249 |
->first_time_configuration_action |
| 250 |
->set_enable_tracking( $request->get_json_params() ); |
| 251 |
|
| 252 |
return new WP_REST_Response( $data, $data->status ); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Checks if the current user has the right capability. |
| 257 |
* |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
public function can_manage_options() { |
| 261 |
return \current_user_can( 'wpseo_manage_options' ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Checks if the current user has the capability to edit a specific user. |
| 266 |
* |
| 267 |
* @param WP_REST_Request $request The request. |
| 268 |
* |
| 269 |
* @return bool |
| 270 |
*/ |
| 271 |
public function can_edit_user( WP_REST_Request $request ) { |
| 272 |
$response = $this->first_time_configuration_action->check_capability( $request->get_param( 'user_id' ) ); |
| 273 |
return $response->success; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Checks if the current user has the capability to edit posts of other users. |
| 278 |
* |
| 279 |
* @return bool |
| 280 |
*/ |
| 281 |
public function can_edit_other_posts() { |
| 282 |
return \current_user_can( 'edit_others_posts' ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Saves the first time configuration state. |
| 287 |
* |
| 288 |
* @param WP_REST_Request $request The request. |
| 289 |
* |
| 290 |
* @return WP_REST_Response |
| 291 |
*/ |
| 292 |
public function save_configuration_state( WP_REST_Request $request ) { |
| 293 |
$data = $this |
| 294 |
->first_time_configuration_action |
| 295 |
->save_configuration_state( $request->get_json_params() ); |
| 296 |
|
| 297 |
return new WP_REST_Response( $data, $data->status ); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Returns the first time configuration state. |
| 302 |
* |
| 303 |
* @return WP_REST_Response the state of the configuration. |
| 304 |
*/ |
| 305 |
public function get_configuration_state() { |
| 306 |
$data = $this |
| 307 |
->first_time_configuration_action |
| 308 |
->get_configuration_state(); |
| 309 |
|
| 310 |
return new WP_REST_Response( $data, $data->status ); |
| 311 |
} |
| 312 |
} |
| 313 |
|