[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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_Media 16 * @see WP_Widget 17 */ 18 class WP_Widget_Media_Gallery extends WP_Widget_Media { 19 20 /** 21 * Constructor. 22 * 23 * @since 4.9.0 24 */ 25 public function __construct() { 26 parent::__construct( 27 'media_gallery', 28 __( 'Gallery' ), 29 array( 30 'description' => __( 'Displays an image gallery.' ), 31 'mime_type' => 'image', 32 ) 33 ); 34 35 $this->l10n = array_merge( 36 $this->l10n, 37 array( 38 'no_media_selected' => __( 'No images selected' ), 39 'add_media' => _x( 'Add Images', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), 40 'replace_media' => '', 41 'edit_media' => _x( 'Edit Gallery', 'label for button in the gallery widget; should not be longer than ~13 characters long' ), 42 ) 43 ); 44 } 45 46 /** 47 * Get schema for properties of a widget instance (item). 48 * 49 * @since 4.9.0 50 * 51 * @see WP_REST_Controller::get_item_schema() 52 * @see WP_REST_Controller::get_additional_fields() 53 * @link https://core.trac.wordpress.org/ticket/35574 54 * 55 * @return array Schema for properties. 56 */ 57 public function get_instance_schema() { 58 $schema = array( 59 'title' => array( 60 'type' => 'string', 61 'default' => '', 62 'sanitize_callback' => 'sanitize_text_field', 63 'description' => __( 'Title for the widget' ), 64 'should_preview_update' => false, 65 ), 66 'ids' => array( 67 'type' => 'array', 68 'items' => array( 69 'type' => 'integer', 70 ), 71 'default' => array(), 72 'sanitize_callback' => 'wp_parse_id_list', 73 ), 74 'columns' => array( 75 'type' => 'integer', 76 'default' => 3, 77 'minimum' => 1, 78 'maximum' => 9, 79 ), 80 'size' => array( 81 'type' => 'string', 82 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ), 83 'default' => 'thumbnail', 84 ), 85 'link_type' => array( 86 'type' => 'string', 87 'enum' => array( 'post', 'file', 'none' ), 88 'default' => 'post', 89 'media_prop' => 'link', 90 'should_preview_update' => false, 91 ), 92 'orderby_random' => array( 93 'type' => 'boolean', 94 'default' => false, 95 'media_prop' => '_orderbyRandom', 96 'should_preview_update' => false, 97 ), 98 ); 99 100 /** This filter is documented in wp-includes/widgets/class-wp-widget-media.php */ 101 $schema = apply_filters( "widget_{$this->id_base}_instance_schema", $schema, $this ); 102 103 return $schema; 104 } 105 106 /** 107 * Render the media on the frontend. 108 * 109 * @since 4.9.0 110 * 111 * @param array $instance Widget instance props. 112 */ 113 public function render_media( $instance ) { 114 $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); 115 116 $shortcode_atts = array_merge( 117 $instance, 118 array( 119 'link' => $instance['link_type'], 120 ) 121 ); 122 123 // @codeCoverageIgnoreStart 124 if ( $instance['orderby_random'] ) { 125 $shortcode_atts['orderby'] = 'rand'; 126 } 127 128 // @codeCoverageIgnoreEnd 129 echo gallery_shortcode( $shortcode_atts ); 130 } 131 132 /** 133 * Loads the required media files for the media manager and scripts for media widgets. 134 * 135 * @since 4.9.0 136 */ 137 public function enqueue_admin_scripts() { 138 parent::enqueue_admin_scripts(); 139 140 $handle = 'media-gallery-widget'; 141 wp_enqueue_script( $handle ); 142 143 $exported_schema = array(); 144 foreach ( $this->get_instance_schema() as $field => $field_schema ) { 145 $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update', 'items' ) ); 146 } 147 wp_add_inline_script( 148 $handle, 149 sprintf( 150 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;', 151 wp_json_encode( $this->id_base ), 152 wp_json_encode( $exported_schema ) 153 ) 154 ); 155 156 wp_add_inline_script( 157 $handle, 158 sprintf( 159 ' 160 wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s; 161 _.extend( wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s ); 162 ', 163 wp_json_encode( $this->id_base ), 164 wp_json_encode( $this->widget_options['mime_type'] ), 165 wp_json_encode( $this->l10n ) 166 ) 167 ); 168 } 169 170 /** 171 * Render form template scripts. 172 * 173 * @since 4.9.0 174 */ 175 public function render_control_template_scripts() { 176 parent::render_control_template_scripts(); 177 ?> 178 <script type="text/html" id="tmpl-wp-media-widget-gallery-preview"> 179 <# 180 var ids = _.filter( data.ids, function( id ) { 181 return ( id in data.attachments ); 182 } ); 183 #> 184 <# if ( ids.length ) { #> 185 <ul class="gallery media-widget-gallery-preview" role="list"> 186 <# _.each( ids, function( id, index ) { #> 187 <# var attachment = data.attachments[ id ]; #> 188 <# if ( index < 6 ) { #> 189 <li class="gallery-item"> 190 <div class="gallery-icon"> 191 <img alt="{{ attachment.alt }}" 192 <# if ( index === 5 && data.ids.length > 6 ) { #> aria-hidden="true" <# } #> 193 <# if ( attachment.sizes.thumbnail ) { #> 194 src="{{ attachment.sizes.thumbnail.url }}" width="{{ attachment.sizes.thumbnail.width }}" height="{{ attachment.sizes.thumbnail.height }}" 195 <# } else { #> 196 src="{{ attachment.url }}" 197 <# } #> 198 <# if ( ! attachment.alt && attachment.filename ) { #> 199 aria-label=" 200 <?php 201 echo esc_attr( 202 sprintf( 203 /* translators: %s: The image file name. */ 204 __( 'The current image has no alternative text. The file name is: %s' ), 205 '{{ attachment.filename }}' 206 ) 207 ); 208 ?> 209 " 210 <# } #> 211 /> 212 <# if ( index === 5 && data.ids.length > 6 ) { #> 213 <div class="gallery-icon-placeholder"> 214 <p class="gallery-icon-placeholder-text" aria-label=" 215 <?php 216 printf( 217 /* translators: %s: The amount of additional, not visible images in the gallery widget preview. */ 218 __( 'Additional images added to this gallery: %s' ), 219 '{{ data.ids.length - 5 }}' 220 ); 221 ?> 222 ">+{{ data.ids.length - 5 }}</p> 223 </div> 224 <# } #> 225 </div> 226 </li> 227 <# } #> 228 <# } ); #> 229 </ul> 230 <# } else { #> 231 <div class="attachment-media-view"> 232 <button type="button" class="placeholder button-add-media"><?php echo esc_html( $this->l10n['add_media'] ); ?></button> 233 </div> 234 <# } #> 235 </script> 236 <?php 237 } 238 239 /** 240 * Whether the widget has content to show. 241 * 242 * @since 4.9.0 243 * @access protected 244 * 245 * @param array $instance Widget instance props. 246 * @return bool Whether widget has content. 247 */ 248 protected function has_content( $instance ) { 249 if ( ! empty( $instance['ids'] ) ) { 250 $attachments = wp_parse_id_list( $instance['ids'] ); 251 // Prime attachment post caches. 252 _prime_post_caches( $attachments, false, false ); 253 foreach ( $attachments as $attachment ) { 254 if ( 'attachment' !== get_post_type( $attachment ) ) { 255 return false; 256 } 257 } 258 return true; 259 } 260 return false; 261 } 262 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |