/wp-content/plugins/all-in-one-seo-pack/vendor/woocommerce/action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php

https://gitlab.com/ebrjose/comcebu · PHP · 158 lines · 66 code · 14 blank · 78 comment · 2 complexity · 1dc64b6e45bd9afb8c7e1ab1fb74b682 MD5 · raw file

  1. <?php
  2. /**
  3. * Commands for Action Scheduler.
  4. */
  5. class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command {
  6. /**
  7. * Run the Action Scheduler
  8. *
  9. * ## OPTIONS
  10. *
  11. * [--batch-size=<size>]
  12. * : The maximum number of actions to run. Defaults to 100.
  13. *
  14. * [--batches=<size>]
  15. * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
  16. *
  17. * [--cleanup-batch-size=<size>]
  18. * : The maximum number of actions to clean up. Defaults to the value of --batch-size.
  19. *
  20. * [--hooks=<hooks>]
  21. * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three`
  22. *
  23. * [--group=<group>]
  24. * : Only run actions from the specified group. Omitting this option runs actions from all groups.
  25. *
  26. * [--free-memory-on=<count>]
  27. * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
  28. *
  29. * [--pause=<seconds>]
  30. * : The number of seconds to pause when freeing memory. Default no pause.
  31. *
  32. * [--force]
  33. * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
  34. *
  35. * @param array $args Positional arguments.
  36. * @param array $assoc_args Keyed arguments.
  37. * @throws \WP_CLI\ExitException When an error occurs.
  38. *
  39. * @subcommand run
  40. */
  41. public function run( $args, $assoc_args ) {
  42. // Handle passed arguments.
  43. $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
  44. $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
  45. $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
  46. $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
  47. $hooks = array_filter( array_map( 'trim', $hooks ) );
  48. $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
  49. $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
  50. $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
  51. $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
  52. ActionScheduler_DataController::set_free_ticks( $free_on );
  53. ActionScheduler_DataController::set_sleep_time( $sleep );
  54. $batches_completed = 0;
  55. $actions_completed = 0;
  56. $unlimited = $batches === 0;
  57. try {
  58. // Custom queue cleaner instance.
  59. $cleaner = new ActionScheduler_QueueCleaner( null, $clean );
  60. // Get the queue runner instance
  61. $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
  62. // Determine how many tasks will be run in the first batch.
  63. $total = $runner->setup( $batch, $hooks, $group, $force );
  64. // Run actions for as long as possible.
  65. while ( $total > 0 ) {
  66. $this->print_total_actions( $total );
  67. $actions_completed += $runner->run();
  68. $batches_completed++;
  69. // Maybe set up tasks for the next batch.
  70. $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
  71. }
  72. } catch ( Exception $e ) {
  73. $this->print_error( $e );
  74. }
  75. $this->print_total_batches( $batches_completed );
  76. $this->print_success( $actions_completed );
  77. }
  78. /**
  79. * Print WP CLI message about how many actions are about to be processed.
  80. *
  81. * @author Jeremy Pry
  82. *
  83. * @param int $total
  84. */
  85. protected function print_total_actions( $total ) {
  86. WP_CLI::log(
  87. sprintf(
  88. /* translators: %d refers to how many scheduled taks were found to run */
  89. _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ),
  90. number_format_i18n( $total )
  91. )
  92. );
  93. }
  94. /**
  95. * Print WP CLI message about how many batches of actions were processed.
  96. *
  97. * @author Jeremy Pry
  98. *
  99. * @param int $batches_completed
  100. */
  101. protected function print_total_batches( $batches_completed ) {
  102. WP_CLI::log(
  103. sprintf(
  104. /* translators: %d refers to the total number of batches executed */
  105. _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ),
  106. number_format_i18n( $batches_completed )
  107. )
  108. );
  109. }
  110. /**
  111. * Convert an exception into a WP CLI error.
  112. *
  113. * @author Jeremy Pry
  114. *
  115. * @param Exception $e The error object.
  116. *
  117. * @throws \WP_CLI\ExitException
  118. */
  119. protected function print_error( Exception $e ) {
  120. WP_CLI::error(
  121. sprintf(
  122. /* translators: %s refers to the exception error message */
  123. __( 'There was an error running the action scheduler: %s', 'action-scheduler' ),
  124. $e->getMessage()
  125. )
  126. );
  127. }
  128. /**
  129. * Print a success message with the number of completed actions.
  130. *
  131. * @author Jeremy Pry
  132. *
  133. * @param int $actions_completed
  134. */
  135. protected function print_success( $actions_completed ) {
  136. WP_CLI::success(
  137. sprintf(
  138. /* translators: %d refers to the total number of taskes completed */
  139. _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ),
  140. number_format_i18n( $actions_completed )
  141. )
  142. );
  143. }
  144. }