<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][12931] trunk/src: Add an option to display a datebox Profile Field type value as an age</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/12931">12931</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/12931","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-29 10:20:39 +0000 (Thu, 29 Apr 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Add an option to display a datebox Profile Field type value as an age
This xProfile field type now support a new date format allowing Community site admins to display the age of their members according to their birth date.
- Moves the date difference calculation from `bp_core_time_since()` to a newly introduced function: `bp_core_time_diff()`
- Introduce the function `bp_core_time_old()` to format a birth date as an age eg: "20 years old".
Props Venutius
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/8083">#8083</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcorefunctionsphp">trunk/src/bp-core/bp-core-functions.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldtypedateboxphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcorefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-functions.php 2021-04-29 08:31:02 UTC (rev 12930)
+++ trunk/src/bp-core/bp-core-functions.php 2021-04-29 10:20:39 UTC (rev 12931)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1177,75 +1177,31 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get an English-language representation of the time elapsed since a given date.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Calculate the human time difference between two dates.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Based on function created by Dunstan Orchard - http://1976design.com
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This function will return an English representation of the time elapsed
- * since a given date.
- * eg: 2 hours and 50 minutes
- * eg: 4 days
- * eg: 4 weeks and 6 days
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 8.0.0
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Note that fractions of minutes are not represented in the return string. So
- * an interval of 3 minutes will be represented by "3 minutes ago", as will an
- * interval of 3 minutes 59 seconds.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $args {
+ * An array of arguments. All arguments are technically optional.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 1.0.0
- *
- * @param int|string $older_date The earlier time from which you're calculating
- * the time elapsed. Enter either as an integer Unix timestamp,
- * or as a date string of the format 'Y-m-d h:i:s'.
- * @param int|bool $newer_date Optional. Unix timestamp of date to compare older
- * date to. Default: false (current time).
- * @return string String representing the time since the older date, eg
- * "2 hours and 50 minutes".
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type int|string $older_date An integer Unix timestamp or a date string of the format 'Y-m-d h:i:s'.
+ * @type int|string $newer_date An integer Unix timestamp or a date string of the format 'Y-m-d h:i:s'.
+ * @type int $time_chunks The number of time chunks to get (1 or 2).
+ * }
+ * @return null|array|false Null if there's no time diff. An array containing 1 or 2 chunks
+ * of human time. False if travelling into the future.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function bp_core_time_since( $older_date, $newer_date = false ) {
-
- /**
- * Filters whether or not to bypass BuddyPress' time_since calculations.
- *
- * @since 1.7.0
- *
- * @param bool $value Whether or not to bypass.
- * @param string $older_date Earlier time from which we're calculating time elapsed.
- * @param string $newer_date Unix timestamp of date to compare older time to.
- */
- $pre_value = apply_filters( 'bp_core_time_since_pre', false, $older_date, $newer_date );
- if ( false !== $pre_value ) {
- return $pre_value;
- }
-
- /**
- * Filters the value to use if the time since is unknown.
- *
- * @since 1.5.0
- *
- * @param string $value String representing the time since the older date.
- */
- $unknown_text = apply_filters( 'bp_core_time_since_unknown_text', __( 'sometime', 'buddypress' ) );
-
- /**
- * Filters the value to use if the time since is right now.
- *
- * @since 1.5.0
- *
- * @param string $value String representing the time since the older date.
- */
- $right_now_text = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', 'buddypress' ) );
-
- /**
- * Filters the value to use if the time since is some time ago.
- *
- * @since 1.5.0
- *
- * @param string $value String representing the time since the older date.
- */
- $ago_text = apply_filters(
- 'bp_core_time_since_ago_text',
- /* translators: %s: the human time diff. */
- __( '%s ago', 'buddypress' )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function bp_core_time_diff( $args = array() ) {
+ $retval = null;
+ $r = wp_parse_args(
+ $args,
+ array(
+ 'older_date' => 0,
+ 'newer_date' => bp_core_current_time( true, 'timestamp' ),
+ 'time_chunks' => 2,
+ )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Array of time period chunks.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1259,137 +1215,221 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 1
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( !empty( $older_date ) && !is_numeric( $older_date ) ) {
- $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
- $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
- $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] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( array( 'older_date', 'newer_date' ) as $date ) {
+ if ( ! $r[ $date ] ) {
+ continue;
+ }
+
+ if ( ! is_numeric( $r[ $date ] ) ) {
+ $time_chunks = explode( ':', str_replace( ' ', ':', $r[ $date ] ) );
+ $date_chunks = explode( '-', str_replace( ' ', '-', $r[ $date ] ) );
+ $r[ $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]
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * $newer_date will equal false if we want to know the time elapsed between
- * a date and the current time. $newer_date will have a value if we want to
- * work out time elapsed between two known dates.
- */
- $newer_date = ( !$newer_date ) ? bp_core_current_time( true, 'timestamp' ) : $newer_date;
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Difference in seconds.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $since = $newer_date - $older_date;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $diff = $r['newer_date'] - $r['older_date'];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Something went wrong with date calculation and we ended up with a negative date.
- if ( 0 > $since ) {
- $output = $unknown_text;
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * We only want to output two chunks of time here, eg:
- * x years, xx months
- * x days, xx hours
- * so there's only two bits of calculation below:
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * We only want to return one or two chunks of time here, eg:
+ * - `array( 'x years', 'xx months' )`,
+ * - `array( 'x days', 'xx hours' )`.
+ * So there's only two bits of calculation below.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 0 <= $diff && (int) $r['time_chunks'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Step one: the first chunk.
</span><span class="cx" style="display: block; padding: 0 10px"> for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $seconds = $chunks[$i];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Finding the biggest chunk (if the chunk fits, break).
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $count = floor( $since / $seconds );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $count = floor( $diff / $seconds );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( 0 != $count ) {
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If $i iterates all the way to $j, then the event happened 0 seconds ago.
- if ( !isset( $chunks[$i] ) ) {
- $output = $right_now_text;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Add the first chunk of time diff.
+ if ( isset( $chunks[ $i ] ) ) {
+ $retval = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
-
- // Set output var.
</del><span class="cx" style="display: block; padding: 0 10px"> switch ( $seconds ) {
</span><span class="cx" style="display: block; padding: 0 10px"> case YEAR_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of years. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s year', '%s years', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s year', '%s years', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case 30 * DAY_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of months. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s month', '%s months', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s month', '%s months', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case WEEK_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of weeks. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s week', '%s weeks', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[]= sprintf( _n( '%s week', '%s weeks', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case DAY_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of days. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s day', '%s days', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s day', '%s days', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case HOUR_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of hours. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s hour', '%s hours', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s hour', '%s hours', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case MINUTE_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of minutes. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s minute', '%s minutes', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s minute', '%s minutes', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> default:
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of seconds. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output = sprintf( _n( '%s second', '%s seconds', $count, 'buddypress' ), $count );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s second', '%s seconds', $count, 'buddypress' ), $count );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Step two: the second chunk
- // A quirk in the implementation means that this
- // condition fails in the case of minutes and seconds.
- // We've left the quirk in place, since fractions of a
- // minute are not a useful piece of information for our
- // purposes.
- if ( $i + 2 < $j ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Step two: the second chunk.
+ *
+ * A quirk in the implementation means that this condition fails in the case of minutes and seconds.
+ * We've left the quirk in place, since fractions of a minute are not a useful piece of information
+ * for our purposes.
+ */
+ if ( 2 === (int) $r['time_chunks'] && $i + 2 < $j ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $seconds2 = $chunks[$i + 1];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $count2 = floor( ( $diff - ( $seconds * $count ) ) / $seconds2 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Add to output var.
- if ( 0 != $count2 ) {
- $output .= _x( ',', 'Separator in time since', 'buddypress' ) . ' ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Add the second chunk of time diff.
+ if ( 0 !== (int) $count2 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> switch ( $seconds2 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> case 30 * DAY_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of months. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s month', '%s months', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s month', '%s months', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case WEEK_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of weeks. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s week', '%s weeks', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s week', '%s weeks', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case DAY_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of days. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s day', '%s days', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s day', '%s days', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case HOUR_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of hours. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s hour', '%s hours', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s hour', '%s hours', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> case MINUTE_IN_SECONDS :
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of minutes. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s minute', '%s minutes', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s minute', '%s minutes', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px"> default:
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: the number of seconds. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $output .= sprintf( _n( '%s second', '%s seconds', $count2, 'buddypress' ), $count2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval[] = sprintf( _n( '%s second', '%s seconds', $count2, 'buddypress' ), $count2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- // No output, so happened right now.
- if ( ! (int) trim( $output ) ) {
- $output = $right_now_text;
- }
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } else {
+ // Something went wrong with date calculation and we ended up with a negative date.
+ $retval = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Append 'ago' to the end of time-since if not 'right now'.
- if ( $output != $right_now_text ) {
- $output = sprintf( $ago_text, $output );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $retval;
+}
+
+/**
+ * Get an English-language representation of the time elapsed since a given date.
+ *
+ * This function will return an English representation of the time elapsed
+ * since a given date.
+ * eg: 2 hours, 50 minutes
+ * eg: 4 days
+ * eg: 4 weeks, 6 days
+ *
+ * Note that fractions of minutes are not represented in the return string. So
+ * an interval of 3 minutes will be represented by "3 minutes ago", as will an
+ * interval of 3 minutes 59 seconds.
+ *
+ * @since 1.0.0
+ * @since 8.0.0 Move the time difference calculation into `bp_core_time_diff()`.
+ *
+ * @param int|string $older_date The earlier time from which you're calculating
+ * the time elapsed. Enter either as an integer Unix timestamp,
+ * or as a date string of the format 'Y-m-d h:i:s'.
+ * @param int|bool $newer_date Optional. Unix timestamp of date to compare older
+ * date to. Default: false (current time).
+ * @return string String representing the time since the older date, eg
+ * "2 hours, 50 minutes".
+ */
+function bp_core_time_since( $older_date, $newer_date = false ) {
+
+ /**
+ * Filters whether or not to bypass BuddyPress' time_since calculations.
+ *
+ * @since 1.7.0
+ *
+ * @param bool $value Whether or not to bypass.
+ * @param string $older_date Earlier time from which we're calculating time elapsed.
+ * @param string $newer_date Unix timestamp of date to compare older time to.
+ */
+ $pre_value = apply_filters( 'bp_core_time_since_pre', false, $older_date, $newer_date );
+ if ( false !== $pre_value ) {
+ return $pre_value;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $newer_date = (int) $newer_date;
+ $args = array(
+ 'older_date' => $older_date,
+ );
+
+ if ( $newer_date) {
+ $args['newer_date'] = $newer_date;
+ }
+
+ // Calculate the time difference.
+ $time_diff = bp_core_time_diff( $args );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters the value to use if the time since is some time ago.
+ *
+ * @since 1.5.0
+ *
+ * @param string $value String representing the time since the older date.
+ */
+ $ago_text = apply_filters(
+ 'bp_core_time_since_ago_text',
+ /* translators: %s: the human time diff. */
+ __( '%s ago', 'buddypress' )
+ );
+
+ /**
+ * Filters the value to use if the time since is right now.
+ *
+ * @since 1.5.0
+ *
+ * @param string $value String representing the time since the older date.
+ */
+ $output = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', 'buddypress' ) );
+
+ if ( is_array( $time_diff ) ) {
+ $separator = _x( ',', 'Separator in time since', 'buddypress' ) . ' ';
+ $diff_text = implode( $separator, $time_diff );
+ $output = sprintf( $ago_text, $diff_text );
+ } elseif ( false === $time_diff ) {
+ /**
+ * Filters the value to use if the time since is unknown.
+ *
+ * @since 1.5.0
+ *
+ * @param string $value String representing the time since the older date.
+ */
+ $unknown_text = apply_filters( 'bp_core_time_since_unknown_text', __( 'sometime', 'buddypress' ) );
+ $output = sprintf( $ago_text, $unknown_text );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Filters the English-language representation of the time elapsed since a given date.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1402,6 +1442,42 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Get an age to display according to the birth date.
+ *
+ * @since 8.0.0
+ *
+ * @param int|string $birth_date A timestamp or a MySQL formatted date.
+ * @return string The age to display.
+ */
+function bp_core_time_old( $birth_date ) {
+ $time_diff = bp_core_time_diff( array( 'older_date' => $birth_date, 'time_chunks' => 1 ) );
+ $retval = '—';
+
+ if ( $time_diff ) {
+ $age = reset( $time_diff );
+
+ /**
+ * Filters the value to use to display the age.
+ *
+ * @since 8.0.0
+ *
+ * @param string $value String representing the time since the older date.
+ * @param int $age The age.
+ */
+ $age_text = apply_filters(
+ 'bp_core_time_old_text',
+ /* translators: %d: the age . */
+ __( '%s old', 'buddypress' ),
+ $age
+ );
+
+ $retval = sprintf( $age_text, $age );
+ }
+
+ return $retval;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Output an ISO-8601 date from a date string.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.7.0
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypedateboxphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php 2021-04-29 08:31:02 UTC (rev 12930)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php 2021-04-29 10:20:39 UTC (rev 12931)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -479,11 +479,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <div class="bp-date-format-option">
</span><span class="cx" style="display: block; padding: 0 10px"> <label for="date-format-elapsed">
</span><span class="cx" style="display: block; padding: 0 10px"> <input type="radio" name="field-settings[date_format]" id="date-format-elapsed" <?php checked( 'elapsed', $settings['date_format'] ); ?> value="elapsed" aria-describedby="date-format-elapsed-setting" />
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <span class="date-format-label" id="date-format-elapsed-setting"><?php esc_html_e( 'Time elapsed', 'buddypress' ); ?></span> <?php _e( '<code>4 years ago</code>, <code>4 years from now</code>', 'buddypress' ); ?>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <span class="date-format-label" id="date-format-elapsed-setting"><?php esc_html_e( 'Time elapsed', 'buddypress' ); ?></span> <?php printf( '<code>%1$s</code>, <code>%2$s</code>', esc_html__( '4 years ago', 'buddypress' ), esc_html__( '4 years from now', 'buddypress' ) ); ?>
</ins><span class="cx" style="display: block; padding: 0 10px"> </label>
</span><span class="cx" style="display: block; padding: 0 10px"> </div>
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> <div class="bp-date-format-option">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <label for="date-format-age">
+ <input type="radio" name="field-settings[date_format]" id="date-format-age" <?php checked( 'age', $settings['date_format'] ); ?> value="age" aria-describedby="date-format-age-setting" />
+ <span class="date-format-label" id="date-format-age-setting"><?php esc_html_e( 'Age', 'buddypress' ); ?></span> <?php printf( '<code>%s</code>', esc_html__( '20 years old', 'buddypress' ) ); ?>
+ </label>
+ </div>
+
+ <div class="bp-date-format-option">
</ins><span class="cx" style="display: block; padding: 0 10px"> <label for="date-format-custom">
</span><span class="cx" style="display: block; padding: 0 10px"> <input type="radio" name="field-settings[date_format]" id="date-format-custom" <?php checked( 'custom', $settings['date_format'] ); ?> value="custom" />
</span><span class="cx" style="display: block; padding: 0 10px"> <span class="date-format-label"><?php esc_html_e( 'Custom:', 'buddypress' ); ?></span>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -601,6 +608,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $formatted = bp_core_time_since( $field_value );
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ case 'age' :
+ $formatted = bp_core_time_old( $field_value );
+ break;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> case 'custom' :
</span><span class="cx" style="display: block; padding: 0 10px"> $formatted = date_i18n( $settings['date_format_custom'], $field_value );
</span><span class="cx" style="display: block; padding: 0 10px"> break;
</span></span></pre>
</div>
</div>
</body>
</html>