NF_AJAX_Controllers_Form

The Ninja Forms NF AJAX Controllers Form class.

Defined (1)

The class is defined in the following location(s).

/includes/AJAX/Controllers/Form.php  
  1. class NF_AJAX_Controllers_Form extends NF_Abstracts_Controller 
  2. private $publish_processing; 
  3.  
  4. public function __construct() 
  5. add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); 
  6.  
  7. add_action( 'wp_ajax_nf_save_form', array( $this, 'save' ) ); 
  8. add_action( 'wp_ajax_nf_delete_form', array( $this, 'delete' ) ); 
  9.  
  10. public function plugins_loaded() 
  11. $this->publish_processing = new NF_Database_PublishProcessing(); 
  12.  
  13. public function save() 
  14. check_ajax_referer( 'ninja_forms_builder_nonce', 'security' ); 
  15.  
  16. if( ! isset( $_POST[ 'form' ] ) ) { 
  17. $this->_errors[] = __( 'Form Not Found', 'ninja-forms' ); 
  18. $this->_respond(); 
  19.  
  20. $form_data = json_decode( stripslashes( $_POST['form'] ), ARRAY_A ); 
  21.  
  22. if( is_string( $form_data[ 'id' ] ) ) { 
  23. $tmp_id = $form_data[ 'id' ]; 
  24. $form = Ninja_Forms()->form()->get(); 
  25. $form->save(); 
  26. $form_data[ 'id' ] = $form->get_id(); 
  27. $this->_data[ 'new_ids' ][ 'forms' ][ $tmp_id ] = $form_data[ 'id' ]; 
  28. } else { 
  29. $form = Ninja_Forms()->form($form_data['id'])->get(); 
  30.  
  31. $form->update_settings( $form_data[ 'settings' ] )->save(); 
  32.  
  33. if( isset( $form_data[ 'fields' ] ) ) { 
  34.  
  35. foreach ($form_data['fields'] as $field_data) { 
  36.  
  37. if( 'unknown' == $field_data[ 'settings' ][ 'type' ] ) continue; 
  38.  
  39. $id = $field_data['id']; 
  40.  
  41. $field = Ninja_Forms()->form( $form_data[ 'id' ] )->get_field($id); 
  42.  
  43. if ($field->get_tmp_id()) { 
  44.  
  45. $field->save(); 
  46. $tmp_id = $field->get_tmp_id(); 
  47. $this->_data['new_ids']['fields'][$tmp_id] = $field->get_id(); 
  48.  
  49. $this->publish_processing->push_to_queue( array( 
  50. 'id' => $field->get_id(),  
  51. 'type' => 'field',  
  52. 'settings' => $field_data[ 'settings' ] 
  53. )); 
  54.  
  55. $this->_data[ 'fields' ][ $id ] = $field->get_settings(); 
  56.  
  57. $this->publish_processing->save()->dispatch(); 
  58.  
  59. if( isset( $form_data[ 'deleted_fields' ] ) ) { 
  60.  
  61. foreach( $form_data[ 'deleted_fields' ] as $deleted_field_id ) { 
  62.  
  63. $field = Ninja_Forms()->form()->get_field( $deleted_field_id ); 
  64. $field->delete(); 
  65.  
  66. if( isset( $form_data[ 'actions' ] ) ) { 
  67.  
  68. /** 
  69. * Loop Actions and fire Save() hooks. 
  70. */ 
  71. foreach ($form_data['actions'] as $action_data) { 
  72.  
  73. $id = $action_data['id']; 
  74.  
  75. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $id ); 
  76.  
  77. $action->update_settings($action_data['settings'])->save(); 
  78.  
  79. $action_type = $action->get_setting( 'type' ); 
  80.  
  81. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) { 
  82. $action_class = Ninja_Forms()->actions[ $action_type ]; 
  83.  
  84. $action_settings = $action_class->save( $action_data['settings'] ); 
  85. if( $action_settings ) { 
  86. $action_data['settings'] = $action_settings; 
  87. $action->update_settings( $action_settings )->save(); 
  88.  
  89. if ($action->get_tmp_id()) { 
  90.  
  91. $tmp_id = $action->get_tmp_id(); 
  92. $this->_data['new_ids']['actions'][$tmp_id] = $action->get_id(); 
  93.  
  94. $this->_data[ 'actions' ][ $id ] = $action->get_settings(); 
  95.  
  96. /** 
  97. * Loop Actions and fire Publish() hooks. 
  98. */ 
  99. foreach ($form_data['actions'] as $action_data) { 
  100.  
  101. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $action_data['id'] ); 
  102.  
  103. $action_type = $action->get_setting( 'type' ); 
  104.  
  105. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) { 
  106. $action_class = Ninja_Forms()->actions[ $action_type ]; 
  107.  
  108. if( $action->get_setting( 'active' ) && method_exists( $action_class, 'publish' ) ) { 
  109. $data = $action_class->publish( $this->_data ); 
  110. if ($data) { 
  111. $this->_data = $data; 
  112.  
  113. if( isset( $form_data[ 'deleted_actions' ] ) ) { 
  114.  
  115. foreach( $form_data[ 'deleted_actions' ] as $deleted_action_id ) { 
  116.  
  117. $action = Ninja_Forms()->form()->get_action( $deleted_action_id ); 
  118. $action->delete(); 
  119.  
  120. delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] ); 
  121. update_option( 'nf_form_' . $form_data[ 'id' ], $form_data ); 
  122.  
  123. do_action( 'ninja_forms_save_form', $form->get_id() ); 
  124.  
  125. $this->_respond(); 
  126.  
  127. public function delete() 
  128. check_ajax_referer( 'ninja_forms_builder_nonce', 'security' ); 
  129.  
  130. $this->_respond();