/** * Generate an activity when a new gallery is created * We will not generate any activity for profile galleries though. * * @param type $gallery_id */ function mpp_new_gallery_activity($gallery_id) { //if the admin settings does not ask us to create new activity, //Or it is explicitly restricted, do not proceed if (!mpp_is_auto_publish_to_activity_enabled('create_gallery') || apply_filters('mpp_do_not_record_create_gallery_activity', false)) { return; } $gallery = mpp_get_gallery($gallery_id); $user_link = bp_core_get_userlink($gallery->user_id); $link = mpp_get_gallery_permalink($gallery); mpp_gallery_record_activity(array('gallery_id' => $gallery_id, 'type' => 'create_gallery', 'content' => '', 'action' => sprintf(__('%s created a %s <a href="%s">gallery</a>', 'mediapress'), $user_link, $gallery->type, $link))); //record gallery creation as activity, there will be issue with wall gallery though }
/** * Record a new upload activity if auto publishing is enabled in the * @param type $media_id */ function mpp_action_record_new_media_activity($media_id) { if (!mpp_is_auto_publish_to_activity_enabled('add_media') || apply_filters('mpp_do_not_record_add_media_activity', false)) { return; } $media = mpp_get_media($media_id); //if media is upload from activity, do not publish it again to activity if ($media->context == 'activity') { return; } $user_link = mpp_get_user_link($media->user_id); $link = mpp_get_media_permalink($media); mpp_media_record_activity(array('media_id' => $media_id, 'type' => 'add_media', 'content' => '', 'action' => sprintf(__('%s added a new <a href="%s">%s</a>', 'mediapress'), $user_link, $link, $media->type))); }
/** * Add new media via ajax * This method will be refactored in future to allow adding media from web * */ public function add_media() { 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 = $_POST['component']; $component_id = absint($_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(); //should we pass the component? ////should we check for the existence of the default storage method? //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 it // 1. let us make sure that the wall gallery creation activity is never recorded add_filter('mpp_do_not_record_create_gallery_activity', '__return_true'); //do not record gallery activity $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)); //remove the filter we added remove_filter('mpp_do_not_record_create_gallery_activity', '__return_true'); 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); } //for preexisting activity wall gallery, the original status may not be enabled if (!mpp_is_active_status($gallery->status)) { //the current wall gallery status is invalid, //update wall gallery status to current default privacy mpp_update_gallery_status($gallery, mpp_get_default_status()); } } //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)) { $error_message = apply_filters('mpp_upload_permission_denied_message', __("You don't have sufficient permissions to upload.", 'mediapress')); wp_send_json_error(array('message' => $error_message)); } //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 if (apply_filters('mpp_use_processed_file_name_as_media_title', false)) { $title = wp_basename($uploaded['file']); //$_FILES[ $file_id ][ 'name' ]; } else { $title = wp_basename($_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); //if the media is not uploaded from activity and auto publishing is not enabled, record as unpublished if ($context != 'activity' && !mpp_is_auto_publish_to_activity_enabled('add_media')) { mpp_gallery_add_unpublished_media($gallery_id, $id); } //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'])); } }