step-processing.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class for performing actions incrementally. Internally used for converting submissions, exporting submissions, etc.
  4. * Very useful when interacting with large amounts of data.
  5. *
  6. * @package Ninja Forms
  7. * @subpackage Classes/Step Processing
  8. * @copyright Copyright (c) 2014, WPNINJAS
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  10. * @since 2.7.4
  11. */
  12. class NF_Step_Processing
  13. {
  14. /**
  15. * @var action
  16. */
  17. var $action = '';
  18. /**
  19. * @var step
  20. */
  21. var $step = '';
  22. /**
  23. * @var total_steps
  24. */
  25. var $total_steps = '';
  26. /**
  27. * @var redirect
  28. */
  29. var $redirect = '';
  30. /**
  31. * @var array
  32. */
  33. var $errors = array();
  34. /**
  35. * @var args
  36. */
  37. var $args = array();
  38. /**
  39. * Get things rolling
  40. *
  41. * @since 2.7.4
  42. * @return void
  43. */
  44. function __construct() {
  45. //Bail if we aren't in the admin.
  46. if ( ! is_admin() )
  47. return false;
  48. if ( ! WPN_Helper::is_func_disabled( 'ignore_user_abort' ) ) {
  49. ignore_user_abort( true );
  50. }
  51. // if ( ! nf_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
  52. // //set_time_limit( 0 );
  53. // }
  54. add_action( 'wp_ajax_nf_' . $this->action, array( $this, 'processing' ) );
  55. }
  56. /**
  57. * Process our request.
  58. * Call the appropriate loading or step functions.
  59. *
  60. * @since 2.7.6
  61. * @return void
  62. */
  63. public function processing() {
  64. // Get our passed arguments. These come from the querysting of the processing page.
  65. if ( isset ( $_REQUEST['args'] ) ) {
  66. $this->args = $_REQUEST['args'];
  67. if ( isset ( $this->args['redirect'] ) ) {
  68. $this->redirect = $this->args['redirect'];
  69. }
  70. } else {
  71. $this->args = array();
  72. }
  73. // Get our current step.
  74. $this->step = isset ( $_REQUEST['step'] )? esc_html( $_REQUEST['step'] ) : 'loading';
  75. // Get our total steps
  76. $this->total_steps = isset ( $_REQUEST['total_steps'] )? esc_html( $_REQUEST['total_steps'] ) : 0;
  77. // If our step is loading, then we need to return how many total steps there are along with the next step, which is 1.
  78. if ( 'loading' == $this->step ) {
  79. $return = $this->loading();
  80. if ( ! isset ( $return['step'] ) ) {
  81. $saved_step = get_user_option( 'nf_step_processing_' . $this->action . '_step' );
  82. if ( ! empty ( $saved_step ) ) {
  83. $this->step = $saved_step;
  84. } else {
  85. $this->step = 1;
  86. }
  87. $return['step'] = $this->step;
  88. }
  89. if ( ! isset ( $return['complete'] ) ) {
  90. $return['complete'] = false;
  91. }
  92. } else { // We aren't on the loading step, so do our processing.
  93. $return = $this->step();
  94. if ( ! isset ( $return['step'] ) ) {
  95. $this->step++;
  96. $return['step'] = $this->step;
  97. }
  98. if ( ! isset ( $return['complete'] ) ) {
  99. if ( $this->step > $this->total_steps ) {
  100. $complete = true;
  101. } else {
  102. $complete = false;
  103. }
  104. $return['complete'] = $complete;
  105. }
  106. $return['total_steps'] = $this->total_steps;
  107. }
  108. $user_id = get_current_user_id();
  109. if ( $return['complete'] ) {
  110. // Delete our step option
  111. delete_user_option( $user_id, 'nf_step_processing_' . $this->action . '_step' );
  112. // Set our redirect variable.
  113. $return['redirect'] = $this->redirect;
  114. // Run our complete function
  115. $this->complete();
  116. } else {
  117. // Save our current step so that we can resume if necessary
  118. update_user_option( $user_id, 'nf_step_processing_' . $this->action . '_step', $this->step );
  119. }
  120. if ( isset ( $this->redirect ) && ! empty ( $this->redirect ) ) {
  121. $this->args['redirect'] = $this->redirect;
  122. }
  123. $return['errors'] = ( $this->errors ) ? $this->errors : FALSE;
  124. $return['args'] = $this->args;
  125. echo json_encode( $return );
  126. die();
  127. }
  128. /**
  129. * Run our loading process.
  130. * This function should be overwritten in child classes.
  131. *
  132. * @since 2.7.4
  133. * @return array $args
  134. */
  135. public function loading() {
  136. // This space left intentionally blank.
  137. }
  138. /**
  139. * This function is called for every step.
  140. * This function should be overwritten in child classes.
  141. *
  142. * @since 2.7.4
  143. * @return array $args
  144. */
  145. public function step() {
  146. // This space left intentionally blank.
  147. }
  148. /**
  149. * This function is called for every step.
  150. * This function should be overwritten in child classes.
  151. *
  152. * @since 2.7.4
  153. * @return array $args
  154. */
  155. public function complete() {
  156. // This space left intentionally blank.
  157. }
  158. }