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

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