PluginProbe
seQura / 3.2.2
seQura v3.2.2
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 3.2.2, at src/Controllers/Rest/class-rest-controller.php

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