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

544 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 * @deprecated 4.2.1
239 */
240 public static function get( $var, $default = false, $type = '', $env = 'request' ) {
241 switch ( strtolower( $env ) ) {
242 case 'post':
243 $env = LP_Helper::sanitize_params_submitted( $_POST );
244 break;
245 case 'get':
246 $env = LP_Helper::sanitize_params_submitted( $_GET );
247 break;
248 case 'put':
249 case 'delete':
250 $data = file_get_contents( 'php://input' );
251 $env = array();
252 parse_str( $data, $env );
253 break;
254 case 'wp':
255 global $wp;
256 $env = $wp->query_vars;
257 break;
258 default:
259 $env = LP_Helper::sanitize_params_submitted( $_REQUEST );
260 }
261
262 $return = array_key_exists( $var, $env ) ? $env[ $var ] : $default;
263 switch ( $type ) {
264 case 'int':
265 $return = intval( $return );
266 break;
267 case 'float':
268 $return = floatval( $return );
269 break;
270 case 'bool':
271 try {
272 $value = strtolower( $return );
273 } catch ( Exception $e ) {
274 $value = $return;
275 }
276
277 if ( in_array( $value, array( 'true', 'yes', 'on', 'enable' ) ) ) {
278 $return = true;
279 } elseif ( in_array( $value, array( 'false', 'no', 'off', 'disable' ) ) ) {
280 $return = false;
281 } else {
282 $return = ! ! $return;
283 }
284 break;
285 case 'string':
286 $return = (string) $return;
287 break;
288 case 'array':
289 $return = $return ? (array) $return : array();
290 break;
291 }
292
293 LP_Helper::sanitize_params_submitted( $return );
294
295 return $return;
296 }
297
298 /**
299 * Get value int from environment.
300 *
301 * @param string $var
302 * @param mixed $default
303 * @param string $env
304 *
305 * @return int
306 */
307 public static function get_int( $var, $default = false, $env = 'request' ) {
308 return self::get( $var, $default, 'int', $env );
309 }
310
311 /**
312 * Get value float from environment.
313 *
314 * @param string $var
315 * @param mixed $default
316 * @param string $env
317 *
318 * @return float
319 */
320 public static function get_float( $var, $default = false, $env = 'request' ) {
321 return self::get( $var, $default, 'float', $env );
322 }
323
324 /**
325 * Get value bool from environment.
326 *
327 * @param string $var
328 * @param mixed $default
329 * @param string $env
330 *
331 * @return bool
332 */
333 public static function get_bool( $var, $default = false, $env = 'request' ) {
334 return self::get( $var, $default, 'bool', $env );
335 }
336
337 /**
338 * Get value string from environment.
339 *
340 * @param string $var
341 * @param mixed $default
342 * @param string $env
343 *
344 * @return string
345 */
346 public static function get_string( $var, $default = false, $env = 'request' ) {
347 return self::get( $var, $default, 'string', $env );
348 }
349
350 /**
351 * Get value array from environment.
352 *
353 * @param string $var
354 * @param mixed $default
355 * @param string $env
356 *
357 * @return array
358 */
359 public static function get_array( $var, $default = false, $env = 'request' ) {
360 return self::get( $var, $default, 'array', $env );
361 }
362
363 /**
364 * Get value from $_POST.
365 *
366 * @param string $var
367 * @param mixed $default
368 *
369 * @return mixed
370 */
371 public static function get_post( $var, $default = false, $type = '' ) {
372 return self::get( $var, $default, $type, 'post' );
373 }
374
375 /**
376 * Get value int from $_POST.
377 *
378 * @param string $var
379 * @param mixed $default
380 *
381 * @return int
382 */
383 public static function get_post_int( $var, $default = false ) {
384 return self::get_post( $var, $default, 'int' );
385 }
386
387 /**
388 * Get value float from $_POST.
389 *
390 * @param string $var
391 * @param mixed $default
392 *
393 * @return float
394 */
395 public static function get_post_float( $var, $default = false ) {
396 return self::get_post( $var, $default, 'float' );
397 }
398
399 /**
400 * Get value bool from $_POST.
401 *
402 * @param string $var
403 * @param mixed $default
404 *
405 * @return bool
406 */
407 public static function get_post_bool( $var, $default = false ) {
408 return self::get_post( $var, $default, 'bool' );
409 }
410
411 /**
412 * Get value string from $_POST.
413 *
414 * @param string $var
415 * @param mixed $default
416 *
417 * @return string
418 */
419 public static function get_post_string( $var, $default = false ) {
420 return self::get_post( $var, $default, 'string' );
421 }
422
423 /**
424 * Get value array from $_POST.
425 *
426 * @param string $var
427 * @param mixed $default
428 *
429 * @return array
430 */
431 public static function get_post_array( $var, $default = false ) {
432 return self::get_post( $var, $default, 'array' );
433 }
434
435 /**
436 * Get email field and validate.
437 *
438 * @param string $var
439 * @param bool $default
440 *
441 * @return bool|string
442 */
443 public static function get_email( $var, $default = false ) {
444 $email = self::get_string( $var, $default );
445 if ( ! is_email( $email ) ) {
446 $email = $default;
447 }
448
449 return $email;
450 }
451
452 /**
453 * Get a batch of params from request into an array.
454 *
455 * @return array
456 */
457 public static function get_list() {
458 if ( func_num_args() < 1 ) {
459 return array();
460 }
461
462 $list = array();
463 foreach ( func_get_args() as $key ) {
464 $list[ $key ] = self::get( $key );
465 }
466
467 return $list;
468 }
469
470 /**
471 * Parse string from request to array by comma.
472 *
473 * @param string $var
474 * @param string $separator
475 *
476 * @return array
477 */
478 public static function get_list_array( $var, $separator = ',' ) {
479 $list = self::get_string( $var );
480
481 if ( ! $list ) {
482 return array();
483 }
484
485 if ( $separator === ',' ) {
486 $list = preg_split( '!\s?,\s?!', $list );
487 } else {
488 $list = explode( $separator, $list );
489 }
490
491 return array_map( 'trim', $list );
492 }
493
494 /**
495 * Get param 'redirect' in request.
496 *
497 * @param string $default
498 *
499 * @return string
500 */
501 public static function get_redirect( $default = '' ) {
502 $redirect = self::get_string( 'redirect' );
503
504 if ( $redirect ) {
505 $redirect = urldecode( $redirect );
506 } else {
507 $redirect = $default;
508 }
509
510 return $redirect;
511 }
512
513 public static function get_cookie( $name, $def = false, $global = false ) {
514 if ( $global ) {
515 return $_COOKIE[ $name ] ?? $def;
516 }
517
518 $cookie = isset( $_COOKIE['LP'] ) ? (array) json_decode( stripslashes( $_COOKIE['LP'] ) ) : array();
519
520 return $cookie[ $name ] ?? $def;
521 }
522
523 /*public static function set_cookie( $name, $value, $expires = '', $domain = '', $path = '', $secure = false ) {
524 if ( func_num_args() > 2 ) {
525 learn_press_setcookie( $name, $value, $expires, $secure );
526 } else {
527 $cookie = isset( $_COOKIE['LP'] ) ? maybe_unserialize( $_COOKIE['LP'] ) : array();
528
529 $cookie[ $name ] = $value;
530 learn_press_setcookie( 'LP', $value );
531 }
532 }*/
533 }
534
535 LP_Request::init();
536
537 /**
538 * @deprecated 4.1.7.3
539 * using in the addon course review 4.0.3, wishlist 4.0.3
540 */
541 class LP_Request_Handler extends LP_Request {
542
543 }
544