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.

Built from https://develop.svn.wordpress.org/trunk@53158


git-svn-id: http://core.svn.wordpress.org/trunk@52747 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
audrasjb 2022-04-12 15:40:11 +00:00
parent ab8a964858
commit bb723f9920
3 changed files with 78 additions and 19 deletions

View File

@ -630,6 +630,7 @@ add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
// Widgets.
add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 );
add_action( 'init', 'wp_widgets_init', 1 );
add_action( 'change_locale', array( 'WP_Widget_Media', 'reset_default_labels' ) );
// Admin Bar.
// Don't remove. Wrong way to disable.

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-53157';
$wp_version = '6.0-alpha-53158';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -41,6 +41,22 @@ abstract class WP_Widget_Media extends WP_Widget {
*/
protected $registered = false;
/**
* The default widget description.
*
* @since 6.0.0
* @var string
*/
protected static $default_description = '';
/**
* The default localized strings used by the widget.
*
* @since 6.0.0
* @var string[]
*/
protected static $l10n_defaults = array();
/**
* Constructor.
*
@ -57,7 +73,7 @@ abstract class WP_Widget_Media extends WP_Widget {
$widget_opts = wp_parse_args(
$widget_options,
array(
'description' => __( 'A media item.' ),
'description' => self::get_default_description(),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
'mime_type' => '',
@ -66,23 +82,7 @@ abstract class WP_Widget_Media extends WP_Widget {
$control_opts = wp_parse_args( $control_options, array() );
$l10n_defaults = array(
'no_media_selected' => __( 'No media selected' ),
'add_media' => _x( 'Add Media', 'label for button in the media widget' ),
'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
'add_to_widget' => __( 'Add to Widget' ),
'missing_attachment' => sprintf(
/* translators: %s: URL to media library. */
__( 'That file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d: Widget count. */
'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
'media_library_state_single' => __( 'Media Widget' ),
'unsupported_file_type' => __( 'Looks like this is not the correct kind of file. Please link to an appropriate file instead.' ),
);
$this->l10n = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
$this->l10n = array_merge( self::get_l10n_defaults(), array_filter( $this->l10n ) );
parent::__construct(
$id_base,
@ -440,6 +440,16 @@ abstract class WP_Widget_Media extends WP_Widget {
<?php
}
/**
* Resets the cache for the default labels.
*
* @since 6.0.0
*/
public static function reset_default_labels() {
self::$default_description = '';
self::$l10n_defaults = array();
}
/**
* Whether the widget has content to show.
*
@ -451,4 +461,52 @@ abstract class WP_Widget_Media extends WP_Widget {
protected function has_content( $instance ) {
return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
}
/**
* Returns the default description of the widget.
*
* @since 6.0.0
*
* @var string
*/
protected static function get_default_description() {
if ( self::$default_description ) {
return self::$default_description;
}
self::$default_description = __( 'A media item.' );
return self::$default_description;
}
/**
* Returns the default localized strings used by the widget.
*
* @since 6.0.0
*
* @return string[]
*/
protected static function get_l10n_defaults() {
if ( ! empty( self::$l10n_defaults ) ) {
return self::$l10n_defaults;
}
self::$l10n_defaults = array(
'no_media_selected' => __( 'No media selected' ),
'add_media' => _x( 'Add Media', 'label for button in the media widget' ),
'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
'add_to_widget' => __( 'Add to Widget' ),
'missing_attachment' => sprintf(
/* translators: %s: URL to media library. */
__( 'That file cannot be found. Check your <a href="%s">media library</a> and make sure it was not deleted.' ),
esc_url( admin_url( 'upload.php' ) )
),
/* translators: %d: Widget count. */
'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
'media_library_state_single' => __( 'Media Widget' ),
'unsupported_file_type' => __( 'Looks like this is not the correct kind of file. Please link to an appropriate file instead.' ),
);
return self::$l10n_defaults;
}
}