class-wp-widget-media-image.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Media_Image class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.8.0
  8. */
  9. /**
  10. * Core class that implements an image widget.
  11. *
  12. * @since 4.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Media_Image extends WP_Widget_Media {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 4.8.0
  21. */
  22. public function __construct() {
  23. parent::__construct( 'media_image', __( 'Image' ), array(
  24. 'description' => __( 'Displays an image.' ),
  25. 'mime_type' => 'image',
  26. ) );
  27. $this->l10n = array_merge( $this->l10n, array(
  28. 'no_media_selected' => __( 'No image selected' ),
  29. 'add_media' => _x( 'Add Image', 'label for button in the image widget' ),
  30. 'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
  31. 'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ),
  32. 'missing_attachment' => sprintf(
  33. /* translators: %s: URL to media library */
  34. __( 'We can&#8217;t find that image. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
  35. esc_url( admin_url( 'upload.php' ) )
  36. ),
  37. /* translators: %d: widget count */
  38. 'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ),
  39. 'media_library_state_single' => __( 'Image Widget' ),
  40. ) );
  41. }
  42. /**
  43. * Get schema for properties of a widget instance (item).
  44. *
  45. * @since 4.8.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. * @return array Schema for properties.
  51. */
  52. public function get_instance_schema() {
  53. return array_merge(
  54. parent::get_instance_schema(),
  55. array(
  56. 'size' => array(
  57. 'type' => 'string',
  58. 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ),
  59. 'default' => 'medium',
  60. 'description' => __( 'Size' ),
  61. ),
  62. 'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'.
  63. 'type' => 'integer',
  64. 'minimum' => 0,
  65. 'default' => 0,
  66. 'description' => __( 'Width' ),
  67. ),
  68. 'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'.
  69. 'type' => 'integer',
  70. 'minimum' => 0,
  71. 'default' => 0,
  72. 'description' => __( 'Height' ),
  73. ),
  74. 'caption' => array(
  75. 'type' => 'string',
  76. 'default' => '',
  77. 'sanitize_callback' => 'wp_kses_post',
  78. 'description' => __( 'Caption' ),
  79. 'should_preview_update' => false,
  80. ),
  81. 'alt' => array(
  82. 'type' => 'string',
  83. 'default' => '',
  84. 'sanitize_callback' => 'sanitize_text_field',
  85. 'description' => __( 'Alternative Text' ),
  86. ),
  87. 'link_type' => array(
  88. 'type' => 'string',
  89. 'enum' => array( 'none', 'file', 'post', 'custom' ),
  90. 'default' => 'custom',
  91. 'media_prop' => 'link',
  92. 'description' => __( 'Link To' ),
  93. 'should_preview_update' => true,
  94. ),
  95. 'link_url' => array(
  96. 'type' => 'string',
  97. 'default' => '',
  98. 'format' => 'uri',
  99. 'media_prop' => 'linkUrl',
  100. 'description' => __( 'URL' ),
  101. 'should_preview_update' => true,
  102. ),
  103. 'image_classes' => array(
  104. 'type' => 'string',
  105. 'default' => '',
  106. 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
  107. 'media_prop' => 'extraClasses',
  108. 'description' => __( 'Image CSS Class' ),
  109. 'should_preview_update' => false,
  110. ),
  111. 'link_classes' => array(
  112. 'type' => 'string',
  113. 'default' => '',
  114. 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
  115. 'media_prop' => 'linkClassName',
  116. 'should_preview_update' => false,
  117. 'description' => __( 'Link CSS Class' ),
  118. ),
  119. 'link_rel' => array(
  120. 'type' => 'string',
  121. 'default' => '',
  122. 'sanitize_callback' => array( $this, 'sanitize_token_list' ),
  123. 'media_prop' => 'linkRel',
  124. 'description' => __( 'Link Rel' ),
  125. 'should_preview_update' => false,
  126. ),
  127. 'link_target_blank' => array(
  128. 'type' => 'boolean',
  129. 'default' => false,
  130. 'media_prop' => 'linkTargetBlank',
  131. 'description' => __( 'Open link in a new tab' ),
  132. 'should_preview_update' => false,
  133. ),
  134. 'image_title' => array(
  135. 'type' => 'string',
  136. 'default' => '',
  137. 'sanitize_callback' => 'sanitize_text_field',
  138. 'media_prop' => 'title',
  139. 'description' => __( 'Image Title Attribute' ),
  140. 'should_preview_update' => false,
  141. ),
  142. /*
  143. * There are two additional properties exposed by the PostImage modal
  144. * that don't seem to be relevant, as they may only be derived read-only
  145. * values:
  146. * - originalUrl
  147. * - aspectRatio
  148. * - height (redundant when size is not custom)
  149. * - width (redundant when size is not custom)
  150. */
  151. )
  152. );
  153. }
  154. /**
  155. * Render the media on the frontend.
  156. *
  157. * @since 4.8.0
  158. *
  159. * @param array $instance Widget instance props.
  160. * @return void
  161. */
  162. public function render_media( $instance ) {
  163. $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
  164. $instance = wp_parse_args( $instance, array(
  165. 'size' => 'thumbnail',
  166. ) );
  167. $attachment = null;
  168. if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
  169. $attachment = get_post( $instance['attachment_id'] );
  170. }
  171. if ( $attachment ) {
  172. $caption = '';
  173. if ( ! isset( $instance['caption'] ) ) {
  174. $caption = $attachment->post_excerpt;
  175. } elseif ( trim( $instance['caption'] ) ) {
  176. $caption = $instance['caption'];
  177. }
  178. $image_attributes = array(
  179. 'class' => sprintf( 'image wp-image-%d %s', $attachment->ID, $instance['image_classes'] ),
  180. 'style' => 'max-width: 100%; height: auto;',
  181. );
  182. if ( ! empty( $instance['image_title'] ) ) {
  183. $image_attributes['title'] = $instance['image_title'];
  184. }
  185. if ( $instance['alt'] ) {
  186. $image_attributes['alt'] = $instance['alt'];
  187. }
  188. $size = $instance['size'];
  189. if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
  190. $size = array( $instance['width'], $instance['height'] );
  191. }
  192. $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
  193. $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
  194. $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
  195. $width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
  196. } else {
  197. if ( empty( $instance['url'] ) ) {
  198. return;
  199. }
  200. $instance['size'] = 'custom';
  201. $caption = $instance['caption'];
  202. $width = $instance['width'];
  203. $classes = 'image ' . $instance['image_classes'];
  204. if ( 0 === $instance['width'] ) {
  205. $instance['width'] = '';
  206. }
  207. if ( 0 === $instance['height'] ) {
  208. $instance['height'] = '';
  209. }
  210. $image = sprintf( '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />',
  211. esc_attr( $classes ),
  212. esc_url( $instance['url'] ),
  213. esc_attr( $instance['alt'] ),
  214. esc_attr( $instance['width'] ),
  215. esc_attr( $instance['height'] )
  216. );
  217. } // End if().
  218. $url = '';
  219. if ( 'file' === $instance['link_type'] ) {
  220. $url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];
  221. } elseif ( $attachment && 'post' === $instance['link_type'] ) {
  222. $url = get_attachment_link( $attachment->ID );
  223. } elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {
  224. $url = $instance['link_url'];
  225. }
  226. if ( $url ) {
  227. $link = sprintf( '<a href="%s"', esc_url( $url ) );
  228. if ( ! empty( $instance['link_classes'] ) ) {
  229. $link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );
  230. }
  231. if ( ! empty( $instance['link_rel'] ) ) {
  232. $link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );
  233. }
  234. if ( ! empty( $instance['link_target_blank'] ) ) {
  235. $link .= ' target="_blank"';
  236. }
  237. $link .= '>';
  238. $link .= $image;
  239. $link .= '</a>';
  240. $image = $link;
  241. }
  242. if ( $caption ) {
  243. $image = img_caption_shortcode( array(
  244. 'width' => $width,
  245. 'caption' => $caption,
  246. ), $image );
  247. }
  248. echo $image;
  249. }
  250. /**
  251. * Loads the required media files for the media manager and scripts for media widgets.
  252. *
  253. * @since 4.8.0
  254. */
  255. public function enqueue_admin_scripts() {
  256. parent::enqueue_admin_scripts();
  257. $handle = 'media-image-widget';
  258. wp_enqueue_script( $handle );
  259. $exported_schema = array();
  260. foreach ( $this->get_instance_schema() as $field => $field_schema ) {
  261. $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
  262. }
  263. wp_add_inline_script(
  264. $handle,
  265. sprintf(
  266. 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
  267. wp_json_encode( $this->id_base ),
  268. wp_json_encode( $exported_schema )
  269. )
  270. );
  271. wp_add_inline_script(
  272. $handle,
  273. sprintf(
  274. '
  275. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
  276. wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
  277. ',
  278. wp_json_encode( $this->id_base ),
  279. wp_json_encode( $this->widget_options['mime_type'] ),
  280. wp_json_encode( $this->l10n )
  281. )
  282. );
  283. }
  284. /**
  285. * Render form template scripts.
  286. *
  287. * @since 4.8.0
  288. */
  289. public function render_control_template_scripts() {
  290. parent::render_control_template_scripts();
  291. ?>
  292. <script type="text/html" id="tmpl-wp-media-widget-image-fields">
  293. <# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #>
  294. <# if ( data.url ) { #>
  295. <p class="media-widget-image-link">
  296. <label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label>
  297. <input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link" value="{{ data.link_url }}" placeholder="http://" pattern="((\w+:)?\/\/\w.*|\w+:(?!\/\/$)|\/|\?|#).*">
  298. </p>
  299. <# } #>
  300. </script>
  301. <script type="text/html" id="tmpl-wp-media-widget-image-preview">
  302. <# var describedById = 'describedBy-' + String( Math.random() ); #>
  303. <# if ( data.error && 'missing_attachment' === data.error ) { #>
  304. <div class="notice notice-error notice-alt notice-missing-attachment">
  305. <p><?php echo $this->l10n['missing_attachment']; ?></p>
  306. </div>
  307. <# } else if ( data.error ) { #>
  308. <div class="notice notice-error notice-alt">
  309. <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
  310. </div>
  311. <# } else if ( data.url ) { #>
  312. <img class="attachment-thumb" src="{{ data.url }}" draggable="false" alt="{{ data.alt }}" <# if ( ! data.alt && data.currentFilename ) { #> aria-describedby="{{ describedById }}" <# } #> />
  313. <# if ( ! data.alt && data.currentFilename ) { #>
  314. <p class="hidden" id="{{ describedById }}"><?php
  315. /* translators: placeholder is image filename */
  316. echo sprintf( __( 'Current image: %s' ), '{{ data.currentFilename }}' );
  317. ?></p>
  318. <# } #>
  319. <# } #>
  320. </script>
  321. <?php
  322. }
  323. }