wp/wp-includes/widgets/class-wp-widget-media-image.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    10 /**
    10 /**
    11  * Core class that implements an image widget.
    11  * Core class that implements an image widget.
    12  *
    12  *
    13  * @since 4.8.0
    13  * @since 4.8.0
    14  *
    14  *
       
    15  * @see WP_Widget_Media
    15  * @see WP_Widget
    16  * @see WP_Widget
    16  */
    17  */
    17 class WP_Widget_Media_Image extends WP_Widget_Media {
    18 class WP_Widget_Media_Image extends WP_Widget_Media {
    18 
    19 
    19 	/**
    20 	/**
    37 				'no_media_selected'          => __( 'No image selected' ),
    38 				'no_media_selected'          => __( 'No image selected' ),
    38 				'add_media'                  => _x( 'Add Image', 'label for button in the image widget' ),
    39 				'add_media'                  => _x( 'Add Image', 'label for button in the image widget' ),
    39 				'replace_media'              => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    40 				'replace_media'              => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    40 				'edit_media'                 => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    41 				'edit_media'                 => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
    41 				'missing_attachment'         => sprintf(
    42 				'missing_attachment'         => sprintf(
    42 					/* translators: %s: URL to media library */
    43 					/* translators: %s: URL to media library. */
    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 					__( '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 					esc_url( admin_url( 'upload.php' ) )
    45 				),
    46 				),
    46 				/* translators: %d: widget count */
    47 				/* translators: %d: Widget count. */
    47 				'media_library_state_multi'  => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
    48 				'media_library_state_multi'  => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
    48 				'media_library_state_single' => __( 'Image Widget' ),
    49 				'media_library_state_single' => __( 'Image Widget' ),
    49 			)
    50 			)
    50 		);
    51 		);
    51 	}
    52 	}
    56 	 * @since 4.8.0
    57 	 * @since 4.8.0
    57 	 *
    58 	 *
    58 	 * @see WP_REST_Controller::get_item_schema()
    59 	 * @see WP_REST_Controller::get_item_schema()
    59 	 * @see WP_REST_Controller::get_additional_fields()
    60 	 * @see WP_REST_Controller::get_additional_fields()
    60 	 * @link https://core.trac.wordpress.org/ticket/35574
    61 	 * @link https://core.trac.wordpress.org/ticket/35574
       
    62 	 *
    61 	 * @return array Schema for properties.
    63 	 * @return array Schema for properties.
    62 	 */
    64 	 */
    63 	public function get_instance_schema() {
    65 	public function get_instance_schema() {
    64 		return array_merge(
    66 		return array_merge(
    65 			array(
    67 			array(
   169 	 * Render the media on the frontend.
   171 	 * Render the media on the frontend.
   170 	 *
   172 	 *
   171 	 * @since 4.8.0
   173 	 * @since 4.8.0
   172 	 *
   174 	 *
   173 	 * @param array $instance Widget instance props.
   175 	 * @param array $instance Widget instance props.
   174 	 * @return void
       
   175 	 */
   176 	 */
   176 	public function render_media( $instance ) {
   177 	public function render_media( $instance ) {
   177 		$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
   178 		$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
   178 		$instance = wp_parse_args(
   179 		$instance = wp_parse_args(
   179 			$instance,
   180 			$instance,
   181 				'size' => 'thumbnail',
   182 				'size' => 'thumbnail',
   182 			)
   183 			)
   183 		);
   184 		);
   184 
   185 
   185 		$attachment = null;
   186 		$attachment = null;
       
   187 
   186 		if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
   188 		if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
   187 			$attachment = get_post( $instance['attachment_id'] );
   189 			$attachment = get_post( $instance['attachment_id'] );
   188 		}
   190 		}
       
   191 
   189 		if ( $attachment ) {
   192 		if ( $attachment ) {
   190 			$caption = '';
   193 			$caption = '';
   191 			if ( ! isset( $instance['caption'] ) ) {
   194 			if ( ! isset( $instance['caption'] ) ) {
   192 				$caption = $attachment->post_excerpt;
   195 				$caption = $attachment->post_excerpt;
   193 			} elseif ( trim( $instance['caption'] ) ) {
   196 			} elseif ( trim( $instance['caption'] ) ) {
   205 			if ( $instance['alt'] ) {
   208 			if ( $instance['alt'] ) {
   206 				$image_attributes['alt'] = $instance['alt'];
   209 				$image_attributes['alt'] = $instance['alt'];
   207 			}
   210 			}
   208 
   211 
   209 			$size = $instance['size'];
   212 			$size = $instance['size'];
       
   213 
   210 			if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
   214 			if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
   211 				$size = array( $instance['width'], $instance['height'] );
   215 				$size  = array( $instance['width'], $instance['height'] );
   212 			}
   216 				$width = $instance['width'];
       
   217 			} else {
       
   218 				$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
       
   219 				$width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
       
   220 			}
       
   221 
   213 			$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
   222 			$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
   214 
   223 
   215 			$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
   224 			$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
   216 
       
   217 			$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
       
   218 			$width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
       
   219 
   225 
   220 		} else {
   226 		} else {
   221 			if ( empty( $instance['url'] ) ) {
   227 			if ( empty( $instance['url'] ) ) {
   222 				return;
   228 				return;
   223 			}
   229 			}
   332 		<script type="text/html" id="tmpl-wp-media-widget-image-fields">
   338 		<script type="text/html" id="tmpl-wp-media-widget-image-fields">
   333 			<# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #>
   339 			<# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #>
   334 			<# if ( data.url ) { #>
   340 			<# if ( data.url ) { #>
   335 			<p class="media-widget-image-link">
   341 			<p class="media-widget-image-link">
   336 				<label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label>
   342 				<label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label>
   337 				<input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="http://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
   343 				<input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="https://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
   338 			</p>
   344 			</p>
   339 			<# } #>
   345 			<# } #>
   340 		</script>
   346 		</script>
   341 		<script type="text/html" id="tmpl-wp-media-widget-image-preview">
   347 		<script type="text/html" id="tmpl-wp-media-widget-image-preview">
   342 			<# if ( data.error && 'missing_attachment' === data.error ) { #>
   348 			<# if ( data.error && 'missing_attachment' === data.error ) { #>
   352 					<# if ( ! data.alt && data.currentFilename ) { #>
   358 					<# if ( ! data.alt && data.currentFilename ) { #>
   353 						aria-label="
   359 						aria-label="
   354 						<?php
   360 						<?php
   355 						echo esc_attr(
   361 						echo esc_attr(
   356 							sprintf(
   362 							sprintf(
   357 								/* translators: %s: the image file name. */
   363 								/* translators: %s: The image file name. */
   358 								__( 'The current image has no alternative text. The file name is: %s' ),
   364 								__( 'The current image has no alternative text. The file name is: %s' ),
   359 								'{{ data.currentFilename }}'
   365 								'{{ data.currentFilename }}'
   360 							)
   366 							)
   361 						);
   367 						);
   362 						?>
   368 						?>