Make WordPress Core


Ignore:
Timestamp:
01/08/2019 07:55:31 PM (6 years ago)
Author:
adamsilverstein
Message:

Media: improve the human_readable_duration function and tests.

Improve the human_readable_duration added in #39667:

  • Remove upper limit.
  • More resilient handling: remove negative prefix, trim.
  • Correct @since to 5.1.0.
  • Adds more test cases and improve inline docs.

Props birgire.
Fixes #39667.

File:
1 edited

Legend:

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

    r44480 r44481  
    324324
    325325/**
    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  */
    333 function human_readable_duration( $filelength = '' ) {
    334     // Return false if filelength is empty or not in format.
    335     if ( ( empty( $filelength ) || ! is_string( $filelength ) ) ) {
     326 * Convert a duration to human readable format.
     327 *
     328 * @since 5.1.0
     329 *
     330 * @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss),
     331 *                         with a possible prepended negative sign (-).
     332 * @return string|false A human readable duration string, false on failure.
     333 */
     334function human_readable_duration( $duration = '' ) {
     335    if ( ( empty( $duration ) || ! is_string( $duration ) ) ) {
    336336        return false;
    337337    }
    338338
    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 );
     339    $duration = trim( $duration );
     340
     341    // Remove prepended negative sign.
     342    if ( '-' === substr( $duration, 0, 1 ) ) {
     343        $duration = substr( $duration, 1 );
     344    }
     345
     346    // Extract duration parts.
     347    $duration_parts = array_reverse( explode( ':', $duration ) );
     348    $duration_count = count( $duration_parts );
     349
     350    $hour   = null;
     351    $minute = null;
     352    $second = null;
    349353
    350354    if ( 3 === $duration_count ) {
     355        // Validate HH:ii:ss duration format.
     356        if ( ! ( (bool) preg_match( '/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {
     357            return false;
     358        }
    351359        // Three parts: hours, minutes & seconds.
    352         list( $second, $minute, $hour ) = $durations;
     360        list( $second, $minute, $hour ) = $duration_parts;
    353361    } elseif ( 2 === $duration_count ) {
     362        // Validate ii:ss duration format.
     363        if ( ! ( (bool) preg_match( '/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) {
     364            return false;
     365        }
    354366        // Two parts: minutes & seconds.
    355         list( $second, $minute ) = $durations;
     367        list( $second, $minute ) = $duration_parts;
    356368    } else {
    357369        return false;
    358370    }
    359371
     372    $human_readable_duration = array();
     373
    360374    // Add the hour part to the string.
    361     if ( ! empty( $hour ) && is_numeric( $hour ) ) {
     375    if ( is_numeric( $hour ) ) {
    362376        /* translators: Time duration in hour or hours. */
    363377        $human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour );
     
    365379
    366380    // Add the minute part to the string.
    367     if ( ! empty( $minute ) && is_numeric( $minute ) ) {
     381    if ( is_numeric( $minute ) ) {
    368382        /* translators: Time duration in minute or minutes. */
    369383        $human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute );
     
    371385
    372386    // Add the second part to the string.
    373     if ( ! empty( $second ) && is_numeric( $second ) ) {
     387    if ( is_numeric( $second ) ) {
    374388        /* translators: Time duration in second or seconds. */
    375389        $human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second );
Note: See TracChangeset for help on using the changeset viewer.