Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/10/2016 12:55:49 PM (10 years ago)
Author:
djpaul
Message:

Fix inaccurate timestamps caused by page caching.

If any template with a timestamp is statically cached, then the timestamps quickly become inaccurate.
The change adds livestamp.js and moment.js to dynamically update timestamps with the real relative time.

A consequence is that the labels surrounding the timestamps, notably in our widgets, have slightly changed.
We're going to keep an eye on this for the remainder of the 2.7 development cycle, and may make further adjustments as testing proves necessary.

Fixes #5757

Props r-a-y, imath, DJPaul

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-cssjs.php

    r10899 r11008  
    4848                'bp-cover-image' => array( 'file' => "{$url}cover-image{$min}.js", 'dependencies' => array(), 'footer' => true ),
    4949
     50                // Version 2.7.
     51                'bp-moment'    => array( 'file' => "{$url}moment{$min}.js", 'dependencies' => array(), 'footer' => true ),
     52                'bp-livestamp' => array( 'file' => "{$url}livestamp{$min}.js", 'dependencies' => array( 'jquery', 'bp-moment' ), 'footer' => true ),
    5053        ) );
    5154
     
    461464}
    462465add_action( 'bp_enqueue_scripts', 'bp_add_cover_image_inline_css', 11 );
     466
     467/**
     468 * Enqueues livestamp.js on BuddyPress pages.
     469 *
     470 * @since 2.7.0
     471 */
     472function bp_core_add_livestamp() {
     473        if ( ! is_buddypress() ) {
     474                return;
     475        }
     476
     477        bp_core_enqueue_livestamp();
     478}
     479add_action( 'bp_enqueue_scripts', 'bp_core_add_livestamp' );
     480
     481/**
     482 * Enqueue and localize livestamp.js script.
     483 *
     484 * @since 2.7.0
     485 */
     486function bp_core_enqueue_livestamp() {
     487        // If bp-livestamp isn't enqueued, do it now.
     488        if ( wp_script_is( 'bp-livestamp' ) ) {
     489                return;
     490        }
     491
     492        wp_enqueue_script( 'bp-livestamp' );
     493
     494        // We're only localizing the relative time strings for moment.js since that's all we need for now.
     495        wp_localize_script( 'bp-livestamp', 'BP_Moment_i18n', array(
     496                'future' => __( 'in %s',         'buddypress' ),
     497                'past'   => __( '%s ago',        'buddypress' ),
     498                's'      => __( 'a few seconds', 'buddypress' ),
     499                'm'      => __( 'a minute',      'buddypress' ),
     500                'mm'     => __( '%d minutes',    'buddypress' ),
     501                'h'      => __( 'an hour',       'buddypress' ),
     502                'hh'     => __( '%d hours',      'buddypress' ),
     503                'd'      => __( 'a day',         'buddypress' ),
     504                'dd'     => __( '%d days',       'buddypress' ),
     505                'M'      => __( 'a month',       'buddypress' ),
     506                'MM'     => __( '%d months',     'buddypress' ),
     507                'y'      => __( 'a year',        'buddypress' ),
     508                'yy'     => __( '%d years',      'buddypress' ),
     509        ) );
     510
     511        if ( function_exists( 'wp_add_inline_script' ) ) {
     512                wp_add_inline_script ( 'bp-livestamp', bp_core_moment_js_config() );
     513        } else {
     514                add_action( 'wp_footer', '_bp_core_moment_js_config_footer', 20 );
     515        }
     516}
     517
     518/**
     519 * Return moment.js config.
     520 *
     521 * @since 2.7.0
     522 *
     523 * @return string
     524 */
     525function bp_core_moment_js_config() {
     526        $inline_js = <<<EOD
     527jQuery(function() {
     528        moment.locale( 'bp', {
     529                relativeTime : BP_Moment_i18n
     530        });
     531});
     532EOD;
     533
     534        return $inline_js;
     535}
     536
     537/**
     538 * Print moment.js config in page footer.
     539 *
     540 * Will be removed once we set our minimum version of WP 4.5.
     541 *
     542 * @since 2.7.0
     543 *
     544 * @access private
     545 */
     546function _bp_core_moment_js_config_footer() {
     547        if ( ! wp_script_is( 'bp-livestamp' ) ) {
     548                return;
     549        }
     550
     551        printf( '<script>%s</script>', bp_core_moment_js_config() );
     552}
Note: See TracChangeset for help on using the changeset viewer.