/** * Send a message to Slack * * @since 1.0.0 * * @param array $config Processor config * @param array $form Form config */ function cf_send_slack_message($config, $form) { // create fields $fields = array(); foreach ($form['fields'] as $field) { if ($field['type'] === 'html' || $field['type'] === 'button') { continue; } $entry_value = Caldera_Forms::get_field_data($field['ID'], $form); $fields[] = array('title' => $field['label'], 'value' => $entry_value, 'short' => strlen($entry_value) < 100 ? true : false); } // Create Payload $payload = array("username" => Caldera_Forms::do_magic_tags($config['username'])); // icon if (!empty($config['file'])) { $payload['icon_url'] = $config['file']; } // override channel if set $channel = trim($config['channel']); if (!empty($channel)) { $payload['channel'] = Caldera_Forms::do_magic_tags(trim($config['channel'])); } // attach if setup if (!empty($config['attach'])) { $payload['attachments'] = array(array("fallback" => Caldera_Forms::do_magic_tags($config['text']), "pretext" => Caldera_Forms::do_magic_tags($config['text']), "color" => $config['color'], "fields" => $fields)); } else { $payload['text'] = Caldera_Forms::do_magic_tags($config['text']); } /** * Filter request before sending message to Slack API * * Runs before encoding to JSON * * @since 1.1.0 * * @param array $payload Arguments for API request * @param array $config Processor config * @param array $form Form config */ add_filter('cf_slack_message_pre_request', $payload, $config, $form); $args = array('body' => array('payload' => json_encode($payload))); $response = wp_remote_post($config['url'], $args); /** * Get response from Slack API message request. * * Runs after request is sent, but before form processor ends * * @since 1.1.0 * * @param WP_Error|array $response The response or WP_Error on failure. * @param array $payload Arguments for API request * @param array $config Processor config * @param array $form Form config */ do_action('cf_slack_invite_request_sent', $response, $payload, $config, $form); }
/** * Process edd-licensed-downloads processor to validate setup * * @since 0.1.0 * * @param array $config Processor config * @param array $form Form config * * @return array|void */ function cf_edd_validate($config, $form) { $value = Caldera_Forms::get_field_data($config['edd_licensed_downloads'], $form); // direct field bind can get data, magic tags wont work. $_user = Caldera_Forms::do_magic_tags($config['edd_licensed_downloads_user']); if (0 < absint($_user)) { $user = $_user; } else { $user = null; } $downloads = cf_edd_get_downloads_by_licensed_user($user); if (!in_array($value, array_keys($downloads))) { return array('type' => 'error', 'note' => __("Selected User Does Note Have A License For This Download.", 'cf-edd')); } }
/** * @param $form array * @param int $entry_id * @return null|object */ public function convertData($form, $entry_id) { if (is_array($form)) { $title = $form['name']; $postedData = array(); $uploadedFiles = array(); $fields = $form['fields']; foreach ($fields as $field_id => $field) { $field_value = Caldera_Forms::get_field_data($field_id, $form, $entry_id); // $this->plugin->getErrorLog()->log("$field_id=" . print_r($field_value, true)); // debug // $this->plugin->getErrorLog()->log("$field_id=" . print_r($field, true)); // debug if (!array_key_exists($field_id, $form['fields'])) { // ignore non-field entries _entry_id and _entry_token continue; } $field_name = $field['label']; $is_file = in_array($field['type'], array('file', 'advanced_file')); if (is_array($field_value)) { $postedData[$field_name] = implode(',', $field_value); } else { if ($is_file && $field_value != null) { // $field_value is a URL to the file like // http://SITE.com/wp-content/uploads/2015/05/my_file.png $postedData[$field_name] = basename($field_value); if (!function_exists('get_home_path')) { include_once ABSPATH . '/wp-admin/includes/file.php'; } $path = get_home_path() . $this->getUrlWithoutSchemeHostAndPort($field_value); $uploadedFiles[$field_name] = $path; } else { $postedData[$field_name] = $field_value; } } } return (object) array('title' => $title, 'posted_data' => $postedData, 'uploaded_files' => $uploadedFiles); } return null; }
/** * Create Pod entry from submission * * @since 1.0.0 * * @param array $config Settings for the processor * @param array $form Full form structure * * @return array */ function pods_cf_capture_entry($config, $form) { global $transdata; // get pod ID $pod_id = null; if (!empty($config['pod_id'])) { $pod_id = Caldera_Forms::do_magic_tags($config['pod_id']); } // build entry $entry = array(); // add object fields if (!empty($config['object_fields'])) { foreach ($config['object_fields'] as $object_field => $binding) { if (!empty($binding)) { $entry[$object_field] = Caldera_Forms::get_field_data($binding, $form); } } } $pods = pods($config['pod']); $fields = $pods->fields(); // add pod fields if (!empty($config['fields'])) { foreach ($config['fields'] as $pod_field => $binding) { if (!empty($binding)) { $entry[$pod_field] = Caldera_Forms::get_field_data($binding, $form); } } } // Save Entry if (!empty($pod_id)) { $pod_id = $pods->save($entry, null, $pod_id); } else { $pod_id = $pods->add($entry); } // return entry id for metadata return array('pod_id' => $pod_id, 'permalink' => get_permalink($pod_id)); }
/** * Get values from POST data and set in the value property * * @since 1.3.0 * * @access protected * * @param $config * @param $form */ protected function set_value($config, $form) { foreach ($this->fields as $field => $args) { if (isset($config[$field])) { if ($args['magic']) { $value = Caldera_Forms::do_magic_tags($config[$field]); } else { $value = $config[$field]; } $field_id_passed = strpos($value, 'fld_'); if (false !== $field_id_passed) { $value = Caldera_Forms::get_field_data($value, $form); } } else { $value = null; } if (!empty($value)) { $value = call_user_func($args['sanatize'], $value); } /** * Filter value for field of processor * * @since 1.3.1 * * @param mixed $value The value of the field. * @param string $field The name of the field. * @param array $args Config for this field. * @param array $config Processor config. * @param array $form Form config. */ $value = apply_filters('caldera_forms_processor_value', $value, $field, $args, $config, $form); if (!empty($value)) { $this->values[$field] = $value; } else { if ($args['required']) { $this->add_error($args['message']); } else { $this->values[$field] = null; } } } }
/** * Proccesss submission * * @since 0.1.0 * * @param array $config Processor config * @param array $form Form config * * @return array */ function cf_form_connector_process($config, $form) { global $form; $form['form_connection'] = $config; $form['form_connection']['entry_id'] = Caldera_Forms::get_field_data('_entry_id', $form); }
/** * Get values from POST data and set in the value property * * @since 1.3.0 * * @access protected * * @param $config * @param $form */ protected function set_value($config, $form) { foreach ($this->fields as $field => $args) { if (isset($config[$field])) { if ($args['magic']) { $value = Caldera_Forms::do_magic_tags($config[$field]); } else { $value = $config[$field]; } $field_id_passed = strpos($value, 'fld_'); if (false !== $field_id_passed) { $value = Caldera_Forms::get_field_data($value, $form); } } else { $value = null; } if (!empty($value)) { $value = call_user_func($args['sanatize'], $value); } if (!empty($value)) { $this->values[$field] = $value; } else { if ($args['required']) { $this->add_error($args['message']); } else { $this->values[$field] = null; } } } }
/** * Save form in database * * @since 1.2.3 * * @param array $form Form config * @param int|null $entryid Optional. ID of entry to send. If not provided, will be determined from current POST data '_entry_id' key. */ public static function save_in_db($form, $entryid = null) { global $wpdb, $processed_meta; if (!empty($form['db_support'])) { // new entry or update if (empty($entryid)) { $entryid = Caldera_Forms::get_field_data('_entry_id', $form); foreach ($form['fields'] as $field_id => $field) { // add new and update Caldera_Forms::save_field_data($field, $entryid, $form); } // save form meta if any if (isset($processed_meta[$form['ID']])) { foreach ($processed_meta[$form['ID']] as $process_id => $meta_data) { foreach ($meta_data as $meta_key => $meta_value) { if (is_array($meta_value)) { foreach ($meta_value as &$check_value) { if (is_array($check_value)) { foreach ($check_value as &$sub_check_value) { if (!is_array($sub_check_value)) { $sub_check_value = Caldera_Forms::do_magic_tags($sub_check_value); } } } else { $check_value = Caldera_Forms::do_magic_tags($check_value); } } } else { $meta_value = Caldera_Forms::do_magic_tags($meta_value); } if (count($meta_value) > 1) { $meta_value = json_encode($meta_value); } else { $meta_value = $meta_value[0]; if (is_array($meta_value) || is_object($meta_value)) { $meta_value = json_encode($meta_value); } } $meta_entry = array('entry_id' => $entryid, 'process_id' => $process_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value); $wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry); } } } // update status $wpdb->update($wpdb->prefix . 'cf_form_entries', array('status' => 'active'), array('id' => $entryid)); } else { // do update foreach ($form['fields'] as $field_id => $field) { // add new and update Caldera_Forms::update_field_data($field, $entryid, $form); } if (isset($processed_meta[$form['ID']])) { foreach ($processed_meta[$form['ID']] as $process_id => $meta_data) { foreach ($meta_data as $meta_key => $meta_value) { if (count($meta_value) > 1) { $meta_value = json_encode($meta_value); } else { $meta_value = $meta_value[0]; } $meta_entry = array('entry_id' => $entryid, 'process_id' => $process_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value); $wpdb->insert($wpdb->prefix . 'cf_form_entry_meta', $meta_entry); } } } // return return; } } }
/** * 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)); }
function cf_handle_file_upload($entry, $field, $form) { // check transdata $transdata = get_transient($entry); if (!empty($transdata)) { return $transdata; } if (isset($_POST['_cf_frm_edt'])) { if (!isset($_FILES) || isset($_FILES[$field['ID']]['size'][0]) && 0 == $_FILES[$field['ID']]['size'][0] || isset($_FILES[$field['ID']]['size']) && 0 == $_FILES[$field['ID']]['size']) { $entry = Caldera_Forms::get_field_data($field['ID'], $form, absint($_POST['_cf_frm_edt'])); return $entry; } } $required = false; if (isset($field['required']) && $field['required']) { $required = true; } if (!empty($_FILES[$field['ID']]['size'])) { // check is allowed if (!empty($field['config']['allowed'])) { $types = explode(',', $field['config']['allowed']); foreach ($types as &$type) { $type = trim(trim($type, '.')); } foreach ((array) $_FILES[$field['ID']]['name'] as $file_name) { if (empty($file_name)) { return $entry; } $check = pathinfo($file_name); if (!in_array($check['extension'], $types)) { if (count($types) > 1) { return new WP_Error('fail', __('File type not allowed. Allowed types are: ', 'caldera-forms') . ' ' . implode(', ', $types)); } else { return new WP_Error('fail', __('File type needs to be', 'caldera-forms') . ' .' . $types[0]); } } } } if (!function_exists('wp_handle_upload')) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $files = array(); foreach ((array) $_FILES[$field['ID']] as $file_key => $file_parts) { foreach ((array) $file_parts as $part_index => $part_value) { $files[$part_index][$file_key] = $part_value; } } $uploads = array(); foreach ($files as $file) { if (!$required && 0 == $file['size']) { continue; } $upload = wp_handle_upload($file, array('test_form' => false), date('Y/m')); if (!empty($upload['error'])) { return new WP_Error('fail', $upload['error']); } $uploads[] = $upload['url']; } if (count($uploads) > 1) { return $uploads; } if (empty($uploads)) { return array(); } return $uploads[0]; } else { if (filter_var($entry, FILTER_VALIDATE_URL)) { return $entry; } } }
/** * 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)); }