public static function update_post_meta($meta_vars, $post_id) { // prepare the meta vars list($meta_key_value, $multi_repeated, $files) = self::prepare_meta_fields($meta_vars); // set featured image if there's any // if not in admin or if in admin (but doing an ajax call) if (is_admin() && defined('DOING_AJAX') && DOING_AJAX || !is_admin()) { if (isset($_POST['feat-image-id']) && $_POST['feat-image-id'] != 0) { $attachment_id = $_POST['feat-image-id']; fes_associate_attachment($attachment_id, $post_id); set_post_thumbnail($post_id, $attachment_id); } if (!isset($_POST['feat-image-id']) || $_POST['feat-image-id'] == 0) { delete_post_thumbnail($post_id); } } // save all custom fields foreach ($meta_key_value as $meta_key => $meta_value) { update_post_meta($post_id, $meta_key, $meta_value); } // save any multicolumn repeatable fields foreach ($multi_repeated as $repeat_key => $repeat_value) { // first, delete any previous repeatable fields delete_post_meta($post_id, $repeat_key); // now add them foreach ($repeat_value as $repeat_field) { update_post_meta($post_id, $repeat_key, $repeat_field); } } // save any files attached foreach ($files as $file_input) { if (!isset($_POST[$file_input['name']])) { continue; } $ids = array(); // We need to detach all previously attached files for this field. See #559 $old_files = get_post_meta($post_id, $file_input['name'], true); if (!empty($old_files) && is_array($old_files)) { foreach ($old_files as $file_id) { global $wpdb; $wpdb->update($wpdb->posts, array('post_parent' => 0), array('ID' => $file_id), array('%d'), array('%d')); } } foreach ($_POST[$file_input['name']] as $file => $url) { if (empty($url)) { continue; } $author_id = 0; if (!current_user_can('manage_shop_settings')) { $author_id = get_post_field('post_author', $post_id); } $attachment_id = fes_get_attachment_id_from_url($url, $author_id); fes_associate_attachment($attachment_id, $post_id); $ids[] = $attachment_id; } update_post_meta($post_id, $file_input['name'], $ids); } }
/** * Uploads files to Amazon S3 during FES form submissions * * Only runs if Frontend Submissions is active * * @since 2.1 * * @access public * @return array */ public function send_fes_files_to_s3($files = array(), $post_id = 0) { if (!function_exists('fes_get_attachment_id_from_url')) { return $files; } if (!empty($files) && is_array($files)) { foreach ($files as $key => $file) { $attachment_id = fes_get_attachment_id_from_url($file['file'], get_current_user_id()); if (!$attachment_id) { continue; } $user = get_userdata(get_current_user_id()); $folder = trailingslashit($user->user_login); $args = array('file' => get_attached_file($attachment_id, false), 'name' => $folder . basename($file['name']), 'type' => get_post_mime_type($attachment_id)); $this->upload_file($args); $files[$key]['file'] = edd_get_option('edd_amazon_s3_bucket') . '/' . $folder . basename($file['file']); wp_delete_attachment($attachment_id, true); } } return $files; }
public static function update_post_meta($meta_vars, $post_id) { // prepare the meta vars list($meta_key_value, $multi_repeated, $files) = self::prepare_meta_fields($meta_vars); // set featured image if there's any if (isset($_POST['feat-image-id'])) { $attachment_id = $_POST['feat-image-id']; fes_associate_attachment($attachment_id, $post_id); set_post_thumbnail($post_id, $attachment_id); } // save all custom fields foreach ($meta_key_value as $meta_key => $meta_value) { update_post_meta($post_id, $meta_key, $meta_value); } // save any multicolumn repeatable fields foreach ($multi_repeated as $repeat_key => $repeat_value) { // first, delete any previous repeatable fields delete_post_meta($post_id, $repeat_key); // now add them foreach ($repeat_value as $repeat_field) { update_post_meta($post_id, $repeat_key, $repeat_field); } } // save any files attached foreach ($files as $file_input) { if (!isset($_POST[$file_input['name']])) { continue; } $ids = array(); delete_post_meta($post_id, $file_input['name']); foreach ($_POST[$file_input['name']] as $file => $url) { if (empty($url)) { continue; } $author_id = 0; if (!current_user_can('manage_shop_settings')) { $author_id = get_post_field('post_author', $post_id); } $attachment_id = fes_get_attachment_id_from_url($url, $author_id); fes_associate_attachment($attachment_id, $post_id); $ids[] = $attachment_id; } update_post_meta($post_id, $file_input['name'], $ids); } }