Ticket #39667: 39667.2.diff
File 39667.2.diff, 5.7 KB (added by , 8 years ago) |
---|
-
src/wp-includes/functions.php
diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 966dcd8..e9bef99 100644
a b function size_format( $bytes, $decimals = 0 ) { 274 274 } 275 275 276 276 /** 277 * Convert a filelength to human readable format. 278 * 279 * @since 4.8 280 * 281 * @param string $filelength Duration will be in string format (HH:ii:ss) OR (ii:ss). 282 * 283 * @return boolean|string A human readable filelength string, false on failure. 284 */ 285 function human_readable_duration( $filelength = '' ) { 286 287 // Return false if filelength is empty or not in format. 288 if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) { 289 return false; 290 } 291 292 // Validate filelength format. 293 if ( ! ( (bool) preg_match( '/^(([0-3]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/', $filelength ) ) ) { 294 return false; 295 } 296 297 $human_readable_duration = []; 298 299 // Extract duration. 300 $durations = array_reverse( explode( ':', $filelength ) ); 301 $duration_count = count( $durations ); 302 303 if ( 3 === $duration_count ) { 304 305 // Three parts: hours, minutes & seconds. 306 list ( $second, $minute, $hour ) = $durations; 307 } elseif ( 2 === $duration_count ) { 308 309 // Two parts: minutes & seconds. 310 list ( $second, $minute ) = $durations; 311 } else { 312 return false; 313 } 314 315 // Add the hour part to the string. 316 if ( ! empty( $hour ) && is_numeric( $hour ) ) { 317 /* translators: Time duration in hour or hours */ 318 $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour ); 319 } 320 321 // Add the minute part to the string. 322 if ( ! empty( $minute ) && is_numeric( $minute ) ) { 323 /* translators: Time duration in minute or minutes */ 324 $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute ); 325 } 326 327 // Add the second part to the string. 328 if ( ! empty( $second ) && is_numeric( $second ) ) { 329 /* translators: Time duration in second or seconds */ 330 $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second ); 331 } 332 333 return implode( ', ', $human_readable_duration ); 334 } 335 336 /** 277 337 * Get the week start and end from the datetime or date string from MySQL. 278 338 * 279 339 * @since 0.71 -
src/wp-includes/media-template.php
diff --git a/src/wp-includes/media-template.php b/src/wp-includes/media-template.php index 7f59935..96e281f 100644
a b function wp_print_media_templates() { 344 344 <div class="file-size"><strong><?php _e( 'File size:' ); ?></strong> {{ data.filesizeHumanReadable }}</div> 345 345 <# if ( 'image' === data.type && ! data.uploading ) { #> 346 346 <# if ( data.width && data.height ) { #> 347 <div class="dimensions"><strong><?php _e( 'Dimensions:' ); ?></strong> {{ data.width }} × {{ data.height }}</div> 347 <div class="dimensions"><strong><?php _e( 'Dimensions:' ); ?></strong> 348 <?php 349 /* translators: 1: a number of pixels wide, 2: a number of pixels tall */ 350 printf( __( '%1$s by %2$s pixels' ),'{{ data.width }}', '{{ data.height }}' ); 351 ?> 352 </div> 348 353 <# } #> 349 354 <# } #> 350 355 351 <# if ( data.fileLength ) { #> 352 <div class="file-length"><strong><?php _e( 'Length:' ); ?></strong> {{ data.fileLength }}</div> 356 <# if ( data.fileLength && data.fileLengthHumanReadable ) { #> 357 <div class="file-length"><strong><?php _e( 'Length:' ); ?></strong> 358 <span aria-label="{{ data.fileLengthHumanReadable }}">{{ data.fileLength }}</span> 359 </div> 353 360 <# } #> 354 361 355 362 <# if ( 'audio' === data.type && data.meta.bitrate ) { #> -
src/wp-includes/media.php
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 6253bd5..707fa79 100644
a b function wp_prepare_attachment_for_js( $attachment ) { 3219 3219 } 3220 3220 3221 3221 if ( $meta && ( 'audio' === $type || 'video' === $type ) ) { 3222 if ( isset( $meta['length_formatted'] ) ) 3223 $response['fileLength'] = $meta['length_formatted']; 3222 3223 if ( isset( $meta['length_formatted'] ) ) { 3224 $response['fileLength'] = $meta['length_formatted']; 3225 $response['fileLengthHumanReadable'] = duration_format( $meta['length_formatted'] ); 3226 } 3224 3227 3225 3228 $response['meta'] = array(); 3226 3229 foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) { -
tests/phpunit/tests/functions.php
diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 8400720..d1e203d 100644
a b class Tests_Functions extends WP_UnitTestCase { 1114 1114 1115 1115 return $data; 1116 1116 } 1117 1118 /** 1119 * Test the human_readable_duration function. 1120 * 1121 * @dataProvider _datahuman_readable_duration 1122 * 1123 * @param $input 1124 * @param $expected 1125 * @ticket 39667 1126 */ 1127 public function test_duration_format( $input, $expected ) { 1128 $this->assertSame( $expected, human_readable_duration( $input ) ); 1129 } 1130 1131 public function _datahuman_readable_duration() { 1132 return array( 1133 array( array(), false ), 1134 array( '30:00', '30 minutes, 0 seconds' ), 1135 array( 'Batman Begins !', false ), 1136 array( '', false ), 1137 array( '-1', false ), 1138 array( -1, false ), 1139 array( 0, false ), 1140 array( 1, false ), 1141 array( '00', false ), 1142 array( '00:00', '0 minutes, 0 seconds' ), 1143 array( '00:00:00', '0 hours, 0 minutes, 0 seconds' ), 1144 array( '10:30:34', '10 hours, 30 minutes, 34 seconds' ), 1145 array( '00:30:34', '0 hours, 30 minutes, 34 seconds' ), 1146 array( 'MM:30:00', false ), 1147 array( '30:MM', false ), 1148 array( 'MM:00', false ), 1149 array( 'MM:MM', false ), 1150 array( '01:01', '1 minute, 1 second' ), 1151 array( '01:01:01', '1 hour, 1 minute, 1 second' ), 1152 array( '0:05', '5 seconds' ), 1153 array( '1:02:00', '1 hour, 2 minutes, 0 seconds' ), 1154 ); 1155 } 1117 1156 }