Skip to:
Content

BuddyPress.org

Changeset 13373


Ignore:
Timestamp:
11/24/2022 11:14:42 PM (2 years ago)
Author:
imath
Message:

Deprecate bp-moment JS dependency in favor of WP's moment one

Doing so is taking care of using a fresher version of moment.js (v2.29.4).

Props thomaslhotta, dcavins

Closes https://github.com/buddypress/buddypress/pull/39
Fixes #8766

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

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

    r13306 r13373  
    2020    $url = buddypress()->plugin_url . 'bp-core/js/';
    2121
     22    // Is WordPress' moment dist library registered?
     23    $is_moment_registered = wp_script_is( 'moment', 'registered' );
     24    $moment_locale_url    = '';
     25
    2226    /*
    23      * Moment.js locale.
    24      *
    25      * Try to map current WordPress locale to a moment.js locale file for loading.
    26      *
    27      * eg. French (France) locale for WP is fr_FR. Here, we try to find fr-fr.js
    28      *     (this file doesn't exist).
    29      */
    30     $wp_locale = sanitize_file_name( strtolower( get_locale() ) );
    31 
    32     // WP uses ISO 639-2 or -3 codes for some locales, which we must translate back to ISO 639-1.
    33     $iso_locales = array(
    34         'bel' => 'be',
    35         'bre' => 'br',
    36         'kir' => 'ky',
    37         'mri' => 'mi',
    38         'ssw' => 'ss',
    39     );
    40 
    41     if ( isset( $iso_locales[ $wp_locale ] ) ) {
    42         $locale = $iso_locales[ $wp_locale ];
    43     } else {
    44         $locale = $wp_locale;
    45     }
    46 
    47     $locale = str_replace( '_', '-', $locale );
    48     if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
    49         $moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
    50 
    51     /*
    52      * Try to find the short-form locale.
    53      *
    54      * eg. French (France) locale for WP is fr_FR. Here, we try to find fr.js
    55      *     (this exists).
    56      */
    57     } else {
    58         $locale = substr( $locale, 0, strpos( $locale, '-' ) );
     27     * In 11.0.0 we are deprecating `bp-moment` in favor of WordPress' already bundled `moment`.
     28     * @todo completely remove `bp-moment` in 12.0.0.
     29     */
     30    if ( ! $is_moment_registered ) {
     31        /*
     32        * Moment.js locale.
     33        *
     34        * Try to map current WordPress locale to a moment.js locale file for loading.
     35        *
     36        * eg. French (France) locale for WP is fr_FR. Here, we try to find fr-fr.js
     37        *     (this file doesn't exist).
     38        */
     39        $wp_locale = sanitize_file_name( strtolower( get_locale() ) );
     40
     41        // WP uses ISO 639-2 or -3 codes for some locales, which we must translate back to ISO 639-1.
     42        $iso_locales = array(
     43            'bel' => 'be',
     44            'bre' => 'br',
     45            'kir' => 'ky',
     46            'mri' => 'mi',
     47            'ssw' => 'ss',
     48        );
     49
     50        if ( isset( $iso_locales[ $wp_locale ] ) ) {
     51            $locale = $iso_locales[ $wp_locale ];
     52        } else {
     53            $locale = $wp_locale;
     54        }
     55
     56        $locale = str_replace( '_', '-', $locale );
    5957        if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
    6058            $moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
     59
     60        /*
     61        * Try to find the short-form locale.
     62        *
     63        * eg. French (France) locale for WP is fr_FR. Here, we try to find fr.js
     64        *     (this exists).
     65        */
     66        } else {
     67            $locale = substr( $locale, 0, strpos( $locale, '-' ) );
     68            if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
     69                $moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
     70            }
    6171        }
    6272    }
     
    8494
    8595        // Version 2.7.
    86         'bp-moment'    => array( 'file' => "{$url}vendor/moment-js/moment{$min}.js", 'dependencies' => array(), 'footer' => true ),
    87         'bp-livestamp' => array( 'file' => "{$url}vendor/livestamp{$min}.js", 'dependencies' => array( 'jquery', 'bp-moment' ), 'footer' => true ),
     96        'bp-livestamp' => array( 'file' => "{$url}vendor/livestamp{$min}.js", 'dependencies' => array( 'jquery', 'moment' ), 'footer' => true ),
    8897
    8998        // Version 9.0.
     
    91100    );
    92101
    93     // Version 2.7 - Add Moment.js locale to our $scripts array if we found one.
    94     if ( isset( $moment_locale_url ) ) {
    95         $scripts['bp-moment-locale'] = array( 'file' => esc_url( $moment_locale_url ), 'dependencies' => array( 'bp-moment' ), 'footer' => true );
     102    /*
     103     * In 11.0.0 we are deprecating `bp-moment` in favor of WordPress' already bundled `moment`.
     104     * @todo completely remove `bp-moment` in 12.0.0.
     105     */
     106    if ( ! $is_moment_registered ) {
     107        $scripts['bp-moment']         = array( 'file' => "{$url}vendor/moment-js/moment{$min}.js", 'dependencies' => array(), 'footer' => true );
     108        $bp_livestamp                 = $scripts['bp-livestamp'];
     109        $bp_livestamp['dependencies'] = array( 'jquery', 'bp-moment' );
     110
     111        // Reset 'bp-livestamp' after 'bp-moment'.
     112        unset( $scripts['bp-livestamp'] );
     113        $scripts['bp-livestamp'] = $bp_livestamp;
     114
     115        // Version 2.7 - Add Moment.js locale to our $scripts array if we found one.
     116        if ( $moment_locale_url ) {
     117            $scripts['bp-moment-locale'] = array( 'file' => esc_url( $moment_locale_url ), 'dependencies' => array( 'bp-moment' ), 'footer' => true );
     118        }
    96119    }
    97120
     
    556579    if ( wp_script_is( 'bp-moment-locale', 'registered' ) ) {
    557580        wp_enqueue_script( 'bp-moment-locale' );
    558         wp_add_inline_script ( 'bp-livestamp', bp_core_moment_js_config() );
     581        wp_add_inline_script( 'bp-livestamp', bp_core_moment_js_config() );
     582    } else {
     583        wp_add_inline_script(
     584            'moment',
     585            sprintf(
     586                "moment.updateLocale( '%s', %s );",
     587                get_user_locale(),
     588                wp_json_encode(
     589                    array(
     590                        'relativeTime' => array(
     591                            /* Translators: %s is the relative time (eg: in a few seconds). */
     592                            'future' => __( 'in %s', 'buddypress' ),
     593                            /* Translators: %s is the relative time (eg: 10 minutes ago). */
     594                            'past'   => __( '%s ago', 'buddypress' ),
     595                            's'      => __( 'a few seconds', 'buddypress' ),
     596                            'm'      => __( 'a minute', 'buddypress' ),
     597                            /* Translators: %d is the amount of minutes. */
     598                            'mm'     => __( '%d minutes', 'buddypress' ),
     599                            'h'      => __( 'an hour', 'buddypress' ),
     600                            /* Translators: %d is the amount of hours. */
     601                            'hh'     => __( '%d hours', 'buddypress' ),
     602                            'd'      => __( 'a day', 'buddypress' ),
     603                            /* Translators: %d is the amount of days. */
     604                            'dd'     => __( '%d days', 'buddypress' ),
     605                            'M'      => __( 'a month', 'buddypress' ),
     606                            /* Translators: %d is the amount of months. */
     607                            'MM'     => __( '%d months', 'buddypress' ),
     608                            'y'      => __( 'a year', 'buddypress' ),
     609                            /* Translators: %d is the amount of years. */
     610                            'yy'     => __( '%d years', 'buddypress' ),
     611                        ),
     612                    )
     613                )
     614            )
     615        );
    559616    }
    560617
     
    566623 *
    567624 * @since 2.7.0
     625 * @deprecated 11.0.0 Soflty deprecated as we're keeping the function into this file
     626 *                    to avoid fatal errors if deprecated code is ignored.
    568627 *
    569628 * @return string
    570629 */
    571630function bp_core_moment_js_config() {
     631    _deprecated_function( __FUNCTION__, '11.0.0' );
     632
    572633    // Grab the locale from the enqueued JS.
    573634    $moment_locale = wp_scripts()->query( 'bp-moment-locale' );
  • trunk/src/bp-core/classes/class-bp-admin.php

    r13334 r13373  
    11391139                <a href="https://mattbradley.github.io/livestampjs/">Livestamp.js</a>,
    11401140                <a href="https://www.mediawiki.org/wiki/MediaWiki">MediaWiki</a>,
    1141                 <a href="https://momentjs.com/">Moment.js</a>,
    11421141                <a href="https://wordpress.org">WordPress</a>.
    11431142            </p>
Note: See TracChangeset for help on using the changeset viewer.