endpoint
2 months ago
abstract-action-route.php
3 months ago
abstract-indexation-route.php
4 years ago
alert-dismissal-route.php
3 months ago
first-time-configuration-route.php
3 months ago
importing-route.php
3 months ago
indexables-head-route.php
2 years ago
indexing-route.php
3 months ago
integrations-route.php
3 months ago
meta-search-route.php
6 months ago
route-interface.php
5 years ago
semrush-route.php
3 months ago
supported-features-route.php
3 months ago
wincher-route.php
2 years ago
workouts-route.php
3 months ago
yoast-head-rest-field.php
2 years ago
workouts-route.php
128 lines
| 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 | public 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( Options_Helper $options_helper ) { |
| 38 | $this->options_helper = $options_helper; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Registers routes with WordPress. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function register_routes() { |
| 47 | $edit_others_posts = static function () { |
| 48 | return \current_user_can( 'edit_others_posts' ); |
| 49 | }; |
| 50 | |
| 51 | $workouts_route = [ |
| 52 | [ |
| 53 | 'methods' => 'GET', |
| 54 | 'callback' => [ $this, 'get_workouts' ], |
| 55 | 'permission_callback' => $edit_others_posts, |
| 56 | ], |
| 57 | [ |
| 58 | 'methods' => 'POST', |
| 59 | 'callback' => [ $this, 'set_workouts' ], |
| 60 | 'permission_callback' => $edit_others_posts, |
| 61 | 'args' => $this->get_workouts_routes_args(), |
| 62 | ], |
| 63 | ]; |
| 64 | |
| 65 | \register_rest_route( Main::API_V1_NAMESPACE, self::WORKOUTS_ROUTE, $workouts_route ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the workouts as configured for the site. |
| 70 | * |
| 71 | * @return WP_REST_Response the configuration of the workouts. |
| 72 | */ |
| 73 | public function get_workouts() { |
| 74 | $workouts_option = $this->options_helper->get( 'workouts_data' ); |
| 75 | |
| 76 | /** |
| 77 | * Filter: 'Yoast\WP\SEO\workouts_options' - Allows adding workouts options by the add-ons. |
| 78 | * |
| 79 | * @param array $workouts_option The content of the `workouts_data` option in Free. |
| 80 | */ |
| 81 | $workouts_option = \apply_filters( 'Yoast\WP\SEO\workouts_options', $workouts_option ); |
| 82 | |
| 83 | return new WP_REST_Response( |
| 84 | [ 'json' => $workouts_option ], |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Sets the workout configuration. |
| 90 | * |
| 91 | * @param WP_REST_Request $request The request object. |
| 92 | * |
| 93 | * @return WP_REST_Response the configuration of the workouts. |
| 94 | */ |
| 95 | public function set_workouts( $request ) { |
| 96 | $workouts_data = $request->get_json_params(); |
| 97 | |
| 98 | /** |
| 99 | * Filter: 'Yoast\WP\SEO\workouts_route_save' - Allows the add-ons to save the options data in their own options. |
| 100 | * |
| 101 | * @param mixed|null $result The result of the previous saving operation. |
| 102 | * |
| 103 | * @param array $workouts_data The full set of workouts option data to save. |
| 104 | */ |
| 105 | $result = \apply_filters( 'Yoast\WP\SEO\workouts_route_save', null, $workouts_data ); |
| 106 | |
| 107 | return new WP_REST_Response( |
| 108 | [ 'json' => $result ], |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets the args for all the registered workouts. |
| 114 | * |
| 115 | * @return array |
| 116 | */ |
| 117 | private function get_workouts_routes_args() { |
| 118 | $args_array = []; |
| 119 | |
| 120 | /** |
| 121 | * Filter: 'Yoast\WP\SEO\workouts_route_args' - Allows the add-ons add their own arguments to the route registration. |
| 122 | * |
| 123 | * @param array $args_array The array of arguments for the route registration. |
| 124 | */ |
| 125 | return \apply_filters( 'Yoast\WP\SEO\workouts_route_args', $args_array ); |
| 126 | } |
| 127 | } |
| 128 |