| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'LP_Abstract_Background_Process' ) ) { |
| 7 |
/** |
| 8 |
* Class LP_Abstract_Background_Process |
| 9 |
* |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
class LP_Abstract_Background_Process extends WP_Background_Process { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var int |
| 16 |
*/ |
| 17 |
protected $queue_lock_time = 60; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $action = ''; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var bool |
| 26 |
*/ |
| 27 |
protected $safe = true; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $_safe = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $prefix = 'lp'; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var array |
| 41 |
* |
| 42 |
* @since 3.3.0 |
| 43 |
*/ |
| 44 |
protected $query_args = array(); |
| 45 |
|
| 46 |
/** |
| 47 |
* LP_Abstract_Background_Process constructor. |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
parent::__construct(); |
| 51 |
|
| 52 |
$this->query_args = array( |
| 53 |
'lp-background-process' => $this->get_id(), |
| 54 |
); |
| 55 |
|
| 56 |
/** |
| 57 |
* Priority is important that will fix issue with WC cart doesnt remove |
| 58 |
* after completing checkout and get order details |
| 59 |
* |
| 60 |
* @since 3.0.8 |
| 61 |
*/ |
| 62 |
// add_action( 'shutdown', array( $this, 'dispatch_queue' ), 1000 ); |
| 63 |
} |
| 64 |
|
| 65 |
public function dispatch_queue() { |
| 66 |
if ( ! empty( $this->data ) ) { |
| 67 |
$this->save()->dispatch(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function is_safe( $safe = null ) { |
| 72 |
if ( is_bool( $safe ) ) { |
| 73 |
if ( $this->_safe === '' ) { |
| 74 |
$this->_safe = $this->safe; |
| 75 |
} |
| 76 |
$this->safe = $safe; |
| 77 |
} |
| 78 |
|
| 79 |
return $this->safe; |
| 80 |
} |
| 81 |
|
| 82 |
public function reset_safe() { |
| 83 |
$this->safe = $this->_safe; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param mixed $data |
| 88 |
* |
| 89 |
* @return mixed |
| 90 |
*/ |
| 91 |
public function push_to_queue( $data ) { |
| 92 |
// Check to preventing loop |
| 93 |
if ( $this->safe ) { |
| 94 |
if ( learn_press_is_ajax() || ! empty( $_REQUEST['action'] ) ) { |
| 95 |
// return $this; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return parent::push_to_queue( $data ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get unique ID |
| 104 |
* |
| 105 |
* @since 3.3.0 |
| 106 |
* |
| 107 |
* @return mixed|string |
| 108 |
*/ |
| 109 |
public function get_id() { |
| 110 |
return $this->identifier; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Schedule fallback event. |
| 115 |
*/ |
| 116 |
protected function schedule_event() { |
| 117 |
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
| 118 |
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @since 3.3.0 |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public function has_queued() { |
| 128 |
return ! $this->is_queue_empty(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
protected function task( $item ) { |
| 133 |
ob_start(); |
| 134 |
|
| 135 |
print_r( $item ); |
| 136 |
print_r( $_REQUEST ); |
| 137 |
|
| 138 |
$msg = ob_get_clean(); |
| 139 |
|
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get query args |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
protected function get_query_args() { |
| 149 |
return array_merge( |
| 150 |
array( |
| 151 |
'action' => $this->identifier, |
| 152 |
'nonce' => wp_create_nonce( $this->identifier ), |
| 153 |
), |
| 154 |
$this->query_args |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
public function clear_queue() { |
| 159 |
$this->data = array(); |
| 160 |
|
| 161 |
return $this; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Array of singleton classes. |
| 166 |
* |
| 167 |
* @var array |
| 168 |
*/ |
| 169 |
protected static $instances = array(); |
| 170 |
|
| 171 |
/** |
| 172 |
* @return LP_Abstract_Background_Process|mixed |
| 173 |
*/ |
| 174 |
public static function instance() { |
| 175 |
$name = self::_get_called_class(); |
| 176 |
|
| 177 |
if ( false === $name ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
if ( empty( self::$instances[ $name ] ) ) { |
| 182 |
self::$instances[ $name ] = new $name(); |
| 183 |
} |
| 184 |
|
| 185 |
return self::$instances[ $name ]; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @return bool|string |
| 190 |
*/ |
| 191 |
protected static function _get_called_class() { |
| 192 |
if ( function_exists( 'get_called_class' ) ) { |
| 193 |
return get_called_class(); |
| 194 |
} |
| 195 |
|
| 196 |
$backtrace = debug_backtrace(); |
| 197 |
|
| 198 |
if ( empty( $backtrace[2] ) ) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
if ( empty( $backtrace[2]['args'][0] ) ) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
return $backtrace[2]['args'][0]; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|