Changeset 6298
- Timestamp:
- 02/21/2017 10:14:20 PM (8 years ago)
- Location:
- trunk/src/includes/common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/common/formatting.php
r6057 r6298 459 459 return $matches[1] . $link; 460 460 } 461 462 /** Numbers *******************************************************************/ 463 464 /** 465 * A bbPress specific method of formatting numeric values 466 * 467 * @since 2.0.0 bbPress (r2486) 468 * 469 * @param string $number Number to format 470 * @param string $decimals Optional. Display decimals 471 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, 472 * number and display decimals bool 473 * @return string Formatted string 474 */ 475 function bbp_number_format( $number = 0, $decimals = false, $dec_point = '.', $thousands_sep = ',' ) { 476 477 // If empty, set $number to (int) 0 478 if ( ! is_numeric( $number ) ) { 479 $number = 0; 480 } 481 482 return apply_filters( 'bbp_number_format', number_format( $number, $decimals, $dec_point, $thousands_sep ), $number, $decimals, $dec_point, $thousands_sep ); 483 } 484 485 /** 486 * A bbPress specific method of formatting numeric values 487 * 488 * @since 2.1.0 bbPress (r3857) 489 * 490 * @param string $number Number to format 491 * @param string $decimals Optional. Display decimals 492 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, 493 * number and display decimals bool 494 * @return string Formatted string 495 */ 496 function bbp_number_format_i18n( $number = 0, $decimals = false ) { 497 498 // If empty, set $number to (int) 0 499 if ( ! is_numeric( $number ) ) { 500 $number = 0; 501 } 502 503 return apply_filters( 'bbp_number_format_i18n', number_format_i18n( $number, $decimals ), $number, $decimals ); 504 } 505 506 /** Dates *********************************************************************/ 507 508 /** 509 * Convert time supplied from database query into specified date format. 510 * 511 * @since 2.0.0 bbPress (r2544) 512 * 513 * @param string $time Time to convert 514 * @param string $d Optional. Default is 'U'. Either 'G', 'U', or php date 515 * format 516 * @param bool $translate Optional. Default is false. Whether to translate the 517 * result 518 * @uses mysql2date() To convert the format 519 * @uses apply_filters() Calls 'bbp_convert_date' with the time, date format 520 * and translate bool 521 * @return string Returns timestamp 522 */ 523 function bbp_convert_date( $time, $d = 'U', $translate = false ) { 524 $new_time = mysql2date( $d, $time, $translate ); 525 526 return apply_filters( 'bbp_convert_date', $new_time, $d, $translate, $time ); 527 } 528 529 /** 530 * Output formatted time to display human readable time difference. 531 * 532 * @since 2.0.0 bbPress (r2544) 533 * 534 * @param string $older_date Unix timestamp from which the difference begins. 535 * @param string $newer_date Optional. Unix timestamp from which the 536 * difference ends. False for current time. 537 * @param int $gmt Optional. Whether to use GMT timezone. Default is false. 538 * @uses bbp_get_time_since() To get the formatted time 539 */ 540 function bbp_time_since( $older_date, $newer_date = false, $gmt = false ) { 541 echo bbp_get_time_since( $older_date, $newer_date, $gmt ); 542 } 543 /** 544 * Return formatted time to display human readable time difference. 545 * 546 * @since 2.0.0 bbPress (r2544) 547 * 548 * @param string $older_date Unix timestamp from which the difference begins. 549 * @param string $newer_date Optional. Unix timestamp from which the 550 * difference ends. False for current time. 551 * @param int $gmt Optional. Whether to use GMT timezone. Default is false. 552 * @uses current_time() To get the current time in mysql format 553 * @uses human_time_diff() To get the time differene in since format 554 * @uses apply_filters() Calls 'bbp_get_time_since' with the time 555 * difference and time 556 * @return string Formatted time 557 */ 558 function bbp_get_time_since( $older_date, $newer_date = false, $gmt = false ) { 559 560 // Setup the strings 561 $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) ); 562 $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) ); 563 $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) ); 564 565 // array of time period chunks 566 $chunks = array( 567 array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ), 568 array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ), 569 array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ), 570 array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ), 571 array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ), 572 array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ), 573 array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) ) 574 ); 575 576 if ( ! empty( $older_date ) && ! is_numeric( $older_date ) ) { 577 $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) ); 578 $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) ); 579 $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] ); 580 } 581 582 // $newer_date will equal false if we want to know the time elapsed 583 // between a date and the current time. $newer_date will have a value if 584 // we want to work out time elapsed between two known dates. 585 $newer_date = ( ! $newer_date ) ? strtotime( current_time( 'mysql', $gmt ) ) : $newer_date; 586 587 // Difference in seconds 588 $since = $newer_date - $older_date; 589 590 // Something went wrong with date calculation and we ended up with a negative date. 591 if ( 0 > $since ) { 592 $output = $unknown_text; 593 594 // We only want to output two chunks of time here, eg: 595 // x years, xx months 596 // x days, xx hours 597 // so there's only two bits of calculation below: 598 } else { 599 600 // Step one: the first chunk 601 for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) { 602 $seconds = $chunks[ $i ][0]; 603 604 // Finding the biggest chunk (if the chunk fits, break) 605 $count = floor( $since / $seconds ); 606 if ( 0 != $count ) { 607 break; 608 } 609 } 610 611 // If $i iterates all the way to $j, then the event happened 0 seconds ago 612 if ( ! isset( $chunks[ $i ] ) ) { 613 $output = $right_now_text; 614 615 } else { 616 617 // Set output var 618 $output = ( 1 == $count ) ? '1 '. $chunks[ $i ][1] : $count . ' ' . $chunks[ $i ][2]; 619 620 // Step two: the second chunk 621 if ( $i + 2 < $j ) { 622 $seconds2 = $chunks[ $i + 1 ][0]; 623 $name2 = $chunks[ $i + 1 ][1]; 624 $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); 625 626 // Add to output var 627 if ( 0 != $count2 ) { 628 $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[ $i + 1 ][2]; 629 } 630 } 631 632 // No output, so happened right now 633 if ( ! (int) trim( $output ) ) { 634 $output = $right_now_text; 635 } 636 } 637 } 638 639 // Append 'ago' to the end of time-since if not 'right now' 640 if ( $output != $right_now_text ) { 641 $output = sprintf( $ago_text, $output ); 642 } 643 644 return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date ); 645 } 646 647 /** Revisions *****************************************************************/ 648 649 /** 650 * Formats the reason for editing the topic/reply. 651 * 652 * Does these things: 653 * - Trimming 654 * - Removing periods from the end of the string 655 * - Trimming again 656 * 657 * @since 2.0.0 bbPress (r2782) 658 * 659 * @param string $reason Optional. User submitted reason for editing. 660 * @return string Status of topic 661 */ 662 function bbp_format_revision_reason( $reason = '' ) { 663 $reason = (string) $reason; 664 665 // Format reason for proper display 666 if ( empty( $reason ) ) { 667 return $reason; 668 } 669 670 // Trimming 671 $reason = trim( $reason ); 672 673 // We add our own full stop. 674 while ( substr( $reason, -1 ) === '.' ) { 675 $reason = substr( $reason, 0, -1 ); 676 } 677 678 // Trim again 679 $reason = trim( $reason ); 680 681 return $reason; 682 } 683 -
trunk/src/includes/common/functions.php
r6289 r6298 14 14 defined( 'ABSPATH' ) || exit; 15 15 16 /** Formatting ****************************************************************/ 17 18 /** 19 * A bbPress specific method of formatting numeric values 20 * 21 * @since 2.0.0 bbPress (r2486) 22 * 23 * @param string $number Number to format 24 * @param string $decimals Optional. Display decimals 25 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, 26 * number and display decimals bool 27 * @return string Formatted string 28 */ 29 function bbp_number_format( $number = 0, $decimals = false, $dec_point = '.', $thousands_sep = ',' ) { 30 31 // If empty, set $number to (int) 0 32 if ( ! is_numeric( $number ) ) { 33 $number = 0; 34 } 35 36 return apply_filters( 'bbp_number_format', number_format( $number, $decimals, $dec_point, $thousands_sep ), $number, $decimals, $dec_point, $thousands_sep ); 37 } 38 39 /** 40 * A bbPress specific method of formatting numeric values 41 * 42 * @since 2.1.0 bbPress (r3857) 43 * 44 * @param string $number Number to format 45 * @param string $decimals Optional. Display decimals 46 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, 47 * number and display decimals bool 48 * @return string Formatted string 49 */ 50 function bbp_number_format_i18n( $number = 0, $decimals = false ) { 51 52 // If empty, set $number to (int) 0 53 if ( ! is_numeric( $number ) ) { 54 $number = 0; 55 } 56 57 return apply_filters( 'bbp_number_format_i18n', number_format_i18n( $number, $decimals ), $number, $decimals ); 58 } 59 60 /** 61 * Convert time supplied from database query into specified date format. 62 * 63 * @since 2.0.0 bbPress (r2544) 64 * 65 * @param string $time Time to convert 66 * @param string $d Optional. Default is 'U'. Either 'G', 'U', or php date 67 * format 68 * @param bool $translate Optional. Default is false. Whether to translate the 69 * result 70 * @uses mysql2date() To convert the format 71 * @uses apply_filters() Calls 'bbp_convert_date' with the time, date format 72 * and translate bool 73 * @return string Returns timestamp 74 */ 75 function bbp_convert_date( $time, $d = 'U', $translate = false ) { 76 $new_time = mysql2date( $d, $time, $translate ); 77 78 return apply_filters( 'bbp_convert_date', $new_time, $d, $translate, $time ); 79 } 80 81 /** 82 * Output formatted time to display human readable time difference. 83 * 84 * @since 2.0.0 bbPress (r2544) 85 * 86 * @param string $older_date Unix timestamp from which the difference begins. 87 * @param string $newer_date Optional. Unix timestamp from which the 88 * difference ends. False for current time. 89 * @param int $gmt Optional. Whether to use GMT timezone. Default is false. 90 * @uses bbp_get_time_since() To get the formatted time 91 */ 92 function bbp_time_since( $older_date, $newer_date = false, $gmt = false ) { 93 echo bbp_get_time_since( $older_date, $newer_date, $gmt ); 94 } 95 /** 96 * Return formatted time to display human readable time difference. 97 * 98 * @since 2.0.0 bbPress (r2544) 99 * 100 * @param string $older_date Unix timestamp from which the difference begins. 101 * @param string $newer_date Optional. Unix timestamp from which the 102 * difference ends. False for current time. 103 * @param int $gmt Optional. Whether to use GMT timezone. Default is false. 104 * @uses current_time() To get the current time in mysql format 105 * @uses human_time_diff() To get the time differene in since format 106 * @uses apply_filters() Calls 'bbp_get_time_since' with the time 107 * difference and time 108 * @return string Formatted time 109 */ 110 function bbp_get_time_since( $older_date, $newer_date = false, $gmt = false ) { 111 112 // Setup the strings 113 $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) ); 114 $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) ); 115 $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) ); 116 117 // array of time period chunks 118 $chunks = array( 119 array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ), 120 array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ), 121 array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ), 122 array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ), 123 array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ), 124 array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ), 125 array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) ) 126 ); 127 128 if ( ! empty( $older_date ) && ! is_numeric( $older_date ) ) { 129 $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) ); 130 $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) ); 131 $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] ); 132 } 133 134 // $newer_date will equal false if we want to know the time elapsed 135 // between a date and the current time. $newer_date will have a value if 136 // we want to work out time elapsed between two known dates. 137 $newer_date = ( ! $newer_date ) ? strtotime( current_time( 'mysql', $gmt ) ) : $newer_date; 138 139 // Difference in seconds 140 $since = $newer_date - $older_date; 141 142 // Something went wrong with date calculation and we ended up with a negative date. 143 if ( 0 > $since ) { 144 $output = $unknown_text; 145 146 // We only want to output two chunks of time here, eg: 147 // x years, xx months 148 // x days, xx hours 149 // so there's only two bits of calculation below: 150 } else { 151 152 // Step one: the first chunk 153 for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) { 154 $seconds = $chunks[ $i ][0]; 155 156 // Finding the biggest chunk (if the chunk fits, break) 157 $count = floor( $since / $seconds ); 158 if ( 0 != $count ) { 159 break; 160 } 161 } 162 163 // If $i iterates all the way to $j, then the event happened 0 seconds ago 164 if ( ! isset( $chunks[ $i ] ) ) { 165 $output = $right_now_text; 166 167 } else { 168 169 // Set output var 170 $output = ( 1 == $count ) ? '1 '. $chunks[ $i ][1] : $count . ' ' . $chunks[ $i ][2]; 171 172 // Step two: the second chunk 173 if ( $i + 2 < $j ) { 174 $seconds2 = $chunks[ $i + 1 ][0]; 175 $name2 = $chunks[ $i + 1 ][1]; 176 $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); 177 178 // Add to output var 179 if ( 0 != $count2 ) { 180 $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[ $i + 1 ][2]; 181 } 182 } 183 184 // No output, so happened right now 185 if ( ! (int) trim( $output ) ) { 186 $output = $right_now_text; 187 } 188 } 189 } 190 191 // Append 'ago' to the end of time-since if not 'right now' 192 if ( $output != $right_now_text ) { 193 $output = sprintf( $ago_text, $output ); 194 } 195 196 return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date ); 197 } 198 199 /** 200 * Formats the reason for editing the topic/reply. 201 * 202 * Does these things: 203 * - Trimming 204 * - Removing periods from the end of the string 205 * - Trimming again 206 * 207 * @since 2.0.0 bbPress (r2782) 208 * 209 * @param string $reason Optional. User submitted reason for editing. 210 * @return string Status of topic 211 */ 212 function bbp_format_revision_reason( $reason = '' ) { 213 $reason = (string) $reason; 214 215 // Format reason for proper display 216 if ( empty( $reason ) ) { 217 return $reason; 218 } 219 220 // Trimming 221 $reason = trim( $reason ); 222 223 // We add our own full stop. 224 while ( substr( $reason, -1 ) === '.' ) { 225 $reason = substr( $reason, 0, -1 ); 226 } 227 228 // Trim again 229 $reason = trim( $reason ); 230 231 return $reason; 232 } 233 234 /** Misc **********************************************************************/ 16 /** URLs **********************************************************************/ 235 17 236 18 /** … … 328 110 return 1; 329 111 } 112 113 /** Misc **********************************************************************/ 330 114 331 115 /**
Note: See TracChangeset
for help on using the changeset viewer.