Skip to:
Content

BuddyPress.org

Changeset 10194


Ignore:
Timestamp:
10/06/2015 11:53:33 PM (10 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

Location:
trunk
Files:
4 edited

Legend:

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

    r10178 r10194  
    9494     * @since 2.4.0
    9595     *
     96     * @param  array $upload_dir The original Uploads dir.
    9697     * @return array upload data (path, url, basedir...)
    9798     */
    98     public function upload_dir_filter() {
     99    public function upload_dir_filter( $upload_dir = array() ) {
    99100        // Default values are for profiles
    100101        $object_id = bp_displayed_user_id();
     
    115116        $subdir  = '/' . $object_directory . '/' . $object_id . '/cover-image';
    116117
    117         return apply_filters( 'bp_attachments_cover_image_upload_datas', array(
     118        /**
     119         * Filters the cover image upload directory.
     120         *
     121         * @since 2.4.0
     122         *
     123         * @param array $value       Array containing the path, URL, and other helpful settings.
     124         * @param array $upload_dir The original Uploads dir.
     125         */
     126        return apply_filters( 'bp_attachments_cover_image_upload_dir', array(
    118127            'path'    => $this->upload_path . $subdir,
    119128            'url'     => $this->url . $subdir,
     
    122131            'baseurl' => $this->url,
    123132            'error'   => false
    124         ) );
     133        ), $upload_dir );
    125134    }
    126135
  • 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
  • trunk/tests/phpunit/assets/attachment-extensions.php

    r9831 r10194  
    88        return parent::__construct( $args );
    99    }
     10
     11    public function upload_dir_filter( $upload_dir = array() ) {
     12        $this->original_upload_dir = $upload_dir;
     13
     14        return parent::upload_dir_filter( $upload_dir );
     15    }
    1016}
  • trunk/tests/phpunit/testcases/core/class-bp-attachment.php

    r10178 r10194  
    1313    public function setUp() {
    1414        parent::setUp();
    15         add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    16         add_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
     15        add_filter( 'bp_attachment_upload_overrides',        array( $this, 'filter_overrides' ),       10, 1 );
     16        add_filter( 'upload_dir',                            array( $this, 'filter_upload_dir' ),      20, 1 );
     17        add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 );
    1718        $this->upload_results = array();
    1819        $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
     20        $this->original_upload_dir = array();
    1921    }
    2022
    2123    public function tearDown() {
    2224        parent::tearDown();
    23         remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    24         remove_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
     25        remove_filter( 'bp_attachment_upload_overrides',     array( $this, 'filter_overrides' ),       10, 1 );
     26        remove_filter( 'upload_dir',                         array( $this, 'filter_upload_dir' ),      20, 1 );
     27        add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 );
    2528        $this->upload_results = array();
    2629        $this->image_file = '';
     30        $this->original_upload_dir = array();
    2731    }
    2832
     
    4448
    4549        return $upload_dir;
     50    }
     51
     52    public function filter_cover_image_dir( $cover_dir, $upload_dir ) {
     53        $this->original_upload_dir = $upload_dir;
     54
     55        return $cover_dir;
    4656    }
    4757
     
    454464        $this->assertTrue( 3 === $image_data['meta']['orientation'] );
    455465    }
     466
     467    /**
     468     * @group upload
     469     * @group cover_images
     470     */
     471    public function test_bp_attachment_upload_dir_filter_arg() {
     472        $reset_files = $_FILES;
     473        $reset_post = $_POST;
     474
     475        $attachment_class = new BPTest_Attachment_Extension( array(
     476            'action'                 => 'attachment_action',
     477            'file_input'             => 'attachment_file_input',
     478            'base_dir'               => 'attachment_base_dir',
     479            'upload_dir_filter_args' => 1,
     480        ) );
     481
     482        $_POST['action'] = $attachment_class->action;
     483        $_FILES[ $attachment_class->file_input ] = array(
     484            'tmp_name' => $this->image_file,
     485            'name'     => 'mystery-man.jpg',
     486            'type'     => 'image/jpeg',
     487            'error'    => 0,
     488            'size'     => filesize( $this->image_file ),
     489        );
     490
     491        // Simulate an upload
     492        $attachment_class->upload( $_FILES );
     493
     494        // Remove the filter used to fake uploads
     495        remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 );
     496
     497        $this->assertSame( $attachment_class->original_upload_dir, wp_upload_dir() );
     498
     499        // Restore the filter used to fake uploads
     500        add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 );
     501
     502        $this->assertTrue( 1 === $attachment_class->upload_dir_filter_args );
     503
     504        $cover_image_class = new BP_Attachment_Cover_Image();
     505
     506        // Simulate an upload
     507        $cover_image_class->upload( $_FILES );
     508
     509        // Should be empty
     510        $this->assertEmpty( $this->original_upload_dir );
     511
     512        $this->assertTrue( 0 === $cover_image_class->upload_dir_filter_args );
     513
     514        $_FILES = $reset_files;
     515        $_POST = $reset_post;
     516    }
    456517}
Note: See TracChangeset for help on using the changeset viewer.