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

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