/**
 * Process entry and save as post/ post meta.
 *
 * @since 1.1.0
 *
 * @param array $config Processor config.
 * @param array $form From config.
 *
 * @return array
 */
function cf_custom_fields_capture_entry($config, $form)
{
    $user_id = get_current_user_id();
    if (!empty($config['post_author'])) {
        $user_id = Caldera_Forms::do_magic_tags($config['post_author']);
    }
    $entry = array('post_title' => Caldera_Forms::get_field_data($config['post_title'], $form), 'post_status' => Caldera_Forms::do_magic_tags($config['post_status']), 'post_type' => $config['post_type'], 'post_content' => Caldera_Forms::get_field_data($config['post_content'], $form), 'post_parent' => Caldera_Forms::do_magic_tags($config['post_parent']), 'to_ping' => Caldera_Forms::do_magic_tags($config['to_ping']), 'post_password' => Caldera_Forms::do_magic_tags($config['post_password']), 'post_excerpt' => Caldera_Forms::do_magic_tags($config['post_excerpt']), 'comment_status' => $config['comment_status']);
    if (empty($entry['post_content'])) {
        $entry['post_content'] = '';
    }
    // set the ID
    if (!empty($config['ID'])) {
        $is_post_id = Caldera_Forms::do_magic_tags($config['ID']);
        $post = get_post($is_post_id);
        if (!empty($post) && $post->post_type == $entry['post_type']) {
            $entry['ID'] = $is_post_id;
        }
    }
    // set author
    if (!empty($user_id)) {
        $entry['post_author'] = $user_id;
    }
    //is edit?
    if (!empty($_POST['_cf_frm_edt'])) {
        // need to work on this still. SIGH!
    } else {
        // Insert the post into the database
        $entry_id = wp_insert_post($entry);
        if (empty($entry_id)) {
            return;
        }
    }
    // do upload + attach
    if (!empty($config['featured_image'])) {
        $featured_image = Caldera_Forms::get_field_data($config['featured_image'], $form);
        foreach ((array) $featured_image as $filename) {
            $featured_image = cf_custom_fields_attach_file($filename, $entry_id);
            update_post_meta($entry_id, '_thumbnail_id', $featured_image);
        }
    }
    //handle taxonomies
    $terms_saved = false;
    $tax_fields = cf_custom_fields_get_taxonomy_fields($config);
    if (!empty($tax_fields)) {
        $terms_saved = cf_custom_fields_save_terms($tax_fields, $entry_id);
        if ($terms_saved) {
            $term_values = wp_list_pluck($tax_fields, 'terms');
        }
    }
    //get post fields into an array of fields not to save as meta.
    $post_fields = array_keys($entry);
    // get all submission data
    $data = Caldera_Forms::get_submission_data($form);
    update_post_meta($entry_id, '_cf_form_id', $form['ID']);
    foreach ($data as $field => $value) {
        if ('_entry_token' != $field && '_entry_id' != $field) {
            if (in_array($field, $post_fields) || in_array($form['fields'][$field]['ID'], $post_fields)) {
                continue;
            }
        }
        if ($terms_saved) {
            if (is_array($value)) {
                $_value = implode(', ', $value);
            } else {
                $_value = $value;
            }
            if (in_array($_value, $term_values)) {
                continue;
            }
        }
        if (empty($form['fields'][$field])) {
            continue;
        }
        if (in_array($form['fields'][$field]['type'], array('button', 'html'))) {
            continue;
        }
        if ($form['fields'][$field]['type'] == 'file') {
            if ($field == $config['featured_image']) {
                continue;
                // dont attache twice.
            }
            foreach ((array) $value as $file) {
                cf_custom_fields_attach_file($file, $entry_id);
            }
        }
        $slug = $form['fields'][$field]['slug'];
        /**
         * Filter value before saving using to post type processor
         *
         * @since 2.0.3
         *
         * @param mixed $value The value to be saved
         * @param string $slug Slug of field
         * @param int $entry ID of post
         */
        $value = apply_filters('cf_custom_fields_pre_save_meta_key_to_post_type', $value, $slug, $entry_id);
        update_post_meta($entry_id, $slug, $value);
    }
    return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink($entry_id));
}
/**
 * Process entry and save as post/ post meta.
 *
 * @since 1.1.0
 *
 * @param array $config Processor config.
 * @param array $form From config.
 *
 * @return array
 */
function cf_custom_fields_capture_entry($config, $form)
{
    $user_id = get_current_user_id();
    if (!empty($config['post_author'])) {
        $user_id = Caldera_Forms::do_magic_tags($config['post_author']);
    }
    $entry = array('post_title' => Caldera_Forms::get_field_data($config['post_title'], $form), 'post_status' => Caldera_Forms::do_magic_tags($config['post_status']), 'post_type' => $config['post_type'], 'post_content' => Caldera_Forms::get_field_data($config['post_content'], $form), 'post_parent' => Caldera_Forms::do_magic_tags($config['post_parent']), 'to_ping' => Caldera_Forms::do_magic_tags($config['to_ping']), 'post_password' => Caldera_Forms::do_magic_tags($config['post_password']), 'post_excerpt' => Caldera_Forms::do_magic_tags($config['post_excerpt']), 'comment_status' => $config['comment_status']);
    // set the ID
    if (!empty($config['ID'])) {
        $is_post_id = Caldera_Forms::do_magic_tags($config['ID']);
        $post = get_post($is_post_id);
        if (!empty($post) && $post->post_type == $entry['post_type']) {
            $entry['ID'] = $is_post_id;
        }
    }
    // set author
    if (!empty($user_id)) {
        $entry['post_author'] = $user_id;
    }
    //is edit?
    if (!empty($_POST['_cf_frm_edt'])) {
        // need to work on this still. SIGH!
    } else {
        // Insert the post into the database
        $entry_id = wp_insert_post($entry);
        if (empty($entry_id)) {
            return;
        }
    }
    // do upload + attach
    if (!empty($config['featured_image'])) {
        $featured_image = Caldera_Forms::get_field_data($config['featured_image'], $form);
        foreach ((array) $featured_image as $filename) {
            $featured_image = cf_custom_fields_attach_file($filename, $entry_id);
            update_post_meta($entry_id, '_thumbnail_id', $featured_image);
        }
    }
    //get post fields into an array of fields not to save as meta.
    $post_fields = array_keys($entry);
    // get all submission data
    $data = Caldera_Forms::get_submission_data($form);
    update_post_meta($entry_id, '_cf_form_id', $form['ID']);
    foreach ($data as $field => $value) {
        if ('_entry_token' != $field && '_entry_id' != $field) {
            if (in_array($field, $post_fields) || in_array($form['fields'][$field]['ID'], $post_fields)) {
                continue;
            }
        }
        if (empty($form['fields'][$field])) {
            continue;
        }
        if (in_array($form['fields'][$field]['type'], array('button', 'html'))) {
            continue;
        }
        if ($form['fields'][$field]['type'] == 'file') {
            if ($field == $config['featured_image']) {
                continue;
                // dont attache twice.
            }
            foreach ((array) $value as $file) {
                cf_custom_fields_attach_file($file, $entry_id);
            }
        }
        update_post_meta($entry_id, $form['fields'][$field]['slug'], $value);
    }
    return array('Post ID' => $entry_id, 'ID' => $entry_id, 'permalink' => get_permalink($entry_id));
}