PluginProbe
Polylang / 3.8.8
Polylang v3.8.8
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 / Request.php

Request.php in Polylang 3.8.8, at src/modules/REST/Request.php

267 lines 6.3 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;
7
8 use Closure;
9 use PLL_Model;
10 use PLL_Language;
11 use WP_REST_Request;
12 use WP_REST_Posts_Controller;
13 use WP_REST_Terms_Controller;
14
15 /**
16 * Class that mediates the current request.
17 *
18 * @since 3.8
19 */
20 class Request {
21 /**
22 * @var WP_REST_Request|null
23 *
24 * @phpstan-var WP_REST_Request<array>|null
25 */
26 private $request;
27
28 /**
29 * @var array|null
30 */
31 private $handler;
32
33 /**
34 * @var PLL_Model
35 */
36 private $model;
37
38 /**
39 * Constructor.
40 *
41 * @since 3.8
42 *
43 * @param PLL_Model $model Instance of PLL_Model.
44 */
45 public function __construct( PLL_Model $model ) {
46 $this->model = $model;
47
48 /*
49 * Priorities 0 and 10000 allow to have a stored request from the very beginning until the very end of the
50 * process. This allows to filter queries (see `Filtered_Object\Post::parse_query()` for example) that may be located
51 * in callbacks hooked to `rest_request_before_callbacks` and `rest_request_after_callbacks`.
52 */
53 add_filter( 'rest_request_before_callbacks', Closure::fromCallable( array( $this, 'save_request' ) ), 0, 3 );
54 add_filter( 'rest_request_after_callbacks', Closure::fromCallable( array( $this, 'reset_request' ) ), 10000 );
55 }
56
57 /**
58 * Stores the request to use, for example, parameters when filtering queries.
59 *
60 * @since 3.2
61 * @since 3.8 Added the `$server` parameter.
62 * Hooked to `rest_pre_dispatch`.
63 * Moved from PLL_REST_Filtered_Object.
64 *
65 * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
66 * @param array $handler Route handler used for the request.
67 * @param WP_REST_Request $request Request used to generate the response.
68 * @return WP_REST_Response|WP_HTTP_Response|WP_Error|mixed Response to send to the client.
69 *
70 * @phpstan-template T of array
71 * @phpstan-param WP_REST_Request<T> $request
72 */
73 private function save_request( $response, $handler, $request ) {
74 $this->request = $request;
75 $this->handler = $handler;
76
77 return $response;
78 }
79
80 /**
81 * Resets the stored request.
82 * Prevents to keep it after the request ends.
83 *
84 * @since 3.8
85 *
86 * @param mixed $result Response.
87 * @return mixed
88 */
89 private function reset_request( $result ) {
90 $this->request = null;
91 $this->handler = null;
92
93 return $result;
94 }
95
96 /**
97 * Returns a parameter from the current request if defined.
98 *
99 * @since 3.8
100 *
101 * @param string $param Parameter name.
102 * @return mixed|null Parameter value or null if not defined.
103 */
104 public function get_param( string $param ) {
105 if ( ! $this->request ) {
106 return null;
107 }
108
109 return $this->request->get_param( $param );
110 }
111
112 /**
113 * Returns the parameters of the current request.
114 *
115 * @since 3.8
116 *
117 * @return array Parameters of the current request if any.
118 */
119 public function get_params(): array {
120 if ( ! $this->request ) {
121 return array();
122 }
123
124 return $this->request->get_params();
125 }
126
127 /**
128 * Returns the language of the current request.
129 *
130 * @since 3.8
131 *
132 * @return PLL_Language|null Language of the current request, or null if no request is set or the language is not found.
133 */
134 public function get_language(): ?PLL_Language {
135 if ( ! $this->request ) {
136 return null;
137 }
138
139 $lang = $this->get_param( 'lang' );
140
141 if ( empty( $lang ) || ! is_string( $lang ) ) {
142 return null;
143 }
144
145 $lang = $this->model->get_language( $lang );
146
147 if ( ! $lang ) {
148 return null;
149 }
150
151 return $lang;
152 }
153
154 /**
155 * Returns the ID of the current requested object if defined.
156 *
157 * @since 3.8
158 *
159 * @return int|null ID of the current requested object, or null if no request is set or the ID is not defined.
160 */
161 public function get_id(): ?int {
162 if ( ! $this->request ) {
163 return null;
164 }
165
166 $id = $this->get_param( 'id' );
167 if ( empty( $id ) || ! is_numeric( $id ) ) {
168 return null;
169 }
170
171 return (int) $id;
172 }
173
174 /**
175 * Returns the attributes of the current request.
176 *
177 * @since 3.8
178 *
179 * @return array|null Attributes of the current request, or null if no request is set.
180 */
181 public function get_attributes(): ?array {
182 if ( ! $this->request ) {
183 return null;
184 }
185
186 return $this->request->get_attributes();
187 }
188
189 /**
190 * Tells if the current request is a "read only" request, i.e. not `POST`, `PUT`, `PATCH`, `DELETE`.
191 *
192 * @since 3.8
193 *
194 * @return bool
195 */
196 public function is_read_only(): bool {
197 return ! empty( $this->request ) && ! in_array( $this->request->get_method(), array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true );
198 }
199
200 /**
201 * Returns the object type of the current request.
202 *
203 * @since 3.8
204 *
205 * @return string|null Object type of the current request, or null if not defined.
206 * Returned values are 'post' and 'term'.
207 *
208 * @phpstan-return 'post'|'term'|null
209 */
210 public function get_object_type(): ?string {
211 if ( ! $this->request || ! $this->handler ) {
212 return null;
213 }
214
215 /**
216 * Filters the object type of the current request.
217 *
218 * @since 3.8
219 *
220 * @param string|null $type Object type of the current request, or null if not defined.
221 * @param array $handler Route handler used for the request.
222 * @param WP_REST_Request $request Request used to generate the response.
223 * Accepted values are 'post' and 'term'.
224 */
225 $type = apply_filters( 'pll_rest_request_object_type', null, $this->handler, $this->request );
226
227 if ( in_array( $type, array( 'post', 'term' ), true ) ) {
228 return $type;
229 }
230
231 if ( ! is_array( $this->handler['callback'] ) ) {
232 return null;
233 }
234
235 $controller = reset( $this->handler['callback'] );
236 if ( $controller instanceof WP_REST_Posts_Controller ) {
237 return 'post';
238 } elseif ( $controller instanceof WP_REST_Terms_Controller ) {
239 return 'term';
240 }
241
242 return null;
243 }
244
245 /**
246 * Returns the route of the current request.
247 *
248 * @since 3.8
249 *
250 * @return string Route of the current request, or empty string if no request is set.
251 */
252 public function get_route(): string {
253 return $this->request && is_string( $this->request->get_route() ) ? $this->request->get_route() : '';
254 }
255
256 /**
257 * Returns the HTTP method of the current request.
258 *
259 * @since 3.8
260 *
261 * @return string
262 */
263 public function get_method(): string {
264 return $this->request ? $this->request->get_method() : '';
265 }
266 }
267