cf_ajax_redirect

The Caldera Forms cf ajax redirect function.

Description

cf_ajax_redirect( $type, $url, $form ); 

Parameters (3)

0. $type
The type.
1. $url
The url.
2. $form
The form.

Usage

  1. if ( !function_exists( 'cf_ajax_redirect' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-forms/includes/ajax.php'; 
  3.  
  4. // The type. 
  5. $type = null; 
  6.  
  7. // The url. 
  8. $url = null; 
  9.  
  10. // The form. 
  11. $form = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = cf_ajax_redirect($type, $url, $form); 
  15.  

Defined (1)

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

/includes/ajax.php  
  1. function cf_ajax_redirect($type, $url, $form) { 
  2.  
  3. if(empty($form['form_ajax'])) { 
  4. return; 
  5.  
  6. if( empty( $_POST['cfajax'] ) || empty( $_POST['action'] ) || $_POST['action'] != 'cf_process_ajax_submit' ) { 
  7. return; 
  8.  
  9. $data = Caldera_Forms::get_submission_data($form); 
  10.  
  11. // setup notcies 
  12. $urlparts = parse_url($url); 
  13. $query = array(); 
  14.  
  15. if(!empty($urlparts['query'])) { 
  16. parse_str($urlparts['query'], $query); 
  17.  
  18. $notices = array(); 
  19. $note_general_classes = Caldera_Forms_Render_Notices::get_note_general_classes( $form ); 
  20.  
  21. // base id 
  22. $form_id = 'caldera_form_1'; 
  23.  
  24. if($type == 'complete') { 
  25. if(isset($query['cf_su'])) { 
  26. $notices['success']['note'] = $form['success']; 
  27. $form_id = 'caldera_form_' . $query['cf_su']; 
  28. }else{ 
  29. $out['url'] = $url; 
  30. $notices['success']['note'] = esc_html__( 'Redirecting', 'caldera-forms'); 
  31. }elseif($type == 'preprocess') { 
  32. if(isset($query['cf_er'])) { 
  33. $data = Caldera_Forms_Transient::get_transient( $query['cf_er'] ); 
  34. if(!empty($data['note'])) { 
  35. $notices[$data['type']]['note'] = $data['note']; 
  36.  
  37. }else{ 
  38. $out['url'] = $url; 
  39. $notices['success']['note'] = esc_html__( 'Redirecting', 'caldera-forms'); 
  40.  
  41. }elseif($type == error) { 
  42. $data = Caldera_Forms_Transient::get_transient( $query['cf_er'] ); 
  43. if(!empty($data['note'])) { 
  44. $notices[error]['note'] = $data['note']; 
  45.  
  46. // check for field erors 
  47. if(!empty($data['fields'])) { 
  48. foreach($form['fields'] as $fieldid=>$field) { 
  49. if( isset( $data['fields'][$fieldid] ) ) { 
  50.  
  51. if($urlparts['path'] == 'api') { 
  52. $out['fields'][$field['slug']] = $data['fields'][$fieldid]; 
  53. }else{ 
  54. $out['fields'][$fieldid] = $data['fields'][$fieldid]; 
  55.  
  56. $notices = Caldera_Forms_Render_Notices::prepare_notices( $notices, $form ); 
  57. $note_classes = Caldera_Forms_Render_Notices::get_note_classes( $note_general_classes, $form ); 
  58.  
  59. $html = Caldera_Forms_Render_Notices::html_from_notices( $notices, $note_classes ); 
  60.  
  61. if(!empty($result)) { 
  62. $out['result'] = $result; 
  63.  
  64. if(!empty($query)) { 
  65. if(!empty($query['cf_su'])) { 
  66. unset($query['cf_su']); 
  67. if(!empty($query['cf_ee'])) { 
  68. $out['entry'] = $query['cf_ee']; 
  69. $out['data'] = $query; 
  70. $out['html'] = $html; 
  71. $out['type'] = ( isset($data['type']) ? $data['type'] : $type ); 
  72. $out['form_id'] = $form['ID']; 
  73. $out['form_name'] = $form['name'];  
  74. $out['status'] = $type; 
  75.  
  76. if( ! empty( $form[ 'scroll_top' ] ) ) { 
  77. $out[ 'scroll' ] = Caldera_Forms_Render_Util::notice_element_id( $form, absint( $_POST[ '_cf_frm_ct' ] ) ); 
  78.  
  79. $out = apply_filters( 'caldera_forms_ajax_return', $out, $form); 
  80.  
  81. wp_send_json( $out ); 
  82. exit; 
  83.