/wp-content/plugins/wpforms-lite/src/Tasks/Task.php

https://gitlab.com/ebrjose/comcebu · PHP · 333 lines · 123 code · 54 blank · 156 comment · 12 complexity · 14294034ad7acb10f6115af4b3e8f5fd MD5 · raw file

  1. <?php
  2. namespace WPForms\Tasks;
  3. /**
  4. * Class Task.
  5. *
  6. * @since 1.5.9
  7. */
  8. class Task {
  9. /**
  10. * This task is async (runs asap).
  11. *
  12. * @since 1.5.9
  13. */
  14. const TYPE_ASYNC = 'async';
  15. /**
  16. * This task is a recurring.
  17. *
  18. * @since 1.5.9
  19. */
  20. const TYPE_RECURRING = 'scheduled';
  21. /**
  22. * This task is run once.
  23. *
  24. * @since 1.5.9
  25. */
  26. const TYPE_ONCE = 'once';
  27. /**
  28. * Type of the task.
  29. *
  30. * @since 1.5.9
  31. *
  32. * @var string
  33. */
  34. private $type;
  35. /**
  36. * Action that will be used as a hook.
  37. *
  38. * @since 1.5.9
  39. *
  40. * @var string
  41. */
  42. private $action;
  43. /**
  44. * Task meta ID.
  45. *
  46. * @since 1.5.9
  47. *
  48. * @var int
  49. */
  50. private $meta_id;
  51. /**
  52. * All the params that should be passed to the hook.
  53. *
  54. * @since 1.5.9
  55. *
  56. * @var array
  57. */
  58. private $params;
  59. /**
  60. * When the first instance of the job will run.
  61. * Used for ONCE ane RECURRING tasks.
  62. *
  63. * @since 1.5.9
  64. *
  65. * @var int
  66. */
  67. private $timestamp;
  68. /**
  69. * How long to wait between runs.
  70. * Used for RECURRING tasks.
  71. *
  72. * @since 1.5.9
  73. *
  74. * @var int
  75. */
  76. private $interval;
  77. /**
  78. * Task meta.
  79. *
  80. * @since 1.7.0
  81. *
  82. * @var Meta
  83. */
  84. private $meta;
  85. /**
  86. * Task constructor.
  87. *
  88. * @since 1.5.9
  89. *
  90. * @param string $action Action of the task.
  91. *
  92. * @throws \InvalidArgumentException When action is not a string.
  93. * @throws \UnexpectedValueException When action is empty.
  94. */
  95. public function __construct( $action ) {
  96. if ( ! is_string( $action ) ) {
  97. throw new \InvalidArgumentException( 'Task action should be a string.' );
  98. }
  99. $this->action = sanitize_key( $action );
  100. $this->meta = new Meta();
  101. if ( empty( $this->action ) ) {
  102. throw new \UnexpectedValueException( 'Task action cannot be empty.' );
  103. }
  104. }
  105. /**
  106. * Define the type of the task as async.
  107. *
  108. * @since 1.5.9
  109. *
  110. * @return \WPForms\Tasks\Task
  111. */
  112. public function async() {
  113. $this->type = self::TYPE_ASYNC;
  114. return $this;
  115. }
  116. /**
  117. * Define the type of the task as recurring.
  118. *
  119. * @since 1.5.9
  120. *
  121. * @param int $timestamp When the first instance of the job will run.
  122. * @param int $interval How long to wait between runs.
  123. *
  124. * @return \WPForms\Tasks\Task
  125. */
  126. public function recurring( $timestamp, $interval ) {
  127. $this->type = self::TYPE_RECURRING;
  128. $this->timestamp = (int) $timestamp;
  129. $this->interval = (int) $interval;
  130. return $this;
  131. }
  132. /**
  133. * Define the type of the task as one-time.
  134. *
  135. * @since 1.5.9
  136. *
  137. * @param int $timestamp When the first instance of the job will run.
  138. *
  139. * @return \WPForms\Tasks\Task
  140. */
  141. public function once( $timestamp ) {
  142. $this->type = self::TYPE_ONCE;
  143. $this->timestamp = (int) $timestamp;
  144. return $this;
  145. }
  146. /**
  147. * Pass any number of params that should be saved to Meta table.
  148. *
  149. * @since 1.5.9
  150. *
  151. * @return \WPForms\Tasks\Task
  152. */
  153. public function params() {
  154. $this->params = func_get_args();
  155. return $this;
  156. }
  157. /**
  158. * Register the action.
  159. * Should be the final call in a chain.
  160. *
  161. * @since 1.5.9
  162. *
  163. * @return null|string Action ID.
  164. */
  165. public function register() {
  166. $action_id = null;
  167. // No processing if ActionScheduler is not usable.
  168. if ( ! wpforms()->get( 'tasks' )->is_usable() ) {
  169. return $action_id;
  170. }
  171. // Save data to tasks meta table.
  172. if ( $this->params !== null ) {
  173. $this->meta_id = $this->meta->add(
  174. [
  175. 'action' => $this->action,
  176. 'data' => $this->params,
  177. ]
  178. );
  179. if ( empty( $this->meta_id ) ) {
  180. return $action_id;
  181. }
  182. }
  183. // Prevent 500 errors when Action Scheduler tables don't exist.
  184. try {
  185. switch ( $this->type ) {
  186. case self::TYPE_ASYNC:
  187. $action_id = $this->register_async();
  188. break;
  189. case self::TYPE_RECURRING:
  190. $action_id = $this->register_recurring();
  191. break;
  192. case self::TYPE_ONCE:
  193. $action_id = $this->register_once();
  194. break;
  195. }
  196. } catch ( \RuntimeException $exception ) {
  197. $action_id = null;
  198. }
  199. return $action_id;
  200. }
  201. /**
  202. * Register the async task.
  203. *
  204. * @since 1.5.9
  205. *
  206. * @return null|string Action ID.
  207. */
  208. protected function register_async() {
  209. if ( ! function_exists( 'as_enqueue_async_action' ) ) {
  210. return null;
  211. }
  212. return as_enqueue_async_action(
  213. $this->action,
  214. [ 'tasks_meta_id' => $this->meta_id ],
  215. Tasks::GROUP
  216. );
  217. }
  218. /**
  219. * Register the recurring task.
  220. *
  221. * @since 1.5.9
  222. *
  223. * @return null|string Action ID.
  224. */
  225. protected function register_recurring() {
  226. if ( ! function_exists( 'as_schedule_recurring_action' ) ) {
  227. return null;
  228. }
  229. return as_schedule_recurring_action(
  230. $this->timestamp,
  231. $this->interval,
  232. $this->action,
  233. [ 'tasks_meta_id' => $this->meta_id ],
  234. Tasks::GROUP
  235. );
  236. }
  237. /**
  238. * Register the one-time task.
  239. *
  240. * @since 1.5.9
  241. *
  242. * @return null|string Action ID.
  243. */
  244. protected function register_once() {
  245. if ( ! function_exists( 'as_schedule_single_action' ) ) {
  246. return null;
  247. }
  248. return as_schedule_single_action(
  249. $this->timestamp,
  250. $this->action,
  251. [ 'tasks_meta_id' => $this->meta_id ],
  252. Tasks::GROUP
  253. );
  254. }
  255. /**
  256. * Cancel all occurrences of this task.
  257. *
  258. * @since 1.6.1
  259. *
  260. * @return null|bool|string Null if no matching action found,
  261. * false if AS library is missing,
  262. * true if scheduled task has no params,
  263. * string of the scheduled action ID if a scheduled action was found and unscheduled.
  264. */
  265. public function cancel() {
  266. if ( ! function_exists( 'as_unschedule_all_actions' ) ) {
  267. return false;
  268. }
  269. if ( $this->params === null ) {
  270. as_unschedule_all_actions( $this->action );
  271. return true;
  272. }
  273. $this->meta_id = $this->meta->get_meta_id( $this->action, $this->params );
  274. if ( $this->meta_id === null ) {
  275. return null;
  276. }
  277. return as_unschedule_action( $this->action, [ 'tasks_meta_id' => $this->meta_id ], Tasks::GROUP );
  278. }
  279. }