wp/wp-includes/widgets/class-wp-widget-media-gallery.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Widget API: WP_Widget_Media_Gallery class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Widgets
       
     7  * @since 4.9.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class that implements a gallery widget.
       
    12  *
       
    13  * @since 4.9.0
       
    14  *
       
    15  * @see WP_Widget
       
    16  */
       
    17 class WP_Widget_Media_Gallery extends WP_Widget_Media {
       
    18 
       
    19 	/**
       
    20 	 * Constructor.
       
    21 	 *
       
    22 	 * @since 4.9.0
       
    23 	 */
       
    24 	public function __construct() {
       
    25 		parent::__construct( 'media_gallery', __( 'Gallery' ), array(
       
    26 			'description' => __( 'Displays an image gallery.' ),
       
    27 			'mime_type'   => 'image',
       
    28 		) );
       
    29 
       
    30 		$this->l10n = array_merge( $this->l10n, array(
       
    31 			'no_media_selected' => __( 'No images selected' ),
       
    32 			'add_media' => _x( 'Add Images', 'label for button in the gallery widget; should not be longer than ~13 characters long' ),
       
    33 			'replace_media' => '',
       
    34 			'edit_media' => _x( 'Edit Gallery', 'label for button in the gallery widget; should not be longer than ~13 characters long' ),
       
    35 		) );
       
    36 	}
       
    37 
       
    38 	/**
       
    39 	 * Get schema for properties of a widget instance (item).
       
    40 	 *
       
    41 	 * @since 4.9.0
       
    42 	 *
       
    43 	 * @see WP_REST_Controller::get_item_schema()
       
    44 	 * @see WP_REST_Controller::get_additional_fields()
       
    45 	 * @link https://core.trac.wordpress.org/ticket/35574
       
    46 	 * @return array Schema for properties.
       
    47 	 */
       
    48 	public function get_instance_schema() {
       
    49 		$schema = array(
       
    50 			'title' => array(
       
    51 				'type' => 'string',
       
    52 				'default' => '',
       
    53 				'sanitize_callback' => 'sanitize_text_field',
       
    54 				'description' => __( 'Title for the widget' ),
       
    55 				'should_preview_update' => false,
       
    56 			),
       
    57 			'ids' => array(
       
    58 				'type' => 'array',
       
    59 				'items' => array(
       
    60 					'type' => 'integer',
       
    61 				),
       
    62 				'default' => array(),
       
    63 				'sanitize_callback' => 'wp_parse_id_list',
       
    64 			),
       
    65 			'columns' => array(
       
    66 				'type' => 'integer',
       
    67 				'default' => 3,
       
    68 				'minimum' => 1,
       
    69 				'maximum' => 9,
       
    70 			),
       
    71 			'size' => array(
       
    72 				'type' => 'string',
       
    73 				'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
       
    74 				'default' => 'thumbnail',
       
    75 			),
       
    76 			'link_type' => array(
       
    77 				'type' => 'string',
       
    78 				'enum' => array( 'post', 'file', 'none' ),
       
    79 				'default' => 'post',
       
    80 				'media_prop' => 'link',
       
    81 				'should_preview_update' => false,
       
    82 			),
       
    83 			'orderby_random' => array(
       
    84 				'type'                  => 'boolean',
       
    85 				'default'               => false,
       
    86 				'media_prop'            => '_orderbyRandom',
       
    87 				'should_preview_update' => false,
       
    88 			),
       
    89 		);
       
    90 
       
    91 		/** This filter is documented in wp-includes/widgets/class-wp-widget-media.php */
       
    92 		$schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this );
       
    93 
       
    94 		return $schema;
       
    95 	}
       
    96 
       
    97 	/**
       
    98 	 * Render the media on the frontend.
       
    99 	 *
       
   100 	 * @since 4.9.0
       
   101 	 *
       
   102 	 * @param array $instance Widget instance props.
       
   103 	 * @return void
       
   104 	 */
       
   105 	public function render_media( $instance ) {
       
   106 		$instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
       
   107 
       
   108 		$shortcode_atts = array_merge(
       
   109 			$instance,
       
   110 			array(
       
   111 				'link' => $instance['link_type'],
       
   112 			)
       
   113 		);
       
   114 
       
   115 		// @codeCoverageIgnoreStart
       
   116 		if ( $instance['orderby_random'] ) {
       
   117 			$shortcode_atts['orderby'] = 'rand';
       
   118 		}
       
   119 
       
   120 		// @codeCoverageIgnoreEnd
       
   121 		echo gallery_shortcode( $shortcode_atts );
       
   122 	}
       
   123 
       
   124 	/**
       
   125 	 * Loads the required media files for the media manager and scripts for media widgets.
       
   126 	 *
       
   127 	 * @since 4.9.0
       
   128 	 */
       
   129 	public function enqueue_admin_scripts() {
       
   130 		parent::enqueue_admin_scripts();
       
   131 
       
   132 		$handle = 'media-gallery-widget';
       
   133 		wp_enqueue_script( $handle );
       
   134 
       
   135 		$exported_schema = array();
       
   136 		foreach ( $this->get_instance_schema() as $field => $field_schema ) {
       
   137 			$exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update', 'items' ) );
       
   138 		}
       
   139 		wp_add_inline_script(
       
   140 			$handle,
       
   141 			sprintf(
       
   142 				'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
       
   143 				wp_json_encode( $this->id_base ),
       
   144 				wp_json_encode( $exported_schema )
       
   145 			)
       
   146 		);
       
   147 
       
   148 		wp_add_inline_script(
       
   149 			$handle,
       
   150 			sprintf(
       
   151 				'
       
   152 					wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
       
   153 					_.extend( wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
       
   154 				',
       
   155 				wp_json_encode( $this->id_base ),
       
   156 				wp_json_encode( $this->widget_options['mime_type'] ),
       
   157 				wp_json_encode( $this->l10n )
       
   158 			)
       
   159 		);
       
   160 	}
       
   161 
       
   162 	/**
       
   163 	 * Render form template scripts.
       
   164 	 *
       
   165 	 * @since 4.9.0
       
   166 	 */
       
   167 	public function render_control_template_scripts() {
       
   168 		parent::render_control_template_scripts();
       
   169 		?>
       
   170 		<script type="text/html" id="tmpl-wp-media-widget-gallery-preview">
       
   171 			<# var describedById = 'describedBy-' + String( Math.random() ); #>
       
   172 			<# if ( data.ids.length ) { #>
       
   173 				<div class="gallery media-widget-gallery-preview">
       
   174 					<# _.each( data.ids, function( id, index ) { #>
       
   175 						<#
       
   176 						var attachment = data.attachments[ id ];
       
   177 						if ( ! attachment ) {
       
   178 							return;
       
   179 						}
       
   180 						#>
       
   181 						<# if ( index < 6 ) { #>
       
   182 							<dl class="gallery-item">
       
   183 								<dt class="gallery-icon">
       
   184 								<# if ( attachment.sizes.thumbnail ) { #>
       
   185 									<img src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}" alt="" />
       
   186 								<# } else { #>
       
   187 									<img src="{{ attachment.url }}" alt="" />
       
   188 								<# } #>
       
   189 								<# if ( index === 5 && data.ids.length > 6 ) { #>
       
   190 									<div class="gallery-icon-placeholder">
       
   191 										<p class="gallery-icon-placeholder-text">+{{ data.ids.length - 5 }}</p>
       
   192 									</div>
       
   193 								<# } #>
       
   194 								</dt>
       
   195 							</dl>
       
   196 						<# } #>
       
   197 					<# } ); #>
       
   198 				</div>
       
   199 			<# } else { #>
       
   200 				<div class="attachment-media-view">
       
   201 					<p class="placeholder"><?php echo esc_html( $this->l10n['no_media_selected'] ); ?></p>
       
   202 				</div>
       
   203 			<# } #>
       
   204 		</script>
       
   205 		<?php
       
   206 	}
       
   207 
       
   208 	/**
       
   209 	 * Whether the widget has content to show.
       
   210 	 *
       
   211 	 * @since 4.9.0
       
   212 	 * @access protected
       
   213 	 *
       
   214 	 * @param array $instance Widget instance props.
       
   215 	 * @return bool Whether widget has content.
       
   216 	 */
       
   217 	protected function has_content( $instance ) {
       
   218 		if ( ! empty( $instance['ids'] ) ) {
       
   219 			$attachments = wp_parse_id_list( $instance['ids'] );
       
   220 			foreach ( $attachments as $attachment ) {
       
   221 				if ( 'attachment' !== get_post_type( $attachment ) ) {
       
   222 					return false;
       
   223 				}
       
   224 			}
       
   225 			return true;
       
   226 		}
       
   227 		return false;
       
   228 	}
       
   229 }