PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 4.2.1 All 138 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.2.0, at inc/class-lp-request-handler.php

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