Make WordPress Core

Ticket #32228: message-timeago.32228.diff

File message-timeago.32228.diff, 10.9 KB (added by iambriansreed, 8 years ago)

Message timeago. (my first patch)

  • src/wp-admin/edit-form-advanced.php

    From cf322bb14f375a50dba282e095d99b61967c8ad2 Mon Sep 17 00:00:00 2001
    From: Brian Reed <me@iambrian.com>
    Date: Sat, 5 Dec 2015 16:28:24 -0500
    Subject: [PATCH] Resolves #32228
    
    ---
     src/wp-admin/edit-form-advanced.php         |   9 +-
     src/wp-includes/js/jquery/jquery.timeago.js | 225 ++++++++++++++++++++++++++++
     src/wp-includes/js/timeago/init.js          |   3 +
     src/wp-includes/script-loader.php           |   2 +
     4 files changed, 237 insertions(+), 2 deletions(-)
     create mode 100644 src/wp-includes/js/jquery/jquery.timeago.js
     create mode 100644 src/wp-includes/js/timeago/init.js
    
    diff --git a/src/wp-admin/edit-form-advanced.php b/src/wp-admin/edit-form-advanced.php
    index aff0413..490197c 100644
    a b if ( $viewable ) { 
    132132/* translators: Publish box date format, see http://php.net/date */
    133133$scheduled_date = date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_date ) );
    134134
     135$saved_timeago = ' <em><abbr title="' . date( 'c', current_time( 'timestamp' ) ) . '" class="xwp-ui-text-icon timeago">Just Now</abbr></em>';
     136
    135137$messages['post'] = array(
    136138         0 => '', // Unused. Messages start at index 1.
    137          1 => __( 'Post updated.' ) . $view_post_link_html,
     139         1 => __( 'Post updated ' . $saved_timeago . '.' ) . $view_post_link_html,
    138140         2 => __( 'Custom field updated.' ),
    139141         3 => __( 'Custom field deleted.' ),
    140142         4 => __( 'Post updated.' ),
    $messages['attachment'] = array_fill( 1, 10, __( 'Media attachment updated.' ) ) 
    171173 */
    172174$messages = apply_filters( 'post_updated_messages', $messages );
    173175
     176wp_enqueue_script( 'jquery-timeago-init' );
     177
    174178$message = false;
    175179if ( isset($_GET['message']) ) {
     180       
    176181        $_GET['message'] = absint( $_GET['message'] );
    177182        if ( isset($messages[$post_type][$_GET['message']]) )
    178183                $message = $messages[$post_type][$_GET['message']];
    if ( post_type_supports( $post_type, 'comments' ) ) 
    744749<script type="text/javascript">
    745750try{document.post.title.focus();}catch(e){}
    746751</script>
    747 <?php endif; ?>
     752<?php endif; ?>
     753 No newline at end of file
  • new file src/wp-includes/js/jquery/jquery.timeago.js

    diff --git a/src/wp-includes/js/jquery/jquery.timeago.js b/src/wp-includes/js/jquery/jquery.timeago.js
    new file mode 100644
    index 0000000..2fb0ad4
    - +  
     1/**
     2 * Timeago is a jQuery plugin that makes it easy to support automatically
     3 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
     4 *
     5 * @name timeago
     6 * @version 1.4.3
     7 * @requires jQuery v1.2.3+
     8 * @author Ryan McGeary
     9 * @license MIT License - http://www.opensource.org/licenses/mit-license.php
     10 *
     11 * For usage and examples, visit:
     12 * http://timeago.yarp.com/
     13 *
     14 * Copyright (c) 2008-2015, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
     15 */
     16
     17(function (factory) {
     18  if (typeof define === 'function' && define.amd) {
     19    // AMD. Register as an anonymous module.
     20    define(['jquery'], factory);
     21  } else if (typeof module === 'object' && typeof module.exports === 'object') {
     22    factory(require('jquery'));
     23  } else {
     24    // Browser globals
     25    factory(jQuery);
     26  }
     27}(function ($) {
     28  $.timeago = function(timestamp) {
     29    if (timestamp instanceof Date) {
     30      return inWords(timestamp);
     31    } else if (typeof timestamp === "string") {
     32      return inWords($.timeago.parse(timestamp));
     33    } else if (typeof timestamp === "number") {
     34      return inWords(new Date(timestamp));
     35    } else {
     36      return inWords($.timeago.datetime(timestamp));
     37    }
     38  };
     39  var $t = $.timeago;
     40
     41  $.extend($.timeago, {
     42    settings: {
     43      refreshMillis: 60000,
     44      allowPast: true,
     45      allowFuture: false,
     46      localeTitle: false,
     47      cutoff: 0,
     48      autoDispose: true,
     49      strings: {
     50        prefixAgo: null,
     51        prefixFromNow: null,
     52        suffixAgo: "ago",
     53        suffixFromNow: "from now",
     54        inPast: 'any moment now',
     55        seconds: "less than a minute",
     56        minute: "about a minute",
     57        minutes: "%d minutes",
     58        hour: "about an hour",
     59        hours: "about %d hours",
     60        day: "a day",
     61        days: "%d days",
     62        month: "about a month",
     63        months: "%d months",
     64        year: "about a year",
     65        years: "%d years",
     66        wordSeparator: " ",
     67        numbers: []
     68      }
     69    },
     70
     71    inWords: function(distanceMillis) {
     72      if(!this.settings.allowPast && ! this.settings.allowFuture) {
     73          throw 'timeago allowPast and allowFuture settings can not both be set to false.';
     74      }
     75
     76      var $l = this.settings.strings;
     77      var prefix = $l.prefixAgo;
     78      var suffix = $l.suffixAgo;
     79      if (this.settings.allowFuture) {
     80        if (distanceMillis < 0) {
     81          prefix = $l.prefixFromNow;
     82          suffix = $l.suffixFromNow;
     83        }
     84      }
     85
     86      if(!this.settings.allowPast && distanceMillis >= 0) {
     87        return this.settings.strings.inPast;
     88      }
     89
     90      var seconds = Math.abs(distanceMillis) / 1000;
     91      var minutes = seconds / 60;
     92      var hours = minutes / 60;
     93      var days = hours / 24;
     94      var years = days / 365;
     95
     96      function substitute(stringOrFunction, number) {
     97        var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
     98        var value = ($l.numbers && $l.numbers[number]) || number;
     99        return string.replace(/%d/i, value);
     100      }
     101
     102      var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
     103        seconds < 90 && substitute($l.minute, 1) ||
     104        minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
     105        minutes < 90 && substitute($l.hour, 1) ||
     106        hours < 24 && substitute($l.hours, Math.round(hours)) ||
     107        hours < 42 && substitute($l.day, 1) ||
     108        days < 30 && substitute($l.days, Math.round(days)) ||
     109        days < 45 && substitute($l.month, 1) ||
     110        days < 365 && substitute($l.months, Math.round(days / 30)) ||
     111        years < 1.5 && substitute($l.year, 1) ||
     112        substitute($l.years, Math.round(years));
     113
     114      var separator = $l.wordSeparator || "";
     115      if ($l.wordSeparator === undefined) { separator = " "; }
     116      return $.trim([prefix, words, suffix].join(separator));
     117    },
     118
     119    parse: function(iso8601) {
     120      var s = $.trim(iso8601);
     121      s = s.replace(/\.\d+/,""); // remove milliseconds
     122      s = s.replace(/-/,"/").replace(/-/,"/");
     123      s = s.replace(/T/," ").replace(/Z/," UTC");
     124      s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
     125      s = s.replace(/([\+\-]\d\d)$/," $100"); // +09 -> +0900
     126      return new Date(s);
     127    },
     128    datetime: function(elem) {
     129      var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
     130      return $t.parse(iso8601);
     131    },
     132    isTime: function(elem) {
     133      // jQuery's `is()` doesn't play well with HTML5 in IE
     134      return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
     135    }
     136  });
     137
     138  // functions that can be called via $(el).timeago('action')
     139  // init is default when no action is given
     140  // functions are called with context of a single element
     141  var functions = {
     142    init: function(){
     143      var refresh_el = $.proxy(refresh, this);
     144      refresh_el();
     145      var $s = $t.settings;
     146      if ($s.refreshMillis > 0) {
     147        this._timeagoInterval = setInterval(refresh_el, $s.refreshMillis);
     148      }
     149    },
     150    update: function(time){
     151      var parsedTime = $t.parse(time);
     152      $(this).data('timeago', { datetime: parsedTime });
     153      if($t.settings.localeTitle) $(this).attr("title", parsedTime.toLocaleString());
     154      refresh.apply(this);
     155    },
     156    updateFromDOM: function(){
     157      $(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
     158      refresh.apply(this);
     159    },
     160    dispose: function () {
     161      if (this._timeagoInterval) {
     162        window.clearInterval(this._timeagoInterval);
     163        this._timeagoInterval = null;
     164      }
     165    }
     166  };
     167
     168  $.fn.timeago = function(action, options) {
     169    var fn = action ? functions[action] : functions.init;
     170    if(!fn){
     171      throw new Error("Unknown function name '"+ action +"' for timeago");
     172    }
     173    // each over objects here and call the requested function
     174    this.each(function(){
     175      fn.call(this, options);
     176    });
     177    return this;
     178  };
     179
     180  function refresh() {
     181    var $s = $t.settings;
     182
     183    //check if it's still visible
     184    if($s.autoDispose && !$.contains(document.documentElement,this)){
     185      //stop if it has been removed
     186      $(this).timeago("dispose");
     187      return this;
     188    }
     189
     190    var data = prepareData(this);
     191
     192    if (!isNaN(data.datetime)) {
     193      if ( $s.cutoff == 0 || Math.abs(distance(data.datetime)) < $s.cutoff) {
     194        $(this).text(inWords(data.datetime));
     195      }
     196    }
     197    return this;
     198  }
     199
     200  function prepareData(element) {
     201    element = $(element);
     202    if (!element.data("timeago")) {
     203      element.data("timeago", { datetime: $t.datetime(element) });
     204      var text = $.trim(element.text());
     205      if ($t.settings.localeTitle) {
     206        element.attr("title", element.data('timeago').datetime.toLocaleString());
     207      } else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
     208        element.attr("title", text);
     209      }
     210    }
     211    return element.data("timeago");
     212  }
     213
     214  function inWords(date) {
     215    return $t.inWords(distance(date));
     216  }
     217
     218  function distance(date) {
     219    return (new Date().getTime() - date.getTime());
     220  }
     221
     222  // fix for IE6 suckage
     223  document.createElement("abbr");
     224  document.createElement("time");
     225}));
  • new file src/wp-includes/js/timeago/init.js

    diff --git a/src/wp-includes/js/timeago/init.js b/src/wp-includes/js/timeago/init.js
    new file mode 100644
    index 0000000..9d34a59
    - +  
     1jQuery(document).ready(function() {
     2  jQuery("abbr.timeago").timeago();
     3});
     4 No newline at end of file
  • src/wp-includes/script-loader.php

    diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php
    index 2ac34ed..cc7930b 100644
    a b function wp_default_scripts( &$scripts ) { 
    240240        $scripts->add( 'jquery-serialize-object', "/wp-includes/js/jquery/jquery.serialize-object.js", array('jquery'), '0.2', 1 );
    241241        $scripts->add( 'jquery-hotkeys', "/wp-includes/js/jquery/jquery.hotkeys$suffix.js", array('jquery'), '0.0.2m', 1 );
    242242        $scripts->add( 'jquery-table-hotkeys', "/wp-includes/js/jquery/jquery.table-hotkeys$suffix.js", array('jquery', 'jquery-hotkeys'), false, 1 );
     243        $scripts->add( 'jquery-timeago', "/wp-includes/js/jquery/jquery.timeago$suffix.js", array('jquery' ), false, 1 );
     244        $scripts->add( 'jquery-timeago-init', "/wp-includes/js/timeago/init$suffix.js", array('jquery', 'jquery-timeago' ), false, 1 );
    243245        $scripts->add( 'jquery-touch-punch', "/wp-includes/js/jquery/jquery.ui.touch-punch.js", array('jquery-ui-widget', 'jquery-ui-mouse'), '0.2.2', 1 );
    244246
    245247        // Masonry v2 depended on jQuery. v3 does not. The older jquery-masonry handle is a shiv.