diff --git src/bp-core/bp-core-attachments.php src/bp-core/bp-core-attachments.php
index eeee64bd6..1f7255bf0 100644
|
|
|
function bp_attachments_get_max_upload_file_size( $type = '' ) {
|
| 156 | 156 | * Get allowed types for any attachment. |
| 157 | 157 | * |
| 158 | 158 | * @since 2.4.0 |
| | 159 | * @since 11.0.0 Adds the support for .webp images to Avatars and Cover images. |
| 159 | 160 | * |
| 160 | 161 | * @param string $type The extension types to get. |
| 161 | 162 | * Default: 'avatar'. |
| … |
… |
function bp_attachments_get_max_upload_file_size( $type = '' ) {
|
| 163 | 164 | */ |
| 164 | 165 | function bp_attachments_get_allowed_types( $type = 'avatar' ) { |
| 165 | 166 | // Defaults to BuddyPress supported image extensions. |
| 166 | | $exts = array( 'jpeg', 'gif', 'png' ); |
| 167 | | $wp_exts = wp_get_ext_types(); |
| | 167 | $exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); |
| | 168 | if ( bp_is_running_wp( '5.8.0', '>=' ) ) { |
| | 169 | $exts[] = 'webp'; |
| | 170 | } |
| 168 | 171 | |
| 169 | | /** |
| 170 | | * It's not a BuddyPress feature, get the allowed extensions |
| 171 | | * matching the $type requested. |
| 172 | | */ |
| 173 | | if ( 'avatar' !== $type && 'cover_image' !== $type ) { |
| | 172 | // Avatar and cover image are images. |
| | 173 | if ( 'image' !== $type && 'avatar' !== $type && 'cover_image' !== $type ) { |
| 174 | 174 | // Reset the default exts. |
| 175 | | $exts = array(); |
| | 175 | $exts = array(); |
| | 176 | $wp_exts = wp_get_ext_types(); |
| 176 | 177 | |
| 177 | 178 | if ( 'video' === $type ) { |
| 178 | 179 | $exts = wp_get_video_extensions(); |
| … |
… |
function bp_attachments_get_plupload_l10n() {
|
| 689 | 690 | 'dismiss' => __( 'Dismiss', 'buddypress' ), |
| 690 | 691 | 'crunching' => __( 'Crunching…', 'buddypress' ), |
| 691 | 692 | 'unique_file_warning' => __( 'Make sure to upload a unique file', 'buddypress' ), |
| | 693 | 'noneditable_image' => __( 'This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading.', 'buddypress' ), |
| 692 | 694 | |
| 693 | 695 | /* translators: %s: File name. */ |
| 694 | 696 | 'error_uploading' => __( '“%s” has failed to upload.', 'buddypress' ), |
| … |
… |
function bp_attachments_enqueue_scripts( $class = '' ) {
|
| 779 | 781 | $defaults['filters']['max_file_size'] = $args['max_file_size'] . 'b'; |
| 780 | 782 | } |
| 781 | 783 | |
| | 784 | if ( isset( $args['mime_types'] ) && $args['mime_types'] ) { |
| | 785 | $defaults['filters']['mime_types'] = array( array( 'extensions' => $args['mime_types'] ) ); |
| | 786 | } |
| | 787 | |
| | 788 | // Check if WebP images can be edited. |
| | 789 | if ( bp_is_running_wp( '5.8.0', '>=' ) && ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
| | 790 | $defaults['webp_upload_error'] = true; |
| | 791 | } |
| | 792 | |
| 782 | 793 | // Specific to BuddyPress Avatars. |
| 783 | 794 | if ( 'bp_avatar_upload' === $defaults['multipart_params']['action'] ) { |
| 784 | 795 | |
diff --git src/bp-core/classes/class-bp-attachment-avatar.php src/bp-core/classes/class-bp-attachment-avatar.php
index 21bfedd94..0c6d76569 100644
|
|
|
class BP_Attachment_Avatar extends BP_Attachment {
|
| 399 | 399 | // Get default script data. |
| 400 | 400 | $script_data = parent::script_data(); |
| 401 | 401 | |
| | 402 | // Adds the list of supported image types. |
| | 403 | $script_data['mime_types'] = implode( ',', bp_core_get_allowed_avatar_types() ); |
| | 404 | |
| 402 | 405 | // Defaults to Avatar Backbone script. |
| 403 | 406 | $js_scripts = array( 'bp-avatar' ); |
| 404 | 407 | |
diff --git src/bp-core/classes/class-bp-attachment-cover-image.php src/bp-core/classes/class-bp-attachment-cover-image.php
index 14a5a2ef8..8fc50113a 100644
|
|
|
class BP_Attachment_Cover_Image extends BP_Attachment {
|
| 196 | 196 | // Get default script data. |
| 197 | 197 | $script_data = parent::script_data(); |
| 198 | 198 | |
| | 199 | // Adds the list of supported image types. |
| | 200 | $script_data['mime_types'] = implode( ',', bp_attachments_get_allowed_types( 'cover_image' ) ); |
| | 201 | |
| 199 | 202 | if ( bp_is_user() ) { |
| 200 | 203 | $item_id = bp_displayed_user_id(); |
| 201 | 204 | |
diff --git src/bp-core/classes/class-bp-attachment.php src/bp-core/classes/class-bp-attachment.php
index 17bb3c553..6e92bab95 100644
|
|
|
abstract class BP_Attachment {
|
| 493 | 493 | $check_types['dst_file'] = array( 'file' => $r['dst_file'], 'error' => _x( 'destination file', 'Attachment destination file', 'buddypress' ) ); |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | | /** |
| 497 | | * WordPress image supported types. |
| 498 | | * @see wp_attachment_is() |
| 499 | | */ |
| 500 | | $supported_image_types = array( |
| 501 | | 'jpg' => 1, |
| 502 | | 'jpeg' => 1, |
| 503 | | 'jpe' => 1, |
| 504 | | 'gif' => 1, |
| 505 | | 'png' => 1, |
| 506 | | ); |
| | 496 | // Set supported image types. |
| | 497 | $supported_image_types = array_fill_keys( bp_attachments_get_allowed_types( 'image' ), 1 ); |
| 507 | 498 | |
| 508 | 499 | foreach ( $check_types as $file ) { |
| 509 | 500 | $is_image = wp_check_filetype( $file['file'] ); |
diff --git src/bp-core/js/bp-plupload.js src/bp-core/js/bp-plupload.js
index 3416de196..e81e7bb57 100644
|
|
|
window.bp = window.bp || {};
|
| 162 | 162 | return; |
| 163 | 163 | } |
| 164 | 164 | |
| | 165 | if ( file.type === 'image/webp' && uploader.settings.webp_upload_error ) { |
| | 166 | // Disallow uploading of WebP images if the server cannot edit them. |
| | 167 | $( self ).trigger( 'bp-uploader-warning', self.strings.noneditable_image ); |
| | 168 | uploader.removeFile( file ); |
| | 169 | return; |
| | 170 | } |
| | 171 | |
| 165 | 172 | if ( max > hundredmb && file.size > hundredmb && uploader.runtime !== 'html5' ) { |
| 166 | 173 | _this.uploadSizeError( uploader, file, true ); |
| 167 | 174 | } else { |
diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php
index 98ecf0cfe..6d5195ada 100644
|
|
|
class BP_Tests_Avatars extends BP_UnitTestCase {
|
| 257 | 257 | public function test_bp_core_get_allowed_avatar_types_filter() { |
| 258 | 258 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 259 | 259 | |
| 260 | | $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| | 260 | $this->assertEquals( array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ), bp_core_get_allowed_avatar_types() ); |
| 261 | 261 | |
| 262 | 262 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 263 | 263 | |
| 264 | 264 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 265 | 265 | |
| 266 | | $this->assertEquals( array( 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| | 266 | $this->assertEquals( array( 'jpeg', 'jpe', 'gif', 'png', 'webp' ), bp_core_get_allowed_avatar_types() ); |
| 267 | 267 | |
| 268 | 268 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 269 | 269 | |
| 270 | 270 | add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 271 | 271 | |
| 272 | | $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() ); |
| | 272 | $this->assertEquals( array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ), bp_core_get_allowed_avatar_types() ); |
| 273 | 273 | |
| 274 | 274 | remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 275 | 275 | } |
| … |
… |
class BP_Tests_Avatars extends BP_UnitTestCase {
|
| 281 | 281 | public function test_bp_core_get_allowed_avatar_mimes() { |
| 282 | 282 | $mimes = bp_core_get_allowed_avatar_mimes(); |
| 283 | 283 | |
| 284 | | $this->assertEqualSets( array( 'jpeg', 'gif', 'png', 'jpg' ), array_keys( $mimes ) ); |
| 285 | | $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( $mimes ) ); |
| | 284 | $this->assertEqualSets( array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ), array_keys( $mimes ) ); |
| | 285 | $this->assertEqualSets( array( 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/webp' ), array_values( $mimes ) ); |
| 286 | 286 | |
| 287 | 287 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 288 | 288 | |
| 289 | | $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| | 289 | $this->assertEqualSets( array( 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/webp' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 290 | 290 | |
| 291 | 291 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) ); |
| 292 | 292 | |
| 293 | 293 | add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 294 | 294 | |
| 295 | | $this->assertEqualSets( array( 'image/gif', 'image/png' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| | 295 | $this->assertEqualSets( array( 'image/jpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/webp', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 296 | 296 | |
| 297 | 297 | remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) ); |
| 298 | 298 | |
| 299 | 299 | add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 300 | 300 | |
| 301 | | $this->assertEqualSets( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| | 301 | $this->assertEqualSets( array( 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/webp' ), array_values( bp_core_get_allowed_avatar_mimes() ) ); |
| 302 | 302 | |
| 303 | 303 | remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' ); |
| 304 | 304 | } |
diff --git tests/phpunit/testcases/core/functions.php tests/phpunit/testcases/core/functions.php
index 144b61be9..4eaa053c8 100644
|
|
|
class BP_Tests_Core_Functions extends BP_UnitTestCase {
|
| 784 | 784 | * @group bp_attachments |
| 785 | 785 | */ |
| 786 | 786 | public function test_bp_attachments_get_allowed_types() { |
| 787 | | $supported = array( 'jpeg', 'gif', 'png' ); |
| | 787 | $supported = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ); |
| 788 | 788 | |
| 789 | 789 | $avatar = bp_attachments_get_allowed_types( 'avatar' ); |
| 790 | 790 | $this->assertSame( $supported, $avatar ); |