예제 #1
0
 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);
     }
 }
예제 #2
0
 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);
     }
 }