PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / src / modules / REST / V1 / Settings.php

Settings.php in Polylang trunk, at src/modules/REST/V1/Settings.php

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