public function update($new_instance, $old_instance)
 {
     $instance = $old_instance;
     $instance['title'] = strip_tags($new_instance['title']);
     if (mpp_is_active_component($new_instance['component'])) {
         $instance['component'] = $new_instance['component'];
     }
     if (mpp_is_active_type($new_instance['type'])) {
         $instance['type'] = $new_instance['type'];
     }
     if (mpp_is_active_status($new_instance['status'])) {
         $instance['status'] = $new_instance['status'];
     }
     $instance['per_page'] = absint($new_instance['per_page']);
     $instance['orderby'] = $new_instance['orderby'];
     $instance['order'] = $new_instance['order'];
     return $instance;
 }
 /**
  * Initilize settings based on the current stack of action variables and component etc
  */
 public function init()
 {
     $current_action = $this->get_variable(0);
     //on create or upload we don't need to setup Media or Gallery Query
     if ($current_action == 'create' || $current_action == 'upload') {
         mediapress()->set_action($current_action);
         mediapress()->set_edit_action($current_action);
         return;
     }
     //check for type/status
     $type_or_status = $this->get_variable(1);
     if ($current_action === 'type' && $type_or_status && mpp_is_active_type($type_or_status)) {
         $this->type = $type_or_status;
         mediapress()->is_gallery_home = true;
     } elseif ($current_action == 'status' && $type_or_status && mpp_is_active_status($type_or_status)) {
         $this->status = $type_or_status;
         mediapress()->is_gallery_home = true;
     } elseif ($gallery = mpp_gallery_exists($current_action, $this->component, $this->component_id)) {
         //Are we looking at single gallery? or Media?
         //current action in this case is checked for being  a gallery slug
         //setup current gallery & gallery query
         mediapress()->current_gallery = mpp_get_gallery($gallery);
         $this->gallery_id = $gallery->ID;
         $this->current_action = $this->get_variable(1);
         $this->current_manage_action = $this->get_variable(2);
         //setup pagination for single gallery media
         if ($this->get_variable(1) == 'page' && $this->get_variable(2) > 0) {
             $this->mpage = (int) $this->get_variable(2);
         }
     } else {
         if ($this->get_variable(0) == 'page' && $this->get_variable(1) > 0) {
             $this->gpage = (int) $this->get_variable(1);
         }
         //set it is the user galleries list view
         mediapress()->is_gallery_home = true;
     }
     //check and setup pagination args for status/type archives
     if ($this->get_variable(2) == 'page' && $this->get_variable(3) > 0) {
         $this->gpage = (int) $this->get_variable(3);
     }
 }
/**
 * 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);
}
function mpp_update_gallery($args)
{
    if (!isset($args['id'])) {
        return new WP_Error(0, __('Must provide ID to update a gallery.', 'mediapress'));
    }
    $gallery = get_post($args['id']);
    //we need meaning full error handling in future
    if (!$gallery) {
        return new WP_Error(0, __('Gallery Does not exist!', 'mediapress'));
    }
    // $gdata =  get_object_vars( $gallery );
    $gallery_data = array('id' => $gallery->ID, 'title' => $gallery->post_title, 'description' => $gallery->post_content, 'slug' => $gallery->post_name, 'creator_id' => $gallery->post_author, 'order' => $gallery->menu_order, 'component_id' => mpp_get_gallery_meta($gallery->ID, '_mpp_component_id'), 'component' => '', 'status' => '', 'type' => '', 'date_created' => '', 'date_modified' => '');
    $args = wp_parse_args($args, $gallery_data);
    extract($args);
    if (empty($id) || empty($title)) {
        return false;
    }
    //now let us check which fields need to be updated
    if ($creator_id) {
        $gallery->post_author = $creator_id;
    }
    if ($title) {
        $gallery->post_title = $title;
    }
    if ($description) {
        $gallery->post_content = $description;
    }
    if ($slug) {
        $gallery->post_name = $slug;
    }
    if ($order) {
        $gallery->menu_order = $order;
    }
    $post = get_object_vars($gallery);
    $tax = array();
    if ($component && mpp_is_active_component($component)) {
        $tax[mpp_get_component_taxname()] = mpp_underscore_it($component);
    }
    if ($type && mpp_is_active_type($type)) {
        $tax[mpp_get_type_taxname()] = mpp_underscore_it($type);
    }
    if ($status && mpp_is_active_status($status)) {
        $tax[mpp_get_status_taxname()] = mpp_underscore_it($status);
    }
    if (!empty($tax)) {
        $post['tax_input'] = $tax;
    }
    //$post['ID'] = $gallery->id;
    if (!empty($date_created)) {
        $post_data['post_date'] = $date_created;
    }
    if (!empty($date_updated)) {
        $post_data['post_modified'] = $date_updated;
    }
    $gallery_id = wp_update_post($post);
    if (is_wp_error($gallery_id)) {
        return $gallery_id;
        //error
    }
    do_action('mpp_gallery_updated', $gallery_id);
    return $gallery_id;
}
Exemple #5
0
 /**
  * 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']));
     }
 }
/**
 * Handles gallery details updation
 * 
 * @return type
 */
function mpp_action_edit_gallery()
{
    //allow gallery to be created from anywhere
    if (empty($_POST['mpp-action']) || $_POST['mpp-action'] != 'edit-gallery') {
        return;
    }
    $referer = wp_get_referer();
    //if we are here, It is gallery create action
    if (!wp_verify_nonce($_POST['mpp-nonce'], 'mpp-edit-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;
    }
    $gallery_id = absint($_POST['mpp-gallery-id']);
    $gallery = mpp_get_gallery($gallery_id);
    if (!$gallery_id || empty($gallery)) {
        return;
    }
    //check for permission
    //we may want to allow passing of component from the form in future!
    if (!mpp_user_can_edit_gallery($gallery_id)) {
        mpp_add_feedback(__("You don't have permission to edit this 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'];
    $status = $_POST['mpp-gallery-status'];
    $errors = array();
    if (!mpp_is_active_status($status)) {
        $errors['status'] = __('Invalid Gallery 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-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_update_gallery(array('title' => $title, 'description' => $description, 'status' => $status, 'id' => $gallery_id));
    if (!$gallery_id) {
        mpp_add_feedback(__('Unable to update 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_settings_url($gallery_id);
    mpp_add_feedback(__('Gallery updated successfully!', 'mediapress'));
    mpp_redirect($redirect_url);
}