wp/wp-includes/widgets/class-wp-widget-media.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    21 	 *
    21 	 *
    22 	 * @since 4.8.0
    22 	 * @since 4.8.0
    23 	 * @var array
    23 	 * @var array
    24 	 */
    24 	 */
    25 	public $l10n = array(
    25 	public $l10n = array(
    26 		'add_to_widget' => '',
    26 		'add_to_widget'              => '',
    27 		'replace_media' => '',
    27 		'replace_media'              => '',
    28 		'edit_media' => '',
    28 		'edit_media'                 => '',
    29 		'media_library_state_multi' => '',
    29 		'media_library_state_multi'  => '',
    30 		'media_library_state_single' => '',
    30 		'media_library_state_single' => '',
    31 		'missing_attachment' => '',
    31 		'missing_attachment'         => '',
    32 		'no_media_selected' => '',
    32 		'no_media_selected'          => '',
    33 		'add_media' => '',
    33 		'add_media'                  => '',
    34 	);
    34 	);
    35 
    35 
    36 	/**
    36 	/**
    37 	 * Whether or not the widget has been registered yet.
    37 	 * Whether or not the widget has been registered yet.
    38 	 *
    38 	 *
    52 	 *                                information on accepted arguments. Default empty array.
    52 	 *                                information on accepted arguments. Default empty array.
    53 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control()
    53 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control()
    54 	 *                                for information on accepted arguments. Default empty array.
    54 	 *                                for information on accepted arguments. Default empty array.
    55 	 */
    55 	 */
    56 	public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    56 	public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    57 		$widget_opts = wp_parse_args( $widget_options, array(
    57 		$widget_opts = wp_parse_args(
    58 			'description' => __( 'A media item.' ),
    58 			$widget_options,
    59 			'customize_selective_refresh' => true,
    59 			array(
    60 			'mime_type' => '',
    60 				'description'                 => __( 'A media item.' ),
    61 		) );
    61 				'customize_selective_refresh' => true,
       
    62 				'mime_type'                   => '',
       
    63 			)
       
    64 		);
    62 
    65 
    63 		$control_opts = wp_parse_args( $control_options, array() );
    66 		$control_opts = wp_parse_args( $control_options, array() );
    64 
    67 
    65 		$l10n_defaults = array(
    68 		$l10n_defaults = array(
    66 			'no_media_selected' => __( 'No media selected' ),
    69 			'no_media_selected'          => __( 'No media selected' ),
    67 			'add_media' => _x( 'Add Media', 'label for button in the media widget' ),
    70 			'add_media'                  => _x( 'Add Media', 'label for button in the media widget' ),
    68 			'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    71 			'replace_media'              => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    69 			'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    72 			'edit_media'                 => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ),
    70 			'add_to_widget' => __( 'Add to Widget' ),
    73 			'add_to_widget'              => __( 'Add to Widget' ),
    71 			'missing_attachment' => sprintf(
    74 			'missing_attachment'         => sprintf(
    72 				/* translators: %s: URL to media library */
    75 				/* translators: %s: URL to media library */
    73 				__( 'We can&#8217;t find that file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
    76 				__( 'We can&#8217;t find that file. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
    74 				esc_url( admin_url( 'upload.php' ) )
    77 				esc_url( admin_url( 'upload.php' ) )
    75 			),
    78 			),
    76 			/* translators: %d: widget count */
    79 			/* translators: %d: widget count */
    77 			'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
    80 			'media_library_state_multi'  => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ),
    78 			'media_library_state_single' => __( 'Media Widget' ),
    81 			'media_library_state_single' => __( 'Media Widget' ),
    79 			'unsupported_file_type' => __( 'Looks like this isn&#8217;t the correct kind of file. Please link to an appropriate file instead.' ),
    82 			'unsupported_file_type'      => __( 'Looks like this isn&#8217;t the correct kind of file. Please link to an appropriate file instead.' ),
    80 		);
    83 		);
    81 		$this->l10n = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
    84 		$this->l10n    = array_merge( $l10n_defaults, array_filter( $this->l10n ) );
    82 
    85 
    83 		parent::__construct(
    86 		parent::__construct(
    84 			$id_base,
    87 			$id_base,
    85 			$name,
    88 			$name,
    86 			$widget_opts,
    89 			$widget_opts,
   117 	}
   120 	}
   118 
   121 
   119 	/**
   122 	/**
   120 	 * Get schema for properties of a widget instance (item).
   123 	 * Get schema for properties of a widget instance (item).
   121 	 *
   124 	 *
   122 	 * @since  4.8.0
   125 	 * @since 4.8.0
   123 	 *
   126 	 *
   124 	 * @see WP_REST_Controller::get_item_schema()
   127 	 * @see WP_REST_Controller::get_item_schema()
   125 	 * @see WP_REST_Controller::get_additional_fields()
   128 	 * @see WP_REST_Controller::get_additional_fields()
   126 	 * @link https://core.trac.wordpress.org/ticket/35574
   129 	 * @link https://core.trac.wordpress.org/ticket/35574
   127 	 * @return array Schema for properties.
   130 	 * @return array Schema for properties.
   128 	 */
   131 	 */
   129 	public function get_instance_schema() {
   132 	public function get_instance_schema() {
   130 		$schema = array(
   133 		$schema = array(
   131 			'attachment_id' => array(
   134 			'attachment_id' => array(
   132 				'type' => 'integer',
   135 				'type'        => 'integer',
   133 				'default' => 0,
   136 				'default'     => 0,
   134 				'minimum' => 0,
   137 				'minimum'     => 0,
   135 				'description' => __( 'Attachment post ID' ),
   138 				'description' => __( 'Attachment post ID' ),
   136 				'media_prop' => 'id',
   139 				'media_prop'  => 'id',
   137 			),
   140 			),
   138 			'url' => array(
   141 			'url'           => array(
   139 				'type' => 'string',
   142 				'type'        => 'string',
   140 				'default' => '',
   143 				'default'     => '',
   141 				'format' => 'uri',
   144 				'format'      => 'uri',
   142 				'description' => __( 'URL to the media file' ),
   145 				'description' => __( 'URL to the media file' ),
   143 			),
   146 			),
   144 			'title' => array(
   147 			'title'         => array(
   145 				'type' => 'string',
   148 				'type'                  => 'string',
   146 				'default' => '',
   149 				'default'               => '',
   147 				'sanitize_callback' => 'sanitize_text_field',
   150 				'sanitize_callback'     => 'sanitize_text_field',
   148 				'description' => __( 'Title for the widget' ),
   151 				'description'           => __( 'Title for the widget' ),
   149 				'should_preview_update' => false,
   152 				'should_preview_update' => false,
   150 			),
   153 			),
   151 		);
   154 		);
   152 
   155 
   153 		/**
   156 		/**
   320 	 * @param array $instance Current settings.
   323 	 * @param array $instance Current settings.
   321 	 * @return void
   324 	 * @return void
   322 	 */
   325 	 */
   323 	final public function form( $instance ) {
   326 	final public function form( $instance ) {
   324 		$instance_schema = $this->get_instance_schema();
   327 		$instance_schema = $this->get_instance_schema();
   325 		$instance = wp_array_slice_assoc(
   328 		$instance        = wp_array_slice_assoc(
   326 			wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ),
   329 			wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ),
   327 			array_keys( $instance_schema )
   330 			array_keys( $instance_schema )
   328 		);
   331 		);
   329 
   332 
   330 		foreach ( $instance as $name => $value ) : ?>
   333 		foreach ( $instance as $name => $value ) : ?>
   334 				class="media-widget-instance-property"
   337 				class="media-widget-instance-property"
   335 				name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
   338 				name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
   336 				id="<?php echo esc_attr( $this->get_field_id( $name ) ); // Needed specifically by wpWidgets.appendTitle(). ?>"
   339 				id="<?php echo esc_attr( $this->get_field_id( $name ) ); // Needed specifically by wpWidgets.appendTitle(). ?>"
   337 				value="<?php echo esc_attr( is_array( $value ) ? join( ',', $value ) : strval( $value ) ); ?>"
   340 				value="<?php echo esc_attr( is_array( $value ) ? join( ',', $value ) : strval( $value ) ); ?>"
   338 			/>
   341 			/>
   339 		<?php
   342 			<?php
   340 		endforeach;
   343 		endforeach;
   341 	}
   344 	}
   342 
   345 
   343 	/**
   346 	/**
   344 	 * Filters the default media display states for items in the Media list table.
   347 	 * Filters the default media display states for items in the Media list table.
   406 				<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
   409 				<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
   407 				<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
   410 				<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
   408 			</p>
   411 			</p>
   409 			<div class="media-widget-preview <?php echo esc_attr( $this->id_base ); ?>">
   412 			<div class="media-widget-preview <?php echo esc_attr( $this->id_base ); ?>">
   410 				<div class="attachment-media-view">
   413 				<div class="attachment-media-view">
   411 					<div class="placeholder"><?php echo esc_html( $this->l10n['no_media_selected'] ); ?></div>
   414 					<button type="button" class="select-media button-add-media not-selected">
       
   415 						<?php echo esc_html( $this->l10n['add_media'] ); ?>
       
   416 					</button>
   412 				</div>
   417 				</div>
   413 			</div>
   418 			</div>
   414 			<p class="media-widget-buttons">
   419 			<p class="media-widget-buttons">
   415 				<button type="button" class="button edit-media selected">
   420 				<button type="button" class="button edit-media selected">
   416 					<?php echo esc_html( $this->l10n['edit_media'] ); ?>
   421 					<?php echo esc_html( $this->l10n['edit_media'] ); ?>
   418 			<?php if ( ! empty( $this->l10n['replace_media'] ) ) : ?>
   423 			<?php if ( ! empty( $this->l10n['replace_media'] ) ) : ?>
   419 				<button type="button" class="button change-media select-media selected">
   424 				<button type="button" class="button change-media select-media selected">
   420 					<?php echo esc_html( $this->l10n['replace_media'] ); ?>
   425 					<?php echo esc_html( $this->l10n['replace_media'] ); ?>
   421 				</button>
   426 				</button>
   422 			<?php endif; ?>
   427 			<?php endif; ?>
   423 				<button type="button" class="button select-media not-selected">
       
   424 					<?php echo esc_html( $this->l10n['add_media'] ); ?>
       
   425 				</button>
       
   426 			</p>
   428 			</p>
   427 			<div class="media-widget-fields">
   429 			<div class="media-widget-fields">
   428 			</div>
   430 			</div>
   429 		</script>
   431 		</script>
   430 		<?php
   432 		<?php