| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\REST\V1; |
| 7 |
|
| 8 |
use PLL_Model; |
| 9 |
use WP_Error; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
use WP_REST_Server; |
| 13 |
use WP_Syntex\Polylang\Model\Languages; |
| 14 |
use WP_Syntex\Polylang\Options\Options; |
| 15 |
use WP_Syntex\Polylang\REST\Abstract_Controller; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Settings REST controller. |
| 21 |
* |
| 22 |
* @since 3.7 |
| 23 |
*/ |
| 24 |
class Settings extends Abstract_Controller { |
| 25 |
/** |
| 26 |
* @var Options |
| 27 |
*/ |
| 28 |
private $options; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Languages |
| 32 |
*/ |
| 33 |
private $languages; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor. |
| 37 |
* |
| 38 |
* @since 3.7 |
| 39 |
* |
| 40 |
* @param PLL_Model $model Polylang's model. |
| 41 |
*/ |
| 42 |
public function __construct( PLL_Model $model ) { |
| 43 |
$this->namespace = 'pll/v1'; |
| 44 |
$this->rest_base = 'settings'; |
| 45 |
$this->options = $model->options; |
| 46 |
$this->languages = $model->languages; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Registers the routes for options. |
| 51 |
* |
| 52 |
* @since 3.7 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function register_routes(): void { |
| 57 |
register_rest_route( |
| 58 |
$this->namespace, |
| 59 |
"/{$this->rest_base}", |
| 60 |
array( |
| 61 |
array( |
| 62 |
'methods' => WP_REST_Server::READABLE, |
| 63 |
'callback' => array( $this, 'get_item' ), |
| 64 |
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
| 65 |
), |
| 66 |
array( |
| 67 |
'methods' => WP_REST_Server::EDITABLE, |
| 68 |
'callback' => array( $this, 'update_item' ), |
| 69 |
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
| 70 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 71 |
), |
| 72 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 73 |
'allow_batch' => array( 'v1' => true ), |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Retrieves all options. |
| 80 |
* |
| 81 |
* @since 3.7 |
| 82 |
* |
| 83 |
* @param WP_REST_Request $request Full details about the request. |
| 84 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 85 |
* |
| 86 |
* @phpstan-template T of array |
| 87 |
* @phpstan-param WP_REST_Request<T> $request |
| 88 |
*/ |
| 89 |
public function get_item( $request ) { |
| 90 |
return $this->prepare_item_for_response( $this->options->get_all(), $request ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Updates option(s). |
| 95 |
* This allows to update one or several options. |
| 96 |
* |
| 97 |
* @since 3.7 |
| 98 |
* |
| 99 |
* @param WP_REST_Request $request Full details about the request. |
| 100 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 101 |
* |
| 102 |
* @phpstan-template T of array |
| 103 |
* @phpstan-param WP_REST_Request<T> $request |
| 104 |
*/ |
| 105 |
public function update_item( $request ) { |
| 106 |
$errors = new WP_Error(); |
| 107 |
$schema = $this->options->get_schema(); |
| 108 |
$options = array_intersect_key( |
| 109 |
$request->get_params(), |
| 110 |
rest_get_endpoint_args_for_schema( $schema, WP_REST_Server::EDITABLE ) // Remove fields with `readonly`. |
| 111 |
); |
| 112 |
|
| 113 |
foreach ( $options as $option_name => $new_value ) { |
| 114 |
$previous_value = $this->options->get( $option_name ); |
| 115 |
|
| 116 |
if ( 'default_lang' === $option_name ) { |
| 117 |
$result = $this->languages->update_default( $new_value ); |
| 118 |
} else { |
| 119 |
$result = $this->options->set( $option_name, $new_value ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( $result->has_errors() ) { |
| 123 |
$errors->merge_from( $result ); |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
if ( $this->options->get( $option_name ) === $previous_value ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
switch ( $option_name ) { |
| 132 |
case 'rewrite': |
| 133 |
case 'force_lang': |
| 134 |
case 'hide_default': |
| 135 |
flush_rewrite_rules(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ( $errors->has_errors() ) { |
| 140 |
return $this->add_status_to_error( $errors ); |
| 141 |
} |
| 142 |
|
| 143 |
return $this->prepare_item_for_response( $this->options->get_all(), $request ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Checks if a given request has access to update the options. |
| 148 |
* |
| 149 |
* @since 3.7 |
| 150 |
* |
| 151 |
* @param WP_REST_Request $request Full details about the request. |
| 152 |
* @return true|WP_Error True if the request has access to update the option, WP_Error object otherwise. |
| 153 |
* |
| 154 |
* @phpstan-template T of array |
| 155 |
* @phpstan-param WP_REST_Request<T> $request |
| 156 |
*/ |
| 157 |
public function update_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 158 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 159 |
return new WP_Error( |
| 160 |
'rest_forbidden_context', |
| 161 |
__( 'Sorry, you are not allowed to edit options.', 'polylang' ), |
| 162 |
array( 'status' => rest_authorization_required_code() ) |
| 163 |
); |
| 164 |
} |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Prepares the option value for the REST response. |
| 170 |
* |
| 171 |
* @since 3.7 |
| 172 |
* |
| 173 |
* @param array $item Option values. |
| 174 |
* @param WP_REST_Request $request Request object. |
| 175 |
* @return WP_REST_Response Response object. |
| 176 |
* |
| 177 |
* @phpstan-template T of array |
| 178 |
* @phpstan-param array<non-falsy-string, mixed> $item |
| 179 |
* @phpstan-param WP_REST_Request<T> $request |
| 180 |
*/ |
| 181 |
public function prepare_item_for_response( $item, $request ) { |
| 182 |
$fields = $this->get_fields_for_response( $request ); |
| 183 |
$response = array(); |
| 184 |
|
| 185 |
foreach ( $item as $option => $value ) { |
| 186 |
if ( rest_is_field_included( $option, $fields ) ) { |
| 187 |
$response[ $option ] = $value; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** @var WP_REST_Response */ |
| 192 |
return rest_ensure_response( $response ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Retrieves the options' schema, conforming to JSON Schema. |
| 197 |
* |
| 198 |
* @since 3.7 |
| 199 |
* |
| 200 |
* @return array Item schema data. |
| 201 |
*/ |
| 202 |
public function get_item_schema(): array { |
| 203 |
return $this->add_additional_fields_schema( $this->options->get_schema() ); |
| 204 |
} |
| 205 |
} |
| 206 |
|