ChunkPublish.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_Batch_Process
  4. */
  5. class NF_Admin_Processes_ChunkPublish extends NF_Abstracts_BatchProcess
  6. {
  7. // header( 'Content-Type: application/json' );
  8. private $data;
  9. private $form_id;
  10. private $response = array(
  11. 'last_request' => 'failure',
  12. 'batch_complete' => false,
  13. );
  14. protected $_data = array();
  15. protected $_errors = array();
  16. protected $_debug = array();
  17. /**
  18. * Constructor
  19. */
  20. public function __construct( $data = array() )
  21. {
  22. //Bail if we aren't in the admin.
  23. if ( ! is_admin() )
  24. return false;
  25. // Record our data if we have any.
  26. $this->data = $data[ 'data' ];
  27. $this->form_id = $this->data[ 'form_id' ];
  28. // Run process.
  29. $this->process();
  30. }
  31. /**
  32. * Function to loop over the batch.
  33. *
  34. * @return JSON
  35. * Str last_response = success/failure
  36. * Bool batch_complete = true/false
  37. * Int requesting = x
  38. */
  39. public function process()
  40. {
  41. // If we were told this is a new publish...
  42. if ( $this->data[ 'new_publish' ] === 'true' ) {
  43. // Delete our option to reset the process.
  44. $this->remove_option();
  45. }
  46. // Fetch our option to see what step we're on.
  47. $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
  48. // If we don't have an option to see what step we're on...
  49. if ( ! $batch ) {
  50. // Run startup.
  51. $this->startup();
  52. // Fetch our option now that it's created.
  53. $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
  54. }
  55. $batch = explode( ',', $batch );
  56. // Update the chunk.
  57. $this->add_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $batch[ 0 ], stripslashes( $this->data[ 'chunk' ] ) );
  58. // Increment our step.
  59. $batch[ 0 ]++;
  60. // If this was our last step...
  61. if ( $batch[ 0 ] == $batch[ 1 ] ) {
  62. // Run cleanup.
  63. $this->cleanup();
  64. } // Otherwise... (We have more steps.)
  65. else {
  66. // Update our step option.
  67. $this->add_chunk( 'nf_chunk_publish_' . $this->form_id, implode( ',', $batch ) );
  68. // Request our next chunk.
  69. $this->response[ 'requesting' ] = $batch[ 0 ];
  70. }
  71. $this->response[ 'last_request' ] = 'success';
  72. echo wp_json_encode( $this->response );
  73. wp_die();
  74. }
  75. /**
  76. * Function to run any setup steps necessary to begin processing.
  77. */
  78. public function startup()
  79. {
  80. $value = '0,' . $this->data[ 'chunk_total' ];
  81. // Write our option to manage the process.
  82. $this->add_chunk( 'nf_chunk_publish_' . $this->form_id, $value );
  83. // Process the first item.
  84. $this->process();
  85. }
  86. /**
  87. * Function to cleanup any lingering temporary elements of a batch process after completion.
  88. */
  89. public function cleanup()
  90. {
  91. // Get all of the chunks.
  92. $build = '';
  93. $batch = $this->get_chunk( 'nf_chunk_publish_' . $this->form_id );
  94. $batch = explode( ',', $batch );
  95. // Add all of our chunks into a string.
  96. for ( $i = 0; $i < $batch[ 1 ]; $i++ ) {
  97. $build .= $this->get_chunk( 'nf_form_' . $this->form_id . '_publishing_' . $i );
  98. }
  99. $form_data = json_decode( $build, ARRAY_A );
  100. // Start copied code.
  101. if( is_string( $form_data[ 'id' ] ) ) {
  102. $tmp_id = $form_data[ 'id' ];
  103. $form = Ninja_Forms()->form()->get();
  104. $form->save();
  105. $form_data[ 'id' ] = $form->get_id();
  106. $this->_data[ 'new_ids' ][ 'forms' ][ $tmp_id ] = $form_data[ 'id' ];
  107. } else {
  108. $form = Ninja_Forms()->form($form_data['id'])->get();
  109. }
  110. unset( $form_data[ 'settings' ][ '_seq_num' ] );
  111. $form->update_settings( $form_data[ 'settings' ] )->save();
  112. if( isset( $form_data[ 'fields' ] ) ) {
  113. $db_fields_controller = new NF_Database_FieldsController( $form_data[ 'id' ], $form_data[ 'fields' ] );
  114. $db_fields_controller->run();
  115. $form_data[ 'fields' ] = $db_fields_controller->get_updated_fields_data();
  116. $this->_data['new_ids']['fields'] = $db_fields_controller->get_new_field_ids();
  117. }
  118. if( isset( $form_data[ 'deleted_fields' ] ) ){
  119. foreach( $form_data[ 'deleted_fields' ] as $deleted_field_id ){
  120. $field = Ninja_Forms()->form( $form_data[ 'id' ])->get_field( $deleted_field_id );
  121. $field->delete();
  122. }
  123. }
  124. if( isset( $form_data[ 'actions' ] ) ) {
  125. /*
  126. * Loop Actions and fire Save() hooks.
  127. */
  128. foreach ($form_data['actions'] as &$action_data) {
  129. $id = $action_data['id'];
  130. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $id );
  131. $action->update_settings($action_data['settings'])->save();
  132. $action_type = $action->get_setting( 'type' );
  133. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) {
  134. $action_class = Ninja_Forms()->actions[ $action_type ];
  135. $action_settings = $action_class->save( $action_data['settings'] );
  136. if( $action_settings ){
  137. $action_data['settings'] = $action_settings;
  138. $action->update_settings( $action_settings )->save();
  139. }
  140. }
  141. if ($action->get_tmp_id()) {
  142. $tmp_id = $action->get_tmp_id();
  143. $this->_data['new_ids']['actions'][$tmp_id] = $action->get_id();
  144. $action_data[ 'id' ] = $action->get_id();
  145. }
  146. $this->_data[ 'actions' ][ $action->get_id() ] = $action->get_settings();
  147. }
  148. }
  149. /*
  150. * Loop Actions and fire Publish() hooks.
  151. */
  152. foreach ($form_data['actions'] as &$action_data) {
  153. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $action_data['id'] );
  154. $action_type = $action->get_setting( 'type' );
  155. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) {
  156. $action_class = Ninja_Forms()->actions[ $action_type ];
  157. if( $action->get_setting( 'active' ) && method_exists( $action_class, 'publish' ) ) {
  158. $data = $action_class->publish( $this->_data );
  159. if ($data) {
  160. $this->_data = $data;
  161. }
  162. }
  163. }
  164. }
  165. if( isset( $form_data[ 'deleted_actions' ] ) ){
  166. foreach( $form_data[ 'deleted_actions' ] as $deleted_action_id ){
  167. $action = Ninja_Forms()->form()->get_action( $deleted_action_id );
  168. $action->delete();
  169. }
  170. }
  171. delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] );
  172. WPN_Helper::update_nf_cache( $form_data[ 'id' ], $form_data );
  173. do_action( 'ninja_forms_save_form', $form->get_id() );
  174. if( isset( $this->_data['debug'] ) ) {
  175. $this->_debug = array_merge( $this->_debug, $this->_data[ 'debug' ] );
  176. }
  177. if( isset( $this->_data['errors'] ) && $this->_data[ 'errors' ] ) {
  178. $this->_errors = array_merge( $this->_errors, $this->_data[ 'errors' ] );
  179. }
  180. // Remove our option.
  181. $this->remove_option();
  182. $response = array( 'data' => $this->_data, 'errors' => $this->_errors, 'debug' => $this->_debug, 'batch_complete' => true );
  183. echo wp_json_encode( $response );
  184. wp_die(); // this is required to terminate immediately and return a proper response
  185. }
  186. /**
  187. * Function to get our chunk data from the chunks table.
  188. *
  189. * @param $slug (string) The name of the option in the db.
  190. * @return string or FALSE
  191. */
  192. public function get_chunk( $slug ) {
  193. global $wpdb;
  194. // Get our option from our chunks table.
  195. $sql = $wpdb->prepare( "SELECT `value` FROM `{$wpdb->prefix}nf3_chunks` WHERE `name` = %s", $slug );
  196. $data = $wpdb->get_results( $sql, 'ARRAY_A' );
  197. // If it exists there...
  198. if ( ! empty( $data ) ) {
  199. // Hand it off.
  200. return $data[ 0 ][ 'value' ];
  201. } // Otherwise... (It does not exist there.)
  202. else {
  203. // Try to fetch it from the options table.
  204. return get_option( $slug );
  205. }
  206. }
  207. /**
  208. * Function to replace update_option.
  209. *
  210. * @param $slug (string) The name of the option in the db.
  211. * @param $content (string) The data to be stored in the option.
  212. */
  213. public function add_chunk( $slug, $content ) {
  214. // Check for an existing option.
  215. global $wpdb;
  216. $sql = "SELECT id FROM `{$wpdb->prefix}nf3_chunks` WHERE name = '{$slug}'";
  217. $result = $wpdb->query( $sql );
  218. // If we don't have one...
  219. if ( empty ( $result ) ) {
  220. // Insert it.
  221. $sql = $wpdb->prepare( "INSERT INTO `{$wpdb->prefix}nf3_chunks` (name, value) VALUES ( %s, %s )", $slug, $content );
  222. } // Otherwise... (We do have one.)
  223. else {
  224. // Update the existing one.
  225. $sql = $wpdb->prepare( "UPDATE `{$wpdb->prefix}nf3_chunks` SET value = %s WHERE name = %s", $content, $slug );
  226. }
  227. $wpdb->query( $sql );
  228. }
  229. /*
  230. * Function to remove our management option and remove any temporary chunk data.
  231. */
  232. public function remove_option() {
  233. // Remove our option to manage the process.
  234. global $wpdb;
  235. $sql = $wpdb->prepare( "DELETE FROM `{$wpdb->prefix}nf3_chunks` WHERE name = %s", 'nf_chunk_publish_' . $this->form_id );
  236. $wpdb->query( $sql );
  237. // If our form_id was a temp id...
  238. if ( ! is_numeric( $this->form_id ) ) {
  239. // Remove all of our chunk options.
  240. $sql = $wpdb->prepare( "DELETE FROM `" . $wpdb->prefix . "nf3_chunks` WHERE name LIKE %s", 'nf_form_' . $this->form_id . '_publishing_%' );
  241. $wpdb->query( $sql );
  242. }
  243. $this->data[ 'new_publish' ] = 'false';
  244. }
  245. }