| 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\Conditionals\No_Conditionals; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Main; |
| 10 |
|
| 11 |
/** |
| 12 |
* Workouts_Route class. |
| 13 |
*/ |
| 14 |
class Workouts_Route implements Route_Interface { |
| 15 |
|
| 16 |
use No_Conditionals; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents workouts route. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
const WORKOUTS_ROUTE = '/workouts'; |
| 24 |
|
| 25 |
/** |
| 26 |
* The Options helper. |
| 27 |
* |
| 28 |
* @var Options_Helper |
| 29 |
*/ |
| 30 |
private $options_helper; |
| 31 |
|
| 32 |
/** |
| 33 |
* Workouts_Route constructor. |
| 34 |
* |
| 35 |
* @param Options_Helper $options_helper The options helper. |
| 36 |
*/ |
| 37 |
public function __construct( |
| 38 |
Options_Helper $options_helper |
| 39 |
) { |
| 40 |
$this->options_helper = $options_helper; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Registers routes with WordPress. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function register_routes() { |
| 49 |
$edit_others_posts = static function() { |
| 50 |
return \current_user_can( 'edit_others_posts' ); |
| 51 |
}; |
| 52 |
|
| 53 |
$workouts_route = [ |
| 54 |
[ |
| 55 |
'methods' => 'GET', |
| 56 |
'callback' => [ $this, 'get_workouts' ], |
| 57 |
'permission_callback' => $edit_others_posts, |
| 58 |
], |
| 59 |
[ |
| 60 |
'methods' => 'POST', |
| 61 |
'callback' => [ $this, 'set_workouts' ], |
| 62 |
'permission_callback' => $edit_others_posts, |
| 63 |
'args' => $this->get_workouts_routes_args(), |
| 64 |
], |
| 65 |
]; |
| 66 |
|
| 67 |
\register_rest_route( Main::API_V1_NAMESPACE, self::WORKOUTS_ROUTE, $workouts_route ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the workouts as configured for the site. |
| 72 |
* |
| 73 |
* @return WP_REST_Response the configuration of the workouts. |
| 74 |
*/ |
| 75 |
public function get_workouts() { |
| 76 |
$workouts_option = $this->options_helper->get( 'workouts_data' ); |
| 77 |
|
| 78 |
/** |
| 79 |
* Filter: 'Yoast\WP\SEO\workouts_options' - Allows adding workouts options by the add-ons. |
| 80 |
* |
| 81 |
* @api array $workouts_option The content of the `workouts_data` option in Free. |
| 82 |
*/ |
| 83 |
$workouts_option = \apply_filters( 'Yoast\WP\SEO\workouts_options', $workouts_option ); |
| 84 |
|
| 85 |
return new WP_REST_Response( |
| 86 |
[ 'json' => $workouts_option ] |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the workout configuration. |
| 92 |
* |
| 93 |
* @param WP_REST_Request $request The request object. |
| 94 |
* |
| 95 |
* @return WP_REST_Response the configuration of the workouts. |
| 96 |
*/ |
| 97 |
public function set_workouts( $request ) { |
| 98 |
$workouts_data = $request->get_json_params(); |
| 99 |
|
| 100 |
$free_workouts_data = []; |
| 101 |
$free_workouts_data['configuration'] = $workouts_data['configuration']; |
| 102 |
|
| 103 |
$result = $this->options_helper->set( 'workouts_data', $free_workouts_data ); |
| 104 |
|
| 105 |
if ( \count( $free_workouts_data['configuration']['finishedSteps'] ) === 5 ) { |
| 106 |
$this->options_helper->set( 'first_time_install', false ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Filter: 'Yoast\WP\SEO\workouts_route_save' - Allows the add-ons to save the options data in their own options. |
| 111 |
* |
| 112 |
* @api mixed|null $result The result of the previous saving operation. |
| 113 |
* |
| 114 |
* @param array $workouts_data The full set of workouts option data to save. |
| 115 |
*/ |
| 116 |
$result = \apply_filters( 'Yoast\WP\SEO\workouts_route_save', $result, $workouts_data ); |
| 117 |
|
| 118 |
return new WP_REST_Response( |
| 119 |
[ 'json' => $result ] |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Gets the args for all the registered workouts. |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
private function get_workouts_routes_args() { |
| 129 |
$args_array = [ |
| 130 |
'configuration' => [ |
| 131 |
'required' => true, |
| 132 |
], |
| 133 |
]; |
| 134 |
|
| 135 |
/** |
| 136 |
* Filter: 'Yoast\WP\SEO\workouts_route_args' - Allows the add-ons add their own arguments to the route registration. |
| 137 |
* |
| 138 |
* @api array $args_array The array of arguments for the route registration. |
| 139 |
*/ |
| 140 |
return \apply_filters( 'Yoast\WP\SEO\workouts_route_args', $args_array ); |
| 141 |
} |
| 142 |
} |
| 143 |
|