Make WordPress Core

Ticket #39667: 39667-4.diff

File 39667-4.diff, 5.6 KB (added by milindmore22, 8 years ago)

Updated with test case improved coding with phpcs

  • src/wp-includes/functions.php

    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index 966dcd8..0a96438 100644
    a b function size_format( $bytes, $decimals = 0 ) { 
    274274}
    275275
    276276/**
     277 * Function will return duration/ file length in human readable format.
     278 * This will be used for making readable string from duration.
     279 *
     280 * @since 4.8
     281 * @param string $filelength duration will be in string format (HH:ii:ss) OR (ii:ss).
     282 * @return boolean | string return human readable string, false otherwise.
     283 */
     284function duration_format( $filelength = '' ) {
     285
     286        // return false if filelenght is empty or not in format.
     287        if ( ( empty( $filelength ) && ! is_string( $filelength ) ) ) {
     288
     289                return false;
     290        }
     291
     292        // validate filelenght format.
     293        if ( ! ( (bool) preg_match( '/^(([0-3]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/', $filelength ) ) ) {
     294
     295                return false;
     296        }
     297
     298        $human_readable_duration = false;
     299        // Extract duration.
     300        $durations              = array_reverse( explode( ':', $filelength ) );
     301        $duration_count = count( $durations );
     302
     303        // extract hours, minutes and seconds elseif extract minutes and senconds
     304        if ( 3 === $duration_count ) {
     305
     306                list ( $second, $minute, $hour ) = $durations;
     307        } elseif ( 2 === $duration_count ) {
     308
     309                list ( $second, $minute ) = $durations;
     310        } else {
     311
     312                return false;
     313        }
     314
     315        if ( ! empty( $hour ) && is_numeric( $hour ) ) {
     316                /* translators: Time duration in hour or hours */
     317                $human_readable_duration .= sprintf( _n( '%s hour', '%s hours', $hour ), $hour ) . ' ';
     318        }
     319
     320        if ( ! empty( $minute ) && is_numeric( $minute ) ) {
     321                /* translators: Time duration in minute or minutes */
     322                $human_readable_duration .= sprintf( _n( '%s minute', '%s minutes', $minute ), $minute ) . ' ';
     323        }
     324
     325        if ( ! empty( $second ) && is_numeric( $second ) ) {
     326                /* translators: Time duration in second or seconds */
     327                $human_readable_duration .= sprintf( _n( '%s second', '%s seconds', $second ), $second );
     328        }
     329        // return human readable duration
     330        return $human_readable_duration;
     331}
     332
     333/**
    277334 * Get the week start and end from the datetime or date string from MySQL.
    278335 *
    279336 * @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() { 
    344344                                <div class="file-size"><strong><?php _e( 'File size:' ); ?></strong> {{ data.filesizeHumanReadable }}</div>
    345345                                <# if ( 'image' === data.type && ! data.uploading ) { #>
    346346                                        <# if ( data.width && data.height ) { #>
    347                                                 <div class="dimensions"><strong><?php _e( 'Dimensions:' ); ?></strong> {{ data.width }} &times; {{ 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>
    348353                                        <# } #>
    349354                                <# } #>
    350355
    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>
    353360                                <# } #>
    354361
    355362                                <# 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 ) { 
    32193219        }
    32203220
    32213221        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                }
    32243227
    32253228                $response['meta'] = array();
    32263229                foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) {
  • new file tests/phpunit/tests/functions/durationFormat.php

    diff --git a/tests/phpunit/tests/functions/durationFormat.php b/tests/phpunit/tests/functions/durationFormat.php
    new file mode 100644
    index 0000000..8294e89
    - +  
     1<?php
     2
     3/**
     4 * Tests for duration_format()
     5 *
     6 * @group functions.php
     7 * @ticket 39667
     8 */
     9class Tests_Functions_Duration_Format extends WP_UnitTestCase {
     10
     11        public function _data_duration_format() {
     12                return array(
     13                        array( array(), false ),
     14                        array( '30:00', '30 minutes 00 seconds' ),
     15                        array( 'Batman Begins !', false ),
     16                        array( '', false ),
     17                        array( '-1', false ),
     18                        array( -1, false ),
     19                        array( 0, false ),
     20                        array( 1, false ),
     21                        array( '00', false ),
     22                        array( '00:00', '00 minutes 00 seconds' ),
     23                        array( '00:00:00', '00 hours 00 minutes 00 seconds' ),
     24                        array( '10:30:34', '10 hours 30 minutes 34 seconds' ),
     25                        array( '00:30:34', '00 hours 30 minutes 34 seconds' ),
     26                        array( 'MM:30:00', false ),
     27                        array( '30:MM', false ),
     28                        array( 'MM:00', false ),
     29                        array( 'MM:MM', false ),
     30                        array( '01:01', '01 minute 01 second' ),
     31                        array( '01:01:01', '01 hour 01 minute 01 second' ),
     32                        array( '0:05', '05 seconds' ),
     33                );
     34        }
     35
     36        /**
     37         * @dataProvider _data_duration_format
     38         *
     39         * @param $input
     40         * @param $expected
     41         */
     42        public function test_duration_format( $input, $expected ) {
     43                $this->assertSame( $expected, duration_format( $input ) );
     44        }
     45
     46}