/** * Generate meta data for the media * * @since 1.0.0 * * @access private * @param int $attachment_id Media ID to process. * @param string $file Filepath of the Attached image. * * @return mixed Metadata for attachment. */ public function generate_metadata($attachment_id, $file) { $attachment = get_post($attachment_id); $mime_type = get_post_mime_type($attachment); $metadata = array(); if (preg_match('!^image/!', $mime_type) && file_is_displayable_image($file)) { $imagesize = getimagesize($file); $metadata['width'] = $imagesize[0]; $metadata['height'] = $imagesize[1]; // Make the file path relative to the upload dir $metadata['file'] = _wp_relative_upload_path($file); //get the registered media sizes $sizes = mpp_get_media_sizes(); $sizes = apply_filters('mpp_intermediate_image_sizes', $sizes, $attachment_id); if ($sizes) { $editor = wp_get_image_editor($file); if (!is_wp_error($editor)) { $metadata['sizes'] = $editor->multi_resize($sizes); } } else { $metadata['sizes'] = array(); } // fetch additional metadata from exif/iptc $image_meta = wp_read_image_metadata($file); if ($image_meta) { $metadata['image_meta'] = $image_meta; } } elseif (preg_match('#^video/#', $mime_type)) { $metadata = wp_read_video_metadata($file); } elseif (preg_match('#^audio/#', $mime_type)) { $metadata = wp_read_audio_metadata($file); } $dir_path = trailingslashit(dirname($file)) . 'covers'; $url = wp_get_attachment_url($attachment_id); $base_url = str_replace(wp_basename($url), '', $url); //processing for audio/video cover if (!empty($metadata['image']['data'])) { $ext = '.jpg'; switch ($metadata['image']['mime']) { case 'image/gif': $ext = '.gif'; break; case 'image/png': $ext = '.png'; break; } $basename = str_replace('.', '-', basename($file)) . '-image' . $ext; $uploaded = $this->upload_bits($basename, $metadata['image']['data'], array('path' => $dir_path, 'url' => $base_url)); if (false === $uploaded['error']) { $attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => ''); $sub_attachment_id = wp_insert_attachment($attachment, $uploaded['file']); $attach_data = $this->generate_metadata($sub_attachment_id, $uploaded['file']); wp_update_attachment_metadata($sub_attachment_id, $attach_data); //if the option is set to set post thumbnail if (mpp_get_option('set_post_thumbnail')) { mpp_update_media_meta($attachment_id, '_thumbnail_id', $sub_attachment_id); } //set the cover id mpp_update_media_cover_id($attachment_id, $sub_attachment_id); } } // remove the blob of binary data from the array if (isset($metadata['image']['data'])) { unset($metadata['image']['data']); } return apply_filters('mpp_generate_metadata', $metadata, $attachment_id); }
function mpp_update_media($args = null) { //updating media can not change the Id & SRC, so if (!isset($args['id'])) { return false; } $default = array('user_id' => get_current_user_id(), 'gallery_id' => false, 'post_parent' => false, 'is_orphan' => false, 'is_uploaded' => '', 'is_remote' => '', 'is_imorted' => '', 'is_embedded' => '', 'embed_url' => '', 'embed_html' => '', 'component_id' => '', 'component' => '', 'context' => '', 'status' => '', 'type' => '', 'storage_method' => '', 'mime_type' => '', 'description' => '', 'sort_order' => 0); $args = wp_parse_args($args, $default); extract($args); //print_r($args ); //return ; if (!$title) { return false; } $post_data = get_post($id, ARRAY_A); if (!$gallery_id) { $gallery_id = $post_data['post_parent']; } if ($title) { $post_data['post_title'] = $title; } if ($description) { $post_data['post_content'] = $description; } if ($gallery_id) { $post_data['post_parent'] = $gallery_id; } //check if the gallery is sorted and the sorting order is not set explicitly //we update it if (!$sort_order && !$post_data['menu_order'] && mpp_is_gallery_sorted($gallery_id)) { //current max sort order +1 $sort_order = (int) mpp_get_max_media_order($gallery_id) + 1; } if ($sort_order) { $post_data['menu_order'] = absin($sort_order); } // Save the data $id = wp_insert_attachment($post_data, false, $gallery_id); if (!is_wp_error($id)) { //set component if ($component) { wp_set_object_terms($id, mpp_underscore_it($component), mpp_get_component_taxname()); } //set _component_id meta key user_id/gallery_id/group id etc if ($component_id) { mpp_update_media_meta($id, '_mpp_component_id', $component_id); } //set upload context if ($context) { mpp_update_media_meta($id, '_mpp_context', $context); } //set media privacy if ($status) { wp_set_object_terms($id, mpp_underscore_it($status), mpp_get_status_taxname()); } //set media type internally as audio/video etc if ($type) { wp_set_object_terms($id, mpp_underscore_it($type), mpp_get_type_taxname()); } // if ($storage_method) { mpp_update_media_meta($id, '_mpp_storage_method', $storage_method); } // //add all extraz here if ($is_orphan) { mpp_update_media_meta($id, '_mpp_is_orphan', $is_orphan); } else { mpp_delete_media_meta($id, '_mpp_is_orphan'); } do_action('mpp_media_updated', $id, $gallery_id); return $id; } return false; // there was an error }
/** * * @param type $media_id * @param type $activity_id * @return type */ function mpp_media_update_activity_id($media_id, $activity_id) { return mpp_update_media_meta($media_id, '_mpp_activity_id', $activity_id); }
/** * Update media cover id * * @param type $media_id * @param type $cover_id * @return type */ function mpp_update_media_cover_id($media_id, $cover_id) { return mpp_update_media_meta($media_id, '_mpp_cover_id', $cover_id); }