wp/wp-includes/widgets/class-wp-widget-media-image.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    17 class WP_Widget_Media_Image extends WP_Widget_Media {
    17 class WP_Widget_Media_Image extends WP_Widget_Media {
    18 
    18 
    19 	/**
    19 	/**
    20 	 * Constructor.
    20 	 * Constructor.
    21 	 *
    21 	 *
    22 	 * @since  4.8.0
    22 	 * @since 4.8.0
    23 	 */
    23 	 */
    24 	public function __construct() {
    24 	public function __construct() {
    25 		parent::__construct( 'media_image', __( 'Image' ), array(
    25 		parent::__construct(
    26 			'description' => __( 'Displays an image.' ),
    26 			'media_image',
    27 			'mime_type'   => 'image',
    27 			__( 'Image' ),
    28 		) );
    28 			array(
    29 
    29 				'description' => __( 'Displays an image.' ),
    30 		$this->l10n = array_merge( $this->l10n, array(
    30 				'mime_type'   => 'image',
    31 			'no_media_selected' => __( 'No image selected' ),
    31 			)
    32 			'add_media' => _x( 'Add Image', 'label for button in the image widget' ),
    32 		);
    33 			'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    33 
    34 			'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    34 		$this->l10n = array_merge(
    35 			'missing_attachment' => sprintf(
    35 			$this->l10n,
    36 				/* translators: %s: URL to media library */
    36 			array(
    37 				__( 'We can&#8217;t find that image. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
    37 				'no_media_selected'          => __( 'No image selected' ),
    38 				esc_url( admin_url( 'upload.php' ) )
    38 				'add_media'                  => _x( 'Add Image', 'label for button in the image widget' ),
    39 			),
    39 				'replace_media'              => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    40 			/* translators: %d: widget count */
    40 				'edit_media'                 => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    41 			'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
    41 				'missing_attachment'         => sprintf(
    42 			'media_library_state_single' => __( 'Image Widget' ),
    42 					/* translators: %s: URL to media library */
    43 		) );
    43 					__( 'We can&#8217;t find that image. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
       
    44 					esc_url( admin_url( 'upload.php' ) )
       
    45 				),
       
    46 				/* translators: %d: widget count */
       
    47 				'media_library_state_multi'  => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
       
    48 				'media_library_state_single' => __( 'Image Widget' ),
       
    49 			)
       
    50 		);
    44 	}
    51 	}
    45 
    52 
    46 	/**
    53 	/**
    47 	 * Get schema for properties of a widget instance (item).
    54 	 * Get schema for properties of a widget instance (item).
    48 	 *
    55 	 *
    49 	 * @since  4.8.0
    56 	 * @since 4.8.0
    50 	 *
    57 	 *
    51 	 * @see WP_REST_Controller::get_item_schema()
    58 	 * @see WP_REST_Controller::get_item_schema()
    52 	 * @see WP_REST_Controller::get_additional_fields()
    59 	 * @see WP_REST_Controller::get_additional_fields()
    53 	 * @link https://core.trac.wordpress.org/ticket/35574
    60 	 * @link https://core.trac.wordpress.org/ticket/35574
    54 	 * @return array Schema for properties.
    61 	 * @return array Schema for properties.
    55 	 */
    62 	 */
    56 	public function get_instance_schema() {
    63 	public function get_instance_schema() {
    57 		return array_merge(
    64 		return array_merge(
    58 			parent::get_instance_schema(),
       
    59 			array(
    65 			array(
    60 				'size' => array(
    66 				'size'              => array(
    61 					'type' => 'string',
    67 					'type'        => 'string',
    62 					'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
    68 					'enum'        => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
    63 					'default' => 'medium',
    69 					'default'     => 'medium',
    64 					'description' => __( 'Size' ),
    70 					'description' => __( 'Size' ),
    65 				),
    71 				),
    66 				'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
    72 				'width'             => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
    67 					'type' => 'integer',
    73 					'type'        => 'integer',
    68 					'minimum' => 0,
    74 					'minimum'     => 0,
    69 					'default' => 0,
    75 					'default'     => 0,
    70 					'description' => __( 'Width' ),
    76 					'description' => __( 'Width' ),
    71 				),
    77 				),
    72 				'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
    78 				'height'            => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
    73 					'type' => 'integer',
    79 					'type'        => 'integer',
    74 					'minimum' => 0,
    80 					'minimum'     => 0,
    75 					'default' => 0,
    81 					'default'     => 0,
    76 					'description' => __( 'Height' ),
    82 					'description' => __( 'Height' ),
    77 				),
    83 				),
    78 
    84 
    79 				'caption' => array(
    85 				'caption'           => array(
    80 					'type' => 'string',
    86 					'type'                  => 'string',
    81 					'default' => '',
    87 					'default'               => '',
    82 					'sanitize_callback' => 'wp_kses_post',
    88 					'sanitize_callback'     => 'wp_kses_post',
    83 					'description' => __( 'Caption' ),
    89 					'description'           => __( 'Caption' ),
    84 					'should_preview_update' => false,
    90 					'should_preview_update' => false,
    85 				),
    91 				),
    86 				'alt' => array(
    92 				'alt'               => array(
    87 					'type' => 'string',
    93 					'type'              => 'string',
    88 					'default' => '',
    94 					'default'           => '',
    89 					'sanitize_callback' => 'sanitize_text_field',
    95 					'sanitize_callback' => 'sanitize_text_field',
    90 					'description' => __( 'Alternative Text' ),
    96 					'description'       => __( 'Alternative Text' ),
    91 				),
    97 				),
    92 				'link_type' => array(
    98 				'link_type'         => array(
    93 					'type' => 'string',
    99 					'type'                  => 'string',
    94 					'enum' => array( 'none', 'file', 'post', 'custom' ),
   100 					'enum'                  => array( 'none', 'file', 'post', 'custom' ),
    95 					'default' => 'custom',
   101 					'default'               => 'custom',
    96 					'media_prop' => 'link',
   102 					'media_prop'            => 'link',
    97 					'description' => __( 'Link To' ),
   103 					'description'           => __( 'Link To' ),
    98 					'should_preview_update' => true,
   104 					'should_preview_update' => true,
    99 				),
   105 				),
   100 				'link_url' => array(
   106 				'link_url'          => array(
   101 					'type' => 'string',
   107 					'type'                  => 'string',
   102 					'default' => '',
   108 					'default'               => '',
   103 					'format' => 'uri',
   109 					'format'                => 'uri',
   104 					'media_prop' => 'linkUrl',
   110 					'media_prop'            => 'linkUrl',
   105 					'description' => __( 'URL' ),
   111 					'description'           => __( 'URL' ),
   106 					'should_preview_update' => true,
   112 					'should_preview_update' => true,
   107 				),
   113 				),
   108 				'image_classes' => array(
   114 				'image_classes'     => array(
   109 					'type' => 'string',
   115 					'type'                  => 'string',
   110 					'default' => '',
   116 					'default'               => '',
   111 					'sanitize_callback' => array( $this, 'sanitize_token_list' ),
   117 					'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
   112 					'media_prop' => 'extraClasses',
   118 					'media_prop'            => 'extraClasses',
   113 					'description' => __( 'Image CSS Class' ),
   119 					'description'           => __( 'Image CSS Class' ),
   114 					'should_preview_update' => false,
   120 					'should_preview_update' => false,
   115 				),
   121 				),
   116 				'link_classes' => array(
   122 				'link_classes'      => array(
   117 					'type' => 'string',
   123 					'type'                  => 'string',
   118 					'default' => '',
   124 					'default'               => '',
   119 					'sanitize_callback' => array( $this, 'sanitize_token_list' ),
   125 					'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
   120 					'media_prop' => 'linkClassName',
   126 					'media_prop'            => 'linkClassName',
   121 					'should_preview_update' => false,
   127 					'should_preview_update' => false,
   122 					'description' => __( 'Link CSS Class' ),
   128 					'description'           => __( 'Link CSS Class' ),
   123 				),
   129 				),
   124 				'link_rel' => array(
   130 				'link_rel'          => array(
   125 					'type' => 'string',
   131 					'type'                  => 'string',
   126 					'default' => '',
   132 					'default'               => '',
   127 					'sanitize_callback' => array( $this, 'sanitize_token_list' ),
   133 					'sanitize_callback'     => array( $this, 'sanitize_token_list' ),
   128 					'media_prop' => 'linkRel',
   134 					'media_prop'            => 'linkRel',
   129 					'description' => __( 'Link Rel' ),
   135 					'description'           => __( 'Link Rel' ),
   130 					'should_preview_update' => false,
   136 					'should_preview_update' => false,
   131 				),
   137 				),
   132 				'link_target_blank' => array(
   138 				'link_target_blank' => array(
   133 					'type' => 'boolean',
   139 					'type'                  => 'boolean',
   134 					'default' => false,
   140 					'default'               => false,
   135 					'media_prop' => 'linkTargetBlank',
   141 					'media_prop'            => 'linkTargetBlank',
   136 					'description' => __( 'Open link in a new tab' ),
   142 					'description'           => __( 'Open link in a new tab' ),
   137 					'should_preview_update' => false,
   143 					'should_preview_update' => false,
   138 				),
   144 				),
   139 				'image_title' => array(
   145 				'image_title'       => array(
   140 					'type' => 'string',
   146 					'type'                  => 'string',
   141 					'default' => '',
   147 					'default'               => '',
   142 					'sanitize_callback' => 'sanitize_text_field',
   148 					'sanitize_callback'     => 'sanitize_text_field',
   143 					'media_prop' => 'title',
   149 					'media_prop'            => 'title',
   144 					'description' => __( 'Image Title Attribute' ),
   150 					'description'           => __( 'Image Title Attribute' ),
   145 					'should_preview_update' => false,
   151 					'should_preview_update' => false,
   146 				),
   152 				),
   147 
   153 
   148 				/*
   154 				/*
   149 				 * There are two additional properties exposed by the PostImage modal
   155 				 * There are two additional properties exposed by the PostImage modal
   152 				 * - originalUrl
   158 				 * - originalUrl
   153 				 * - aspectRatio
   159 				 * - aspectRatio
   154 				 * - height (redundant when size is not custom)
   160 				 * - height (redundant when size is not custom)
   155 				 * - width (redundant when size is not custom)
   161 				 * - width (redundant when size is not custom)
   156 				 */
   162 				 */
   157 			)
   163 			),
       
   164 			parent::get_instance_schema()
   158 		);
   165 		);
   159 	}
   166 	}
   160 
   167 
   161 	/**
   168 	/**
   162 	 * Render the media on the frontend.
   169 	 * Render the media on the frontend.
   163 	 *
   170 	 *
   164 	 * @since  4.8.0
   171 	 * @since 4.8.0
   165 	 *
   172 	 *
   166 	 * @param array $instance Widget instance props.
   173 	 * @param array $instance Widget instance props.
   167 	 * @return void
   174 	 * @return void
   168 	 */
   175 	 */
   169 	public function render_media( $instance ) {
   176 	public function render_media( $instance ) {
   170 		$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
   177 		$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
   171 		$instance = wp_parse_args( $instance, array(
   178 		$instance = wp_parse_args(
   172 			'size' => 'thumbnail',
   179 			$instance,
   173 		) );
   180 			array(
       
   181 				'size' => 'thumbnail',
       
   182 			)
       
   183 		);
   174 
   184 
   175 		$attachment = null;
   185 		$attachment = null;
   176 		if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
   186 		if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
   177 			$attachment = get_post( $instance['attachment_id'] );
   187 			$attachment = get_post( $instance['attachment_id'] );
   178 		}
   188 		}
   203 			$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
   213 			$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
   204 
   214 
   205 			$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
   215 			$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
   206 
   216 
   207 			$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
   217 			$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
   208 			$width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
   218 			$width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
   209 
   219 
   210 		} else {
   220 		} else {
   211 			if ( empty( $instance['url'] ) ) {
   221 			if ( empty( $instance['url'] ) ) {
   212 				return;
   222 				return;
   213 			}
   223 			}
   214 
   224 
   215 			$instance['size'] = 'custom';
   225 			$instance['size'] = 'custom';
   216 			$caption = $instance['caption'];
   226 			$caption          = $instance['caption'];
   217 			$width   = $instance['width'];
   227 			$width            = $instance['width'];
   218 			$classes = 'image ' . $instance['image_classes'];
   228 			$classes          = 'image ' . $instance['image_classes'];
   219 			if ( 0 === $instance['width'] ) {
   229 			if ( 0 === $instance['width'] ) {
   220 				$instance['width'] = '';
   230 				$instance['width'] = '';
   221 			}
   231 			}
   222 			if ( 0 === $instance['height'] ) {
   232 			if ( 0 === $instance['height'] ) {
   223 				$instance['height'] = '';
   233 				$instance['height'] = '';
   224 			}
   234 			}
   225 
   235 
   226 			$image = sprintf( '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />',
   236 			$image = sprintf(
       
   237 				'<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />',
   227 				esc_attr( $classes ),
   238 				esc_attr( $classes ),
   228 				esc_url( $instance['url'] ),
   239 				esc_url( $instance['url'] ),
   229 				esc_attr( $instance['alt'] ),
   240 				esc_attr( $instance['alt'] ),
   230 				esc_attr( $instance['width'] ),
   241 				esc_attr( $instance['width'] ),
   231 				esc_attr( $instance['height'] )
   242 				esc_attr( $instance['height'] )
   253 				$link .= ' target="_blank"';
   264 				$link .= ' target="_blank"';
   254 			}
   265 			}
   255 			$link .= '>';
   266 			$link .= '>';
   256 			$link .= $image;
   267 			$link .= $image;
   257 			$link .= '</a>';
   268 			$link .= '</a>';
   258 			$image = $link;
   269 			$image = wp_targeted_link_rel( $link );
   259 		}
   270 		}
   260 
   271 
   261 		if ( $caption ) {
   272 		if ( $caption ) {
   262 			$image = img_caption_shortcode( array(
   273 			$image = img_caption_shortcode(
   263 				'width' => $width,
   274 				array(
   264 				'caption' => $caption,
   275 					'width'   => $width,
   265 			), $image );
   276 					'caption' => $caption,
       
   277 				),
       
   278 				$image
       
   279 			);
   266 		}
   280 		}
   267 
   281 
   268 		echo $image;
   282 		echo $image;
   269 	}
   283 	}
   270 
   284 
   323 				<input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="http://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
   337 				<input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="http://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
   324 			</p>
   338 			</p>
   325 			<# } #>
   339 			<# } #>
   326 		</script>
   340 		</script>
   327 		<script type="text/html" id="tmpl-wp-media-widget-image-preview">
   341 		<script type="text/html" id="tmpl-wp-media-widget-image-preview">
   328 			<# var describedById = 'describedBy-' + String( Math.random() ); #>
       
   329 			<# if ( data.error && 'missing_attachment' === data.error ) { #>
   342 			<# if ( data.error && 'missing_attachment' === data.error ) { #>
   330 				<div class="notice notice-error notice-alt notice-missing-attachment">
   343 				<div class="notice notice-error notice-alt notice-missing-attachment">
   331 					<p><?php echo $this->l10n['missing_attachment']; ?></p>
   344 					<p><?php echo $this->l10n['missing_attachment']; ?></p>
   332 				</div>
   345 				</div>
   333 			<# } else if ( data.error ) { #>
   346 			<# } else if ( data.error ) { #>
   334 				<div class="notice notice-error notice-alt">
   347 				<div class="notice notice-error notice-alt">
   335 					<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
   348 					<p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
   336 				</div>
   349 				</div>
   337 			<# } else if ( data.url ) { #>
   350 			<# } else if ( data.url ) { #>
   338 				<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}" <# if ( ! data.alt && data.currentFilename ) { #> aria-describedby="{{ describedById }}" <# } #> />
   351 				<img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}"
   339 				<# if ( ! data.alt && data.currentFilename ) { #>
   352 					<# if ( ! data.alt && data.currentFilename ) { #>
   340 					<p class="hidden" id="{{ describedById }}"><?php
   353 						aria-label="
   341 						/* translators: placeholder is image filename */
   354 						<?php
   342 						echo sprintf( __( 'Current image: %s' ), '{{ data.currentFilename }}' );
   355 						echo esc_attr(
   343 					?></p>
   356 							sprintf(
   344 				<# } #>
   357 								/* translators: %s: the image file name. */
       
   358 								__( 'The current image has no alternative text. The file name is: %s' ),
       
   359 								'{{ data.currentFilename }}'
       
   360 							)
       
   361 						);
       
   362 						?>
       
   363 						"
       
   364 					<# } #>
       
   365 				/>
   345 			<# } #>
   366 			<# } #>
   346 		</script>
   367 		</script>
   347 		<?php
   368 		<?php
   348 	}
   369 	}
   349 }
   370 }