Skip to:
Content

BuddyPress.org

Changeset 13315


Ignore:
Timestamp:
08/13/2022 12:16:46 PM (23 months ago)
Author:
imath
Message:

Media: add support for the .webp image file type

NB: this support will only be available when the community site uses WP >= 5.8.

Props dcavins

Fixes #8643

Location:
trunk
Files:
7 edited

Legend:

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

    r13263 r13315  
    157157 *
    158158 * @since 2.4.0
     159 * @since 11.0.0 Adds the support for .webp images to Avatars and Cover images.
    159160 *
    160161 * @param string $type The extension types to get.
     
    164165function bp_attachments_get_allowed_types( $type = 'avatar' ) {
    165166    // Defaults to BuddyPress supported image extensions.
    166     $exts    = array( 'jpeg', 'gif', 'png' );
    167     $wp_exts = wp_get_ext_types();
    168 
    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 ) {
     167    $exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
     168    if ( bp_is_running_wp( '5.8.0', '>=' ) ) {
     169        $exts[] = 'webp';
     170    }
     171
     172    // Avatar and cover image are images.
     173    if ( 'image' !== $type && 'avatar' !== $type && 'cover_image' !== $type ) {
    174174        // Reset the default exts.
    175         $exts = array();
     175        $exts    = array();
     176        $wp_exts = wp_get_ext_types();
    176177
    177178        if ( 'video' === $type ) {
     
    690691            'crunching'                 => __( 'Crunching…', 'buddypress' ),
    691692            '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' ),
    692694
    693695            /* translators: %s: File name. */
     
    778780    if ( ! empty( $args['max_file_size'] ) ) {
    779781        $defaults['filters']['max_file_size'] = $args['max_file_size'] . 'b';
     782    }
     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;
    780791    }
    781792
  • trunk/src/bp-core/classes/class-bp-attachment-avatar.php

    r13215 r13315  
    399399        // Get default script data.
    400400        $script_data = parent::script_data();
     401
     402        // Adds the list of supported image types.
     403        $script_data['mime_types'] = implode( ',', bp_core_get_allowed_avatar_types() );
    401404
    402405        // Defaults to Avatar Backbone script.
  • trunk/src/bp-core/classes/class-bp-attachment-cover-image.php

    r12602 r13315  
    196196        // Get default script data.
    197197        $script_data = parent::script_data();
     198
     199        // Adds the list of supported image types.
     200        $script_data['mime_types'] = implode( ',', bp_attachments_get_allowed_types( 'cover_image' ) );
    198201
    199202        if ( bp_is_user() ) {
  • trunk/src/bp-core/classes/class-bp-attachment.php

    r13175 r13315  
    494494        }
    495495
    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 );
    507498
    508499        foreach ( $check_types as $file ) {
  • trunk/src/bp-core/js/bp-plupload.js

    r13152 r13315  
    160160                // Ignore failed uploads.
    161161                if ( plupload.FAILED === file.status ) {
     162                    return;
     163                }
     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 );
    162169                    return;
    163170                }
  • trunk/tests/phpunit/testcases/core/avatars.php

    r13314 r13315  
    258258        add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
    259259
    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() );
    261261
    262262        remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     
    264264        add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
    265265
    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() );
    267267
    268268        remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     
    270270        add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
    271271
    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() );
    273273
    274274        remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
     
    282282        $mimes = bp_core_get_allowed_avatar_mimes();
    283283
    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 ) );
    286286
    287287        add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
    288288
    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() ) );
    290290
    291291        remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     
    293293        add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
    294294
    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() ) );
    296296
    297297        remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     
    299299        add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
    300300
    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() ) );
    302302
    303303        remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
  • trunk/tests/phpunit/testcases/core/functions.php

    r13314 r13315  
    785785     */
    786786    public function test_bp_attachments_get_allowed_types() {
    787         $supported = array( 'jpeg', 'gif', 'png' );
     787        $supported = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
    788788
    789789        $avatar = bp_attachments_get_allowed_types( 'avatar' );
Note: See TracChangeset for help on using the changeset viewer.