PluginProbe
Polylang / 3.8.9
Polylang v3.8.9
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 3.8.9, at src/modules/REST/V1/Settings.php

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