| 1 |
<?php |
| 2 |
namespace LearnPress\Background; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use LearnPress; |
| 6 |
use LP_Debug; |
| 7 |
use LP_Helper; |
| 8 |
use Throwable; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class LPBackgroundTrigger |
| 14 |
* To handle a function that can be run in background |
| 15 |
* Via call class:method with params |
| 16 |
* |
| 17 |
* @since 4.2.8.7 |
| 18 |
* @version 1.0.0 |
| 19 |
*/ |
| 20 |
class LPBackgroundTrigger extends LPAsyncRequest { |
| 21 |
protected $action = 'background_trigger'; |
| 22 |
protected static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Method async handle |
| 26 |
*/ |
| 27 |
protected function handle() { |
| 28 |
ini_set( 'max_execution_time', 0 ); |
| 29 |
try { |
| 30 |
$params = LP_Helper::sanitize_params_submitted( $_POST['params'] ?? false ); |
| 31 |
$class = LP_Helper::sanitize_params_submitted( $_POST['class'] ?? false ); |
| 32 |
$method = LP_Helper::sanitize_params_submitted( $_POST['method'] ?? false ); |
| 33 |
|
| 34 |
if ( ! $class || ! $params || ! $method ) { |
| 35 |
throw new Exception( 'Params send on background is invalid' ); |
| 36 |
} |
| 37 |
|
| 38 |
// Security: check callback is registered. |
| 39 |
$allow_callbacks = apply_filters( |
| 40 |
'lp/background/allow_callback', |
| 41 |
[] |
| 42 |
); |
| 43 |
|
| 44 |
$callBackStr = $class . ':' . $method; |
| 45 |
if ( ! in_array( $callBackStr, $allow_callbacks ) ) { |
| 46 |
throw new Exception( 'Error: callback is not register!' ); |
| 47 |
} |
| 48 |
|
| 49 |
// Check class and method is callable. |
| 50 |
if ( is_callable( [ $class, $method ] ) ) { |
| 51 |
call_user_func( [ $class, $method ], $params ); |
| 52 |
} else { |
| 53 |
throw new Exception( 'Error: callback is not callable!' ); |
| 54 |
} |
| 55 |
} catch ( Throwable $e ) { |
| 56 |
LP_Debug::error_log( $e ); |
| 57 |
} |
| 58 |
|
| 59 |
ini_set( 'max_execution_time', LearnPress::$time_limit_default_of_sever ); |
| 60 |
die; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return LPBackgroundTrigger |
| 65 |
*/ |
| 66 |
public static function instance(): self { |
| 67 |
if ( is_null( self::$instance ) ) { |
| 68 |
self::$instance = new self(); |
| 69 |
} |
| 70 |
|
| 71 |
return self::$instance; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Must run instance to register ajax. |
| 76 |
LPBackgroundTrigger::instance(); |
| 77 |
|