cf_custom_fields_capture_entry

Process entry and save as post/ post meta.

Description

(array) cf_custom_fields_capture_entry( (array) $config, (array) $form ); 

Returns (array)

Parameters (2)

0. $config (array)
Processor config.
1. $form (array)
From config.

Usage

  1. if ( !function_exists( 'cf_custom_fields_capture_entry' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-custom-fields/includes/to-post-type.php'; 
  3.  
  4. // Processor config. 
  5. $config = array(); 
  6.  
  7. // From config. 
  8. $form = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = cf_custom_fields_capture_entry($config, $form); 
  12.  

Defined (2)

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

/includes/to-post-type.php  
  1. function cf_custom_fields_capture_entry($config, $form) { 
  2.  
  3. $user_id = get_current_user_id(); 
  4. if( !empty( $config['post_author'] ) ) { 
  5. $user_id = Caldera_Forms::do_magic_tags( $config['post_author'] ); 
  6.  
  7. $entry = array( 
  8. 'post_title' => Caldera_Forms::get_field_data( $config['post_title'], $form ),  
  9. 'post_status' => Caldera_Forms::do_magic_tags( $config['post_status'] ),  
  10. 'post_type' => $config['post_type'],  
  11. 'post_content' => Caldera_Forms::get_field_data( $config['post_content'], $form ),  
  12. 'post_parent' => Caldera_Forms::do_magic_tags( $config['post_parent'] ),  
  13. 'to_ping' => Caldera_Forms::do_magic_tags( $config['to_ping'] ),  
  14. 'post_password' => Caldera_Forms::do_magic_tags( $config['post_password'] ),  
  15. 'post_excerpt' => Caldera_Forms::do_magic_tags( $config['post_excerpt'] ),  
  16. 'comment_status'=> $config['comment_status'],  
  17. ); 
  18.  
  19. if( empty( $entry[ 'post_content' ] ) ) { 
  20. $entry[ 'post_content' ] = ''; 
  21.  
  22. // set the ID 
  23. if( !empty( $config['ID'] ) ) { 
  24. $is_post_id = Caldera_Forms::do_magic_tags( $config['ID'] ); 
  25. $post = get_post( $is_post_id ); 
  26. if( !empty( $post ) && $post->post_type == $entry['post_type'] ) { 
  27. $entry['ID'] = $is_post_id; 
  28.  
  29.  
  30. // set author 
  31. if( !empty( $user_id ) ) { 
  32. $entry['post_author'] = $user_id; 
  33.  
  34. //is edit? 
  35. if(!empty($_POST['_cf_frm_edt'])) { 
  36. // need to work on this still. SIGH! 
  37. }else{ 
  38. // Insert the post into the database 
  39. $entry_id = wp_insert_post( $entry ); 
  40. if(empty($entry_id)) { 
  41. return; 
  42.  
  43.  
  44.  
  45. // do upload + attach 
  46. if( !empty( $config['featured_image'] ) ) { 
  47. $featured_image = Caldera_Forms::get_field_data( $config['featured_image'], $form ); 
  48. foreach( (array) $featured_image as $filename ) { 
  49. $featured_image = cf_custom_fields_attach_file( $filename, $entry_id ); 
  50. update_post_meta($entry_id, '_thumbnail_id', $featured_image ); 
  51.  
  52.  
  53. //handle taxonomies 
  54. $terms_saved = false; 
  55. $tax_fields = cf_custom_fields_get_taxonomy_fields( $config ); 
  56. if ( ! empty( $tax_fields ) ) { 
  57. $terms_saved = cf_custom_fields_save_terms( $tax_fields, $entry_id ); 
  58. if ( $terms_saved ) { 
  59. $term_values = wp_list_pluck( $tax_fields, 'terms' ); 
  60.  
  61. //get post fields into an array of fields not to save as meta. 
  62. $post_fields = array_keys( $entry ); 
  63. // get all submission data 
  64. $data = Caldera_Forms::get_submission_data( $form ); 
  65. update_post_meta( $entry_id, '_cf_form_id', $form['ID'] ); 
  66. foreach($data as $field=>$value) { 
  67. if ( '_entry_token' != $field && '_entry_id' != $field ) { 
  68. if ( in_array( $field, $post_fields ) || in_array( $form['fields'][ $field ]['ID'], $post_fields ) ) { 
  69. continue; 
  70.  
  71.  
  72. if ( $terms_saved ) { 
  73. if ( is_array( $value ) ) { 
  74. $_value = implode( ', ', $value ); 
  75. } else { 
  76. $_value = $value; 
  77.  
  78. if( in_array( $_value, $term_values ) ) { 
  79. continue; 
  80.  
  81.  
  82.  
  83.  
  84. if(empty($form['fields'][$field])) { 
  85. continue; 
  86. if( in_array( $form['fields'][$field]['type'], array( 'button', 'html' ) ) ) { 
  87. continue; 
  88. if( $form['fields'][$field]['type'] == 'file' ) { 
  89. if( $field == $config['featured_image'] ) { 
  90. continue; // dont attache twice. 
  91. foreach( (array) $value as $file ) { 
  92. cf_custom_fields_attach_file( $file , $entry_id ); 
  93.  
  94. $slug = $form['fields'][$field]['slug']; 
  95.  
  96. /** 
  97. * Filter value before saving using to post type processor 
  98. * @since 2.0.3 
  99. * @param mixed $value The value to be saved 
  100. * @param string $slug Slug of field 
  101. * @param int $entry ID of post 
  102. */ 
  103. $value = apply_filters( 'cf_custom_fields_pre_save_meta_key_to_post_type', $value, $slug, $entry_id ); 
  104. update_post_meta( $entry_id, $slug, $value ); 
  105.  
  106. return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink( $entry_id ) ); 
  1. function cf_custom_fields_capture_entry($config, $form) { 
  2.  
  3. $user_id = get_current_user_id(); 
  4. if( !empty( $config['post_author'] ) ) { 
  5. $user_id = Caldera_Forms::do_magic_tags( $config['post_author'] ); 
  6.  
  7. $entry = array( 
  8. 'post_title' => Caldera_Forms::get_field_data( $config['post_title'], $form ),  
  9. 'post_status' => Caldera_Forms::do_magic_tags( $config['post_status'] ),  
  10. 'post_type' => $config['post_type'],  
  11. 'post_content' => Caldera_Forms::get_field_data( $config['post_content'], $form ),  
  12. 'post_parent' => Caldera_Forms::do_magic_tags( $config['post_parent'] ),  
  13. 'to_ping' => Caldera_Forms::do_magic_tags( $config['to_ping'] ),  
  14. 'post_password' => Caldera_Forms::do_magic_tags( $config['post_password'] ),  
  15. 'post_excerpt' => Caldera_Forms::do_magic_tags( $config['post_excerpt'] ),  
  16. 'comment_status'=> $config['comment_status'],  
  17. ); 
  18.  
  19. if( empty( $entry[ 'post_content' ] ) ) { 
  20. $entry[ 'post_content' ] = ''; 
  21.  
  22. // set the ID 
  23. if( !empty( $config['ID'] ) ) { 
  24. $is_post_id = Caldera_Forms::do_magic_tags( $config['ID'] ); 
  25. $post = get_post( $is_post_id ); 
  26. if( !empty( $post ) && $post->post_type == $entry['post_type'] ) { 
  27. $entry['ID'] = $is_post_id; 
  28.  
  29.  
  30. // set author 
  31. if( !empty( $user_id ) ) { 
  32. $entry['post_author'] = $user_id; 
  33.  
  34. //is edit? 
  35. if(!empty($_POST['_cf_frm_edt'])) { 
  36. // need to work on this still. SIGH! 
  37. }else{ 
  38. // Insert the post into the database 
  39. $entry_id = wp_insert_post( $entry ); 
  40. if(empty($entry_id)) { 
  41. return; 
  42.  
  43.  
  44.  
  45. // do upload + attach 
  46. if( !empty( $config['featured_image'] ) ) { 
  47. $featured_image = Caldera_Forms::get_field_data( $config['featured_image'], $form ); 
  48. foreach( (array) $featured_image as $filename ) { 
  49. $featured_image = cf_custom_fields_attach_file( $filename, $entry_id ); 
  50. update_post_meta($entry_id, '_thumbnail_id', $featured_image ); 
  51.  
  52.  
  53. //handle taxonomies 
  54. $terms_saved = false; 
  55. $tax_fields = cf_custom_fields_get_taxonomy_fields( $config ); 
  56. if ( ! empty( $tax_fields ) ) { 
  57. $terms_saved = cf_custom_fields_save_terms( $tax_fields, $entry_id ); 
  58. if ( $terms_saved ) { 
  59. $term_values = wp_list_pluck( $tax_fields, 'terms' ); 
  60.  
  61. //get post fields into an array of fields not to save as meta. 
  62. $post_fields = array_keys( $entry ); 
  63. // get all submission data 
  64. $data = Caldera_Forms::get_submission_data( $form ); 
  65. update_post_meta( $entry_id, '_cf_form_id', $form['ID'] ); 
  66. foreach($data as $field=>$value) { 
  67. if ( '_entry_token' != $field && '_entry_id' != $field ) { 
  68. if ( in_array( $field, $post_fields ) || in_array( $form['fields'][ $field ]['ID'], $post_fields ) ) { 
  69. continue; 
  70.  
  71.  
  72. if ( $terms_saved ) { 
  73. if ( is_array( $value ) ) { 
  74. $_value = implode( ', ', $value ); 
  75. } else { 
  76. $_value = $value; 
  77.  
  78. if( in_array( $_value, $term_values ) ) { 
  79. continue; 
  80.  
  81.  
  82.  
  83.  
  84. if(empty($form['fields'][$field])) { 
  85. continue; 
  86. if( in_array( $form['fields'][$field]['type'], array( 'button', 'html' ) ) ) { 
  87. continue; 
  88. if( $form['fields'][$field]['type'] == 'file' ) { 
  89. if( $field == $config['featured_image'] ) { 
  90. continue; // dont attache twice. 
  91. foreach( (array) $value as $file ) { 
  92. cf_custom_fields_attach_file( $file , $entry_id ); 
  93.  
  94. $slug = $form['fields'][$field]['slug']; 
  95.  
  96. /** 
  97. * Filter value before saving using to post type processor 
  98. * @since 2.0.3 
  99. * @param mixed $value The value to be saved 
  100. * @param string $slug Slug of field 
  101. * @param int $entry ID of post 
  102. */ 
  103. $value = apply_filters( 'cf_custom_fields_pre_save_meta_key_to_post_type', $value, $slug, $entry_id ); 
  104. update_post_meta( $entry_id, $slug, $value ); 
  105.  
  106. return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink( $entry_id ) );