Skip to:
Content

BuddyPress.org

Ticket #6591: 6591.04.patch

File 6591.04.patch, 11.4 KB (added by imath, 5 years ago)
  • src/bp-core/classes/class-bp-attachment-cover-image.php

    diff --git src/bp-core/classes/class-bp-attachment-cover-image.php src/bp-core/classes/class-bp-attachment-cover-image.php
    index ed32cbc..7826fde 100644
    class BP_Attachment_Cover_Image extends BP_Attachment { 
    9393         *
    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();
    101102
    class BP_Attachment_Cover_Image extends BP_Attachment { 
    114115                // Set the subdir
    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,
    120129                        'subdir'  => $subdir,
    121130                        'basedir' => $this->upload_path,
    122131                        'baseurl' => $this->url,
    123132                        'error'   => false
    124                 ) );
     133                ), $upload_dir );
    125134        }
    126135
    127136        /**
  • src/bp-core/classes/class-bp-attachment.php

    diff --git src/bp-core/classes/class-bp-attachment.php src/bp-core/classes/class-bp-attachment.php
    index 7a3143d..27f4b17 100644
    abstract class BP_Attachment { 
    3434         * @var array
    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
    4647        /**
    4748         * Construct Upload parameters.
    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()
    6367         * @uses  wp_max_upload_size()
    abstract class BP_Attachment { 
    9195                        } elseif ( 'base_dir' === $key ) {
    9296                                $this->{$key} = sanitize_title( $param );
    9397
     98                        // Sanitize the upload dir filter arg to pass
     99                        } elseif ( 'upload_dir_filter_args' === $key ) {
     100                                $this->{$key} = (int) $param;
     101
    94102                        // Action & File input are already set and sanitized
    95103                        } elseif ( 'action' !== $key && 'file_input' !== $key ) {
    96104                                $this->{$key} = $param;
    abstract class BP_Attachment { 
    268276
    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
    274282                // Upload the attachment
    abstract class BP_Attachment { 
    276284
    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
    282290                // Remove the pre WordPress 4.0 static filter
    abstract class BP_Attachment { 
    345353         * Default filter to save the attachments.
    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                /**
    357367                 * Filters the component's upload directory.
    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(
    364376                        'path'    => $this->upload_path,
    abstract class BP_Attachment { 
    367379                        'basedir' => $this->upload_path,
    368380                        'baseurl' => $this->url,
    369381                        'error'   => false
    370                 ) );
     382                ), $upload_dir );
    371383        }
    372384
    373385        /**
  • tests/phpunit/assets/attachment-extensions.php

    diff --git tests/phpunit/assets/attachment-extensions.php tests/phpunit/assets/attachment-extensions.php
    index 9981683..512488a 100644
    class BPTest_Attachment_Extension extends BP_Attachment { 
    77        public function __construct( $args = array() ) {
    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}
  • tests/phpunit/testcases/core/class-bp-attachment.php

    diff --git tests/phpunit/testcases/core/class-bp-attachment.php tests/phpunit/testcases/core/class-bp-attachment.php
    index 12f7671..a9f76d9 100644
    class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 
    1212
    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
    2933        public function filter_overrides( $overrides ) {
    class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 
    4549                return $upload_dir;
    4650        }
    4751
     52        public function filter_cover_image_dir( $cover_dir, $upload_dir ) {
     53                $this->original_upload_dir = $upload_dir;
     54
     55                return $cover_dir;
     56        }
     57
    4858        /**
    4959         * To avoid copying files in tests, we're faking a succesfull uploads
    5060         * as soon as all the test_form have been executed in _wp_handle_upload
    class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 
    453463
    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}