Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/06/2015 11:53:33 PM (11 years ago)
Author:
imath
Message:

Allow class extending the BP_Attachment class to get the original WordPress upload dir within their upload_dir_filter() method.

To do so, you simply need to add the argument $upload_dir_filter_args to the array you use inside your constructor and set it to 1.

Props rittesh.patel

Fixes #6591

File:
1 edited

Legend:

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

    r10178 r10194  
    3535         */
    3636        protected $default_args = array(
    37                 'original_max_filesize' => 0,
    38                 'allowed_mime_types'    => array(),
    39                 'base_dir'              => '',
    40                 'action'                => '',
    41                 'file_input'            => '',
    42                 'upload_error_strings'  => array(),
    43                 'required_wp_files'     => array( 'file' ),
     37                'original_max_filesize'  => 0,
     38                'allowed_mime_types'     => array(),
     39                'base_dir'               => '',
     40                'action'                 => '',
     41                'file_input'             => '',
     42                'upload_error_strings'   => array(),
     43                'required_wp_files'      => array( 'file' ),
     44                'upload_dir_filter_args' => 0,
    4445        );
    4546
    … …  
    4849         *
    4950         * @since 2.3.0
     51         * @since 2.4.0 Add the $upload_dir_filter_args argument to the $arguments array
    5052         *
    5153         * @param array|string $args {
    52          *     @type int    $original_max_filesize Maximum file size in kilobytes. Defaults to php.ini settings.
    53          *     @type array  $allowed_mime_types    List of allowed file extensions (eg: array( 'jpg', 'gif', 'png' ) ).
    54          *                                         Defaults to WordPress allowed mime types.
    55          *     @type string $base_dir              Component's upload base directory. Defaults to WordPress 'uploads'.
    56          *     @type string $action                The upload action used when uploading a file, $_POST['action'] must be set
    57          *                                         and its value must equal $action {@link wp_handle_upload()} (required).
    58          *     @type string $file_input            The name attribute used in the file input. (required).
    59          *     @type array  $upload_error_strings  A list of specific error messages (optional).
    60          *     @type array  $required_wp_files     The list of required WordPress core files. Default: array( 'file' ).
     54         *     @type int    $original_max_filesize  Maximum file size in kilobytes. Defaults to php.ini settings.
     55         *     @type array  $allowed_mime_types     List of allowed file extensions (eg: array( 'jpg', 'gif', 'png' ) ).
     56         *                                          Defaults to WordPress allowed mime types.
     57         *     @type string $base_dir               Component's upload base directory. Defaults to WordPress 'uploads'.
     58         *     @type string $action                 The upload action used when uploading a file, $_POST['action'] must be set
     59         *                                          and its value must equal $action {@link wp_handle_upload()} (required).
     60         *     @type string $file_input             The name attribute used in the file input. (required).
     61         *     @type array  $upload_error_strings   A list of specific error messages (optional).
     62         *     @type array  $required_wp_files      The list of required WordPress core files. Default: array( 'file' ).
     63         *     @type int    $upload_dir_filter_args 1 to receive the original Upload dir array in the Upload dir filter, 0 otherwise.
     64         *                                          Defaults to 0 (optional).
    6165         * }
    6266         * @uses  sanitize_key()
    … …  
    9195                        } elseif ( 'base_dir' === $key ) {
    9296                                $this->{$key} = sanitize_title( $param );
     97
     98                        // Sanitize the upload dir filter arg to pass
     99                        } elseif ( 'upload_dir_filter_args' === $key ) {
     100                                $this->{$key} = (int) $param;
    93101
    94102                        // Action & File input are already set and sanitized
    … …  
    269277                // Make sure the file will be uploaded in the attachment directory
    270278                if ( ! empty( $upload_dir_filter ) ) {
    271                         add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     279                        add_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args );
    272280                }
    273281
    … …  
    277285                // Restore WordPress Uploads data
    278286                if ( ! empty( $upload_dir_filter ) ) {
    279                         remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
     287                        remove_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args );
    280288                }
    281289
    … …  
    346354         *
    347355         * @since 2.3.0
     356         * @since 2.4.0 Add the $upload_dir parameter to the method
    348357         *
    349358         * @uses apply_filters() call 'bp_attachment_upload_dir' to eventually override the upload location
    350359         *       regarding to context
    351360         *
     361         * @param  array $upload_dir The original Uploads dir.
    352362         * @return array The upload directory data.
    353363         */
    354         public function upload_dir_filter() {
     364        public function upload_dir_filter( $upload_dir = array() ) {
    355365
    356366                /**
    … …  
    358368                 *
    359369                 * @since 2.3.0
     370                 * @since 2.4.0 Include the original Upload directory as the second parameter of the filter.
    360371                 *
    361                  * @param array $value Array containing the path, URL, and other helpful settings.
     372                 * @param array $value          Array containing the path, URL, and other helpful settings.
     373                 * @param array $upload_dir     The original Uploads dir.
    362374                 */
    363375                return apply_filters( 'bp_attachment_upload_dir', array(
    … …  
    368380                        'baseurl' => $this->url,
    369381                        'error'   => false
    370                 ) );
     382                ), $upload_dir );
    371383        }
    372384
Note: See TracChangeset for help on using the changeset viewer.