/** * Remove a Media entry from gallery * * @param type $media_id */ function mpp_delete_media($media_id) { global $wpdb; if (!($media = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $media_id)))) { return $post; } if (mpp_get_media_post_type() != $media->post_type) { return false; } //firs of all delete all media associated with this post $storage_manager = mpp_get_storage_manager($media_id); $storage_manager->delete($media_id); //now proceed to delete the post mpp_delete_media_meta($media_id, '_wp_trash_meta_status'); mpp_delete_media_meta($media_id, '_wp_trash_meta_time'); do_action('mpp_delete_media', $media_id); wp_delete_object_term_relationships($media_id, array('category', 'post_tag')); wp_delete_object_term_relationships($media_id, get_object_taxonomies(mpp_get_media_post_type())); delete_metadata('post', null, '_thumbnail_id', $media_id, true); // delete all for any posts. //dele if it is set as cover delete_metadata('post', null, '_mpp_cover_id', $media_id, true); // delete all for any posts. $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $media_id)); foreach ($comment_ids as $comment_id) { wp_delete_comment($comment_id, true); } //if media has cover, delete the cover if (mpp_media_has_cover_image($media_id)) { mpp_delete_media(mpp_get_media_cover_id($media_id)); } //delete met $post_meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d ", $media_id)); foreach ($post_meta_ids as $mid) { delete_metadata_by_mid('post', $mid); } $result = $wpdb->delete($wpdb->posts, array('ID' => $media_id)); if (!$result) { return false; } //decrease the media_count in gallery by 1 mpp_gallery_decrement_media_count($media->post_parent); //delete all activities related to this media mpp_media_delete_activities($media_id); //delete all activity meta key where this media is associated mpp_media_delete_activity_meta($media_id); clean_post_cache($media); do_action('mpp_media_deleted', $media_id); return $media; }
/** * When an activity is saved, check if there exists a media attachment cookie, * if yes, mark it as non orphaned and store in the activity meta * */ function mpp_activity_mark_attached_media($activity_id) { if (!is_user_logged_in()) { return; } if (empty($_POST['mpp-attached-media'])) { return; } //don't do anything //let us process $media_ids = $_POST['mpp-attached-media']; $media_ids = explode(',', $media_ids); //make an array $media_ids = array_filter(array_unique($media_ids)); foreach ($media_ids as $media_id) { //should we verify the logged in user & owner of media is same? mpp_delete_media_meta($media_id, '_mpp_is_orphan'); //or should we delete the key? } mpp_activity_update_attached_media_ids($activity_id, $media_ids); mpp_activity_update_context($activity_id, 'gallery'); mpp_activity_update_activity_type($activity_id, 'media_upload'); //store the media ids in the activity meta //also add the activity to gallery & gallery to activity link $media = mpp_get_media($media_id); if ($media->gallery_id) { mpp_activity_update_gallery_id($activity_id, $media->gallery_id); } //also update this activity and set its action to be mpp_media_upload $activity = new BP_Activity_Activity($activity_id); // $activity->component = buddypress()->mediapress->id; $activity->type = 'mpp_media_upload'; $activity->save(); //save activity privacy $status_object = mpp_get_status_object($media->status); //if you have BuddyPress Activity privacy plugin enabled, this will work out of the box if ($status_object) { bp_activity_update_meta($activity->id, 'activity-privacy', $status_object->activity_privacy); } do_action('mpp_activity_media_marked_attached', $media_ids); //reset the cookie }
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, 'date_created' => '', 'date_modified' => ''); $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); } if (!empty($date_created)) { $post_data['post_date'] = $date_created; } if (!empty($date_updated)) { $post_data['post_modified'] = $date_updated; } // 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 && $context == 'activity') { //only store context for media uploaded from activity 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 && $storage_method != 'local') { //let us not waste extra entries on local storage, ok. Storge storage info only if it is not the default local storage 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 }
/** * Delete media cover id * * @param type $media_id * * @return type */ function mpp_delete_media_cover_id($media_id) { return mpp_delete_media_meta($media_id, '_mpp_cover_id'); }
public function cover_upload() { check_ajax_referer('mpp_add_media'); //check for the referrer $response = array(); $file = $_FILES; $file_id = '_mpp_file'; //key name in the files array //find the components we are trying to add for $component = $component_id = 0; $context = 'cover'; $gallery_id = absint($_POST['mpp-gallery-id']); $parent_id = absint($_POST['mpp-parent-id']); $parent_type = isset($_POST['mpp-parent-type']) ? trim($_POST['mpp-parent-type']) : 'gallery'; //default upload to gallery cover if (!$gallery_id || !$parent_id) { return; } $gallery = mpp_get_gallery($gallery_id); $component = $gallery->component; $component_id = $gallery->component_id; //get the uploader $uploader = mpp_get_storage_manager(); //should we pass the component? //setup for component //$uploader->setup_for( $component, $component_id ); //check if the server can handle the upload? if (!$uploader->can_handle()) { wp_send_json_error(array('message' => __('Server can not handle this much amount of data. Please upload a smaller file or ask your server administrator to change the settings.', 'mediapress'))); } $media_type = mpp_get_media_type_from_extension(mpp_get_file_extension($file[$file_id]['name'])); //cover is always a photo, if ($media_type != 'photo') { wp_send_json(array('message' => sprintf(__('Please upload a photo. Only <strong>%s</strong> files are allowed!', 'mediapress'), mpp_get_allowed_file_extensions_as_string($media_type)))); } $error = false; //if we are here, all is well :) if (!mpp_user_can_upload($component, $component_id, $gallery)) { wp_send_json_error(array('message' => __("You don't have sufficient permissions to upload.", 'mediapress'))); } //if we are here, we have checked for all the basic errors, so let us just upload now $uploaded = $uploader->upload($file, array('file_id' => $file_id, 'gallery_id' => $gallery_id, 'component' => $component, 'component_id' => $component_id, 'is_cover' => 1)); //upload was succesfull? if (!isset($uploaded['error'])) { //file was uploaded successfully $title = $_FILES[$file_id]['name']; $title_parts = pathinfo($title); $title = trim(substr($title, 0, -(1 + strlen($title_parts['extension'])))); $url = $uploaded['url']; $type = $uploaded['type']; $file = $uploaded['file']; //$title = isset( $_POST['media_title'] ) ? $_POST['media_title'] : ''; $content = isset($_POST['media_description']) ? $_POST['media_description'] : ''; $meta = $uploader->get_meta($uploaded); $title_desc = $this->get_title_desc_from_meta($type, $meta); if (!empty($title_desc)) { if (empty($title) && !empty($title_desc['title'])) { $title = $title_desc['title']; } if (empty($content) && !empty($title_desc['content'])) { $content = $title_desc['content']; } } $status = isset($_POST['media_status']) ? $_POST['media_status'] : ''; if (empty($status) && $gallery) { $status = $gallery->status; //inherit from parent,gallery must have an status } //we may need some more enhancements here if (!$status) { $status = mpp_get_default_status(); } // print_r($upload_info); $is_orphan = 0; $media_data = array('title' => $title, 'description' => $content, 'gallery_id' => $parent_id, 'user_id' => get_current_user_id(), 'is_remote' => false, 'type' => $media_type, 'mime_type' => $type, 'src' => $file, 'url' => $url, 'status' => $status, 'comment_status' => 'open', 'storage_method' => mpp_get_storage_method(), 'component_id' => $component_id, 'component' => $component, 'context' => $context, 'is_orphan' => $is_orphan, 'is_cover' => true); //cover shuld never be recorded as activity add_filter('mpp_do_not_record_add_media_activity', '__return_true'); $id = mpp_add_media($media_data); if ($parent_type == 'gallery') { $old_cover = mpp_get_gallery_cover_id($parent_id); } else { $old_cover = mpp_get_media_cover_id($parent_id); } if ($gallery->type == 'photo') { mpp_gallery_increment_media_count($gallery_id); } else { //mark it as non gallery media mpp_delete_media_meta($id, '_mpp_is_mpp_media'); if ($old_cover) { mpp_delete_media($old_cover); } } mpp_update_media_cover_id($parent_id, $id); $attachment = mpp_media_to_json($id); //$attachment['data']['type_id'] = mpp_get_type_term_id( $gallery->type ); echo json_encode(array('success' => true, 'data' => $attachment)); //wp_send_json_success( array('name'=>'what') ); exit(0); } else { echo json_encode(array('error' => 1, 'message' => $uploaded['error'])); exit(0); } }
/** * When an activity is saved, check if there exists a media attachment cookie, * if yes, mark it as non orphaned and store in the activity meta * */ function mpp_activity_mark_attached_media($activity_id) { if (!is_user_logged_in()) { return; } if (empty($_COOKIE['_mpp_activity_attached_media_ids'])) { return; } //don't do anything //let us process $media_ids = $_COOKIE['_mpp_activity_attached_media_ids']; $media_ids = explode(',', $media_ids); //make an array foreach ($media_ids as $media_id) { //should we verify the logged in user & owner of media is same? mpp_delete_media_meta($media_id, '_mpp_is_orphan'); //or should we delete the key? } mpp_activity_update_attached_media_ids($activity_id, $media_ids); //store the media ids in the activity meta //also add the activity to gallery & gallery to activity link $media = mpp_get_media($media_id); if ($media->gallery_id) { mpp_activity_update_gallery_id($activity_id, $media->gallery_id); } //also update this activity and set its action to be mpp_media_upload $activity = new BP_Activity_Activity($activity_id); // $activity->component = buddypress()->mediapress->id; $activity->type = 'mpp_media_upload'; $activity->save(); mpp_activity_clear_attached_media_cookie(); //clear cookies //reset the cookie }