Make WordPress Core

Changeset 53158


Ignore:
Timestamp:
04/12/2022 03:38:42 PM (3 months ago)
Author:
audrasjb
Message:

Widgets: Avoid 27 duplicate translations in Media Widgets constructor.

This changeset rationalizes the declaration of translation strings in the Media Widgets constructor.

Props Chouby, audrasjb, peterwilsoncc, costdev.
Fixes #54561.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r53011 r53158  
    631631add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 );
    632632add_action( 'init', 'wp_widgets_init', 1 );
     633add_action( 'change_locale', array( 'WP_Widget_Media', 'reset_default_labels' ) );
    633634
    634635// Admin Bar.
  • trunk/src/wp-includes/widgets/class-wp-widget-media.php

    r53136 r53158  
    4343
    4444    /**
     45     * The default widget description.
     46     *
     47     * @since 6.0.0
     48     * @var string
     49     */
     50    protected static $default_description = '';
     51
     52    /**
     53     * The default localized strings used by the widget.
     54     *
     55     * @since 6.0.0
     56     * @var string[]
     57     */
     58    protected static $l10n_defaults = array();
     59
     60    /**
    4561     * Constructor.
    4662     *
     
    5874            $widget_options,
    5975            array(
    60                 'description'                 => __( 'A media item.' ),
     76                'description'                 => self::get_default_description(),
    6177                'customize_selective_refresh' => true,
    6278                'show_instance_in_rest'       => true,
     
    6783        $control_opts = wp_parse_args( $control_options, array() );
    6884
    69         $l10n_defaults = array(
    70             'no_media_selected'          => __( 'No media selected' ),
    71             'add_media'                  => _x( 'Add Media', 'label for button in the media widget' ),
    72             'replace_media'              => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    73             'edit_media'                 => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    74             'add_to_widget'              => __( 'Add to Widget' ),
    75             'missing_attachment'         => sprintf(
    76                 /* translators: %s: URL to media library. */
    77                 __( 'That file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
    78                 esc_url( admin_url( 'upload.php' ) )
    79             ),
    80             /* translators: %d: Widget count. */
    81             'media_library_state_multi'  => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
    82             'media_library_state_single' => __( 'Media Widget' ),
    83             'unsupported_file_type'      => __( 'Looks like this is not the correct kind of file. Please link to an appropriate file instead.' ),
    84         );
    85         $this->l10n    = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
     85        $this->l10n = array_merge( self::get_l10n_defaults(), array_filter( $this->l10n ) );
    8686
    8787        parent::__construct(
     
    442442
    443443    /**
     444     * Resets the cache for the default labels.
     445     *
     446     * @since 6.0.0
     447     */
     448    public static function reset_default_labels() {
     449        self::$default_description = '';
     450        self::$l10n_defaults       = array();
     451    }
     452
     453    /**
    444454     * Whether the widget has content to show.
    445455     *
     
    452462        return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
    453463    }
     464
     465    /**
     466     * Returns the default description of the widget.
     467     *
     468     * @since 6.0.0
     469     *
     470     * @var string
     471     */
     472    protected static function get_default_description() {
     473        if ( self::$default_description ) {
     474            return self::$default_description;
     475        }
     476
     477        self::$default_description = __( 'A media item.' );
     478        return self::$default_description;
     479    }
     480
     481    /**
     482     * Returns the default localized strings used by the widget.
     483     *
     484     * @since 6.0.0
     485     *
     486     * @return string[]
     487     */
     488    protected static function get_l10n_defaults() {
     489        if ( ! empty( self::$l10n_defaults ) ) {
     490            return self::$l10n_defaults;
     491        }
     492
     493        self::$l10n_defaults = array(
     494            'no_media_selected'          => __( 'No media selected' ),
     495            'add_media'                  => _x( 'Add Media', 'label for button in the media widget' ),
     496            'replace_media'              => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
     497            'edit_media'                 => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
     498            'add_to_widget'              => __( 'Add to Widget' ),
     499            'missing_attachment'         => sprintf(
     500                /* translators: %s: URL to media library. */
     501                __( 'That file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
     502                esc_url( admin_url( 'upload.php' ) )
     503            ),
     504            /* translators: %d: Widget count. */
     505            'media_library_state_multi'  => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
     506            'media_library_state_single' => __( 'Media Widget' ),
     507            'unsupported_file_type'      => __( 'Looks like this is not the correct kind of file. Please link to an appropriate file instead.' ),
     508        );
     509
     510        return self::$l10n_defaults;
     511    }
    454512}
Note: See TracChangeset for help on using the changeset viewer.