Ticket #39667: 39667.4.diff
File 39667.4.diff, 7.7 KB (added by , 7 years ago) |
---|
-
src/wp-includes/functions.php
283 283 } 284 284 285 285 /** 286 * Convert a filelength to human readable format. 287 * 288 * @since 5.0 289 * 290 * @param string $filelength Duration will be in string format (HH:ii:ss) OR (ii:ss). 291 * @return boolean|string A human readable filelength string, false on failure. 292 */ 293 function human_readable_duration( $filelength = '' ) { 294 // Return false if filelength is empty or not in format. 295 if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) { 296 return false; 297 } 298 299 // Validate filelength format. 300 if ( ! ( (bool) preg_match( '/^(([0-3]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/', $filelength ) ) ) { 301 return false; 302 } 303 304 $human_readable_duration = []; 305 306 // Extract duration. 307 $durations = array_reverse( explode( ':', $filelength ) ); 308 $duration_count = count( $durations ); 309 310 if ( 3 === $duration_count ) { 311 // Three parts: hours, minutes & seconds. 312 list( $second, $minute, $hour ) = $durations; 313 } elseif ( 2 === $duration_count ) { 314 // Two parts: minutes & seconds. 315 list( $second, $minute ) = $durations; 316 } else { 317 return false; 318 } 319 320 // Add the hour part to the string. 321 if ( ! empty( $hour ) && is_numeric( $hour ) ) { 322 /* translators: Time duration in hour or hours. */ 323 $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour ); 324 } 325 326 // Add the minute part to the string. 327 if ( ! empty( $minute ) && is_numeric( $minute ) ) { 328 /* translators: Time duration in minute or minutes. */ 329 $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute ); 330 } 331 332 // Add the second part to the string. 333 if ( ! empty( $second ) && is_numeric( $second ) ) { 334 /* translators: Time duration in second or seconds. */ 335 $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second ); 336 } 337 338 return implode( ', ', $human_readable_duration ); 339 } 340 341 /** 286 342 * Get the week start and end from the datetime or date string from MySQL. 287 343 * 288 344 * @since 0.71 … … 673 729 /** 674 730 * Determines whether the publish date of the current post in the loop is different 675 731 * from the publish date of the previous post in the loop. 676 * 732 * 677 733 * For more information on this and similar theme functions, check out 678 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 734 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 679 735 * Conditional Tags} article in the Theme Developer Handbook. 680 * 736 * 681 737 * @since 0.71 682 738 * 683 739 * @global string $currentday The day of the current post in the loop. … … 1393 1449 * cache, and the database goes away, then you might have problems. 1394 1450 * 1395 1451 * Checks for the 'siteurl' option for whether WordPress is installed. 1396 * 1452 * 1397 1453 * For more information on this and similar theme functions, check out 1398 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1454 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1399 1455 * Conditional Tags} article in the Theme Developer Handbook. 1400 1456 * 1401 1457 * @since 2.1.0 -
src/wp-includes/media-template.php
371 371 <div class="file-size"><strong><?php _e( 'File size:' ); ?></strong> {{ data.filesizeHumanReadable }}</div> 372 372 <# if ( 'image' === data.type && ! data.uploading ) { #> 373 373 <# if ( data.width && data.height ) { #> 374 <div class="dimensions"><strong><?php _e( 'Dimensions:' ); ?></strong> {{ data.width }} × {{ data.height }}</div> 374 <div class="dimensions"><strong><?php _e( 'Dimensions:' ); ?></strong> 375 <?php 376 /* translators: 1: a number of pixels wide, 2: a number of pixels tall. */ 377 printf( __( '%1$s by %2$s pixels' ), '{{ data.width }}', '{{ data.height }}' ); 378 ?> 379 </div> 375 380 <# } #> 376 381 <# } #> 377 382 378 <# if ( data.fileLength ) { #> 379 <div class="file-length"><strong><?php _e( 'Length:' ); ?></strong> {{ data.fileLength }}</div> 383 <# if ( data.fileLength && data.fileLengthHumanReadable ) { #> 384 <div class="file-length"><strong><?php _e( 'Length:' ); ?></strong> 385 <span aria-hidden="true">{{ data.fileLength }}</span> 386 <span class="screen-reader-text">{{ data.fileLengthHumanReadable }}</span> 387 </div> 380 388 <# } #> 381 389 382 390 <# if ( 'audio' === data.type && data.meta.bitrate ) { #> … … 547 555 <div class="file-size">{{ data.filesizeHumanReadable }}</div> 548 556 <# if ( 'image' === data.type && ! data.uploading ) { #> 549 557 <# if ( data.width && data.height ) { #> 550 <div class="dimensions">{{ data.width }} × {{ data.height }}</div> 558 <div class="dimensions"> 559 <?php 560 /* translators: 1: a number of pixels wide, 2: a number of pixels tall. */ 561 printf( __( '%1$s by %2$s pixels' ), '{{ data.width }}', '{{ data.height }}' ); 562 ?> 563 </div> 551 564 <# } #> 552 565 553 566 <# if ( data.can.save && data.sizes ) { #> … … 555 568 <# } #> 556 569 <# } #> 557 570 558 <# if ( data.fileLength ) { #> 559 <div class="file-length"><?php _e( 'Length:' ); ?> {{ data.fileLength }}</div> 571 <# if ( data.fileLength && data.fileLengthHumanReadable ) { #> 572 <div class="file-length"><?php _e( 'Length:' ); ?> 573 <span aria-hidden="true">{{ data.fileLength }}</span> 574 <span class="screen-reader-text">{{ data.fileLengthHumanReadable }}</span> 575 </div> 560 576 <# } #> 561 577 562 578 <# if ( ! data.uploading && data.can.remove ) { #> -
src/wp-includes/media.php
3372 3372 3373 3373 if ( $meta && ( 'audio' === $type || 'video' === $type ) ) { 3374 3374 if ( isset( $meta['length_formatted'] ) ) { 3375 $response['fileLength'] = $meta['length_formatted']; 3375 $response['fileLength'] = $meta['length_formatted']; 3376 $response['fileLengthHumanReadable'] = human_readable_duration( $meta['length_formatted'] ); 3376 3377 } 3377 3378 3378 3379 $response['meta'] = array(); -
tests/phpunit/tests/functions.php
1451 1451 1452 1452 ); 1453 1453 } 1454 1455 /** 1456 * Test the human_readable_duration function. 1457 * 1458 * @ticket 39667 1459 * @dataProvider _datahuman_readable_duration() 1460 * 1461 * @param $input 1462 * @param $expected 1463 */ 1464 public function test_duration_format( $input, $expected ) { 1465 $this->assertSame( $expected, human_readable_duration( $input ) ); 1466 } 1467 1468 public function _datahuman_readable_duration() { 1469 return array( 1470 array( array(), false ), 1471 array( '30:00', '30 minutes, 0 seconds' ), 1472 array( 'Batman Begins !', false ), 1473 array( '', false ), 1474 array( '-1', false ), 1475 array( -1, false ), 1476 array( 0, false ), 1477 array( 1, false ), 1478 array( '00', false ), 1479 array( '00:00', '0 minutes, 0 seconds' ), 1480 array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1481 array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ), 1482 array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ), 1483 array( 'MM:30:00', false ), 1484 array( '30:MM', false ), 1485 array( 'MM:00', false ), 1486 array( 'MM:MM', false ), 1487 array( '01:01', '1 minute, 1 second' ), 1488 array( '01:01:01', '1 hour, 1 minute, 1 second' ), 1489 array( '0:05', '5 seconds' ), 1490 array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1491 ); 1492 } 1454 1493 }