Advertisement
sbrajesh

something

Nov 24th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.15 KB | None | 0 0
  1.  
  2. function bd_attachments_cover_image_ajax_upload() {
  3.     // Bail if not a POST action
  4.     if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
  5.         wp_die();
  6.     }
  7.  
  8.     /**
  9.      * Sending the json response will be different if
  10.      * the current Plupload runtime is html4
  11.      */
  12.     $is_html4 = false;
  13.     if ( ! empty( $_POST['html4' ] ) ) {
  14.         $is_html4 = true;
  15.     }
  16.  
  17.     // Check the nonce
  18.     check_admin_referer( 'bp-uploader' );
  19.  
  20.     // Init the BuddyPress parameters
  21.     $bp_params = array();
  22.  
  23.     // We need it to carry on
  24.     if ( ! empty( $_POST['bp_params'] ) ) {
  25.         $bp_params = bp_parse_args( $_POST['bp_params'], array(
  26.             'object'  => 'user',
  27.             'item_id' => bp_loggedin_user_id(),
  28.         ), 'attachments_cover_image_ajax_upload' );
  29.     } else {
  30.         bp_attachments_json_response( false, $is_html4 );
  31.     }
  32.  
  33.     // We need the object to set the uploads dir filter
  34.     if ( empty( $bp_params['object'] ) ) {
  35.         bp_attachments_json_response( false, $is_html4 );
  36.     }
  37.  
  38.     // Capability check
  39.     if ( ! bp_attachments_current_user_can( 'edit_cover_image', $bp_params ) ) {
  40.         bp_attachments_json_response( false, $is_html4 );
  41.     }
  42.  
  43.     $bp          = buddypress();
  44.     $needs_reset = array();
  45.  
  46.     // Member's cover image
  47.     if ( 'user' === $bp_params['object'] ) {
  48.         $object_data = array( 'dir' => 'members', 'component' => 'xprofile' );
  49.  
  50.         if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
  51.             $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
  52.             $bp->displayed_user->id = $bp_params['item_id'];
  53.         }
  54.  
  55.     // Group's cover image
  56.     } elseif ( 'group' === $bp_params['object'] ) {
  57.         $object_data = array( 'dir' => 'groups', 'component' => 'groups' );
  58.  
  59.         if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
  60.             $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
  61.             $bp->groups->current_group = groups_get_group( array(
  62.                 'group_id'        => $bp_params['item_id'],
  63.                 'populate_extras' => false,
  64.             ) );
  65.         }
  66.  
  67.     // Other object's cover image
  68.     } else {
  69.         $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] );
  70.     }
  71.  
  72.     // Stop here in case of a missing parameter for the object
  73.     if ( empty( $object_data['dir'] ) || empty( $object_data['component'] ) ) {
  74.         bp_attachments_json_response( false, $is_html4 );
  75.     }
  76.  
  77.     $cover_image_attachment = new BP_Attachment_Cover_Image();
  78.     $uploaded = $cover_image_attachment->upload( $_FILES );
  79.  
  80.     // Reset objects
  81.     if ( ! empty( $needs_reset ) ) {
  82.         if ( ! empty( $needs_reset['component'] ) ) {
  83.             $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value'];
  84.         } else {
  85.             $bp->{$needs_reset['key']} = $needs_reset['value'];
  86.         }
  87.     }
  88.  
  89.     if ( ! empty( $uploaded['error'] ) ) {
  90.         // Upload error response
  91.         bp_attachments_json_response( false, $is_html4, array(
  92.             'type'    => 'upload_error',
  93.             'message' => sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $uploaded['error'] ),
  94.         ) );
  95.     }
  96.  
  97.     // Default error message
  98.     $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' );
  99.  
  100.     // Get BuddyPress Attachments Uploads Dir datas
  101.     $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get();
  102.  
  103.     $cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image';
  104.     $cover_dir    = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $cover_subdir;
  105.  
  106.     if ( ! is_dir( $cover_dir ) ) {
  107.         // Upload error response
  108.         bp_attachments_json_response( false, $is_html4, array(
  109.             'type'    => 'upload_error',
  110.             'message' => $error_message,
  111.         ) );
  112.     }
  113.  
  114.     /**
  115.      * Generate the cover image so that it fit to feature's dimensions
  116.      *
  117.      * Unlike the Avatar, Uploading and generating the cover image is happening during
  118.      * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image
  119.      * class, let's use it.
  120.      */
  121.     $cover = bp_attachments_cover_image_generate_file( array(
  122.         'file'            => $uploaded['file'],
  123.         'component'       => $object_data['component'],
  124.         'cover_image_dir' => $cover_dir
  125.     ), $cover_image_attachment );
  126.  
  127.     if ( ! $cover ) {
  128.         // Upload error response
  129.         bp_attachments_json_response( false, $is_html4, array(
  130.             'type'    => 'upload_error',
  131.             'message' => $error_message,
  132.         ) );
  133.     }
  134.  
  135.     // Build the url to the file
  136.     $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename'];
  137.  
  138.     // Init Feedback code, 1 is success
  139.     $feedback_code = 1;
  140.  
  141.     // 0 is the size warning
  142.     if ( $cover['is_too_small'] ) {
  143.         $feedback_code = 0;
  144.        
  145.         $dimensions = bp_attachments_get_cover_image_dimensions( $object_data['component'] );
  146.         bp_attachments_delete_file( array( 'item_id' => $bp_params['item_id'], 'object_dir' => $object_data['dir'], 'type' => 'cover-image' ) );
  147.         //delete the upload
  148.         bp_attachments_json_response( false, $is_html4, array(
  149.             'type'    => 'upload_error',
  150.             'message' => sprintf( __( 'Please upload an image with with atleast %sx%spx size.'), $dimensions['width'], $dimensions['height'] )
  151.         ) );
  152.     }
  153.  
  154.     // Set the name of the file
  155.     $name = $_FILES['file']['name'];
  156.     $name_parts = pathinfo( $name );
  157.     $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) );
  158.  
  159.     /**
  160.      * Fires if the new cover image was successfully uploaded.
  161.      *
  162.      * The dynamic portion of the hook will be xprofile in case of a user's
  163.      * cover image, groups in case of a group's cover image. For instance:
  164.      * Use add_action( 'xprofile_cover_image_uploaded' ) to run your specific
  165.      * code once the user has set his cover image.
  166.      *
  167.      * @since 2.4.0
  168.      *
  169.      * @param int $item_id Inform about the item id the cover image was set for.
  170.      */
  171.     do_action( $object_data['component'] . '_cover_image_uploaded', (int) $bp_params['item_id'] );
  172.  
  173.     // Finally return the cover image url to the UI
  174.     bp_attachments_json_response( true, $is_html4, array(
  175.         'name'          => $name,
  176.         'url'           => $cover_url,
  177.         'feedback_code' => $feedback_code,
  178.     ) );
  179. }
  180. add_action( 'wp_ajax_bp_cover_image_upload', 'bd_attachments_cover_image_ajax_upload' );
  181.  
  182. remove_action( 'wp_ajax_bp_cover_image_upload', 'bp_attachments_cover_image_ajax_upload' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement