Changeset 52837
- Timestamp:
- 03/10/2022 01:08:19 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/file.php
r52734 r52837 2556 2556 return false; 2557 2557 } 2558 2559 /** 2560 * Wrapper for PHP filesize with filters and casting the result as an integer. 2561 * 2562 * @since 6.0.0 2563 * 2564 * @link https://www.php.net/manual/en/function.filesize.php 2565 * 2566 * @param string $path Path to the file. 2567 * @return int The size of the file in bytes, or 0 in the event of an error. 2568 */ 2569 function wp_filesize( $path ) { 2570 /** 2571 * Filters the result of wp_filesize before the PHP function is run. 2572 * 2573 * @since 6.0.0 2574 * 2575 * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. 2576 * @param string $path Path to the file. 2577 */ 2578 $size = apply_filters( 'pre_wp_filesize', null, $path ); 2579 2580 if ( is_int( $size ) ) { 2581 return $size; 2582 } 2583 2584 $size = (int) @filesize( $path ); 2585 2586 /** 2587 * Filters the size of the file. 2588 * 2589 * @since 6.0.0 2590 * 2591 * @param int $size The result of PHP filesize on the file. 2592 * @param string $path Path to the file. 2593 */ 2594 return (int) apply_filters( 'wp_filesize', $size, $path ); 2595 } -
trunk/src/wp-admin/includes/image.php
r52425 r52837 211 211 $image_meta['original_image'] = wp_basename( $original_file ); 212 212 213 // Add image file size. 214 $image_meta['filesize'] = wp_filesize( $new_file ); 215 213 216 return $image_meta; 214 217 } … … 236 239 // Default image meta. 237 240 $image_meta = array( 238 'width' => $imagesize[0], 239 'height' => $imagesize[1], 240 'file' => _wp_relative_upload_path( $file ), 241 'sizes' => array(), 241 'width' => $imagesize[0], 242 'height' => $imagesize[1], 243 'file' => _wp_relative_upload_path( $file ), 244 'filesize' => wp_filesize( $file ), 245 'sizes' => array(), 242 246 ); 243 247 … … 630 634 unset( $metadata['image']['data'] ); 631 635 636 // Capture file size for cases where it has not been captured yet, such as PDFs. 637 if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) { 638 $metadata['filesize'] = wp_filesize( $file ); 639 } 640 632 641 /** 633 642 * Filters the generated attachment meta data. -
trunk/src/wp-admin/includes/media.php
r52332 r52837 3379 3379 $file_size = $meta['filesize']; 3380 3380 } elseif ( file_exists( $file ) ) { 3381 $file_size = filesize( $file );3381 $file_size = wp_filesize( $file ); 3382 3382 } 3383 3383 -
trunk/src/wp-includes/class-wp-image-editor-gd.php
r52425 r52837 498 498 'height' => $this->size['height'], 499 499 'mime-type' => $mime_type, 500 'filesize' => wp_filesize( $filename ), 500 501 ); 501 502 } -
trunk/src/wp-includes/class-wp-image-editor-imagick.php
r52640 r52837 730 730 'height' => $this->size['height'], 731 731 'mime-type' => $mime_type, 732 'filesize' => wp_filesize( $filename ), 732 733 ); 733 734 } -
trunk/src/wp-includes/media.php
r52797 r52837 4047 4047 $bytes = $meta['filesize']; 4048 4048 } elseif ( file_exists( $attached_file ) ) { 4049 $bytes = filesize( $attached_file );4049 $bytes = wp_filesize( $attached_file ); 4050 4050 } else { 4051 4051 $bytes = ''; -
trunk/src/wp-includes/post.php
r52795 r52837 6539 6539 * 'file', 'width', 'height', and 'mime-type'. 6540 6540 * @type array $image_meta Image metadata. 6541 * @type int $filesize File size of the attachment. 6541 6542 * } 6542 6543 */ -
trunk/tests/phpunit/tests/file.php
r52389 r52837 263 263 return $keys; 264 264 } 265 266 /** 267 * @ticket 49412 268 * @covers ::wp_filesize 269 */ 270 function test_wp_filesize_with_nonexistent_file() { 271 $file = 'nonexistent/file.jpg'; 272 $this->assertEquals( 0, wp_filesize( $file ) ); 273 } 274 275 /** 276 * @ticket 49412 277 * @covers ::wp_filesize 278 */ 279 function test_wp_filesize() { 280 $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; 281 282 $this->assertEquals( filesize( $file ), wp_filesize( $file ) ); 283 284 $filter = function() { 285 return 999; 286 }; 287 288 add_filter( 'wp_filesize', $filter ); 289 290 $this->assertEquals( 999, wp_filesize( $file ) ); 291 292 $pre_filter = function() { 293 return 111; 294 }; 295 296 add_filter( 'pre_wp_filesize', $pre_filter ); 297 298 $this->assertEquals( 111, wp_filesize( $file ) ); 299 } 265 300 } -
trunk/tests/phpunit/tests/image/editorGd.php
r51568 r52837 101 101 'height' => 33, 102 102 'mime-type' => 'image/jpeg', 103 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 103 104 ), 104 105 ); … … 301 302 'height' => 7, 302 303 'mime-type' => 'image/jpeg', 304 'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ), 303 305 ), 304 306 … … 309 311 'height' => 50, 310 312 'mime-type' => 'image/jpeg', 313 'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ), 311 314 ), 312 315 … … 317 320 'height' => 20, 318 321 'mime-type' => 'image/jpeg', 322 'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ), 319 323 ), 320 324 … … 325 329 'height' => 400, 326 330 'mime-type' => 'image/jpeg', 331 'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ), 327 332 ), 328 333 … … 333 338 'height' => 33, 334 339 'mime-type' => 'image/jpeg', 340 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 335 341 ), 336 342 … … 341 347 'height' => 37, 342 348 'mime-type' => 'image/jpeg', 349 'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ), 343 350 ), 344 351 … … 349 356 'height' => 55, 350 357 'mime-type' => 'image/jpeg', 358 'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ), 351 359 ), 352 360 … … 357 365 'height' => 60, 358 366 'mime-type' => 'image/jpeg', 367 'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ), 359 368 ), 360 369 … … 365 374 'height' => 70, 366 375 'mime-type' => 'image/jpeg', 376 'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ), 367 377 ), 368 378 … … 373 383 'height' => 133, 374 384 'mime-type' => 'image/jpeg', 385 'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ), 375 386 ), 376 387 ); -
trunk/tests/phpunit/tests/image/editorImagick.php
r51568 r52837 91 91 'height' => 33, 92 92 'mime-type' => 'image/jpeg', 93 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 93 94 ), 94 95 ); … … 290 291 'height' => 7, 291 292 'mime-type' => 'image/jpeg', 293 'filesize' => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ), 292 294 ), 293 295 … … 298 300 'height' => 50, 299 301 'mime-type' => 'image/jpeg', 302 'filesize' => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ), 300 303 ), 301 304 … … 306 309 'height' => 20, 307 310 'mime-type' => 'image/jpeg', 311 'filesize' => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ), 308 312 ), 309 313 … … 314 318 'height' => 400, 315 319 'mime-type' => 'image/jpeg', 320 'filesize' => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ), 316 321 ), 317 322 … … 322 327 'height' => 33, 323 328 'mime-type' => 'image/jpeg', 329 'filesize' => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ), 324 330 ), 325 331 … … 330 336 'height' => 37, 331 337 'mime-type' => 'image/jpeg', 338 'filesize' => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ), 332 339 ), 333 340 … … 338 345 'height' => 55, 339 346 'mime-type' => 'image/jpeg', 347 'filesize' => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ), 340 348 ), 341 349 … … 346 354 'height' => 60, 347 355 'mime-type' => 'image/jpeg', 356 'filesize' => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ), 348 357 ), 349 358 … … 354 363 'height' => 70, 355 364 'mime-type' => 'image/jpeg', 365 'filesize' => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ), 356 366 ), 357 367 … … 362 372 'height' => 133, 363 373 'mime-type' => 'image/jpeg', 374 'filesize' => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ), 364 375 ), 365 376 ); -
trunk/tests/phpunit/tests/image/functions.php
r52269 r52837 480 480 $this->assertNotEmpty( $attachment_id ); 481 481 482 $temp_dir = get_temp_dir(); 483 484 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 485 482 486 $expected = array( 483 'sizes' => array(487 'sizes' => array( 484 488 'full' => array( 485 489 'file' => 'wordpress-gsoc-flyer-pdf.jpg', … … 487 491 'height' => 1408, 488 492 'mime-type' => 'image/jpeg', 493 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ), 489 494 ), 490 495 'medium' => array( … … 493 498 'height' => 300, 494 499 'mime-type' => 'image/jpeg', 500 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ), 495 501 ), 496 502 'large' => array( … … 499 505 'height' => 1024, 500 506 'mime-type' => 'image/jpeg', 507 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 501 508 ), 502 509 'thumbnail' => array( … … 505 512 'height' => 150, 506 513 'mime-type' => 'image/jpeg', 514 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 507 515 ), 508 516 ), 509 );510 511 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 517 'filesize' => wp_filesize( $test_file ), 518 ); 519 512 520 $this->assertSame( $expected, $metadata ); 513 521 514 522 unlink( $test_file ); 515 $temp_dir = get_temp_dir();516 523 foreach ( $metadata['sizes'] as $size ) { 517 524 unlink( $temp_dir . $size['file'] ); … … 550 557 $this->assertNotEmpty( $attachment_id ); 551 558 559 $temp_dir = get_temp_dir(); 560 561 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 562 552 563 $expected = array( 553 'sizes' => array(564 'sizes' => array( 554 565 'full' => array( 555 566 'file' => 'wordpress-gsoc-flyer-pdf.jpg', … … 557 568 'height' => 1408, 558 569 'mime-type' => 'image/jpeg', 570 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ), 559 571 ), 560 572 'medium' => array( … … 563 575 'height' => 300, 564 576 'mime-type' => 'image/jpeg', 577 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ), 565 578 ), 566 579 'large' => array( … … 569 582 'height' => 1024, 570 583 'mime-type' => 'image/jpeg', 584 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 571 585 ), 572 586 'thumbnail' => array( … … 575 589 'height' => 150, 576 590 'mime-type' => 'image/jpeg', 591 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 577 592 ), 578 593 ), 579 );580 581 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 594 'filesize' => wp_filesize( $test_file ), 595 ); 596 582 597 $this->assertSame( $expected, $metadata ); 583 598 584 599 unlink( $test_file ); 585 600 foreach ( $metadata['sizes'] as $size ) { 586 unlink( get_temp_dir(). $size['file'] );601 unlink( $temp_dir . $size['file'] ); 587 602 } 588 603 } … … 617 632 add_image_size( 'test-size', 100, 100 ); 618 633 add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 ); 634 635 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 636 637 $temp_dir = get_temp_dir(); 619 638 620 639 $expected = array( … … 623 642 'height' => 100, 624 643 'mime-type' => 'image/jpeg', 625 ); 626 627 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 644 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ), 645 ); 646 647 // Different environments produce slightly different filesize results. 648 $this->assertSame( $metadata['sizes']['test-size'], $expected ); 649 628 650 $this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' ); 629 651 $this->assertSame( $expected, $metadata['sizes']['test-size'] ); … … 633 655 634 656 unlink( $test_file ); 635 $temp_dir = get_temp_dir();636 657 foreach ( $metadata['sizes'] as $size ) { 637 658 unlink( $temp_dir . $size['file'] );
Note: See TracChangeset
for help on using the changeset viewer.