Ticket #6591: 6591.04.patch
File 6591.04.patch, 11.4 KB (added by , 9 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 { 93 93 * 94 94 * @since 2.4.0 95 95 * 96 * @param array $upload_dir The original Uploads dir. 96 97 * @return array upload data (path, url, basedir...) 97 98 */ 98 public function upload_dir_filter( ) {99 public function upload_dir_filter( $upload_dir = array() ) { 99 100 // Default values are for profiles 100 101 $object_id = bp_displayed_user_id(); 101 102 … … class BP_Attachment_Cover_Image extends BP_Attachment { 114 115 // Set the subdir 115 116 $subdir = '/' . $object_directory . '/' . $object_id . '/cover-image'; 116 117 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( 118 127 'path' => $this->upload_path . $subdir, 119 128 'url' => $this->url . $subdir, 120 129 'subdir' => $subdir, 121 130 'basedir' => $this->upload_path, 122 131 'baseurl' => $this->url, 123 132 'error' => false 124 ) );133 ), $upload_dir ); 125 134 } 126 135 127 136 /** -
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 { 34 34 * @var array 35 35 */ 36 36 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, 44 45 ); 45 46 46 47 /** 47 48 * Construct Upload parameters. 48 49 * 49 50 * @since 2.3.0 51 * @since 2.4.0 Add the $upload_dir_filter_args argument to the $arguments array 50 52 * 51 53 * @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). 61 65 * } 62 66 * @uses sanitize_key() 63 67 * @uses wp_max_upload_size() … … abstract class BP_Attachment { 91 95 } elseif ( 'base_dir' === $key ) { 92 96 $this->{$key} = sanitize_title( $param ); 93 97 98 // Sanitize the upload dir filter arg to pass 99 } elseif ( 'upload_dir_filter_args' === $key ) { 100 $this->{$key} = (int) $param; 101 94 102 // Action & File input are already set and sanitized 95 103 } elseif ( 'action' !== $key && 'file_input' !== $key ) { 96 104 $this->{$key} = $param; … … abstract class BP_Attachment { 268 276 269 277 // Make sure the file will be uploaded in the attachment directory 270 278 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 ); 272 280 } 273 281 274 282 // Upload the attachment … … abstract class BP_Attachment { 276 284 277 285 // Restore WordPress Uploads data 278 286 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 ); 280 288 } 281 289 282 290 // Remove the pre WordPress 4.0 static filter … … abstract class BP_Attachment { 345 353 * Default filter to save the attachments. 346 354 * 347 355 * @since 2.3.0 356 * @since 2.4.0 Add the $upload_dir parameter to the method 348 357 * 349 358 * @uses apply_filters() call 'bp_attachment_upload_dir' to eventually override the upload location 350 359 * regarding to context 351 360 * 361 * @param array $upload_dir The original Uploads dir. 352 362 * @return array The upload directory data. 353 363 */ 354 public function upload_dir_filter( ) {364 public function upload_dir_filter( $upload_dir = array() ) { 355 365 356 366 /** 357 367 * Filters the component's upload directory. 358 368 * 359 369 * @since 2.3.0 370 * @since 2.4.0 Include the original Upload directory as the second parameter of the filter. 360 371 * 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. 362 374 */ 363 375 return apply_filters( 'bp_attachment_upload_dir', array( 364 376 'path' => $this->upload_path, … … abstract class BP_Attachment { 367 379 'basedir' => $this->upload_path, 368 380 'baseurl' => $this->url, 369 381 'error' => false 370 ) );382 ), $upload_dir ); 371 383 } 372 384 373 385 /** -
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 { 7 7 public function __construct( $args = array() ) { 8 8 return parent::__construct( $args ); 9 9 } 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 } 10 16 } -
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 { 12 12 13 13 public function setUp() { 14 14 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 ); 17 18 $this->upload_results = array(); 18 19 $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg'; 20 $this->original_upload_dir = array(); 19 21 } 20 22 21 23 public function tearDown() { 22 24 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 ); 25 28 $this->upload_results = array(); 26 29 $this->image_file = ''; 30 $this->original_upload_dir = array(); 27 31 } 28 32 29 33 public function filter_overrides( $overrides ) { … … class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 45 49 return $upload_dir; 46 50 } 47 51 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 48 58 /** 49 59 * To avoid copying files in tests, we're faking a succesfull uploads 50 60 * as soon as all the test_form have been executed in _wp_handle_upload … … class BP_Tests_BP_Attachment_TestCases extends BP_UnitTestCase { 453 463 454 464 $this->assertTrue( 3 === $image_data['meta']['orientation'] ); 455 465 } 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 } 456 517 }