暫無描述

Process.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Implements a basic batch process.
  10. *
  11. * @since 1.7.0
  12. */
  13. abstract class PUM_Abstract_Batch_Process implements PUM_Interface_Batch_Process {
  14. /**
  15. * Batch process ID.
  16. *
  17. * @var string
  18. */
  19. public $batch_id;
  20. /**
  21. * The current step being processed.
  22. *
  23. * @var int|string Step number or 'done'.
  24. */
  25. public $step;
  26. /**
  27. * Number of items to process per step.
  28. *
  29. * @var int
  30. */
  31. public $per_step = 100;
  32. /**
  33. * Capability needed to perform the current batch process.
  34. *
  35. * @var string
  36. */
  37. public $capability = 'manage_options';
  38. /**
  39. * Sets up the batch process.
  40. *
  41. * @param int|string $step Step number or 'done'.
  42. */
  43. public function __construct( $step = 1 ) {
  44. $this->step = $step;
  45. if ( has_filter( "pum_batch_per_step_{$this->batch_id}" ) ) {
  46. /**
  47. * Filters the number of items to process per step for the given batch process.
  48. *
  49. * The dynamic portion of the hook name, `$this->export_type` refers to the export
  50. * type defined in each sub-class.
  51. *
  52. * @param int $per_step The number of items to process for each step. Default 100.
  53. * @param PUM_Abstract_Batch_Process $this Batch process instance.
  54. */
  55. $this->per_step = apply_filters( "pum_batch_per_step_{$this->batch_id}", $this->per_step, $this );
  56. }
  57. }
  58. /**
  59. * Determines if the current user can perform the current batch process.
  60. *
  61. * @return bool True if the current user has the needed capability, otherwise false.
  62. */
  63. public function can_process() {
  64. return current_user_can( $this->capability );
  65. }
  66. /**
  67. * Executes a single step in the batch process.
  68. *
  69. * @return int|string|WP_Error Next step number, 'done', or a WP_Error object.
  70. */
  71. public function process_step() {
  72. return 'done';
  73. }
  74. /**
  75. * Retrieves the calculated completion percentage.
  76. *
  77. * @return int Percentage completed.
  78. */
  79. public function get_percentage_complete() {
  80. $percentage = 0;
  81. $current_count = $this->get_current_count();
  82. $total_count = $this->get_total_count();
  83. if ( $total_count > 0 ) {
  84. $percentage = ( $current_count / $total_count ) * 100;
  85. }
  86. if ( $percentage > 100 ) {
  87. $percentage = 100;
  88. }
  89. return $percentage;
  90. }
  91. /**
  92. * Retrieves a message based on the given message code.
  93. *
  94. * @param string $code Message code.
  95. *
  96. * @return string Message.
  97. */
  98. public function get_message( $code ) {
  99. switch ( $code ) {
  100. case 'done':
  101. $final_count = $this->get_current_count();
  102. $message = sprintf( _n( '%s item was successfully processed.', '%s items were successfully processed.', $final_count, 'popup-maker' ), number_format_i18n( $final_count ) );
  103. break;
  104. default:
  105. $message = '';
  106. break;
  107. }
  108. return $message;
  109. }
  110. /**
  111. * Defines logic to execute once batch processing is complete.
  112. *
  113. */
  114. public function finish() {
  115. PUM_DataStorage::delete_by_match( "^{$this->batch_id}[0-9a-z\_]+" );
  116. }
  117. /**
  118. * Calculates and retrieves the offset for the current step.
  119. *
  120. * @return int Number of items to offset.
  121. */
  122. public function get_offset() {
  123. return ( $this->step - 1 ) * $this->per_step;
  124. }
  125. /**
  126. * Retrieves the current, stored count of processed items.
  127. *
  128. * @see get_percentage_complete()
  129. *
  130. * @return int Current number of processed items. Default 0.
  131. */
  132. protected function get_current_count() {
  133. return PUM_DataStorage::get( "{$this->batch_id}_current_count", 0 );
  134. }
  135. /**
  136. * Sets the current count of processed items.
  137. *
  138. * @param int $count Number of processed items.
  139. */
  140. protected function set_current_count( $count ) {
  141. PUM_DataStorage::write( "{$this->batch_id}_current_count", $count );
  142. }
  143. /**
  144. * Retrieves the total, stored count of items to process.
  145. *
  146. * @see get_percentage_complete()
  147. *
  148. * @return int Current number of processed items. Default 0.
  149. */
  150. protected function get_total_count() {
  151. return PUM_DataStorage::get( "{$this->batch_id}_total_count", 0 );
  152. }
  153. /**
  154. * Sets the total count of items to process.
  155. *
  156. * @param int $count Number of items to process.
  157. */
  158. protected function set_total_count( $count ) {
  159. PUM_DataStorage::write( "{$this->batch_id}_total_count", $count );
  160. }
  161. /**
  162. * Deletes the stored current and total counts of processed items.
  163. */
  164. protected function delete_counts() {
  165. PUM_DataStorage::delete( "{$this->batch_id}_current_count" );
  166. PUM_DataStorage::delete( "{$this->batch_id}_total_count" );
  167. }
  168. }