Skip to:
Content

bbPress.org


Ignore:
Timestamp:
12/05/2025 03:03:50 AM (10 months ago)
Author:
johnjamesjacoby
Message:

Tools - Code Improvement: un-indent last remaining _get_ functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/formatting.php

    r7380 r7381  
    648648        echo bbp_get_time_since( $older_date, $newer_date, $gmt );
    649649}
    650         /**
    651          * Return formatted time to display human readable time difference.
    652          *
    653          * @since 2.0.0 bbPress (r2544)
    654          *
    655          * @param string $older_date Unix timestamp from which the difference begins.
    656          * @param string $newer_date Optional. Unix timestamp from which the
    657          *                            difference ends. False for current time.
    658          * @param int $gmt Optional. Whether to use GMT timezone. Default is false.
    659          *
    660          * @return string Formatted time
    661          */
    662         function bbp_get_time_since( $older_date, $newer_date = false, $gmt = false ) {
    663 
    664                 // Setup the strings
    665                 $unknown_text   = apply_filters( 'bbp_core_time_since_unknown_text',   esc_html__( 'sometime',  'bbpress' ) );
    666                 $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', esc_html__( 'right now', 'bbpress' ) );
    667                 /* translators: %s: Time period */
    668                 $ago_text       = apply_filters( 'bbp_core_time_since_ago_text',       esc_html__( '%s ago',    'bbpress' ) );
    669 
    670                 // array of time period chunks
    671                 $chunks = array(
    672                         /* translators: %s: Number of years */
    673                         array( YEAR_IN_SECONDS, _n_noop( '%s year', '%s years', 'bbpress' ) ),
    674 
    675                         /* translators: %s: Number of months */
    676                         array( MONTH_IN_SECONDS, _n_noop( '%s month', '%s months', 'bbpress' ) ),
    677 
    678                         /* translators: %s: Number of weeks */
    679                         array( WEEK_IN_SECONDS, _n_noop( '%s week', '%s weeks', 'bbpress' ) ),
    680 
    681                         /* translators: %s: Number of days */
    682                         array( DAY_IN_SECONDS, _n_noop( '%s day', '%s days', 'bbpress' ) ),
    683 
    684                         /* translators: %s: Number of hours */
    685                         array( HOUR_IN_SECONDS, _n_noop( '%s hour', '%s hours', 'bbpress' ) ),
    686 
    687                         /* translators: %s: Number of minutes */
    688                         array( MINUTE_IN_SECONDS, _n_noop( '%s minute', '%s minutes', 'bbpress' ) ),
    689 
    690                         /* translators: %s: Number of seconds */
    691                         array( 1, _n_noop( '%s second', '%s seconds', 'bbpress' ) ),
    692                 );
    693 
    694                 // Attempt to parse non-numeric older date
    695                 if ( ! empty( $older_date ) && ! is_numeric( $older_date ) ) {
    696                         $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
    697                         $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
    698                         $older_date  = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
     650
     651/**
     652 * Return formatted time to display human readable time difference.
     653 *
     654 * @since 2.0.0 bbPress (r2544)
     655 *
     656 * @param string $older_date Unix timestamp from which the difference begins.
     657 * @param string $newer_date Optional. Unix timestamp from which the
     658 *                            difference ends. False for current time.
     659 * @param int $gmt Optional. Whether to use GMT timezone. Default is false.
     660 *
     661 * @return string Formatted time
     662 */
     663function bbp_get_time_since( $older_date, $newer_date = false, $gmt = false ) {
     664
     665        // Setup the strings
     666        $unknown_text   = apply_filters( 'bbp_core_time_since_unknown_text',   esc_html__( 'sometime',  'bbpress' ) );
     667        $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', esc_html__( 'right now', 'bbpress' ) );
     668        /* translators: %s: Time period */
     669        $ago_text       = apply_filters( 'bbp_core_time_since_ago_text',       esc_html__( '%s ago',    'bbpress' ) );
     670
     671        // array of time period chunks
     672        $chunks = array(
     673                /* translators: %s: Number of years */
     674                array( YEAR_IN_SECONDS, _n_noop( '%s year', '%s years', 'bbpress' ) ),
     675
     676                /* translators: %s: Number of months */
     677                array( MONTH_IN_SECONDS, _n_noop( '%s month', '%s months', 'bbpress' ) ),
     678
     679                /* translators: %s: Number of weeks */
     680                array( WEEK_IN_SECONDS, _n_noop( '%s week', '%s weeks', 'bbpress' ) ),
     681
     682                /* translators: %s: Number of days */
     683                array( DAY_IN_SECONDS, _n_noop( '%s day', '%s days', 'bbpress' ) ),
     684
     685                /* translators: %s: Number of hours */
     686                array( HOUR_IN_SECONDS, _n_noop( '%s hour', '%s hours', 'bbpress' ) ),
     687
     688                /* translators: %s: Number of minutes */
     689                array( MINUTE_IN_SECONDS, _n_noop( '%s minute', '%s minutes', 'bbpress' ) ),
     690
     691                /* translators: %s: Number of seconds */
     692                array( 1, _n_noop( '%s second', '%s seconds', 'bbpress' ) ),
     693        );
     694
     695        // Attempt to parse non-numeric older date
     696        if ( ! empty( $older_date ) && ! is_numeric( $older_date ) ) {
     697                $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
     698                $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
     699                $older_date  = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
     700        }
     701
     702        // Attempt to parse non-numeric newer date
     703        if ( ! empty( $newer_date ) && ! is_numeric( $newer_date ) ) {
     704                $time_chunks = explode( ':', str_replace( ' ', ':', $newer_date ) );
     705                $date_chunks = explode( '-', str_replace( ' ', '-', $newer_date ) );
     706                $newer_date  = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
     707        }
     708
     709        // Set newer date to current time
     710        if ( empty( $newer_date ) ) {
     711                $newer_date = strtotime( current_time( 'mysql', $gmt ) );
     712        }
     713
     714        // Cast both dates to ints to avoid notices & errors with invalid values
     715        $newer_date = intval( $newer_date );
     716        $older_date = intval( $older_date );
     717
     718        // Difference in seconds
     719        $since = intval( $newer_date - $older_date );
     720
     721        // Something went wrong with date calculation and we ended up with a negative date.
     722        if ( 0 > $since ) {
     723                $output = $unknown_text;
     724
     725        // We only want to output two chunks of time here, eg:
     726        //     x years, xx months
     727        //     x days, xx hours
     728        // so there's only two bits of calculation below:
     729        } else {
     730
     731                // Default count values
     732                $count  = 0;
     733                $count2 = 0;
     734
     735                // Step one: the first chunk
     736                for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
     737                        $seconds = $chunks[ $i ][0];
     738
     739                        // Finding the biggest chunk (if the chunk fits, break)
     740                        $count = floor( $since / $seconds );
     741                        if ( ! empty( $count ) ) {
     742                                break;
     743                        }
    699744                }
    700745
    701                 // Attempt to parse non-numeric newer date
    702                 if ( ! empty( $newer_date ) && ! is_numeric( $newer_date ) ) {
    703                         $time_chunks = explode( ':', str_replace( ' ', ':', $newer_date ) );
    704                         $date_chunks = explode( '-', str_replace( ' ', '-', $newer_date ) );
    705                         $newer_date  = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
    706                 }
    707 
    708                 // Set newer date to current time
    709                 if ( empty( $newer_date ) ) {
    710                         $newer_date = strtotime( current_time( 'mysql', $gmt ) );
    711                 }
    712 
    713                 // Cast both dates to ints to avoid notices & errors with invalid values
    714                 $newer_date = intval( $newer_date );
    715                 $older_date = intval( $older_date );
    716 
    717                 // Difference in seconds
    718                 $since = intval( $newer_date - $older_date );
    719 
    720                 // Something went wrong with date calculation and we ended up with a negative date.
    721                 if ( 0 > $since ) {
    722                         $output = $unknown_text;
    723 
    724                 // We only want to output two chunks of time here, eg:
    725                 //     x years, xx months
    726                 //     x days, xx hours
    727                 // so there's only two bits of calculation below:
     746                // If $i iterates all the way to $j, then the event happened 0 seconds ago
     747                if ( ! isset( $chunks[ $i ] ) ) {
     748                        $output = $right_now_text;
     749
    728750                } else {
    729751
    730                         // Default count values
    731                         $count  = 0;
    732                         $count2 = 0;
    733 
    734                         // Step one: the first chunk
    735                         for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
    736                                 $seconds = $chunks[ $i ][0];
    737 
    738                                 // Finding the biggest chunk (if the chunk fits, break)
    739                                 $count = floor( $since / $seconds );
    740                                 if ( ! empty( $count ) ) {
    741                                         break;
     752                        // Set output var
     753                        $output = sprintf( translate_nooped_plural( $chunks[ $i ][1], $count, 'bbpress' ), bbp_number_format_i18n( $count ) );
     754
     755                        // Step two: the second chunk
     756                        if ( $i + 2 < $j ) {
     757                                $seconds2 = $chunks[ $i + 1 ][0];
     758                                $count2   = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
     759
     760                                // Add to output var
     761                                if ( ! empty( $count2 ) ) {
     762                                        $output .= _x( ',', 'Separator in time since', 'bbpress' ) . ' ';
     763                                        $output .= sprintf( translate_nooped_plural( $chunks[ $i + 1 ][1], $count2, 'bbpress' ), bbp_number_format_i18n( $count2 ) );
    742764                                }
    743765                        }
    744766
    745                         // If $i iterates all the way to $j, then the event happened 0 seconds ago
    746                         if ( ! isset( $chunks[ $i ] ) ) {
     767                        // Empty counts, so fallback to right now
     768                        if ( empty( $count ) && empty( $count2 ) ) {
    747769                                $output = $right_now_text;
    748 
    749                         } else {
    750 
    751                                 // Set output var
    752                                 $output = sprintf( translate_nooped_plural( $chunks[ $i ][1], $count, 'bbpress' ), bbp_number_format_i18n( $count ) );
    753 
    754                                 // Step two: the second chunk
    755                                 if ( $i + 2 < $j ) {
    756                                         $seconds2 = $chunks[ $i + 1 ][0];
    757                                         $count2   = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
    758 
    759                                         // Add to output var
    760                                         if ( ! empty( $count2 ) ) {
    761                                                 $output .= _x( ',', 'Separator in time since', 'bbpress' ) . ' ';
    762                                                 $output .= sprintf( translate_nooped_plural( $chunks[ $i + 1 ][1], $count2, 'bbpress' ), bbp_number_format_i18n( $count2 ) );
    763                                         }
    764                                 }
    765 
    766                                 // Empty counts, so fallback to right now
    767                                 if ( empty( $count ) && empty( $count2 ) ) {
    768                                         $output = $right_now_text;
    769                                 }
    770770                        }
    771771                }
    772 
    773                 // Append 'ago' to the end of time-since if not 'right now'
    774                 if ( $output !== $right_now_text ) {
    775                         $output = sprintf( $ago_text, $output );
    776                 }
    777 
    778                 // Filter & return
    779                 return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date );
    780         }
     772        }
     773
     774        // Append 'ago' to the end of time-since if not 'right now'
     775        if ( $output !== $right_now_text ) {
     776                $output = sprintf( $ago_text, $output );
     777        }
     778
     779        // Filter & return
     780        return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date );
     781}
    781782
    782783/** Revisions *****************************************************************/
Note: See TracChangeset for help on using the changeset viewer.