Changeset 10355 for trunk/src/bp-core/classes/class-bp-attachment.php
- Timestamp:
- 11/15/2015 07:13:42 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-attachment.php
r10194 r10355 50 50 * @since 2.3.0 51 51 * @since 2.4.0 Add the $upload_dir_filter_args argument to the $arguments array 52 * @uses sanitize_key() 53 * @uses wp_max_upload_size() 54 * @uses bp_parse_args() 55 * @uses BP_Attachment->set_upload_error_strings() 56 * @uses BP_Attachment->set_upload_dir() 52 57 * 53 58 * @param array|string $args { … … 64 69 * Defaults to 0 (optional). 65 70 * } 66 * @uses sanitize_key()67 * @uses wp_max_upload_size()68 * @uses bp_parse_args()69 * @uses BP_Attachment->set_upload_error_strings()70 * @uses BP_Attachment->set_upload_dir()71 71 */ 72 72 public function __construct( $args = '' ) { 73 // Upload action and the file input name are required parameters 73 // Upload action and the file input name are required parameters. 74 74 if ( empty( $args['action'] ) || empty( $args['file_input'] ) ) { 75 75 return false; 76 76 } 77 77 78 // Sanitize the action ID and the file input name 78 // Sanitize the action ID and the file input name. 79 79 $this->action = sanitize_key( $args['action'] ); 80 80 $this->file_input = sanitize_key( $args['file_input'] ); … … 92 92 $this->{$key} = $this->set_upload_error_strings( $param ); 93 93 94 // Sanitize the base dir 94 // Sanitize the base dir. 95 95 } elseif ( 'base_dir' === $key ) { 96 96 $this->{$key} = sanitize_title( $param ); 97 97 98 // Sanitize the upload dir filter arg to pass 98 // Sanitize the upload dir filter arg to pass. 99 99 } elseif ( 'upload_dir_filter_args' === $key ) { 100 100 $this->{$key} = (int) $param; 101 101 102 // Action & File input are already set and sanitized 102 // Action & File input are already set and sanitized. 103 103 } elseif ( 'action' !== $key && 'file_input' !== $key ) { 104 104 $this->{$key} = $param; … … 106 106 } 107 107 108 // Set the path/url and base dir for uploads 108 // Set the path/url and base dir for uploads. 109 109 $this->set_upload_dir(); 110 110 } … … 118 118 */ 119 119 public function set_upload_dir() { 120 // Set the directory, path, & url variables 120 // Set the directory, path, & url variables. 121 121 $this->upload_dir = bp_upload_dir(); 122 122 … … 128 128 $this->url = $this->upload_dir['baseurl']; 129 129 130 // Ensure URL is https if SSL is set/forced 130 // Ensure URL is https if SSL is set/forced. 131 131 if ( is_ssl() ) { 132 132 $this->url = str_replace( 'http://', 'https://', $this->url ); … … 142 142 $this->url = trailingslashit( $this->url ) . $this->base_dir; 143 143 144 // Finally create the base dir 144 // Finally create the base dir. 145 145 $this->create_dir(); 146 146 } … … 155 155 * 156 156 * @param array $param A list of error messages to add to BuddyPress core ones. 157 * 158 * @return array the list of upload errors 157 * @return array $upload_errors The list of upload errors. 159 158 */ 160 159 public function set_upload_error_strings( $param = array() ) { … … 204 203 * @since 2.3.0 205 204 * 206 * @param array $file The appropriate entry the from $_FILES superglobal.207 * @param string $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional).208 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null.209 *210 205 * @uses wp_handle_upload() To upload the file 211 206 * @uses add_filter() To temporarly overrides WordPress uploads data … … 213 208 * @uses apply_filters() Call 'bp_attachment_upload_overrides' to include specific upload overrides 214 209 * 215 * @return array On success, returns an associative array of file attributes. 216 * On failure, returns an array containing the error message 217 * (eg: array( 'error' => $message ) ) 210 * @param array $file The appropriate entry the from $_FILES superglobal. 211 * @param string $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional). 212 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 213 * @return array On success, returns an associative array of file attributes. 214 * On failure, returns an array containing the error message 215 * (eg: array( 'error' => $message ) ) 218 216 */ 219 217 public function upload( $file, $upload_dir_filter = '', $time = null ) { … … 238 236 add_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 ); 239 237 240 // Set Default overrides 238 // Set Default overrides. 241 239 $overrides = array( 242 240 'action' => $this->action, … … 275 273 } 276 274 277 // Make sure the file will be uploaded in the attachment directory 275 // Make sure the file will be uploaded in the attachment directory. 278 276 if ( ! empty( $upload_dir_filter ) ) { 279 277 add_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args ); 280 278 } 281 279 282 // Upload the attachment 280 // Upload the attachment. 283 281 $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides, $time ); 284 282 285 // Restore WordPress Uploads data 283 // Restore WordPress Uploads data. 286 284 if ( ! empty( $upload_dir_filter ) ) { 287 285 remove_filter( 'upload_dir', $upload_dir_filter, 10, $this->upload_dir_filter_args ); 288 286 } 289 287 290 // Remove the pre WordPress 4.0 static filter 288 // Remove the pre WordPress 4.0 static filter. 291 289 remove_filter( 'wp_handle_upload_prefilter', array( $this, 'validate_upload' ), 10, 1 ); 292 290 293 // Finally return the uploaded file or the error 291 // Finally return the uploaded file or the error. 294 292 return $this->attachment; 295 293 } … … 300 298 * In case of a multisite, the mime types are already restricted by 301 299 * the 'upload_filetypes' setting. BuddyPress will respect this setting. 300 * 302 301 * @see check_upload_mimes() 303 302 * … … 310 309 $valid_mimes = array(); 311 310 312 // Set the allowed mimes for the upload 311 // Set the allowed mimes for the upload. 313 312 foreach ( (array) $this->allowed_mime_types as $ext ) { 314 313 foreach ( $wp_mimes as $ext_pattern => $mime ) { … … 333 332 * 334 333 * @param array $file The temporary file attributes (before it has been moved). 335 *336 334 * @return array The file. 337 335 */ 338 336 public function validate_upload( $file = array() ) { 339 // Bail if already an error 337 // Bail if already an error. 340 338 if ( ! empty( $file['error'] ) ) { 341 339 return $file; … … 346 344 } 347 345 348 // Return the file 346 // Return the file. 349 347 return $file; 350 348 } … … 394 392 */ 395 393 public function create_dir() { 396 // Bail if no specific base dir is set 394 // Bail if no specific base dir is set. 397 395 if ( empty( $this->base_dir ) ) { 398 396 return false; 399 397 } 400 398 401 // Check if upload path already exists 399 // Check if upload path already exists. 402 400 if ( ! is_dir( $this->upload_path ) ) { 403 401 404 // If path does not exist, attempt to create it 402 // If path does not exist, attempt to create it. 405 403 if ( ! wp_mkdir_p( $this->upload_path ) ) { 406 404 return false; … … 408 406 } 409 407 410 // Directory exists 408 // Directory exists. 411 409 return true; 412 410 } … … 452 450 } 453 451 454 // Check image file pathes 452 // Check image file pathes. 455 453 $path_error = __( 'Cropping the file failed: the file path is not allowed.', 'buddypress' ); 456 454 457 // Make sure it's coming from an uploaded file 455 // Make sure it's coming from an uploaded file. 458 456 if ( false === strpos( $r['original_file'], $this->upload_path ) ) { 459 457 $wp_error->add( 'crop_error', $path_error ); … … 464 462 * If no destination file is provided, WordPress will use a default name 465 463 * and will write the file in the source file's folder. 466 * If a destination file is provided, we need to make sure it's going into uploads 464 * If a destination file is provided, we need to make sure it's going into uploads. 467 465 */ 468 466 if ( ! empty( $r['dst_file'] ) && false === strpos( $r['dst_file'], $this->upload_path ) ) { … … 471 469 } 472 470 473 // Check image file types 471 // Check image file types. 474 472 $check_types = array( 'src_file' => array( 'file' => $r['original_file'], 'error' => _x( 'source file', 'Attachment source file', 'buddypress' ) ) ); 475 473 if ( ! empty( $r['dst_file'] ) ) { … … 478 476 479 477 /** 480 * WordPress image supported types 478 * WordPress image supported types. 481 479 * @see wp_attachment_is() 482 480 */ … … 499 497 } 500 498 501 // Add the image.php to the required WordPress files, if it's not already the case 499 // Add the image.php to the required WordPress files, if it's not already the case. 502 500 $required_files = array_flip( $this->required_wp_files ); 503 501 if ( ! isset( $required_files['image'] ) ) { … … 505 503 } 506 504 507 // Load the files 505 // Load the files. 508 506 $this->includes(); 509 507 510 // Finally crop the image 508 // Finally crop the image. 511 509 return wp_crop_image( $r['original_file'], (int) $r['crop_x'], (int) $r['crop_y'], (int) $r['crop_w'], (int) $r['crop_h'], (int) $r['dst_w'], (int) $r['dst_h'], $r['src_abs'], $r['dst_file'] ); 512 510 } … … 545 543 */ 546 544 public static function get_image_data( $file ) { 547 // Try to get image basic data 545 // Try to get image basic data. 548 546 list( $width, $height, $sourceImageType ) = @getimagesize( $file ); 549 547 … … 553 551 } 554 552 555 // Initialize the image data 553 // Initialize the image data. 556 554 $image_data = array( 557 555 'width' => $width, … … 567 565 } 568 566 569 // Now try to get image's meta data 567 // Now try to get image's meta data. 570 568 $meta = wp_read_image_metadata( $file ); 571 569 572 570 if ( ! empty( $meta ) ) { 573 // Before 4.0 the Orientation wasn't included 571 // Before 4.0 the Orientation wasn't included. 574 572 if ( ! isset( $meta['orientation'] ) && 575 573 is_callable( 'exif_read_data' ) && … … 583 581 } 584 582 585 // Now add the metas to image data 583 // Now add the metas to image data. 586 584 $image_data['meta'] = $meta; 587 585 } … … 602 600 * @since 2.4.0 603 601 * 604 * @param string $attachment_type The attachment type (eg: avatar or cover_image). Required.605 * @param array array$args {602 * @param string $attachment_type The attachment type (eg: avatar or cover_image). Required. 603 * @param array $args { 606 604 * @type string $file Absolute path to the image file (required). 607 605 * @type int $max_w Max width attribute for the editor's resize method (optional). … … 612 610 * @type bool $save Whether to use the editor's save method or not (optional). 613 611 * } 614 *615 612 * @return string|WP_Image_Editor|WP_Error The edited image path or the WP_Image_Editor object in case of success, 616 613 * an WP_Error object otherwise. … … 636 633 } 637 634 638 // Get the image editor 635 // Get the image editor. 639 636 $editor = wp_get_image_editor( $r['file'] ); 640 637 … … 648 645 $rotated = $editor->rotate( $r['rotate'] ); 649 646 650 // Stop in case of error 647 // Stop in case of error. 651 648 if ( is_wp_error( $rotated ) ) { 652 649 return $rotated; … … 657 654 $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] ); 658 655 659 // Stop in case of error 656 // Stop in case of error. 660 657 if ( is_wp_error( $resized ) ) { 661 658 return $resized; … … 663 660 } 664 661 665 // Use the editor save method to get a path to the edited image 662 // Use the editor save method to get a path to the edited image. 666 663 if ( true === $r['save'] ) { 667 664 return $editor->save( $editor->generate_filename() ); 668 665 669 // Need to do some other edit actions or use a specific method to save file 666 // Need to do some other edit actions or use a specific method to save file. 670 667 } else { 671 668 return $editor;
Note: See TracChangeset
for help on using the changeset viewer.