NF_AJAX_Controllers_Preview

The Ninja Forms NF AJAX Controllers Preview class.

Defined (1)

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

/includes/AJAX/Controllers/Preview.php  
  1. class NF_AJAX_Controllers_Preview extends NF_Abstracts_Controller 
  2. private static $transient_prefix = 'nf_form_preview_'; 
  3.  
  4. public function __construct() 
  5. add_action( 'wp_ajax_nf_preview_update', array( $this, 'update' ) ); 
  6.  
  7. add_filter( 'ninja_forms_run_action_settings', array( $this, 'filter_action_settings' ), 10, 4 ); 
  8.  
  9. public function update() 
  10. check_ajax_referer( 'ninja_forms_builder_nonce', 'security' ); 
  11.  
  12. $form = json_decode( stripslashes( $_POST['form'] ), ARRAY_A ); 
  13.  
  14. $form_id = $form[ 'id' ]; 
  15.  
  16. $form_data = $this->get_form_data( $form_id ); 
  17.  
  18. /** 
  19. * Form Settings 
  20. */ 
  21.  
  22. if( isset( $form[ 'settings' ] ) && is_array( $form[ 'settings' ] ) ) { 
  23.  
  24. $old_settings = $form_data[ 'settings' ]; 
  25.  
  26. $form_data[ 'settings' ] = array_merge( $old_settings, $form[ 'settings' ] ); 
  27.  
  28. /** 
  29. * Fields and Field Settings 
  30. */ 
  31.  
  32. if( isset( $form[ 'fields' ] ) && is_array( $form[ 'fields' ] ) ) { 
  33.  
  34. foreach( $form[ 'fields' ] as $field ) { 
  35.  
  36. $id = $field[ 'id' ]; 
  37.  
  38. $old_settings = ( isset( $form_data[ 'fields' ][ $id ][ 'settings' ] ) ) ? $form_data[ 'fields' ][ $id ][ 'settings' ] : array(); 
  39.  
  40. $new_settings = array_merge( $old_settings, $field[ 'settings' ] ); 
  41.  
  42. $form_data[ 'fields' ][ $id ][ 'settings' ] = $new_settings; 
  43.  
  44. if( isset( $form[ 'deleted_fields' ] ) ) { 
  45.  
  46. foreach( $form[ 'deleted_fields' ] as $deleted_field ) { 
  47.  
  48. unset( $form_data[ 'fields' ][ $deleted_field ] ); 
  49.  
  50. /** 
  51. * Actions and Action Settings 
  52. */ 
  53.  
  54. if( isset( $form[ 'actions' ] ) && is_array( $form[ 'actions' ] ) ) { 
  55.  
  56. foreach( $form[ 'actions' ] as $action ) { 
  57.  
  58. $id = $action[ 'id' ]; 
  59.  
  60. if( isset( $form[ 'deleted_actions' ][ $id ] ) ) { 
  61.  
  62. unset( $form_data[ 'actions' ][ $id ] ); 
  63. continue; 
  64.  
  65. $old_settings = ( isset ( $form_data[ 'actions' ][ $id ][ 'settings' ] ) ) ? $form_data[ 'actions' ][ $id ][ 'settings' ]: array(); 
  66.  
  67. $new_settings = array_merge( $old_settings, $action[ 'settings' ] ); 
  68.  
  69. $form_data[ 'actions' ][ $id ][ 'settings' ] = $new_settings; 
  70.  
  71. if( isset( $form[ 'deleted_actions' ] ) ) { 
  72.  
  73. foreach( $form[ 'deleted_actions' ] as $deleted_action ) { 
  74.  
  75. unset( $form_data[ 'actions' ][ $deleted_action ] ); 
  76.  
  77.  
  78.  
  79. $this->update_form_data( $form_data ); 
  80.  
  81. $this->_data['form'] = $form_data; 
  82.  
  83. do_action( 'ninja_forms_save_form_preview', $form_id ); 
  84.  
  85. $this->_respond(); 
  86.  
  87. public function filter_action_settings( $action_settings, $form_id, $action_id, $form_settings ) 
  88. if( ! isset( $form_settings[ 'is_preview' ] ) ) return $action_settings; 
  89.  
  90. $form_data = $this->get_form_data( $form_id ); 
  91.  
  92. if( isset( $form_data[ 'actions' ][ $action_id ] ) ) { 
  93.  
  94. $settings = $form_data['actions'][$action_id]['settings']; 
  95. $action_settings = array_merge( $action_settings, $settings ); 
  96.  
  97. return $action_settings; 
  98.  
  99. private function get_form_data( $form_id ) 
  100. $form_data = get_user_option( self::$transient_prefix . $form_id, FALSE ); 
  101.  
  102. if( ! $form_data ) { 
  103.  
  104. if( is_string( $form_id ) ) { 
  105. $form = Ninja_Forms()->form()->get(); 
  106. $form_data['id'] = $form_id; 
  107. $form_data[ 'settings' ] = array(); 
  108. $form_data[ 'fields' ] = array(); 
  109. $form_data[ 'actions' ] = array(); 
  110. } else { 
  111. $form = Ninja_Forms()->form($form_id)->get(); 
  112. $form_data['id'] = $form_id; 
  113.  
  114. $form_data[ 'settings' ] = $form->get_settings(); 
  115.  
  116. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  117. foreach( $fields as $field ) { 
  118.  
  119. $field_id = $field->get_id(); 
  120. $form_data[ 'fields' ][ $field_id ][ 'settings' ] = $field->get_settings(); 
  121.  
  122. $actions = Ninja_Forms()->form( $form_id )->get_actions(); 
  123. foreach( $actions as $action ) { 
  124.  
  125. $action_id = $action->get_id(); 
  126. $form_data[ 'actions' ][ $action_id ][ 'settings' ] = $action->get_settings(); 
  127.  
  128. return $form_data; 
  129.  
  130. private function update_form_data( $form_data ) 
  131. $update = update_user_option( get_current_user_id(), self::$transient_prefix . $form_data['id'], $form_data ); 
  132.  
  133. $this->_data[ 'updated' ] = $update; 
  134.  
  135. if( ! $update ) { 
  136. $this->_errors[ 'Form Preview Not Updated' ] = $form_data; 
  137. $this->_errors[ 'Current User' ] = get_current_user_id(); 
  138. $this->_errors[ 'Option' ] = self::$transient_prefix . $form_data['id'];