Skip to:
Content

BuddyPress.org

Changeset 9874


Ignore:
Timestamp:
05/17/2015 11:14:39 AM (11 years ago)
Author:
imath
Message:

In BP_Attachment->upload(), only filter the WordPress upload_dir if the callback function is set.

This callback function can be passed as the second argument of BP_Attachment->upload(), or dynamically created if the specific $base_dir parameter was provided by the extending class.

About this second case, if the extending class forgets to set the $base_dir parameter or do it on purpose in order to use the WordPress default upload directory, we will not filter the WordPress upload_dir to avoid a warning.

This commit also adds a third argument to BP_Attachment->upload(), in case there is a need to pass a specific time (yyyy/mm) to set the WordPress default upload sub directory.

Fixes #6438

Location:
trunk
Files:
2 edited

Legend:

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

    r9831 r9874  
    197197     * @param  array $file               The appropriate entry the from $_FILES superglobal.
    198198     * @param  string $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional).
     199     * @param  string $time              Optional. Time formatted in 'yyyy/mm'. Default null.
    199200     * @uses   wp_handle_upload()        To upload the file
    200201     * @uses   add_filter()              To temporarly overrides WordPress uploads data
     
    206207     *                                   (eg: array( 'error' => $message ) )
    207208     */
    208     public function upload( $file, $upload_dir_filter = '' ) {
     209    public function upload( $file, $upload_dir_filter = '', $time = null ) {
    209210        /**
    210211         * Upload action and the file input name are required parameters
     
    265266
    266267        // Make sure the file will be uploaded in the attachment directory
    267         add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     268        if ( ! empty( $upload_dir_filter ) ) {
     269            add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     270        }
    268271
    269272        // Upload the attachment
    270         $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides );
     273        $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides, $time );
    271274
    272275        // Restore WordPress Uploads data
    273         remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     276        if ( ! empty( $upload_dir_filter ) ) {
     277            remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     278        }
    274279
    275280        // Remove the pre WordPress 4.0 static filter
  • trunk/tests/phpunit/testcases/core/class-bp-attachment.php

    r9831 r9874  
    1414        parent::setUp();
    1515        add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    16         add_filter( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
    17         add_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
    18         add_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     16        add_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
    1917        $this->upload_results = array();
    2018        $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
     
    2422        parent::tearDown();
    2523        remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    26         remove_filter( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
    27         remove_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
    28         remove_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     24        remove_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
    2925        $this->upload_results = array();
    3026        $this->image_file = '';
     
    176172    /**
    177173     * @group upload
     174     */
     175    public function test_bp_attachment_upload_no_base_dir_specific_time() {
     176        $reset_files = $_FILES;
     177        $reset_post = $_POST;
     178
     179        $attachment_class = new BPTest_Attachment_Extension( array(
     180            'action'                => 'attachment_action',
     181            'file_input'            => 'attachment_file_input',
     182        ) );
     183
     184        $_POST['action'] = $attachment_class->action;
     185        $_FILES[ $attachment_class->file_input ] = array(
     186            'tmp_name' => $this->image_file,
     187            'name'     => 'mystery-man.jpg',
     188            'type'     => 'image/jpeg',
     189            'error'    => 0,
     190            'size'     => filesize( $this->image_file ),
     191        );
     192
     193        $time = '2015/01';
     194
     195        $upload = $attachment_class->upload( $_FILES, '', $time );
     196
     197        // If no base_dir was provided, default WordPress uploads dir should be used.
     198        $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/' . $time . '/mystery-man.jpg' );
     199
     200        // clean up!
     201        $_FILES = $reset_files;
     202        $_POST = $reset_post;
     203    }
     204
     205    /**
     206     * @group upload
    178207     * @group avatar
    179208     */
Note: See TracChangeset for help on using the changeset viewer.