Skip to:
Content

BuddyPress.org

Changeset 9831


Ignore:
Timestamp:
05/02/2015 08:55:07 PM (4 years ago)
Author:
imath
Message:

Make sure the BP Attachments API behaves the right way when WordPress < 4.0

Before WordPress 4.0, there was no dynamic filter based on the name of the action posted in the upload form. So we also need to filter wp_handle_upload_prefilter to make sure the BP_Attachments->validate_upload() function will actually validate the uploads for versions of WordPress < 4.0.

Improve the i18n of the avatar type error, as the avatar types can now be filtered in the BP_Attachment_Avatar class.

Add unit tests for the BP Attachement class.

Fixes #6278

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-attachment-avatar.php

    r9819 r9831  
    2929     */
    3030    public function __construct() {
     31        // Allowed avatar types
     32        $allowed_types = bp_core_get_allowed_avatar_types();
     33
    3134        parent::__construct( array(
    3235            'action'                => 'bp_avatar_upload',
     
    3740            'upload_error_strings'  => array(
    3841                9  => sprintf( __( 'That photo is too big. Please upload one smaller than %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ),
    39                 10 => __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ),
     42                10 => sprintf( _n( 'Please upload only this file type: %s.', 'Please upload only these file types: %s.', count( $allowed_types ), 'buddypress' ), self::get_avatar_types( $allowed_types ) ),
    4043            ),
    4144        ) );
     45    }
     46
     47    /**
     48     * Gets the available avatar types
     49     *
     50     * @since BuddyPress (2.3.0)
     51     * @return string comma separated list of allowed avatar types
     52     */
     53    public static function get_avatar_types( $allowed_types = array() ) {
     54        $types = array_map( 'strtoupper', $allowed_types );
     55        $comma = _x( ',', 'avatar types separator', 'buddypress' );
     56        return join( $comma . ' ', $types );
    4257    }
    4358
  • trunk/src/bp-core/classes/class-bp-attachment.php

    r9819 r9831  
    220220        add_filter( "{$this->action}_prefilter", array( $this, 'validate_upload' ), 10, 1 );
    221221
     222        /**
     223         * The above dynamic filter was introduced in WordPress 4.0, as we support WordPress
     224         * back to 3.6, we need to also use the pre 4.0 static filter and remove it after
     225         * the upload was processed.
     226         */
     227        add_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
     228
    222229        // Set Default overrides
    223230        $overrides = array(
     
    265272        // Restore WordPress Uploads data
    266273        remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     274
     275        // Remove the pre WordPress 4.0 static filter
     276        remove_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 );
    267277
    268278        // Finally return the uploaded file or the error
  • trunk/tests/phpunit/includes/testcase.php

    r9819 r9831  
    424424        $wpdb->query( 'COMMIT;' );
    425425    }
     426
     427    /**
     428     * Clean up created directories/files
     429     */
     430    public function rrmdir( $dir ) {
     431        // Make sure we are only removing files/dir from uploads
     432        if ( 0 !== strpos( $dir, bp_core_avatar_upload_path() ) ) {
     433            return;
     434        }
     435
     436        $d = glob( $dir . '/*' );
     437
     438        if ( ! empty( $d ) ) {
     439            foreach ( $d as $file ) {
     440                if ( is_dir( $file ) ) {
     441                    $this->rrmdir( $file );
     442                } else {
     443                    @unlink( $file );
     444                }
     445            }
     446        }
     447
     448        @rmdir( $dir );
     449    }
    426450}
  • trunk/tests/phpunit/testcases/core/avatars.php

    r9819 r9831  
    1515
    1616        $this->rrmdir( bp_core_avatar_upload_path() . '/' . $avatar_dir );
    17     }
    18 
    19     private function rrmdir( $dir ) {
    20         $d = glob( $dir . '/*' );
    21 
    22         if ( empty( $d ) ) {
    23             return;
    24         }
    25 
    26         foreach ( $d as $file ) {
    27             if ( is_dir( $file ) ) {
    28                 $this->rrmdir( $file );
    29             } else {
    30                 @unlink( $file );
    31             }
    32         }
    33 
    34         @rmdir( $dir );
    3517    }
    3618
  • trunk/tests/phpunit/testcases/core/functions.php

    r9819 r9831  
    599599        }
    600600    }
     601
     602    /**
     603     * @group bp_attachments
     604     * @group bp_upload_dir
     605     */
     606    public function test_bp_upload_dir_ms() {
     607        if ( ! is_multisite() ) {
     608            $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' );
     609        }
     610
     611        $expected_upload_dir = wp_upload_dir();
     612
     613        $b = $this->factory->blog->create();
     614
     615        switch_to_blog( $b );
     616
     617        $tested_upload_dir = bp_upload_dir();
     618
     619        restore_current_blog();
     620
     621        $this->assertSame( $expected_upload_dir, $tested_upload_dir );
     622    }
    601623}
Note: See TracChangeset for help on using the changeset viewer.