step-processing.php 4.4 KB

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