function _mpp_cache_gallery_cover($query) { if (empty($query->posts)) { return; } $gallery_ids = wp_list_pluck($query->posts, 'ID'); //$gallery_ids = $query->get_ids();//all gallery ids //get all the media ids which are set as thumbnails $thumb_ids = array(); foreach ((array) $gallery_ids as $gallery_id) { $media_id = mpp_get_gallery_cover_id($gallery_id); if ($media_id) { $thumb_ids[] = $media_id; } } if (!empty($thumb_ids)) { //ok there are times when we are only looking for one gallery, in that case don't do anything if (count($thumb_ids) <= 1) { return; } //_mpp_update_post_caches( $thumb_ids, false, true ); _prime_post_caches($thumb_ids, true, true); //if we are here, we do query and cache the results //$mppq = new MPP_Media_Query( array( 'in'=> $thumb_ids ) ); } }
/** * Check if the given media(attachment) is cover image? * * @param type $gallery_id * @param type $image_id the given media id * @return boolean */ function mpp_is_cover_image($gallery_id, $image_id) { $gallery = mpp_get_gallery($gallery_id); if (mpp_get_gallery_cover_id($gallery->id) === $image_id) { return true; } return false; }
/** * Check if Gallery has a cover set * * @param type $gallery * @return boolean|int false if no cover else cover image id */ function mpp_gallery_has_cover_image($gallery = null) { $gallery = mpp_get_gallery($gallery); return mpp_get_gallery_cover_id($gallery->id); }
/** * Handles Gallery Cover deletion * * @return type */ function mpp_action_delete_gallery_cover() { if (!mpp_is_gallery_cover_delete()) { return; } if (!$_REQUEST['gallery_id']) { return; } $gallery = mpp_get_gallery(absint($_REQUEST['gallery_id'])); $referer = $redirect_url = mpp_get_gallery_settings_url($gallery); if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'delete-cover')) { //add error message and return back to the old page mpp_add_feedback(__('Action not authorized!', 'mediapress'), 'error'); if ($referer) { mpp_redirect($referer); } return; } //we may want to allow passing of component from the form in future! if (!mpp_user_can_delete_gallery($gallery)) { mpp_add_feedback(__("You don't have permission to delete this cover!", 'mediapress'), 'error'); if ($referer) { mpp_redirect($referer); } return; } //we always need to delete this $cover_id = mpp_get_gallery_cover_id($gallery->id); mpp_delete_gallery_cover_id($gallery->id); //if( $gallery->type != 'photo' ) { //delete the uploaded cover too mpp_delete_media($cover_id); //} mpp_add_feedback(__('Cover deleted successfully!', 'mediapress')); //if we are here, delete gallery and redirect to the component base url mpp_redirect($redirect_url); }
public function delete_gallery_cover() { //verify nonce if (!wp_verify_nonce($_POST['_wpnonce'], 'mpp-manage-gallery')) { wp_send_json(array('message' => __('Invalid action.', 'mediapress'), 'error' => 1)); exit(0); } $gallery = mpp_get_gallery(absint($_REQUEST['gallery_id'])); if (!$gallery) { wp_send_json(array('message' => __('Invalid action.', 'mediapress'), 'error' => 1)); exit(0); } //we may want to allow passing of component from the form in future! if (!mpp_user_can_delete_gallery($gallery)) { wp_send_json(array('message' => __("You don't have permission to delete this cover!", 'mediapress'), 'error' => 1)); exit(0); } //we always need to delete this $cover_id = mpp_get_gallery_cover_id($gallery->id); mpp_delete_gallery_cover_id($gallery->id); mpp_delete_media($cover_id); wp_send_json(array('message' => __("Cover deleted", 'mediapress'), 'success' => 1, 'cover' => mpp_get_gallery_cover_src('thumbnail', $gallery->id))); exit(0); }
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']); 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); $id = mpp_add_media($media_data); $old_cover = mpp_get_gallery_cover_id($gallery_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); } }