PluginProbe
seQura / 4.0.0
seQura v4.0.0
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Controllers / Rest / class-rest-controller.php

class-rest-controller.php in seQura 4.0.0, at src/Controllers/Rest/class-rest-controller.php

312 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper for REST Controllers
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Controllers/Rest
7 */
8
9 namespace SeQura\WC\Controllers\Rest;
10
11 use SeQura\Core\Infrastructure\Utility\RegexProvider;
12 use SeQura\WC\Services\Log\Interface_Logger_Service;
13 use WP_REST_Request;
14
15 /**
16 * Helper for REST Controllers
17 */
18 abstract class REST_Controller extends \WP_REST_Controller {
19
20 protected const PARAM_STORE_ID = 'storeId';
21 protected const PARAM_MERCHANT_ID = 'merchantId';
22
23 /**
24 * Logger service.
25 *
26 * @var Interface_Logger_Service
27 */
28 protected $logger;
29
30 /**
31 * RegEx service
32 *
33 * @var RegexProvider
34 */
35 protected $regex;
36
37 /**
38 * Constructor.
39 *
40 * @param Interface_Logger_Service $logger The logger service.
41 * @param RegexProvider $regex The regex provider.
42 */
43 public function __construct( Interface_Logger_Service $logger, RegexProvider $regex ) {
44 $this->logger = $logger;
45 $this->regex = $regex;
46 }
47
48 /**
49 * Check if the current user can manage options.
50 */
51 public function can_user_manage_options(): bool {
52 return \user_can( \get_current_user_id(), 'manage_options' );
53 }
54
55 /**
56 * Register GET endpoint.
57 *
58 * @param string $endpoint The endpoint.
59 * @param string $fun The function.
60 * @param mixed[] $args The arguments. See https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
61 * @param string $permission_callback The permission callback.
62 */
63 protected function register_get( $endpoint, $fun, $args = array(), $permission_callback = 'can_user_manage_options' ): void {
64 $this->register( \WP_REST_Server::READABLE, $endpoint, $fun, $args, $permission_callback );
65 }
66
67 /**
68 * Register POST endpoint.
69 *
70 * @param string $endpoint The endpoint.
71 * @param string $fun The function.
72 * @param mixed[] $args The arguments. See https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
73 * @param string $permission_callback The permission callback.
74 */
75 protected function register_post( $endpoint, $fun, $args = array(), $permission_callback = 'can_user_manage_options' ): void {
76 $this->register( \WP_REST_Server::CREATABLE, $endpoint, $fun, $args, $permission_callback );
77 }
78
79 /**
80 * Register DELETE endpoint.
81 *
82 * @param string $endpoint The endpoint.
83 * @param string $fun The function.
84 * @param mixed[] $args The arguments. See https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
85 * @param string $permission_callback The permission callback.
86 */
87 protected function register_delete( $endpoint, $fun, $args = array(), $permission_callback = 'can_user_manage_options' ): void {
88 $this->register( \WP_REST_Server::DELETABLE, $endpoint, $fun, $args, $permission_callback );
89 }
90
91 /**
92 * Register endpoint.
93 *
94 * @param string $methods The HTTP Verb.
95 * @param string $endpoint The endpoint.
96 * @param string $fun The function.
97 * @param mixed[] $arguments The arguments. See https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
98 * @param string $permission_callback The permission callback.
99 */
100 private function register( $methods, $endpoint, $fun, $arguments, $permission_callback ): void {
101 $args = array(
102 'methods' => $methods,
103 'callback' => array( $this, $fun ),
104 'permission_callback' => array( $this, $permission_callback ),
105 );
106 if ( ! empty( $arguments ) && is_array( $arguments ) ) {
107 $args['args'] = $arguments;
108 }
109 \register_rest_route( $this->namespace, "{$this->rest_base}/$endpoint", $args );
110 }
111
112 /**
113 * Validate if the parameter is not empty string.
114 *
115 * @param mixed $param The parameter.
116 * @param WP_REST_Request $request The request.
117 * @param string $key The key.
118 */
119 public function validate_not_empty_string( $param, $request, $key ): bool {
120 return is_string( $param ) && '' !== trim( $param );
121 }
122
123 /**
124 * Validate if the parameter is a boolean.
125 *
126 * @param mixed $param The parameter.
127 * @param WP_REST_Request $request The request.
128 * @param string $key The key.
129 */
130 public function validate_is_bool( $param, $request, $key ): bool {
131 return is_bool( $param );
132 }
133
134 /**
135 * Validate if the parameter is an integer.
136 *
137 * @param mixed $param The parameter.
138 * @param WP_REST_Request $request The request.
139 * @param string $key The key.
140 */
141 public function validate_is_int( $param, $request, $key ): bool {
142 return is_int( $param );
143 }
144
145 /**
146 * Validate id the parameter is an array of IP addresses.
147 *
148 * @param mixed $param The parameter.
149 * @param WP_REST_Request $request The request.
150 * @param string $key The key.
151 */
152 public function validate_ip_list( $param, $request, $key ): bool {
153 $ip_regex = $this->regex->getIpRegex();
154 if ( ! is_array( $param ) ) {
155 return false;
156 }
157 foreach ( $param as $ip ) {
158 if ( preg_match( $ip_regex, $ip ) !== 1 ) {
159 return false;
160 }
161 }
162 return true;
163 }
164
165 /**
166 * Check dates (yyyy-mm-dd) and time durations (PnYnMnDTnHnMnS). ISO 8061 regex to validate the date format.
167 *
168 * @param mixed $param The parameter.
169 * @param WP_REST_Request $request The request.
170 * @param string $key The key.
171 */
172 public function validate_time_duration( $param, $request, $key ): bool {
173 $regex = $this->regex->getDateOrDurationRegex();
174 return is_string( $param ) && preg_match( $regex, $param ) === 1 && 'P' !== $param && ! str_ends_with( $param, 'T' );
175 }
176
177 /**
178 * Sanitize boolean.
179 *
180 * @param mixed $param The parameter.
181 */
182 public function sanitize_bool( $param ): bool {
183 return (bool) $param;
184 }
185
186 /**
187 * Sanitize boolean.
188 *
189 * @param mixed $param The parameter.
190 */
191 public function sanitize_int( $param ): int {
192 return intval( $param );
193 }
194
195 /**
196 * Sanitize an array strings.
197 *
198 * @param mixed[] $param The parameter.
199 * @return mixed[]
200 */
201 public function sanitize_array_sanitize_text_field( $param ): array {
202 foreach ( $param as &$value ) {
203 $value = \sanitize_text_field( strval( $value ) );
204 }
205 return $param;
206 }
207
208 /**
209 * Get base argument structure.
210 *
211 * @param bool $required If the argument is required.
212 * @param mixed $default_value The default value. Null will be ignored.
213 * @return mixed[]
214 */
215 protected function get_arg( $required = true, $default_value = null ): array {
216 $arg = array( 'required' => $required );
217 if ( null !== $default_value ) {
218 $arg['default'] = $default_value;
219 }
220 return $arg;
221 }
222
223 /**
224 * Get argument structure for a boolean parameter.
225 *
226 * @param bool $required If the argument is required.
227 * @param mixed $default_value The default value. Null will be ignored.
228 * @return mixed[]
229 */
230 protected function get_arg_bool( $required = true, $default_value = null ): array {
231 return array_merge(
232 $this->get_arg( $required, $default_value ),
233 array(
234 'validate_callback' => array( $this, 'validate_is_bool' ),
235 'sanitize_callback' => array( $this, 'sanitize_bool' ),
236 )
237 );
238 }
239
240 /**
241 * Get argument structure for a integer parameter.
242 *
243 * @param bool $required If the argument is required.
244 * @param mixed $default_value The default value. Null will be ignored.
245 * @return mixed[]
246 */
247 protected function get_arg_int( $required = true, $default_value = null ): array {
248 return array_merge(
249 $this->get_arg( $required, $default_value ),
250 array(
251 'validate_callback' => array( $this, 'validate_is_int' ),
252 'sanitize_callback' => array( $this, 'sanitize_int' ),
253 )
254 );
255 }
256
257 /**
258 * Get argument structure for a boolean parameter.
259 *
260 * @param bool $required If the argument is required.
261 * @param mixed $default_value The default value. Null will be ignored.
262 * @param callable $validate The validate callback. Leave null to use the default.
263 * @param callable $sanitize The sanitize callback. Leave null to use the default.
264 * @return mixed[]
265 */
266 protected function get_arg_string( $required = true, $default_value = null, $validate = null, $sanitize = null ): array {
267 return array_merge(
268 $this->get_arg( $required, $default_value ),
269 array(
270 'validate_callback' => null === $validate ? array( $this, 'validate_not_empty_string' ) : $validate,
271 'sanitize_callback' => null === $sanitize ? 'sanitize_text_field' : $sanitize,
272 )
273 );
274 }
275
276 /**
277 * Get argument structure for an IP list.
278 *
279 * @param bool $required If the argument is required.
280 * @param mixed $default_value The default value. Null will be ignored.
281 * @param callable $validate The validate callback. Leave null to use the default.
282 * @param callable $sanitize The sanitize callback. Leave null to use the default.
283 * @return mixed[]
284 */
285 protected function get_arg_ip_list( $required = true, $default_value = null, $validate = null, $sanitize = null ): array {
286 return array_merge(
287 $this->get_arg( $required, $default_value ),
288 array(
289 'validate_callback' => null === $validate ? array( $this, 'validate_ip_list' ) : $validate,
290 'sanitize_callback' => null === $sanitize ? array( $this, 'sanitize_array_sanitize_text_field' ) : $sanitize,
291 )
292 );
293 }
294
295 /**
296 * Return a valid pattern to be used in a URL to match a parameter.
297 *
298 * @param string $param_name The URL parameter name.
299 * @param string $type The type of the parameter. Default is 'string'.
300 * Supported values are:
301 * - 'string'.
302 */
303 protected function url_param_pattern( $param_name, $type = 'string' ): string {
304 switch ( $type ) {
305 case 'string':
306 return '(?P<' . $param_name . '>[\w]+)';
307 default:
308 return '';
309 }
310 }
311 }
312