Skip to:
Content

BuddyPress.org


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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.