PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
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 4.2.1.1 All 137 releases
learnpress / inc / class-lp-request-handler.php

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

532 lines 11.5 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_value
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_value = '', string $sanitize_type = 'text', string $method = '' ) {
57 switch ( strtolower( $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_value, $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_param( "{$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 * @deprecated
307 */
308 public static function get_int( $var, $default = false, $env = 'request' ) {
309 return self::get( $var, $default, 'int', $env );
310 }
311
312 /**
313 * Get value float from environment.
314 *
315 * @param string $var
316 * @param mixed $default
317 * @param string $env
318 *
319 * @return float
320 */
321 public static function get_float( $var, $default = false, $env = 'request' ) {
322 return self::get( $var, $default, 'float', $env );
323 }
324
325 /**
326 * Get value bool from environment.
327 *
328 * @param string $var
329 * @param mixed $default
330 * @param string $env
331 *
332 * @return bool
333 */
334 public static function get_bool( $var, $default = false, $env = 'request' ) {
335 return self::get( $var, $default, 'bool', $env );
336 }
337
338 /**
339 * Get value string from environment.
340 *
341 * @param string $var
342 * @param mixed $default
343 * @param string $env
344 *
345 * @return string
346 * @deprecated 4.2.7.4
347 */
348 public static function get_string( $var, $default = false, $env = 'request' ) {
349 return self::get( $var, $default, 'string', $env );
350 }
351
352 /**
353 * Get value array from environment.
354 *
355 * @param string $var
356 * @param mixed $default
357 * @param string $env
358 *
359 * @return array
360 */
361 public static function get_array( $var, $default = false, $env = 'request' ) {
362 return self::get( $var, $default, 'array', $env );
363 }
364
365 /**
366 * Get value from $_POST.
367 *
368 * @param string $var
369 * @param mixed $default
370 *
371 * @return mixed
372 */
373 public static function get_post( $var, $default = false, $type = '' ) {
374 return self::get( $var, $default, $type, 'post' );
375 }
376
377 /**
378 * Get value int from $_POST.
379 *
380 * @param string $var
381 * @param mixed $default
382 *
383 * @return int
384 */
385 public static function get_post_int( $var, $default = false ) {
386 return self::get_post( $var, $default, 'int' );
387 }
388
389 /**
390 * Get value float from $_POST.
391 *
392 * @param string $var
393 * @param mixed $default
394 *
395 * @return float
396 */
397 public static function get_post_float( $var, $default = false ) {
398 return self::get_post( $var, $default, 'float' );
399 }
400
401 /**
402 * Get value bool from $_POST.
403 *
404 * @param string $var
405 * @param mixed $default
406 *
407 * @return bool
408 */
409 public static function get_post_bool( $var, $default = false ) {
410 return self::get_post( $var, $default, 'bool' );
411 }
412
413 /**
414 * Get value string from $_POST.
415 *
416 * @param string $var
417 * @param mixed $default
418 *
419 * @return string
420 */
421 public static function get_post_string( $var, $default = false ) {
422 return self::get_post( $var, $default, 'string' );
423 }
424
425 /**
426 * Get value array from $_POST.
427 *
428 * @param string $var
429 * @param mixed $default
430 *
431 * @return array
432 */
433 public static function get_post_array( $var, $default = false ) {
434 return self::get_post( $var, $default, 'array' );
435 }
436
437 /**
438 * Get email field and validate.
439 *
440 * @param string $var
441 * @param bool $default
442 *
443 * @return bool|string
444 */
445 public static function get_email( $var, $default = false ) {
446 $email = self::get_string( $var, $default );
447 if ( ! is_email( $email ) ) {
448 $email = $default;
449 }
450
451 return $email;
452 }
453
454 /**
455 * Get a batch of params from request into an array.
456 *
457 * @return array
458 */
459 public static function get_list() {
460 if ( func_num_args() < 1 ) {
461 return array();
462 }
463
464 $list = array();
465 foreach ( func_get_args() as $key ) {
466 $list[ $key ] = self::get( $key );
467 }
468
469 return $list;
470 }
471
472 /**
473 * Parse string from request to array by comma.
474 *
475 * @param string $var
476 * @param string $separator
477 *
478 * @return array
479 */
480 public static function get_list_array( $var, $separator = ',' ) {
481 $list = self::get_string( $var );
482
483 if ( ! $list ) {
484 return array();
485 }
486
487 if ( $separator === ',' ) {
488 $list = preg_split( '!\s?,\s?!', $list );
489 } else {
490 $list = explode( $separator, $list );
491 }
492
493 return array_map( 'trim', $list );
494 }
495
496 /**
497 * Get param 'redirect' in request.
498 *
499 * @param string $default
500 *
501 * @return string
502 */
503 public static function get_redirect( $default = '' ) {
504 $redirect = self::get_string( 'redirect' );
505
506 if ( $redirect ) {
507 $redirect = urldecode( $redirect );
508 } else {
509 $redirect = $default;
510 }
511
512 return $redirect;
513 }
514
515 public static function get_cookie( $name, $def = false, $global = false ) {
516 return $_COOKIE[ $name ] ?? $def;
517 }
518
519 /*public static function set_cookie( $name, $value, $expires = '', $domain = '', $path = '', $secure = false ) {
520 if ( func_num_args() > 2 ) {
521 learn_press_setcookie( $name, $value, $expires, $secure );
522 } else {
523 $cookie = isset( $_COOKIE['LP'] ) ? maybe_unserialize( $_COOKIE['LP'] ) : array();
524
525 $cookie[ $name ] = $value;
526 learn_press_setcookie( 'LP', $value );
527 }
528 }*/
529 }
530
531 LP_Request::init();
532