WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/08/2018 04:19:40 AM (15 months ago)
Author:
adamsilverstein
Message:

Media: Improve display and accessibility of meta data in detail view.

  • Add a human_readable_duration function including tests.
  • Add 'pixels' after image width/height.
  • Add screen reader text for durations.

Props Presskopp, kiranpotphode, milindmore22, stormrockwell, afercia.
Fixes #39667.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r43599 r43633  
    321321
    322322    return false;
     323}
     324
     325/**
     326 * Convert a filelength to human readable format.
     327 *
     328 * @since 5.0
     329 *
     330 * @param string $filelength Duration will be in string format (HH:ii:ss) OR (ii:ss).
     331 * @return boolean|string A human readable filelength string, false on failure.
     332 */
     333function human_readable_duration( $filelength = '' ) {
     334    // Return false if filelength is empty or not in format.
     335    if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) {
     336        return false;
     337    }
     338
     339    // Validate filelength format.
     340    if ( ! ( (bool) preg_match( '/^(([0-3]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/', $filelength ) ) ) {
     341        return false;
     342    }
     343
     344    $human_readable_duration = array();
     345
     346    // Extract duration.
     347    $durations      = array_reverse( explode( ':', $filelength ) );
     348    $duration_count = count( $durations );
     349
     350    if ( 3 === $duration_count ) {
     351        // Three parts: hours, minutes & seconds.
     352        list( $second, $minute, $hour ) = $durations;
     353    } elseif ( 2 === $duration_count ) {
     354        // Two parts: minutes & seconds.
     355        list( $second, $minute ) = $durations;
     356    } else {
     357        return false;
     358    }
     359
     360    // Add the hour part to the string.
     361    if ( ! empty( $hour ) && is_numeric( $hour ) ) {
     362        /* translators: Time duration in hour or hours. */
     363        $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour );
     364    }
     365
     366    // Add the minute part to the string.
     367    if ( ! empty( $minute ) && is_numeric( $minute ) ) {
     368        /* translators: Time duration in minute or minutes. */
     369        $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute );
     370    }
     371
     372    // Add the second part to the string.
     373    if ( ! empty( $second ) && is_numeric( $second ) ) {
     374        /* translators: Time duration in second or seconds. */
     375        $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second );
     376    }
     377
     378    return implode( ', ', $human_readable_duration );
    323379}
    324380
Note: See TracChangeset for help on using the changeset viewer.