wp/wp-includes/widgets/class-wp-widget-media.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    40 	 * @var bool
    40 	 * @var bool
    41 	 */
    41 	 */
    42 	protected $registered = false;
    42 	protected $registered = false;
    43 
    43 
    44 	/**
    44 	/**
       
    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 	/**
    45 	 * Constructor.
    61 	 * Constructor.
    46 	 *
    62 	 *
    47 	 * @since 4.8.0
    63 	 * @since 4.8.0
    48 	 *
    64 	 *
    49 	 * @param string $id_base         Base ID for the widget, lowercase and unique.
    65 	 * @param string $id_base         Base ID for the widget, lowercase and unique.
    55 	 */
    71 	 */
    56 	public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    72 	public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    57 		$widget_opts = wp_parse_args(
    73 		$widget_opts = wp_parse_args(
    58 			$widget_options,
    74 			$widget_options,
    59 			array(
    75 			array(
    60 				'description'                 => __( 'A media item.' ),
    76 				'description'                 => self::get_default_description(),
    61 				'customize_selective_refresh' => true,
    77 				'customize_selective_refresh' => true,
    62 				'show_instance_in_rest'       => true,
    78 				'show_instance_in_rest'       => true,
    63 				'mime_type'                   => '',
    79 				'mime_type'                   => '',
    64 			)
    80 			)
    65 		);
    81 		);
    66 
    82 
    67 		$control_opts = wp_parse_args( $control_options, array() );
    83 		$control_opts = wp_parse_args( $control_options, array() );
    68 
    84 
    69 		$l10n_defaults = array(
    85 		$this->l10n = array_merge( self::get_l10n_defaults(), array_filter( $this->l10n ) );
    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 				__( 'We can&#8217;t find that file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t 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 isn&#8217;t the correct kind of file. Please link to an appropriate file instead.' ),
       
    84 		);
       
    85 		$this->l10n    = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
       
    86 
    86 
    87 		parent::__construct(
    87 		parent::__construct(
    88 			$id_base,
    88 			$id_base,
    89 			$name,
    89 			$name,
    90 			$widget_opts,
    90 			$widget_opts,
   257 
   257 
   258 	/**
   258 	/**
   259 	 * Sanitizes the widget form values as they are saved.
   259 	 * Sanitizes the widget form values as they are saved.
   260 	 *
   260 	 *
   261 	 * @since 4.8.0
   261 	 * @since 4.8.0
       
   262 	 * @since 5.9.0 Renamed `$instance` to `$old_instance` to match parent class
       
   263 	 *              for PHP 8 named parameter support.
   262 	 *
   264 	 *
   263 	 * @see WP_Widget::update()
   265 	 * @see WP_Widget::update()
   264 	 * @see WP_REST_Request::has_valid_params()
   266 	 * @see WP_REST_Request::has_valid_params()
   265 	 * @see WP_REST_Request::sanitize_params()
   267 	 * @see WP_REST_Request::sanitize_params()
   266 	 *
   268 	 *
   267 	 * @param array $new_instance Values just sent to be saved.
   269 	 * @param array $new_instance Values just sent to be saved.
   268 	 * @param array $instance     Previously saved values from database.
   270 	 * @param array $old_instance Previously saved values from database.
   269 	 * @return array Updated safe values to be saved.
   271 	 * @return array Updated safe values to be saved.
   270 	 */
   272 	 */
   271 	public function update( $new_instance, $instance ) {
   273 	public function update( $new_instance, $old_instance ) {
   272 
   274 
   273 		$schema = $this->get_instance_schema();
   275 		$schema = $this->get_instance_schema();
   274 		foreach ( $schema as $field => $field_schema ) {
   276 		foreach ( $schema as $field => $field_schema ) {
   275 			if ( ! array_key_exists( $field, $new_instance ) ) {
   277 			if ( ! array_key_exists( $field, $new_instance ) ) {
   276 				continue;
   278 				continue;
   301 				$value = call_user_func( $field_schema['sanitize_callback'], $value );
   303 				$value = call_user_func( $field_schema['sanitize_callback'], $value );
   302 			}
   304 			}
   303 			if ( is_wp_error( $value ) ) {
   305 			if ( is_wp_error( $value ) ) {
   304 				continue;
   306 				continue;
   305 			}
   307 			}
   306 			$instance[ $field ] = $value;
   308 			$old_instance[ $field ] = $value;
   307 		}
   309 		}
   308 
   310 
   309 		return $instance;
   311 		return $old_instance;
   310 	}
   312 	}
   311 
   313 
   312 	/**
   314 	/**
   313 	 * Render the media on the frontend.
   315 	 * Render the media on the frontend.
   314 	 *
   316 	 *
   315 	 * @since 4.8.0
   317 	 * @since 4.8.0
   316 	 *
   318 	 *
   317 	 * @param array $instance Widget instance props.
   319 	 * @param array $instance Widget instance props.
   318 	 * @return string
       
   319 	 */
   320 	 */
   320 	abstract public function render_media( $instance );
   321 	abstract public function render_media( $instance );
   321 
   322 
   322 	/**
   323 	/**
   323 	 * Outputs the settings update form.
   324 	 * Outputs the settings update form.
   438 		</script>
   439 		</script>
   439 		<?php
   440 		<?php
   440 	}
   441 	}
   441 
   442 
   442 	/**
   443 	/**
       
   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 	/**
   443 	 * Whether the widget has content to show.
   454 	 * Whether the widget has content to show.
   444 	 *
   455 	 *
   445 	 * @since 4.8.0
   456 	 * @since 4.8.0
   446 	 *
   457 	 *
   447 	 * @param array $instance Widget instance props.
   458 	 * @param array $instance Widget instance props.
   448 	 * @return bool Whether widget has content.
   459 	 * @return bool Whether widget has content.
   449 	 */
   460 	 */
   450 	protected function has_content( $instance ) {
   461 	protected function has_content( $instance ) {
   451 		return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
   462 		return ( $instance['attachment_id'] && 'attachment' === get_post_type( $instance['attachment_id'] ) ) || $instance['url'];
   452 	}
   463 	}
       
   464 
       
   465 	/**
       
   466 	 * Returns the default description of the widget.
       
   467 	 *
       
   468 	 * @since 6.0.0
       
   469 	 *
       
   470 	 * @return 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|array)[]
       
   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 	}
   453 }
   512 }