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