wp/wp-includes/widgets/class-wp-widget-media.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-includes/widgets/class-wp-widget-media.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-includes/widgets/class-wp-widget-media.php	Tue Sep 27 16:37:53 2022 +0200
@@ -42,6 +42,22 @@
 	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.
 	 *
 	 * @since 4.8.0
@@ -57,7 +73,7 @@
 		$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 @@
 
 		$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. */
-				__( 'We can&#8217;t find that file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t 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 isn&#8217;t 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,
@@ -259,16 +259,18 @@
 	 * Sanitizes the widget form values as they are saved.
 	 *
 	 * @since 4.8.0
+	 * @since 5.9.0 Renamed `$instance` to `$old_instance` to match parent class
+	 *              for PHP 8 named parameter support.
 	 *
 	 * @see WP_Widget::update()
 	 * @see WP_REST_Request::has_valid_params()
 	 * @see WP_REST_Request::sanitize_params()
 	 *
 	 * @param array $new_instance Values just sent to be saved.
-	 * @param array $instance     Previously saved values from database.
+	 * @param array $old_instance Previously saved values from database.
 	 * @return array Updated safe values to be saved.
 	 */
-	public function update( $new_instance, $instance ) {
+	public function update( $new_instance, $old_instance ) {
 
 		$schema = $this->get_instance_schema();
 		foreach ( $schema as $field => $field_schema ) {
@@ -303,10 +305,10 @@
 			if ( is_wp_error( $value ) ) {
 				continue;
 			}
-			$instance[ $field ] = $value;
+			$old_instance[ $field ] = $value;
 		}
 
-		return $instance;
+		return $old_instance;
 	}
 
 	/**
@@ -315,7 +317,6 @@
 	 * @since 4.8.0
 	 *
 	 * @param array $instance Widget instance props.
-	 * @return string
 	 */
 	abstract public function render_media( $instance );
 
@@ -440,6 +441,16 @@
 	}
 
 	/**
+	 * 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.
 	 *
 	 * @since 4.8.0
@@ -450,4 +461,52 @@
 	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
+	 *
+	 * @return 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|array)[]
+	 */
+	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;
+	}
 }