Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added 'hybrid_get_time_since' to the utilities functions.

Adds the 'hybrid_get_time_since' function taken from bbpress to allow users to return human readable time. Also added one extra attribute of 'human_time' to '[entry-published]' shortcode and added attributes to the '[comment-published]' shortcode.
  • Loading branch information...
commit 953c88977de62c4592114a7f83fb2ed1f5fa4b0f 1 parent 99990ab
@dronix authored
Showing with 111 additions and 5 deletions.
  1. +21 −4 functions/shortcodes.php
  2. +90 −1 functions/utility.php
View
25 functions/shortcodes.php
@@ -213,9 +213,17 @@ function hybrid_entry_edit_link_shortcode( $attr ) {
* @return string
*/
function hybrid_entry_published_shortcode( $attr ) {
- $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'format' => get_option( 'date_format' ) ), $attr );
+ $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'format' => get_option( 'date_format' ), 'human_time' => false ), $attr );
+
+ /* Change format to 'time ago' if human_time is enabled. */
+ if ( true == (bool)$attr['human_time'] ) {
+ $attr['format'] = hybrid_get_time_since( get_the_time('U'), current_time('timestamp') );
+ $format = $attr['format'];
+ } else {
+ $format = get_the_time( $attr['format'] );
+ }
- $published = '<abbr class="published" title="' . get_the_time( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_the_time( $attr['format'] ) . '</abbr>';
+ $published = '<abbr class="published" title="' . get_the_time( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . $format . '</abbr>';
return $attr['before'] . $published . $attr['after'];
}
@@ -374,8 +382,17 @@ function hybrid_post_format_link_shortcode( $attr ) {
* @access public
* @return string
*/
-function hybrid_comment_published_shortcode() {
- $link = '<span class="published">' . sprintf( __( '%1$s at %2$s', 'hybrid-core' ), '<abbr class="comment-date" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_comment_date() . '</abbr>', '<abbr class="comment-time" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_comment_time() . '</abbr>' ) . '</span>';
+function hybrid_comment_published_shortcode( $attr ) {
+ $attr = shortcode_atts( array( 'format' => get_comment_date(), 'human_time' => false ), $atts);
+
+ /* Change format to 'time ago' if human_time is enabled. */
+ if ( true == (bool)$attr['human_time'] ) {
+ $attr['format'] = hybrid_get_time_since( get_comment_time('U'), current_time('timestamp') );
+ $link = '<span class="published">' . sprintf( __( '%1$s', 'hybrid-core' ), '<abbr class="comment-date" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . $attr['format'] . '</abbr>' ) . '</span>';
+ } else {
+ $link = '<span class="published">' . sprintf( __( '%1$s at %2$s', 'hybrid-core' ), '<abbr class="comment-date" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . $attr['format'] . '</abbr>', '<abbr class="comment-time" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_comment_time() . '</abbr>' ) . '</span>';
+ }
+
return $link;
}
View
91 functions/utility.php
@@ -14,11 +14,14 @@
/* Add extra support for post types. */
add_action( 'init', 'hybrid_add_post_type_support' );
+/* Initialize the human readable time difference function. */
+add_action( 'init', 'hybrid_get_time_since' );
+
/* Add extra file headers for themes. */
add_filter( 'extra_theme_headers', 'hybrid_extra_theme_headers' );
/**
- * This function is for adding extra support for features not default to the core post types.
+ * This function is for adding extra support for features not default to the core post ypes.
* Excerpts are added to the 'page' post type. Comments and trackbacks are added for the
* 'attachment' post type. Technically, these are already used for attachments in core, but
* they're not registered.
@@ -244,4 +247,90 @@ function hybrid_locate_theme_file( $file_names ) {
return $located;
}
+/**
+ * Return the difference between two timestamps in
+ * a human readable format. E.g '10 minutes ago'.
+ *
+ * Function credit to the bbpress folks. - http://bbpress.org/
+ *
+ * @since 1.5.0
+ * @param string $older_date Unix timestamp from which the difference begins.
+ * @param string $newer_date Unix timestamp to end the time difference. Defaults to false.
+ * @return string Formated time.
+ */
+function hybrid_get_time_since( $older_date, $newer_date = false ) {
+
+ /* Setup the strings. */
+ $unknown_text = apply_filters( 'hybrid_core_time_since_unknown_text', __( 'sometime', 'hybrid-core' ) );
+ $right_now_text = apply_filters( 'hybrid_core_time_since_right_now_text', __( 'right now', 'hybrid-core' ) );
+ $ago_text = apply_filters( 'hybrid_core_time_since_ago_text', __( '%s ago', 'hybrid-core' ) );
+
+ /* Array of time period chunks. */
+ $chunks = array(
+ array( 60 * 60 * 24 * 365 , __( 'year', 'hybrid-core' ), __( 'years', 'hybrid-core' ) ),
+ array( 60 * 60 * 24 * 30 , __( 'month', 'hybrid-core' ), __( 'months', 'hybrid-core' ) ),
+ array( 60 * 60 * 24 * 7, __( 'week', 'hybrid-core' ), __( 'weeks', 'hybrid-core' ) ),
+ array( 60 * 60 * 24 , __( 'day', 'hybrid-core' ), __( 'days', 'hybrid-core' ) ),
+ array( 60 * 60 , __( 'hour', 'hybrid-core' ), __( 'hours', 'hybrid-core' ) ),
+ array( 60 , __( 'minute', 'hybrid-core' ), __( 'minutes', 'hybrid-core' ) ),
+ array( 1, __( 'second', 'hybrid-core' ), __( 'seconds', 'hybrid-core' ) )
+ );
+
+ 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] );
+ }
+
+ /* $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 ) ? strtotime( current_time( 'mysql' ) ) : $newer_date;
+
+ /* Difference in seconds. */
+ $since = $newer_date - $older_date;
+
+ /* Something went wrong with date calculation and we ended up with a negative date. */
+ if ( 0 > $since ) {
+ $output = $unknown_text;
+
+ /* We only want to output two chunks of time here, eg:
+ x years
+ x months,
+ x days,
+ x hours
+ so there's only one bit of calculation below: */
+ } else {
+
+ /* Step one: the first chunk. */
+ for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
+ $seconds = $chunks[$i][0];
+
+ /* Finding the biggest chunk (if the chunk fits, break). */
+ $count = floor( $since / $seconds );
+ if ( 0 != $count ) {
+ break;
+ }
+ }
+
+ /* If $i iterates all the way to $j, then the event happened 0 seconds ago. */
+ if ( !isset( $chunks[$i] ) ) {
+ $output = $right_now_text;
+
+ } else {
+
+ /* Set output var. */
+ $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
+
+ }
+ }
+
+ /* Append 'ago' to the end of time-since if not 'right now'. */
+ if ( $output != $right_now_text ) {
+ $output = sprintf( $ago_text, $output );
+ }
+
+ return apply_filters( 'hybrid_get_time_since', $output, $older_date, $newer_date );
+}
+
?>
Please sign in to comment.
Something went wrong with that request. Please try again.