PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / class-lp-request-handler.php

class-lp-request-handler.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.2, at inc/class-lp-request-handler.php

538 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Request
4 *
5 * Process actions by request param
6 *
7 * @author ThimPress
8 * @package LearnPress/Classes
9 * @version 1.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class LP_Request
18 */
19 class LP_Request {
20
21 /**
22 * @var bool
23 */
24 public static $ajax_shutdown = true;
25
26 /**
27 * Constructor
28 */
29 public static function init() {
30
31 self::$ajax_shutdown = learn_press_is_ajax();
32
33 if ( is_admin() ) {
34 add_action( 'init', array( __CLASS__, 'process_request' ), 50 );
35 } else {
36 add_action( 'template_include', array( __CLASS__, 'process_request' ), 50 );
37 }
38
39 self::register( 'lp-ajax', array( __CLASS__, 'do_ajax' ) );
40
41 add_action( 'wp_loaded', array( 'LP_Forms_Handler', 'init' ), 10 );
42 }
43
44 /**
45 * Get value by key from $_REQUEST
46 *
47 * @param string $key
48 * @param mixed $default
49 * @param string $sanitize_type
50 * @param string $method
51 *
52 * @return array|float|int|string
53 */
54 public static function get_param( string $key, $default = '', string $sanitize_type = 'text', string $method = '' ) {
55 switch ( $method ) {
56 case 'post':
57 $values = $_POST ?? [];
58 break;
59 case 'get':
60 $values = $_GET ?? [];
61 break;
62 default:
63 $values = $_REQUEST ?? [];
64 }
65
66 return LP_Helper::sanitize_params_submitted( $values[ $key ] ?? $default, $sanitize_type );
67 }
68
69 public static function get_header() {
70 ob_start();
71 }
72
73 /**
74 * Process actions
75 *
76 * @param string $template
77 *
78 * @return string
79 */
80 public static function process_request( $template ) {
81 if ( ! empty( $_REQUEST ) ) {
82 foreach ( $_REQUEST as $key => $value ) {
83 $key = LP_Helper::sanitize_params_submitted( $key );
84 $value = LP_Helper::sanitize_params_submitted( $value );
85 do_action( 'learn_press_request_handler_' . $key, $value, $key );
86 }
87 }
88
89 return $template;
90 }
91
92 /**
93 * Register new request
94 *
95 * @param string|array $action
96 * @param mixed $function
97 * @param int $priority
98 */
99 public static function register( $action, $function = '', $priority = 5 ) {
100 if ( is_array( $action ) ) {
101 foreach ( $action as $item ) {
102 $item = wp_parse_args(
103 $item,
104 array(
105 'action' => '',
106 'callback' => '',
107 'priority' => 5,
108 )
109 );
110 if ( ! $item['action'] || ! $item['callback'] ) {
111 continue;
112 }
113
114 list( $action, $callback, $priority ) = array_values( $item );
115 add_action( 'learn_press_request_handler_' . $action, $callback, $priority, 2 );
116 }
117 } else {
118 add_action( 'learn_press_request_handler_' . $action, $function, $priority, 2 );
119 }
120 }
121
122 /**
123 * Register ajax action.
124 * Add ajax into queue by an action and then LP check if there is a request
125 * with key lp-ajax=action-name then do the action "action-name". By default,
126 * ajax action is called if user is logged. But, it can be call in case user
127 * is not logged in if the action is passed with key :nopriv at the end.
128 *
129 * E.g:
130 * + Only for user is logged in
131 * LP_Request::register_ajax( 'action', 'function_to_call', 5 )
132 *
133 * + For guest
134 * LP_Request::register_ajax( 'action:nopriv', 'function_to_call', 5 )
135 *
136 * @param string $action
137 * @param mixed $function
138 * @param int $priority
139 */
140 public static function register_ajax( $action, $function, $priority = 5 ) {
141 if ( is_array( $action ) ) {
142 foreach ( $action as $args ) {
143 if ( ! empty( $args['action'] ) && ! empty( $args['callback'] ) ) {
144 self::register_ajax( $args['action'], $args['callback'], ! empty( $args['priority'] ) ? $args['priority'] : 5 );
145 }
146 }
147
148 return;
149 }
150 $actions = self::parse_action( $action );
151
152 if ( isset( $actions['nonce'] ) ) {
153 add_filter( 'learn-press/ajax/verify-none/' . $actions['action'], array( __CLASS__, 'verify_nonce' ) );
154 }
155
156 add_action( 'learn-press/ajax/' . $actions['action'], $function, $priority );
157
158 /**
159 * No requires logged in?
160 */
161 if ( isset( $actions['nopriv'] ) ) {
162 add_action( 'learn-press/ajax/no-priv/' . $actions['action'], $function, $priority );
163 }
164 }
165
166 /**
167 * Do ajax if there is a 'lp-ajax' in $_REQUEST
168 *
169 * @param string $action
170 */
171 public static function do_ajax( $action ) {
172
173 if ( ! defined( 'LP_DOING_AJAX' ) ) {
174 define( 'LP_DOING_AJAX', true );
175 }
176
177 LP_Gateways::instance()->get_available_payment_gateways();
178
179 if ( has_filter( 'learn-press/ajax/verify-none/' . $action ) ) {
180 if ( ! self::verify_nonce( $action ) ) {
181 die( '0' );
182 }
183 }
184
185 if ( is_user_logged_in() ) {
186 $has_action = has_action( 'learn-press/ajax/' . $action );
187
188 /**
189 * @since 3.0.0
190 */
191 do_action( 'learn-press/ajax/' . $action );
192 } else {
193
194 $has_action = has_action( 'learn-press/ajax/no-priv/' . $action );
195
196 /**
197 * @since 3.0.0
198 */
199 do_action( 'learn-press/ajax/no-priv/' . $action );
200 }
201
202 if ( $has_action && self::$ajax_shutdown ) {
203 die( '{END_AJAX}' );
204 }
205 }
206
207 public static function verify_nonce( $action, $nonce = '' ) {
208 return wp_verify_nonce( $nonce ? $nonce : self::get_string( "{$action}-nonce" ), $action );
209 }
210
211 public static function parse_action( $action ) {
212 $args = explode( ':', $action );
213 $actions = array(
214 'action' => $args[0],
215 );
216
217 if ( sizeof( $args ) > 1 ) {
218 array_shift( $args );
219 foreach ( $args as $arg ) {
220 $actions[ $arg ] = $arg;
221 }
222 }
223
224 return $actions;
225 }
226
227 /**
228 * Get variable value from Server environment.
229 *
230 * @param string $var
231 * @param mixed $default
232 * @param string $type
233 * @param string $env
234 *
235 * @return mixed
236 */
237 public static function get( $var, $default = false, $type = '', $env = 'request' ) {
238 switch ( strtolower( $env ) ) {
239 case 'post':
240 $env = LP_Helper::sanitize_params_submitted( $_POST );
241 break;
242 case 'get':
243 $env = LP_Helper::sanitize_params_submitted( $_GET );
244 break;
245 case 'put':
246 case 'delete':
247 $data = file_get_contents( 'php://input' );
248 $env = array();
249 parse_str( $data, $env );
250 break;
251 case 'wp':
252 global $wp;
253 $env = $wp->query_vars;
254 break;
255 default:
256 $env = LP_Helper::sanitize_params_submitted( $_REQUEST );
257 }
258
259 $return = array_key_exists( $var, $env ) ? $env[ $var ] : $default;
260 switch ( $type ) {
261 case 'int':
262 $return = intval( $return );
263 break;
264 case 'float':
265 $return = floatval( $return );
266 break;
267 case 'bool':
268 try {
269 $value = strtolower( $return );
270 } catch ( Exception $e ) {
271 $value = $return;
272 }
273
274 if ( in_array( $value, array( 'true', 'yes', 'on', 'enable' ) ) ) {
275 $return = true;
276 } elseif ( in_array( $value, array( 'false', 'no', 'off', 'disable' ) ) ) {
277 $return = false;
278 } else {
279 $return = ! ! $return;
280 }
281 break;
282 case 'string':
283 $return = (string) $return;
284 break;
285 case 'array':
286 $return = $return ? (array) $return : array();
287 break;
288 }
289
290 LP_Helper::sanitize_params_submitted( $return );
291
292 return $return;
293 }
294
295 /**
296 * Get value int from environment.
297 *
298 * @param string $var
299 * @param mixed $default
300 * @param string $env
301 *
302 * @return int
303 */
304 public static function get_int( $var, $default = false, $env = 'request' ) {
305 return self::get( $var, $default, 'int', $env );
306 }
307
308 /**
309 * Get value float from environment.
310 *
311 * @param string $var
312 * @param mixed $default
313 * @param string $env
314 *
315 * @return float
316 */
317 public static function get_float( $var, $default = false, $env = 'request' ) {
318 return self::get( $var, $default, 'float', $env );
319 }
320
321 /**
322 * Get value bool from environment.
323 *
324 * @param string $var
325 * @param mixed $default
326 * @param string $env
327 *
328 * @return bool
329 */
330 public static function get_bool( $var, $default = false, $env = 'request' ) {
331 return self::get( $var, $default, 'bool', $env );
332 }
333
334 /**
335 * Get value string from environment.
336 *
337 * @param string $var
338 * @param mixed $default
339 * @param string $env
340 *
341 * @return string
342 */
343 public static function get_string( $var, $default = false, $env = 'request' ) {
344 return self::get( $var, $default, 'string', $env );
345 }
346
347 /**
348 * Get value array from environment.
349 *
350 * @param string $var
351 * @param mixed $default
352 * @param string $env
353 *
354 * @return array
355 */
356 public static function get_array( $var, $default = false, $env = 'request' ) {
357 return self::get( $var, $default, 'array', $env );
358 }
359
360 /**
361 * Get value from $_POST.
362 *
363 * @param string $var
364 * @param mixed $default
365 *
366 * @return mixed
367 */
368 public static function get_post( $var, $default = false, $type = '' ) {
369 return self::get( $var, $default, $type, 'post' );
370 }
371
372 /**
373 * Get value int from $_POST.
374 *
375 * @param string $var
376 * @param mixed $default
377 *
378 * @return int
379 */
380 public static function get_post_int( $var, $default = false ) {
381 return self::get_post( $var, $default, 'int' );
382 }
383
384 /**
385 * Get value float from $_POST.
386 *
387 * @param string $var
388 * @param mixed $default
389 *
390 * @return float
391 */
392 public static function get_post_float( $var, $default = false ) {
393 return self::get_post( $var, $default, 'float' );
394 }
395
396 /**
397 * Get value bool from $_POST.
398 *
399 * @param string $var
400 * @param mixed $default
401 *
402 * @return bool
403 */
404 public static function get_post_bool( $var, $default = false ) {
405 return self::get_post( $var, $default, 'bool' );
406 }
407
408 /**
409 * Get value string from $_POST.
410 *
411 * @param string $var
412 * @param mixed $default
413 *
414 * @return string
415 */
416 public static function get_post_string( $var, $default = false ) {
417 return self::get_post( $var, $default, 'string' );
418 }
419
420 /**
421 * Get value array from $_POST.
422 *
423 * @param string $var
424 * @param mixed $default
425 *
426 * @return array
427 */
428 public static function get_post_array( $var, $default = false ) {
429 return self::get_post( $var, $default, 'array' );
430 }
431
432 /**
433 * Get email field and validate.
434 *
435 * @param string $var
436 * @param bool $default
437 *
438 * @return bool|string
439 */
440 public static function get_email( $var, $default = false ) {
441 $email = self::get_string( $var, $default );
442 if ( ! is_email( $email ) ) {
443 $email = $default;
444 }
445
446 return $email;
447 }
448
449 /**
450 * Get a batch of params from request into an array.
451 *
452 * @return array
453 */
454 public static function get_list() {
455 if ( func_num_args() < 1 ) {
456 return array();
457 }
458
459 $list = array();
460 foreach ( func_get_args() as $key ) {
461 $list[ $key ] = self::get( $key );
462 }
463
464 return $list;
465 }
466
467 /**
468 * Parse string from request to array by comma.
469 *
470 * @param string $var
471 * @param string $separator
472 *
473 * @return array
474 */
475 public static function get_list_array( $var, $separator = ',' ) {
476 $list = self::get_string( $var );
477
478 if ( ! $list ) {
479 return array();
480 }
481
482 if ( $separator === ',' ) {
483 $list = preg_split( '!\s?,\s?!', $list );
484 } else {
485 $list = explode( $separator, $list );
486 }
487
488 return array_map( 'trim', $list );
489 }
490
491 /**
492 * Get param 'redirect' in request.
493 *
494 * @param string $default
495 *
496 * @return string
497 */
498 public static function get_redirect( $default = '' ) {
499 $redirect = self::get_string( 'redirect' );
500
501 if ( $redirect ) {
502 $redirect = urldecode( $redirect );
503 } else {
504 $redirect = $default;
505 }
506
507 return $redirect;
508 }
509
510 public static function get_cookie( $name, $def = false, $global = false ) {
511 if ( $global ) {
512 return $_COOKIE[ $name ] ?? $def;
513 }
514
515 $cookie = isset( $_COOKIE['LP'] ) ? (array) json_decode( stripslashes( $_COOKIE['LP'] ) ) : array();
516
517 return $cookie[ $name ] ?? $def;
518 }
519
520 public static function set_cookie( $name, $value, $expires = '', $domain = '', $path = '', $secure = false ) {
521 if ( func_num_args() > 2 ) {
522 learn_press_setcookie( $name, $value, $expires, $secure );
523 } else {
524 $cookie = isset( $_COOKIE['LP'] ) ? maybe_unserialize( $_COOKIE['LP'] ) : array();
525
526 $cookie[ $name ] = $value;
527 learn_press_setcookie( 'LP', $value );
528 }
529 }
530 }
531
532 LP_Request::init();
533
534 // Backward compatibility for 2.x.x
535 class LP_Request_Handler extends LP_Request {
536
537 }
538