class-wp-widget-media-gallery.php 7.1 KB

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