Ticket #6278: 6278.02.patch
File 6278.02.patch, 30.9 KB (added by , 10 years ago) |
---|
-
src/bp-core/bp-core-avatars.php
52 52 function bp_core_set_avatar_globals() { 53 53 $bp = buddypress(); 54 54 55 $bp->avatar = new stdClass; 56 $bp->avatar->thumb = new stdClass; 57 $bp->avatar->full = new stdClass; 55 $bp->avatar = new BP_Attachment_Avatar( array( 58 56 59 // Dimensions60 $bp->avatar->thumb->width = BP_AVATAR_THUMB_WIDTH;61 $bp->avatar->thumb->height = BP_AVATAR_THUMB_HEIGHT;62 $bp->avatar->full->width = BP_AVATAR_FULL_WIDTH;63 $bp->avatar->full->height = BP_AVATAR_FULL_HEIGHT;57 // Dimensions for thumb size 58 'thumb' => (object) array( 59 'width' => absint( BP_AVATAR_THUMB_WIDTH ), 60 'height' => absint( BP_AVATAR_THUMB_HEIGHT ), 61 ), 64 62 65 // Upload maximums 66 $bp->avatar->original_max_width = BP_AVATAR_ORIGINAL_MAX_WIDTH; 67 $bp->avatar->original_max_filesize = BP_AVATAR_ORIGINAL_MAX_FILESIZE; 63 // Dimensions for full size 64 'full' => (object) array( 65 'width' => absint( BP_AVATAR_FULL_WIDTH ), 66 'height' => absint( BP_AVATAR_FULL_HEIGHT ), 67 ), 68 68 69 // Defaults70 $bp->avatar->thumb->default = bp_core_avatar_default_thumb();71 $bp->avatar->full->default = bp_core_avatar_default();69 // Upload maximums 70 'original_max_width' => absint( BP_AVATAR_ORIGINAL_MAX_WIDTH ), 71 'original_max_filesize' => absint( BP_AVATAR_ORIGINAL_MAX_FILESIZE ), 72 72 73 // These have to be set on page load in order to avoid infinite filter loops at runtime74 $bp->avatar->upload_path = bp_core_avatar_upload_path();75 $bp->avatar->url = bp_core_avatar_url();73 // Cache the root blog's show_avatars setting, to avoid unnecessary 74 // calls to switch_to_blog() 75 'show_avatars' => (bool) BP_SHOW_AVATARS, 76 76 77 // Cache the root blog's show_avatars setting, to avoid unnecessary 78 // calls to switch_to_blog() 79 $bp->avatar->show_avatars = (bool) BP_SHOW_AVATARS; 77 // Specific errors for avatars 78 'upload_error_strings' => array( 79 sprintf( __( 'That photo is too big. Please upload one smaller than %s', 'buddypress' ), size_format( BP_AVATAR_ORIGINAL_MAX_FILESIZE ) ), 80 __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 81 ), 82 ) ); 80 83 81 84 // Backpat for pre-1.5 82 if ( ! defined( 'BP_AVATAR_UPLOAD_PATH' ) ) 85 if ( ! defined( 'BP_AVATAR_UPLOAD_PATH' ) ) { 83 86 define( 'BP_AVATAR_UPLOAD_PATH', $bp->avatar->upload_path ); 87 } 84 88 85 89 // Backpat for pre-1.5 86 if ( ! defined( 'BP_AVATAR_URL' ) ) 90 if ( ! defined( 'BP_AVATAR_URL' ) ) { 87 91 define( 'BP_AVATAR_URL', $bp->avatar->url ); 92 } 88 93 89 94 do_action( 'bp_core_set_avatar_globals' ); 90 95 } … … 564 569 * @see bp_core_check_avatar_upload() 565 570 * @see bp_core_check_avatar_type() 566 571 * 567 * @param array $fileThe appropriate entry the from $_FILES superglobal.572 * @param array $file The appropriate entry the from $_FILES superglobal. 568 573 * @param string $upload_dir_filter A filter to be applied to 'upload_dir'. 574 * 569 575 * @return bool True on success, false on failure. 570 576 */ 571 577 function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) { … … 574 580 * You may want to hook into this filter if you want to override this function. 575 581 * Make sure you return false. 576 582 */ 577 if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) )583 if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) ) { 578 584 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 585 } 595 586 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 } 587 // Setup some variables 588 $bp = buddypress(); 589 $upload_path = bp_core_avatar_upload_path(); 600 590 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 } 591 // Upload the file 592 $bp->avatar_admin->original = $bp->avatar->upload( $file, $upload_dir_filter ); 605 593 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'] ) ) { 594 // In case of an error, stop the process and display a feedback to the user 595 if ( ! empty( $bp->avatar_admin->original['error'] ) ) { 619 596 bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' ); 620 597 return false; 621 598 } 622 599 623 // Get imagesize624 $ size = @getimagesize( $bp->avatar_admin->original['file'] );625 $ error = false;600 // Maybe resize 601 $bp->avatar_admin->resized = $bp->avatar->shrink( $bp->avatar_admin->original['file'] ); 602 $bp->avatar_admin->image = new stdClass(); 626 603 627 // Check image size and shrink if too large628 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 errors642 if ( false === $error && is_wp_error( $thumb ) ) {643 $error = $thumb;644 }645 646 // Thumbnail is good so proceed647 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();663 664 604 // We only want to handle one image after resize. 665 605 if ( empty( $bp->avatar_admin->resized ) ) { 666 $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file'] ); 606 $bp->avatar_admin->image->file = $bp->avatar_admin->original['file']; 607 $bp->avatar_admin->image->dir = str_replace( $upload_path, '', $bp->avatar_admin->original['file'] ); 667 608 } else { 668 $bp->avatar_admin->image->dir = str_replace( bp_core_avatar_upload_path(), '', $bp->avatar_admin->resized['path'] ); 609 $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path']; 610 $bp->avatar_admin->image->dir = str_replace( $upload_path, '', $bp->avatar_admin->resized['path'] ); 669 611 @unlink( $bp->avatar_admin->original['file'] ); 670 612 } 671 613 … … 675 617 return false; 676 618 } 677 619 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' ); 620 // If the uploaded image is smaller than the "full" dimensions, throw a warning 621 if ( $bp->avatar->is_too_small( $bp->avatar_admin->image->file ) ) { 622 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' ); 685 623 } 686 624 687 625 // Set the url value for the image … … 926 864 /** 927 865 * Fetch data from the BP root blog's upload directory. 928 866 * 929 * Handy for multisite instances because all uploads are made on the BP root930 * 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 *935 867 * @since BuddyPress (1.8.0) 936 868 * 937 * @uses wp_upload_dir()938 *939 869 * @param string $type The variable we want to return from the $bp->avatars 940 870 * object. Only 'upload_path' and 'url' are supported. Default: 'upload_path'. 941 871 * @return string The avatar upload directory path. … … 943 873 function bp_core_get_upload_dir( $type = 'upload_path' ) { 944 874 $bp = buddypress(); 945 875 946 switch ( $type ) { 947 case 'upload_path' : 948 $constant = 'BP_AVATAR_UPLOAD_PATH'; 949 $key = 'basedir'; 950 951 break; 952 953 case 'url' : 954 $constant = 'BP_AVATAR_URL'; 955 $key = 'baseurl'; 956 957 break; 958 959 default : 960 return false; 961 962 break; 876 if ( ! isset( $bp->avatar->{$type} ) ) { 877 return false; 963 878 } 964 879 965 // See if the value has already been calculated and stashed in the $bp global 966 if ( isset( $bp->avatar->$type ) ) { 967 $retval = $bp->avatar->$type; 968 } else { 969 // If this value has been set in a constant, just use that 970 if ( defined( $constant ) ) { 971 $retval = constant( $constant ); 972 } else { 973 974 // Use cached upload dir data if available 975 if ( ! empty( $bp->avatar->upload_dir ) ) { 976 $upload_dir = $bp->avatar->upload_dir; 977 978 // No cache, so query for it 979 } 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 } 984 985 // Get upload directory information from current site 986 $upload_dir = wp_upload_dir(); 987 988 // Will bail if not switched 989 restore_current_blog(); 990 991 // Stash upload directory data for later use 992 $bp->avatar->upload_dir = $upload_dir; 993 } 994 995 // Directory does not exist and cannot be created 996 if ( ! empty( $upload_dir['error'] ) ) { 997 $retval = ''; 998 999 } else { 1000 $retval = $upload_dir[$key]; 1001 1002 // If $key is 'baseurl', check to see if we're on SSL 1003 // Workaround for WP13941, WP15928, WP19037. 1004 if ( $key == 'baseurl' && is_ssl() ) { 1005 $retval = str_replace( 'http://', 'https://', $retval ); 1006 } 1007 } 1008 1009 } 1010 1011 // Stash in $bp for later use 1012 $bp->avatar->$type = $retval; 1013 } 1014 1015 return $retval; 880 return $bp->avatar->{$type}; 1016 881 } 1017 882 1018 883 /** -
src/bp-core/bp-core-classes.php
20 20 require dirname( __FILE__ ) . '/classes/class-bp-suggestions.php'; 21 21 require dirname( __FILE__ ) . '/classes/class-bp-members-suggestions.php'; 22 22 require dirname( __FILE__ ) . '/classes/class-bp-recursive-query.php'; 23 require dirname( __FILE__ ) . '/classes/class-bp-attachment.php'; 24 require dirname( __FILE__ ) . '/classes/class-bp-attachment-avatar.php'; -
src/bp-core/classes/class-bp-attachment-avatar.php
1 <?php 2 3 /** 4 * Core avatar attachment class 5 * 6 * @package BuddyPress 7 * @subpackage Core 8 */ 9 10 // Exit if accessed directly 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * BP Attachment class to manage your avatar upload needs 15 * 16 * @since BuddyPress (2.3.0) 17 */ 18 class BP_Attachment_Avatar extends BP_Attachment { 19 20 /** Upload properties *****************************************************/ 21 22 /** 23 * Upload base directory. 24 * 25 * @var string 26 */ 27 public $base_dir = 'avatars'; 28 29 /** 30 * The upload action. 31 * Defaults to the avatar upload action 32 * 33 * @var string 34 */ 35 public $action = 'bp_avatar_upload'; 36 37 /** 38 * Construct Upload parameters 39 * 40 * @since BuddyPress (2.3.0) 41 * 42 * @see BP_Attachment::__construct() for list of parameters 43 * @uses sanitize_key() 44 * @uses bp_parse_args() 45 * @uses BP_Attachment->set_upload_error_strings() 46 * @uses BP_Attachment->set_upload_dir() 47 */ 48 public function __construct( $args = '' ) { 49 parent::__construct( $args ); 50 51 // Set default thumb & full values 52 if ( isset( $this->thumb, $this->full ) ) { 53 $this->thumb->default = bp_core_avatar_default_thumb(); 54 $this->full->default = bp_core_avatar_default(); 55 } 56 } 57 58 /** 59 * Set data from the BP root blog's upload directory. 60 * 61 * Handy for multisite instances because all uploads are made on the BP root 62 * blog and we need to query the BP root blog for the upload directory data. 63 * 64 * This function ensures that we only need to use {@link switch_to_blog()} 65 * once to get what we need. 66 * 67 * @since BuddyPress (2.3.0) 68 * 69 * @uses is_multisite() 70 * @uses bp_is_root_blog() 71 * @uses switch_to_blog() 72 * @uses wp_upload_dir() 73 * @uses restore_current_blog() 74 */ 75 public function set_upload_dir() { 76 parent::set_upload_dir(); 77 78 // Defer to constants if set 79 if ( defined( 'BP_AVATAR_UPLOAD_PATH' ) && defined( 'BP_AVATAR_URL' ) ) { 80 $this->upload_path = BP_AVATAR_UPLOAD_PATH; 81 $this->url = BP_AVATAR_URL; 82 } 83 } 84 85 /** 86 * Avatar specific rules 87 * 88 * Adds an error if the avatar size or type don't match BuddyPress needs 89 * The error code is the index of $upload_error_strings 90 * 91 * @since BuddyPress (2.3.0) 92 * 93 * @param array $file the temporary file attributes (before it has been moved) 94 * @uses bp_core_check_avatar_size() 95 * @uses bp_core_check_avatar_type() 96 * @return array the file with extra errors if needed 97 */ 98 public function validate_upload( $file = array() ) { 99 100 // Bail if already an error 101 if ( ! empty( $file['error'] ) ) { 102 return $file; 103 } 104 105 // File size is too big 106 if ( ! bp_core_check_avatar_size( array( 'file' => $file ) ) ) { 107 $file['error'] = 9; 108 109 // File is of invalid type 110 } else if ( ! bp_core_check_avatar_type( array( 'file' => $file ) ) ) { 111 $file['error'] = 10; 112 } 113 114 // Return with error code attached 115 return $file; 116 } 117 118 /** 119 * Maybe shrink the attachment to fit maximum allowed width 120 * 121 * @return mixed 122 */ 123 public static function shrink( $file = array() ) { 124 125 // Get image size 126 $size = @getimagesize( $file ); 127 $retval = false; 128 129 // Check image size and shrink if too large 130 if ( $size[0] > bp_core_avatar_original_max_width() ) { 131 $editor = wp_get_image_editor( $file ); 132 133 if ( ! is_wp_error( $editor ) ) { 134 $editor->set_quality( 100 ); 135 136 $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false ); 137 if ( ! is_wp_error( $resized ) ) { 138 $thumb = $editor->save( $editor->generate_filename() ); 139 } else { 140 $retval = $resized; 141 } 142 143 // Check for thumbnail creation errors 144 if ( ( false === $retval ) && is_wp_error( $thumb ) ) { 145 $retval = $thumb; 146 } 147 148 // Thumbnail is good so proceed 149 if ( false === $retval ) { 150 $retval = $thumb; 151 } 152 153 } else { 154 $retval = $editor; 155 } 156 } 157 158 return $retval; 159 } 160 161 /** 162 * 163 * @param type $file 164 * @return boolean 165 */ 166 public static function is_too_small( $file = array() ) { 167 $uploaded_image = @getimagesize( $file ); 168 $full_width = bp_core_avatar_full_width(); 169 $full_height = bp_core_avatar_full_height(); 170 171 if ( isset( $uploaded_image[0] ) && $uploaded_image[0] < $full_width || $uploaded_image[1] < $full_height ) { 172 return true; 173 } 174 175 return false; 176 } 177 } -
src/bp-core/classes/class-bp-attachment.php
1 <?php 2 3 /** 4 * Core attachment class. 5 * 6 * @package BuddyPress 7 * @subpackage Core 8 */ 9 10 // Exit if accessed directly 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * BP Attachment class to manage your component's uploads 15 * 16 * @since BuddyPress (2.3.0) 17 */ 18 class BP_Attachment { 19 20 /** Upload properties *****************************************************/ 21 22 /** 23 * The file being uploaded 24 * 25 * @var array 26 */ 27 public $attachment = array(); 28 29 /** 30 * Maximum file size in kilobytes 31 * 32 * @var int 33 */ 34 public $original_max_filesize = 5120000; 35 36 /** 37 * component's upload base directory. 38 * 39 * @var string 40 */ 41 public $base_dir = ''; 42 43 /** 44 * The upload action. 45 * Defaults to the avatar upload action 46 * 47 * @var string 48 */ 49 public $action = ''; 50 51 /** 52 * The file input name attribute 53 * 54 * @var string 55 */ 56 public $file_input = 'file'; 57 58 /** 59 * List of upload errors. 60 * 61 * @var array 62 */ 63 public $upload_error_strings = array(); 64 65 /** 66 * List of required core files 67 * 68 * @var array 69 */ 70 public $required_wp_files = array( 'file' ); 71 72 /** 73 * Construct Upload parameters 74 * 75 * @since BuddyPress (2.3.0) 76 * 77 * @param array $args { 78 * @type int $original_max_filesize Maximum file size in kilobytes. Default: 5120000. 79 * @type string $base_dir Component's upload base directory. Defaults to WordPress 'uploads' 80 * @type string $action The upload action used when uploading a file, $_POST['action'] must be set 81 * and its value must equal $action {@link wp_handle_upload()} 82 * @type string $file_input The name attribute used in the file input. Default: file 83 * @type array $upload_error_strings A list of specific error messages (optional). 84 * @type array $required_wp_files The list of required WordPress core files. Default: array( 'file' ); 85 * } 86 * @uses sanitize_key() 87 * @uses bp_parse_args() 88 * @uses BP_Attachment->set_upload_error_strings() 89 * @uses BP_Attachment->set_upload_dir() 90 */ 91 public function __construct( $args = '' ) { 92 93 /** 94 * Sanitize the action ID 95 */ 96 if ( ! empty( $args['action'] ) ) { 97 $this->action = sanitize_key( $args['action'] ); 98 } 99 100 $params = bp_parse_args( $args, get_class_vars( __CLASS__ ), $this->action . '_upload_params' ); 101 102 foreach ( $params as $key => $param ) { 103 if ( 'upload_error_strings' === $key ) { 104 $this->{$key} = $this->set_upload_error_strings( $param ); 105 } else { 106 $this->{$key} = $param; 107 } 108 } 109 110 // Set the path/url and base dir for uploads 111 $this->set_upload_dir(); 112 } 113 114 /** 115 * Set data from the BP root blog's upload directory. 116 * 117 * Handy for multisite instances because all uploads are made on the BP root 118 * blog and we need to query the BP root blog for the upload directory data. 119 * 120 * This function ensures that we only need to use {@link switch_to_blog()} 121 * once to get what we need. 122 * 123 * @since BuddyPress (2.3.0) 124 * 125 * @uses is_multisite() 126 * @uses bp_is_root_blog() 127 * @uses switch_to_blog() 128 * @uses wp_upload_dir() 129 * @uses restore_current_blog() 130 */ 131 public function set_upload_dir() { 132 133 // Do we need to juggle which blog to get the upload dir for? 134 $need_switch = (bool) ( is_multisite() && ! bp_is_root_blog() ); 135 136 // Maybe juggle to root blog 137 if ( true === $need_switch ) { 138 switch_to_blog( bp_get_root_blog_id() ); 139 } 140 141 // Get the upload directory (maybe for root blog) 142 $wp_upload_dir = wp_upload_dir(); 143 144 // Maybe juggle back to current blog 145 if ( true === $need_switch ) { 146 restore_current_blog(); 147 } 148 149 // Bail if an error occurred 150 if ( ! empty( $wp_upload_dir['error'] ) ) { 151 return; 152 } 153 154 // Set our directory, path, & url variables 155 $this->upload_dir = $wp_upload_dir; 156 $this->upload_path = $wp_upload_dir['basedir']; 157 $this->url = $wp_upload_dir['baseurl']; 158 159 // Ensure URL is https if SSL is set/forced 160 if ( is_ssl() ) { 161 $this->url = str_replace( 'http://', 'https://', $this->url ); 162 } 163 164 // Custom base dir 165 if ( ! empty( $this->base_dir ) ) { 166 $this->upload_path = trailingslashit( $this->upload_path ) . $this->base_dir; 167 $this->url = trailingslashit( $this->upload_url ) . $this->base_dir; 168 } 169 } 170 171 /** 172 * Set Upload error messages 173 * 174 * Used into the $overrides argument of BP_Attachment->upload() 175 * 176 * @since BuddyPress (2.3.0) 177 * 178 * @param array $param a list of error messages to add to BuddyPress core ones 179 */ 180 public function set_upload_error_strings( $param = array() ) { 181 /** 182 * Index of the array is the error code 183 * Custom errors will start at 9 code 184 */ 185 return array_merge( array( 186 0 => __( 'The file was uploaded successfully', 'buddypress' ), 187 1 => __( 'The uploaded file exceeds the maximum allowed file size on this site', 'buddypress' ), 188 2 => sprintf( __( 'The uploaded file exceeds the maximum allowed file size of: %s', 'buddypress' ), size_format( $this->original_max_filesize ) ), 189 3 => __( 'The uploaded file was only partially uploaded.', 'buddypress' ), 190 4 => __( 'No file was uploaded.', 'buddypress' ), 191 5 => '', 192 6 => __( 'Missing a temporary folder.', 'buddypress' ), 193 7 => __( 'Failed to write file to disk.', 'buddypress' ), 194 8 => __( 'File upload stopped by extension.', 'buddypress' ), 195 ), (array) $param ); 196 } 197 198 /** 199 * Include the WordPress core needed files 200 * 201 * @since BuddyPress (2.3.0) 202 */ 203 private function includes() { 204 foreach ( $this->required_wp_files as $wp_file ) { 205 if ( ! file_exists( ABSPATH . "/wp-admin/includes/{$wp_file}.php" ) ) { 206 continue; 207 } 208 209 require_once( ABSPATH . "/wp-admin/includes/{$wp_file}.php" ); 210 } 211 } 212 213 /** 214 * Upload the attachment 215 * 216 * @since BuddyPress (2.3.0) 217 * 218 * @param array $file The appropriate entry the from $_FILES superglobal. 219 * @param string $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional). 220 * @uses wp_handle_upload() 221 * @uses apply_filters() Call 'bp_attachment_upload_overrides' to include specific upload overrides 222 * 223 * @return array On success, returns an associative array of file attributes. 224 * On failure, returns an array containing the error message 225 * (eg: array( 'error' => $message ) ) 226 */ 227 public function upload( $file, $upload_dir_filter = '' ) { 228 if ( ! empty( $this->action ) && method_exists( $this, 'validate_upload' ) ) { 229 /** 230 * Add custom rules before enabling the file upload 231 */ 232 add_filter( "{$this->action}_prefilter", array( $this, 'validate_upload' ), 10, 1 ); 233 } 234 235 /** 236 * If you need to add some overrides we haven't thought of 237 * 238 * @var array $overrides the wp_handle_upload overrides 239 */ 240 $overrides = apply_filters( 'bp_attachment_upload_overrides', array( 241 'action' => $this->action, 242 'upload_error_strings' => $this->upload_error_strings, 243 'test_form' => false 244 ) ); 245 246 $this->includes(); 247 248 /** 249 * If the $base_dir was set when constructing the class, 250 * and no specific filter has been requested, use a default 251 * filter to create the specific $base dir 252 * @see BP_Attachment->upload_dir_filter() 253 */ 254 if ( empty( $upload_dir_filter ) && ! empty( $this->base_dir ) ) { 255 $upload_dir_filter = array( $this, 'upload_dir_filter' ); 256 } 257 258 // Make sure the file will be uploaded in the attachment directory 259 add_filter( 'upload_dir', $upload_dir_filter, 10, 0 ); 260 261 // Upload the attachment 262 $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides ); 263 264 // Restore WordPress Uploads data 265 remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 ); 266 267 // Finally return the uploaded file or the error 268 return $this->attachment; 269 } 270 271 /** 272 * Default filter to save the attachments 273 * 274 * @since BuddyPress (2.3.0) 275 * 276 * @uses wp_mkdir_p() 277 * @uses do_action() call 'bp_attachment_base_upload_dir_created' to run specific action once the 278 * $base_dir is created (eg: add an .htaccess file) 279 * @uses apply_filters() call 'bp_attachment_upload_dir' to eventually override the upload location 280 * regarding to context 281 * @return array the upload directory data 282 */ 283 public function upload_dir_filter() { 284 285 /** 286 * Filters the component's upload directory. 287 * 288 * @since BuddyPress (2.3.0) 289 * 290 * @param array $value Array containing the path, URL, and other helpful settings. 291 */ 292 return apply_filters( 'bp_attachment_upload_dir', array( 293 'path' => $this->upload_path, 294 'url' => $this->url, 295 'subdir' => false, 296 'basedir' => $this->upload_path, 297 'baseurl' => $this->url, 298 'error' => false 299 ) ); 300 } 301 302 public function create_dir() { 303 304 // Bail if we are allowing WordPress to make directories for us 305 if ( empty( $this->create_dir ) ) { 306 return; 307 } 308 309 // Check if upload path already exists 310 if ( ! file_exists( $this->upload_path ) ) { 311 312 // If path does not exist, attempt to create it 313 if ( ! wp_mkdir_p( $this->upload_path ) ) { 314 return false; 315 } 316 317 /** 318 * Use this filter if you need to run specific actions 319 * once the directory is created 320 * 321 * @since BuddyPress (2.3.0) 322 * 323 * @var string $component the component id 324 * @var string $base_dir the specific base dir used by the component 325 */ 326 do_action( 'bp_attachment_base_upload_dir_created', $this->component, $this->base_dir ); 327 } 328 } 329 } -
src/bp-groups/bp-groups-functions.php
817 817 $group_id = bp_get_current_group_id(); 818 818 } 819 819 820 $path = bp_core_avatar_upload_path() . '/group-avatars/' . $group_id; 821 $newbdir = $path; 822 823 if ( !file_exists( $path ) ) 824 @wp_mkdir_p( $path ); 825 826 $newurl = bp_core_avatar_url() . '/group-avatars/' . $group_id; 820 $directory = 'group-avatars'; 821 $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $group_id; 822 $newbdir = $path; 823 $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $group_id; 827 824 $newburl = $newurl; 828 $newsubdir = '/ group-avatars/' . $group_id;825 $newsubdir = '/' . $directory . '/' . $group_id; 829 826 830 827 /** 831 828 * Filters the avatar upload directory path for a given group. … … 834 831 * 835 832 * @param array $value Array of parts related to the groups avatar upload directory. 836 833 */ 837 return apply_filters( 'groups_avatar_upload_dir', array( 'path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false ) ); 834 return apply_filters( 'groups_avatar_upload_dir', array( 835 'path' => $path, 836 'url' => $newurl, 837 'subdir' => $newsubdir, 838 'basedir' => $newbdir, 839 'baseurl' => $newburl, 840 'error' => false 841 ) ); 838 842 } 839 843 840 844 /** Group Member Status Checks ************************************************/ -
src/bp-members/bp-members-functions.php
2159 2159 return false; 2160 2160 } 2161 2161 2162 $path = bp_core_avatar_upload_path() . '/avatars/signups/' . $bp->signup->avatar_dir; 2163 $newbdir = $path; 2162 $directory = 'avatars/signups'; 2163 $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $bp->signup->avatar_dir; 2164 $newbdir = $path; 2165 $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $bp->signup->avatar_dir; 2166 $newburl = $newurl; 2167 $newsubdir = '/' . $directory . '/' . $bp->signup->avatar_dir; 2164 2168 2165 if ( ! file_exists( $path ) ) {2166 @wp_mkdir_p( $path );2167 }2168 2169 $newurl = bp_core_avatar_url() . '/avatars/signups/' . $bp->signup->avatar_dir;2170 $newburl = $newurl;2171 $newsubdir = '/avatars/signups/' . $bp->signup->avatar_dir;2172 2173 2169 /** 2174 2170 * Filters the avatar storage directory for use during registration. 2175 2171 * … … 2183 2179 'subdir' => $newsubdir, 2184 2180 'basedir' => $newbdir, 2185 2181 'baseurl' => $newburl, 2186 'error' => false2182 'error' => false 2187 2183 ) ); 2188 2184 } 2189 2185 -
src/bp-xprofile/bp-xprofile-functions.php
680 680 $directory = 'avatars'; 681 681 } 682 682 683 $path = bp_core_avatar_upload_path() . '/' . $directory. '/' . $user_id; 684 $newbdir = $path; 685 686 if ( ! file_exists( $path ) ) { 687 @wp_mkdir_p( $path ); 688 } 689 683 $path = bp_core_avatar_upload_path() . '/' . $directory. '/' . $user_id; 684 $newbdir = $path; 690 685 $newurl = bp_core_avatar_url() . '/' . $directory. '/' . $user_id; 691 686 $newburl = $newurl; 692 687 $newsubdir = '/' . $directory. '/' . $user_id; -
tests/phpunit/testcases/core/avatars.php
35 35 } 36 36 37 37 /** 38 * @group bp_core_set_avatar_globals 39 */ 40 public function test_bp_core_set_avatar_globals() { 41 $bp = buddypress(); 42 43 $bp_avatar = $bp->avatar; 44 $bp->avatar = new stdClass; 45 46 bp_core_set_avatar_globals(); 47 48 $expected = array( 49 'thumb_widh' => BP_AVATAR_THUMB_WIDTH, 50 'thumb_height' => BP_AVATAR_THUMB_HEIGHT, 51 'full_width' => BP_AVATAR_FULL_WIDTH, 52 'full_height' => BP_AVATAR_FULL_HEIGHT, 53 'original_max_width' => BP_AVATAR_ORIGINAL_MAX_WIDTH, 54 'original_max_filesize' => BP_AVATAR_ORIGINAL_MAX_FILESIZE, 55 'thumb_default' => bp_core_avatar_default_thumb(), 56 'full_default' => bp_core_avatar_default(), 57 'upload_path' => bp_core_avatar_upload_path(), 58 'url' => bp_core_avatar_url(), 59 'show_avatars' => (bool) BP_SHOW_AVATARS, 60 ); 61 62 $tested = array( 63 'thumb_widh' => $bp->avatar->thumb->width, 64 'thumb_height' => $bp->avatar->thumb->height, 65 'full_width' => $bp->avatar->full->width, 66 'full_height' => $bp->avatar->full->height, 67 'original_max_width' => $bp->avatar->original_max_width, 68 'original_max_filesize' => $bp->avatar->original_max_filesize, 69 'thumb_default' => $bp->avatar->thumb->default, 70 'full_default' => $bp->avatar->full->default, 71 'upload_path' => $bp->avatar->upload_path, 72 'url' => $bp->avatar->url, 73 'show_avatars' => $bp->avatar->show_avatars, 74 ); 75 76 $this->assertSame( $expected, $tested ); 77 78 // reset $bp->avatar 79 $bp->avatar = $bp_avatar; 80 } 81 82 /** 38 83 * @ticket BP4948 39 84 */ 40 85 function test_avatars_on_non_root_blog() {