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. unset( $form_data[ 'settings' ][ '_seq_num' ] ); 
  32.  
  33. $form->update_settings( $form_data[ 'settings' ] )->save(); 
  34.  
  35. if( isset( $form_data[ 'fields' ] ) ) { 
  36. $db_fields_controller = new NF_Database_FieldsController( $form_data[ 'id' ], $form_data[ 'fields' ] ); 
  37. $db_fields_controller->run(); 
  38. $form_data[ 'fields' ] = $db_fields_controller->get_updated_fields_data(); 
  39. $this->_data['new_ids']['fields'] = $db_fields_controller->get_new_field_ids(); 
  40.  
  41. if( isset( $form_data[ 'deleted_fields' ] ) ) { 
  42.  
  43. foreach( $form_data[ 'deleted_fields' ] as $deleted_field_id ) { 
  44.  
  45. $field = Ninja_Forms()->form()->get_field( $deleted_field_id ); 
  46. $field->delete(); 
  47.  
  48. if( isset( $form_data[ 'actions' ] ) ) { 
  49.  
  50. /** 
  51. * Loop Actions and fire Save() hooks. 
  52. */ 
  53. foreach ($form_data['actions'] as &$action_data) { 
  54.  
  55. $id = $action_data['id']; 
  56.  
  57. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $id ); 
  58.  
  59. $action->update_settings($action_data['settings'])->save(); 
  60.  
  61. $action_type = $action->get_setting( 'type' ); 
  62.  
  63. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) { 
  64. $action_class = Ninja_Forms()->actions[ $action_type ]; 
  65.  
  66. $action_settings = $action_class->save( $action_data['settings'] ); 
  67. if( $action_settings ) { 
  68. $action_data['settings'] = $action_settings; 
  69. $action->update_settings( $action_settings )->save(); 
  70.  
  71. if ($action->get_tmp_id()) { 
  72.  
  73. $tmp_id = $action->get_tmp_id(); 
  74. $this->_data['new_ids']['actions'][$tmp_id] = $action->get_id(); 
  75. $action_data[ 'id' ] = $action->get_id(); 
  76.  
  77. $this->_data[ 'actions' ][ $action->get_id() ] = $action->get_settings(); 
  78.  
  79. /** 
  80. * Loop Actions and fire Publish() hooks. 
  81. */ 
  82. foreach ($form_data['actions'] as &$action_data) { 
  83.  
  84. $action = Ninja_Forms()->form( $form_data[ 'id' ] )->get_action( $action_data['id'] ); 
  85.  
  86. $action_type = $action->get_setting( 'type' ); 
  87.  
  88. if( isset( Ninja_Forms()->actions[ $action_type ] ) ) { 
  89. $action_class = Ninja_Forms()->actions[ $action_type ]; 
  90.  
  91. if( $action->get_setting( 'active' ) && method_exists( $action_class, 'publish' ) ) { 
  92. $data = $action_class->publish( $this->_data ); 
  93. if ($data) { 
  94. $this->_data = $data; 
  95.  
  96. if( isset( $form_data[ 'deleted_actions' ] ) ) { 
  97.  
  98. foreach( $form_data[ 'deleted_actions' ] as $deleted_action_id ) { 
  99.  
  100. $action = Ninja_Forms()->form()->get_action( $deleted_action_id ); 
  101. $action->delete(); 
  102.  
  103. delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_data['id'] ); 
  104. update_option( 'nf_form_' . $form_data[ 'id' ], $form_data ); 
  105.  
  106. do_action( 'ninja_forms_save_form', $form->get_id() ); 
  107.  
  108. $this->_respond(); 
  109.  
  110. public function delete() 
  111. check_ajax_referer( 'ninja_forms_builder_nonce', 'security' ); 
  112.  
  113. $this->_respond();