예제 #1
0
/**
 * Hndles bulk edit action
 * 
 */
function mpp_action_gallery_media_bulkedit()
{
    if (empty($_POST['mpp-action']) || $_POST['mpp-action'] != 'edit-gallery-media') {
        return;
    }
    if (!$_POST['mpp-editing-media-ids']) {
        return;
    }
    $referer = wp_get_referer();
    if (!wp_verify_nonce($_POST['mpp-nonce'], 'mpp-edit-gallery-media')) {
        //add error message and return back to the old page
        mpp_add_feedback(__('Action not authorized!', 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    $media_ids = $_POST['mpp-editing-media-ids'];
    $media_ids = wp_parse_id_list($media_ids);
    $bulk_action = false;
    if (!empty($_POST['mpp-edit-media-bulk-action'])) {
        $bulk_action = $_POST['mpp-edit-media-bulk-action'];
        //we are leaving this to allow future enhancements with other bulk action and not restricting to delete only
    }
    foreach ($media_ids as $media_id) {
        //check what action should we take?
        //1. check if $bulk_action is set? then we may ned to check for deletion
        //otherwise, just update the details :)
        if ($bulk_action == 'delete' && !empty($_POST['mpp-delete-media-check'][$media_id])) {
            //delete and continue
            //check if current user can delete?
            if (!mpp_user_can_delete_media($media_id)) {
                //if the user is unable to delete media, should we just continue the loop or breakout and redirect back with error?
                //I am in favour of showing error
                mpp_add_feedback(__('Not allowed to delete!', 'mediapress'));
                if ($referer) {
                    mpp_redirect($referer);
                }
                return;
            }
            //if we are here, let us delete the media
            mpp_delete_media($media_id);
            mpp_add_feedback(__('Deleted successfully!', 'mediapress'), 'error');
            //it will do for each media, that is not  good thing btw
            continue;
        }
        //since we already handled delete for the media checked above,
        //we don't want to do it for the other media hoping that the user was performing bulk delete and not updating the media info
        if ($bulk_action == 'delete') {
            continue;
        }
        $media_title = $_POST['mpp-media-title'][$media_id];
        $media_description = $_POST['mpp-media-description'][$media_id];
        $status = $_POST['mpp-media-status'][$media_id];
        //type is not editable
        //$type = $_POST['mpp-media-type'][$media_id];
        //if we are here, It must not be a bulk action
        $media_info = array('id' => $media_id, 'title' => $media_title, 'description' => $media_description, 'status' => $status);
        mpp_update_media($media_info);
    }
    if (!$bulk_action) {
        mpp_add_feedback(__('Updated!', 'mediapress'));
    }
    if ($referer) {
        mpp_redirect($referer);
    }
}
/**
 * Handles Single media Edit details
 * 
 * @return type
 */
function mpp_action_edit_media()
{
    //allow media to be edited from anywhere
    if (empty($_POST['mpp-action']) || $_POST['mpp-action'] != 'edit-media') {
        return;
    }
    $referer = wp_get_referer();
    //if we are here, It is media edit action
    if (!wp_verify_nonce($_POST['mpp-nonce'], 'mpp-edit-media')) {
        //add error message and return back to the old page
        mpp_add_feedback(__('Action not authorized!', 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    $media_id = absint($_POST['mpp-media-id']);
    if (!$media_id) {
        return;
    }
    //check for permission
    if (!mpp_user_can_edit_media($media_id)) {
        mpp_add_feedback(__("You don't have permission to edit this!", 'mediapress'), 'error');
        if ($referer) {
            mpp_redirect($referer);
        }
        return;
    }
    //if we are here, validate the data and let us see if we can update
    $title = $_POST['mpp-media-title'];
    $description = $_POST['mpp-media-description'];
    $status = $_POST['mpp-media-status'];
    $errors = array();
    //todo
    //In future, replace with media type functions
    if (!mpp_is_active_status($status)) {
        $errors['status'] = __('Invalid media status!', '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-edit-media-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
    $media_id = mpp_update_media(array('title' => $title, 'description' => $description, 'status' => $status, 'creator_id' => get_current_user_id(), 'id' => $media_id));
    if (!$media_id) {
        mpp_add_feedback(__('Unable to update!', '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_media_edit_url($media_id);
    mpp_add_feedback(__('Updated successfully!', 'mediapress'));
    mpp_redirect($redirect_url);
}
예제 #3
0
 public function bulk_update_media()
 {
     //verify nonce
     if (!wp_verify_nonce($_POST['_wpnonce'], 'mpp-manage-gallery')) {
         wp_send_json(array('message' => __('Invalid action.', 'mediapress'), 'error' => 1));
         exit(0);
     }
     if (!$_POST['mpp-editing-media-ids']) {
         return;
     }
     $gallery_id = absint($_POST['gallery_id']);
     $gallery = mpp_get_gallery($gallery_id);
     if (!$gallery_id || !$gallery) {
         wp_send_json(array('message' => __('Invalid action.', 'mediapress'), 'error' => 1));
         exit(0);
     }
     $message = '';
     $media_ids = $_POST['mpp-editing-media-ids'];
     $media_ids = wp_parse_id_list($media_ids);
     $media_ids = array_filter($media_ids);
     $bulk_action = false;
     if (!empty($_POST['mpp-edit-media-bulk-action'])) {
         $bulk_action = $_POST['mpp-edit-media-bulk-action'];
         //we are leaving this to allow future enhancements with other bulk action and not restricting to delete only
     }
     foreach ($media_ids as $media_id) {
         //check what action should we take?
         //1. check if $bulk_action is set? then we may ned to check for deletion
         //otherwise, just update the details :)
         if ($bulk_action == 'delete' && !empty($_POST['mpp-delete-media-check'][$media_id])) {
             //delete and continue
             //check if current user can delete?
             if (!mpp_user_can_delete_media($media_id)) {
                 //if the user is unable to delete media, should we just continue the loop or breakout and redirect back with error?
                 //I am in favour of showing error
                 $success = 0;
                 wp_send_json(array('message' => __('Not allowed to delete!', 'mediapress'), 'error' => 1));
                 exit(0);
             }
             //if we are here, let us delete the media
             mpp_delete_media($media_id);
             $message = __('Deleted successfully!', 'mediapress');
             //it will do for each media, that is not  good thing btw
             $success = 1;
             continue;
         }
         //since we already handled delete for the media checked above,
         //we don't want to do it for the other media hoping that the user was performing bulk delete and not updating the media info
         if ($bulk_action == 'delete') {
             continue;
         }
         //is it media update
         $media_title = $_POST['mpp-media-title'][$media_id];
         $media_description = $_POST['mpp-media-description'][$media_id];
         $status = $_POST['mpp-media-status'][$media_id];
         //if we are here, It must not be a bulk action
         $media_info = array('id' => $media_id, 'title' => $media_title, 'description' => $media_description, 'status' => $status);
         mpp_update_media($media_info);
     }
     if (!$bulk_action) {
         $message = __('Updated!', 'mediapress');
     } elseif (!$message) {
         $message = __('Please select media to apply bulk actions.', 'mediapress');
     }
     mediapress()->current_gallery = $gallery;
     mediapress()->the_media_query = new MPP_Media_Query(array('gallery_id' => $gallery_id, 'per_page' => -1, 'nopaging' => true));
     global $post;
     $bkp_post = $post;
     ob_start();
     require_once $this->template_dir . 'gallery/edit-media.php';
     $contents = ob_get_clean();
     $post = $bkp_post;
     //remember to add content too
     wp_send_json(array('message' => $message, 'success' => 1, 'contents' => $contents));
     exit(0);
 }