/** * Handles Gallery creation on the front end in non ajax case * * @return type */ function mpp_action_create_gallery() { //allow gallery to be created from anywhere //the form must have mpp-action set and It should be set to 'create-gallery' if (empty($_POST['mpp-action']) || $_POST['mpp-action'] != 'create-gallery') { return; } $referer = wp_get_referer(); //if we are here, It is gallery create action if (!wp_verify_nonce($_POST['mpp-nonce'], 'mpp-create-gallery')) { //add error message and return back to the old page mpp_add_feedback(__('Action not authorized!', 'mediapress'), 'error'); if ($referer) { mpp_redirect($referer); } return; } //update it to allow passing component/id from the form $component = mpp_get_current_component(); $component_id = mpp_get_current_component_id(); //check for permission //we may want to allow passing of component from the form in future! if (!mpp_user_can_create_gallery($component, $component_id)) { mpp_add_feedback(__("You don't have permission to create gallery!", 'mediapress'), 'error'); if ($referer) { mpp_redirect($referer); } return; } //if we are here, validate the data and let us see if we can create $title = $_POST['mpp-gallery-title']; $description = $_POST['mpp-gallery-description']; $type = $_POST['mpp-gallery-type']; $status = $_POST['mpp-gallery-status']; $errors = array(); if (!mpp_is_active_status($status)) { $errors['status'] = __('Invalid Gallery status!', 'mediapress'); } if (!mpp_is_active_type($type)) { $errors['type'] = __('Invalid gallery type!', 'mediapress'); } //check for current component if (!mpp_is_active_component($component)) { $errors['component'] = __('Invalid gallery component!', 'mediapress'); } if (empty($title)) { $errors['title'] = __('Title can not be empty', 'mediapress'); } //give opportunity to other plugins to add their own validation errors $validation_errors = apply_filters('mpp-create-gallery-field-validation', $errors, $_POST); if (!empty($validation_errors)) { //let us add the validation error and return back to the earlier page $message = join('\\r\\n', $validation_errors); mpp_add_feedback($message, 'error'); if ($referer) { mpp_redirect($referer); } return; } //let us create gallery $gallery_id = mpp_create_gallery(array('title' => $title, 'description' => $description, 'type' => $type, 'status' => $status, 'creator_id' => get_current_user_id(), 'component' => $component, 'component_id' => $component_id)); if (!$gallery_id) { mpp_add_feedback(__('Unable to create gallery!', 'mediapress'), 'error'); if ($referer) { mpp_redirect($referer); } return; } //if we are here, the gallery was created successfully, //let us redirect to the gallery_slug/manage/upload page $redirect_url = mpp_get_gallery_add_media_url($gallery_id); mpp_add_feedback(__('Gallery created successfully!', 'mediapress')); mpp_redirect($redirect_url); }
/** * Not used, Oembed support is not yet available in 1.0 branch */ public function add_oembed_media() { check_ajax_referer('mpp_add_media'); //check for the referrer $media_type = ''; $gallery_id = ''; $component = $_POST['component']; $component_id = $_POST['component_id']; $context = mpp_get_upload_context(false, $_POST['context']); if (!$component) { $component = mpp_get_current_component(); } if (!$component_id) { $component_id = mpp_get_current_component_id(); } //get the uploader $uploader = mpp_get_storage_manager('oembed'); //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'))); } if (!mpp_has_available_space($component, $component_id)) { wp_send_json_error(array('message' => __('Unable to upload. You have used the allowed storage quota!', 'mediapress'))); } //if we are here, the server can handle upload //check should be here $gallery_id = 0; if (isset($_POST['gallery_id'])) { $gallery_id = absint($_POST['gallery_id']); } if ($gallery_id) { $gallery = mpp_get_gallery($gallery_id); } else { $gallery = false; } //not set //if there is no gallery id given and the context is activity, we may want to auto create the gallery $media_type = mpp_get_media_type_from_extension(mpp_get_file_extension($file[$file_id]['name'])); if (!$media_type) { wp_send_json_error(array('message' => __("This file type is not supported.", 'mediapress'))); } //if there is no gallery type defined( It wil happen in case of new gallery creation from admin page //we will set the gallery type as the type of the first media if ($gallery && empty($gallery->type)) { //update gallery type //set it to media type mpp_update_gallery_type($gallery, $media_type); } //If the gallery is not given and It is members component, check if the upload context is activity? //Check if we have a profile gallery set for the current user for this type of media //if yes, then use that gallery to upload the media //otherwise we create a gallery of the current media type and set it as the profile gallery for that type if (!$gallery && $context == 'activity') { //if gallery is not given and the component supports wall gallery //then create if (!mpp_is_activity_upload_enabled($component)) { wp_send_json_error(array('message' => __("The gallery is not selected.", 'mediapress'))); } $gallery_id = mpp_get_wall_gallery_id(array('component' => $component, 'component_id' => $component_id, 'media_type' => $media_type)); if (!$gallery_id) { //if gallery does not exist, create 1 $gallery_id = mpp_create_gallery(array('creator_id' => get_current_user_id(), 'title' => sprintf(_x('Wall %s Gallery', 'wall gallery name', 'mediapress'), $media_type), 'description' => '', 'status' => 'public', 'component' => $component, 'component_id' => $component_id, 'type' => $media_type)); if ($gallery_id) { //save the profile gallery id mpp_update_wall_gallery_id(array('component' => $component, 'component_id' => $component_id, 'media_type' => $media_type, 'gallery_id' => $gallery_id)); } } //setup gallery object from the profile gallery id if ($gallery_id) { $gallery = mpp_get_gallery($gallery_id); } } //we may want to check the upload type and set the gallery to activity gallery etc if it is not set already $error = false; //detect media type of uploaded file here and then upload it accordingly also check if the media type uploaded and the gallery type matches or not //let us build our response for javascript //if we are uploading to a gallery, check for type //since we will be allowin g upload without gallery too, It is required to make sure $gallery is present or not if ($gallery && !mpp_is_mixed_gallery($gallery) && $media_type !== $gallery->type) { //if we are uploading to a gallery and It is not a mixed gallery, the media type must match the gallery type wp_send_json_error(array('message' => sprintf(__('This file type is not allowed in current gallery. Only <strong>%s</strong> files are allowed!', 'mediapress'), mpp_get_allowed_file_extensions_as_string($gallery->type)))); } //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)); //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; //Any media uploaded via activity is marked as orphan( Not associated with the mediapress unless the activity to which it was attached is actually created, check core/activity/actions.php to see how the orphaned media is adopted by the activity :) ) if ($context == 'activity') { $is_orphan = 1; } //by default mark all uploaded media via activity as orphan $media_data = array('title' => $title, 'description' => $content, 'gallery_id' => $gallery_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); $id = mpp_add_media($media_data); //should we update and resize images here? // mpp_gallery_increment_media_count($gallery_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 { wp_send_json_error(array('message' => $uploaded['error'])); } }