Skip to:
Content

BuddyPress.org

Changeset 9624


Ignore:
Timestamp:
03/17/2015 06:32:58 PM (5 years ago)
Author:
imath
Message:

Adapts Avatars core functions so that they use the BP_Attachment_Avatar class.

Props johnjamesjacoby, boonebgorges

See #6278

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-avatars.php

    r9455 r9624  
    565565 * @see bp_core_check_avatar_type()
    566566 *
    567  * @param array $file The appropriate entry the from $_FILES superglobal.
     567 * @param array  $file              The appropriate entry the from $_FILES superglobal.
    568568 * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
     569 *
    569570 * @return bool True on success, false on failure.
    570571 */
     
    575576     * Make sure you return false.
    576577     */
    577     if ( !apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) )
     578    if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) ) {
    578579        return true;
    579 
    580     require_once( ABSPATH . '/wp-admin/includes/file.php' );
    581 
    582     $uploadErrors = array(
    583         0 => __( 'The image was uploaded successfully', 'buddypress' ),
    584         1 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
    585         2 => __( 'The image exceeds the maximum allowed file size of: ', 'buddypress' ) . size_format( bp_core_avatar_original_max_filesize() ),
    586         3 => __( 'The uploaded file was only partially uploaded.', 'buddypress' ),
    587         4 => __( 'The image was not uploaded.', 'buddypress' ),
    588         6 => __( 'Missing a temporary folder.', 'buddypress' )
    589     );
    590 
    591     if ( ! bp_core_check_avatar_upload( $file ) ) {
    592         bp_core_add_message( sprintf( __( 'Your upload failed. Please try again. Error was: %s', 'buddypress' ), $uploadErrors[$file['file']['error']] ), 'error' );
    593         return false;
    594     }
    595 
    596     if ( ! bp_core_check_avatar_size( $file ) ) {
    597         bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ), 'error' );
    598         return false;
    599     }
    600 
    601     if ( ! bp_core_check_avatar_type( $file ) ) {
    602         bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' );
    603         return false;
    604     }
    605 
    606     // Filter the upload location
    607     add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
    608 
    609     $bp = buddypress();
    610 
    611     $bp->avatar_admin->original = wp_handle_upload( $file['file'], array( 'action'=> 'bp_avatar_upload' ) );
    612 
    613     // Remove the upload_dir filter, so that other upload URLs on the page
    614     // don't break
    615     remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
    616 
    617     // Move the file to the correct upload location.
    618     if ( !empty( $bp->avatar_admin->original['error'] ) ) {
     580    }
     581
     582    // Setup some variables
     583    $bp          = buddypress();
     584    $upload_path = bp_core_avatar_upload_path();
     585
     586    // Upload the file
     587    $avatar_attachment = new BP_Attachment_Avatar();
     588    $bp->avatar_admin->original = $avatar_attachment->upload( $file, $upload_dir_filter );
     589
     590    // In case of an error, stop the process and display a feedback to the user
     591    if ( ! empty( $bp->avatar_admin->original['error'] ) ) {
    619592        bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' );
    620593        return false;
    621594    }
    622595
    623     // Get image size
    624     $size  = @getimagesize( $bp->avatar_admin->original['file'] );
    625     $error = false;
    626 
    627     // Check image size and shrink if too large
    628     if ( $size[0] > bp_core_avatar_original_max_width() ) {
    629         $editor = wp_get_image_editor( $bp->avatar_admin->original['file'] );
    630 
    631         if ( ! is_wp_error( $editor ) ) {
    632             $editor->set_quality( 100 );
    633 
    634             $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
    635             if ( ! is_wp_error( $resized ) ) {
    636                 $thumb = $editor->save( $editor->generate_filename() );
    637             } else {
    638                 $error = $resized;
    639             }
    640 
    641             // Check for thumbnail creation errors
    642             if ( false === $error && is_wp_error( $thumb ) ) {
    643                 $error = $thumb;
    644             }
    645 
    646             // Thumbnail is good so proceed
    647             if ( false === $error ) {
    648                 $bp->avatar_admin->resized = $thumb;
    649             }
    650 
    651         } else {
    652             $error = $editor;
    653         }
    654 
    655         if ( false !== $error ) {
    656             bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $error->get_error_message() ), 'error' );
    657             return false;
    658         }
    659     }
    660 
    661     if ( ! isset( $bp->avatar_admin->image ) )
    662         $bp->avatar_admin->image = new stdClass();
     596    // Maybe resize
     597    $bp->avatar_admin->resized = $avatar_attachment->shrink( $bp->avatar_admin->original['file'] );
     598    $bp->avatar_admin->image   = new stdClass();
    663599
    664600    // We only want to handle one image after resize.
    665601    if ( empty( $bp->avatar_admin->resized ) ) {
    666         $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file'] );
     602        $bp->avatar_admin->image->file = $bp->avatar_admin->original['file'];
     603        $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->original['file'] );
    667604    } else {
    668         $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path'] );
     605        $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path'];
     606        $bp->avatar_admin->image->dir  = str_replace( $upload_path, '', $bp->avatar_admin->resized['path'] );
    669607        @unlink( $bp->avatar_admin->original['file'] );
    670608    }
     
    676614    }
    677615
    678     // If the uploaded image is smaller than the "full" dimensions, throw
    679     // a warning
    680     $uploaded_image = @getimagesize( bp_core_avatar_upload_path() . buddypress()->avatar_admin->image->dir );
    681     $full_width     = bp_core_avatar_full_width();
    682     $full_height    = bp_core_avatar_full_height();
    683     if ( isset( $uploaded_image[0] ) && $uploaded_image[0] < $full_width || $uploaded_image[1] < $full_height ) {
    684         bp_core_add_message( sprintf( __( 'You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress' ), $full_width, $full_height ), 'error' );
     616    // If the uploaded image is smaller than the "full" dimensions, throw a warning
     617    if ( $avatar_attachment->is_too_small( $bp->avatar_admin->image->file ) ) {
     618        bp_core_add_message( sprintf( __( 'You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress' ), bp_core_avatar_full_width(), bp_core_avatar_full_height() ), 'error' );
    685619    }
    686620
     
    739673     * Make sure you return false.
    740674     */
    741     if ( !apply_filters( 'bp_core_pre_avatar_handle_crop', true, $r ) )
     675    if ( ! apply_filters( 'bp_core_pre_avatar_handle_crop', true, $r ) ) {
    742676        return true;
    743 
    744     extract( $r, EXTR_SKIP );
    745 
    746     if ( empty( $original_file ) )
     677    }
     678
     679    // Crop the file
     680    $avatar_attachment = new BP_Attachment_Avatar();
     681    $cropped           = $avatar_attachment->crop( $r );
     682
     683    // Check for errors
     684    if ( empty( $cropped['full'] ) || empty( $cropped['thumb'] ) || is_wp_error( $cropped['full'] ) || is_wp_error( $cropped['thumb'] ) ) {
    747685        return false;
    748 
    749     $original_file = bp_core_avatar_upload_path() . $original_file;
    750 
    751     if ( !file_exists( $original_file ) )
    752         return false;
    753 
    754     if ( empty( $item_id ) ) {
    755         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', dirname( $original_file ), $item_id, $object, $avatar_dir );
    756     } else {
    757         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
    758     }
    759 
    760     if ( !file_exists( $avatar_folder_dir ) )
    761         return false;
    762 
    763     require_once( ABSPATH . '/wp-admin/includes/image.php' );
    764     require_once( ABSPATH . '/wp-admin/includes/file.php' );
    765 
    766     // Delete the existing avatar files for the object
    767     $existing_avatar = bp_core_fetch_avatar( array(
    768         'object'  => $object,
    769         'item_id' => $item_id,
    770         'html' => false,
    771     ) );
    772 
    773     if ( ! empty( $existing_avatar ) ) {
    774         // Check that the new avatar doesn't have the same name as the
    775         // old one before deleting
    776         $upload_dir           = wp_upload_dir();
    777         $existing_avatar_path = str_replace( $upload_dir['baseurl'], '', $existing_avatar );
    778         $new_avatar_path      = str_replace( $upload_dir['basedir'], '', $original_file );
    779 
    780         if ( $existing_avatar_path !== $new_avatar_path ) {
    781             bp_core_delete_existing_avatar( array( 'object' => $object, 'item_id' => $item_id, 'avatar_path' => $avatar_folder_dir ) );
    782         }
    783     }
    784 
    785 
    786 
    787     // Make sure we at least have a width and height for cropping
    788     if ( empty( $crop_w ) ) {
    789         $crop_w = bp_core_avatar_full_width();
    790     }
    791 
    792     if ( empty( $crop_h ) ) {
    793         $crop_h = bp_core_avatar_full_height();
    794     }
    795 
    796     // Get the file extension
    797     $data = @getimagesize( $original_file );
    798     $ext  = $data['mime'] == 'image/png' ? 'png' : 'jpg';
    799 
    800     // Set the full and thumb filenames
    801     $full_filename  = wp_hash( $original_file . time() ) . '-bpfull.'  . $ext;
    802     $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.' . $ext;
    803 
    804     // Crop the image
    805     $full_cropped  = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_full_width(),  bp_core_avatar_full_height(),  false, $avatar_folder_dir . '/' . $full_filename  );
    806     $thumb_cropped = wp_crop_image( $original_file, (int) $crop_x, (int) $crop_y, (int) $crop_w, (int) $crop_h, bp_core_avatar_thumb_width(), bp_core_avatar_thumb_height(), false, $avatar_folder_dir . '/' . $thumb_filename );
    807 
    808     // Check for errors
    809     if ( empty( $full_cropped ) || empty( $thumb_cropped ) || is_wp_error( $full_cropped ) || is_wp_error( $thumb_cropped ) )
    810         return false;
    811 
    812     // Remove the original
    813     @unlink( $original_file );
     686    }
    814687
    815688    return true;
     
    927800 * Fetch data from the BP root blog's upload directory.
    928801 *
    929  * Handy for multisite instances because all uploads are made on the BP root
    930  * blog and we need to query the BP root blog for the upload directory data.
    931  *
    932  * This function ensures that we only need to use {@link switch_to_blog()}
    933  * once to get what we need.
    934  *
    935802 * @since BuddyPress (1.8.0)
    936  *
    937  * @uses wp_upload_dir()
    938803 *
    939804 * @param string $type The variable we want to return from the $bp->avatars
     
    978843            // No cache, so query for it
    979844            } else {
    980                 // We need to switch to the root blog on multisite installs
    981                 if ( is_multisite() ) {
    982                     switch_to_blog( bp_get_root_blog_id() );
    983                 }
    984845
    985846                // Get upload directory information from current site
    986                 $upload_dir = wp_upload_dir();
    987 
    988                 // Will bail if not switched
    989                 restore_current_blog();
     847                $upload_dir = bp_upload_dir();
    990848
    991849                // Stash upload directory data for later use
     
    1019877 * Get the absolute upload path for the WP installation.
    1020878 *
    1021  * @uses wp_upload_dir To get upload directory info
     879 * @uses bp_core_get_upload_dir() To get upload directory info
    1022880 *
    1023881 * @return string Absolute path to WP upload directory.
     
    1030888 * Get the raw base URL for root site upload location.
    1031889 *
    1032  * @uses wp_upload_dir To get upload directory info.
     890 * @uses bp_core_get_upload_dir() To get upload directory info.
    1033891 *
    1034892 * @return string Full URL to current upload location.
Note: See TracChangeset for help on using the changeset viewer.