17 class WP_Widget_Media_Image extends WP_Widget_Media { |
17 class WP_Widget_Media_Image extends WP_Widget_Media { |
18 |
18 |
19 /** |
19 /** |
20 * Constructor. |
20 * Constructor. |
21 * |
21 * |
22 * @since 4.8.0 |
22 * @since 4.8.0 |
23 */ |
23 */ |
24 public function __construct() { |
24 public function __construct() { |
25 parent::__construct( 'media_image', __( 'Image' ), array( |
25 parent::__construct( |
26 'description' => __( 'Displays an image.' ), |
26 'media_image', |
27 'mime_type' => 'image', |
27 __( 'Image' ), |
28 ) ); |
28 array( |
29 |
29 'description' => __( 'Displays an image.' ), |
30 $this->l10n = array_merge( $this->l10n, array( |
30 'mime_type' => 'image', |
31 'no_media_selected' => __( 'No image selected' ), |
31 ) |
32 'add_media' => _x( 'Add Image', 'label for button in the image widget' ), |
32 ); |
33 'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ), |
33 |
34 'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ), |
34 $this->l10n = array_merge( |
35 'missing_attachment' => sprintf( |
35 $this->l10n, |
36 /* translators: %s: URL to media library */ |
36 array( |
37 __( 'We can’t find that image. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
37 'no_media_selected' => __( 'No image selected' ), |
38 esc_url( admin_url( 'upload.php' ) ) |
38 'add_media' => _x( 'Add Image', 'label for button in the image widget' ), |
39 ), |
39 'replace_media' => _x( 'Replace Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ), |
40 /* translators: %d: widget count */ |
40 'edit_media' => _x( 'Edit Image', 'label for button in the image widget; should preferably not be longer than ~13 characters long' ), |
41 'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ), |
41 'missing_attachment' => sprintf( |
42 'media_library_state_single' => __( 'Image Widget' ), |
42 /* translators: %s: URL to media library */ |
43 ) ); |
43 __( 'We can’t find that image. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
|
44 esc_url( admin_url( 'upload.php' ) ) |
|
45 ), |
|
46 /* translators: %d: widget count */ |
|
47 'media_library_state_multi' => _n_noop( 'Image Widget (%d)', 'Image Widget (%d)' ), |
|
48 'media_library_state_single' => __( 'Image Widget' ), |
|
49 ) |
|
50 ); |
44 } |
51 } |
45 |
52 |
46 /** |
53 /** |
47 * Get schema for properties of a widget instance (item). |
54 * Get schema for properties of a widget instance (item). |
48 * |
55 * |
49 * @since 4.8.0 |
56 * @since 4.8.0 |
50 * |
57 * |
51 * @see WP_REST_Controller::get_item_schema() |
58 * @see WP_REST_Controller::get_item_schema() |
52 * @see WP_REST_Controller::get_additional_fields() |
59 * @see WP_REST_Controller::get_additional_fields() |
53 * @link https://core.trac.wordpress.org/ticket/35574 |
60 * @link https://core.trac.wordpress.org/ticket/35574 |
54 * @return array Schema for properties. |
61 * @return array Schema for properties. |
55 */ |
62 */ |
56 public function get_instance_schema() { |
63 public function get_instance_schema() { |
57 return array_merge( |
64 return array_merge( |
58 parent::get_instance_schema(), |
|
59 array( |
65 array( |
60 'size' => array( |
66 'size' => array( |
61 'type' => 'string', |
67 'type' => 'string', |
62 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ), |
68 'enum' => array_merge( get_intermediate_image_sizes(), array( 'full', 'custom' ) ), |
63 'default' => 'medium', |
69 'default' => 'medium', |
64 'description' => __( 'Size' ), |
70 'description' => __( 'Size' ), |
65 ), |
71 ), |
66 'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'. |
72 'width' => array( // Via 'customWidth', only when size=custom; otherwise via 'width'. |
67 'type' => 'integer', |
73 'type' => 'integer', |
68 'minimum' => 0, |
74 'minimum' => 0, |
69 'default' => 0, |
75 'default' => 0, |
70 'description' => __( 'Width' ), |
76 'description' => __( 'Width' ), |
71 ), |
77 ), |
72 'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'. |
78 'height' => array( // Via 'customHeight', only when size=custom; otherwise via 'height'. |
73 'type' => 'integer', |
79 'type' => 'integer', |
74 'minimum' => 0, |
80 'minimum' => 0, |
75 'default' => 0, |
81 'default' => 0, |
76 'description' => __( 'Height' ), |
82 'description' => __( 'Height' ), |
77 ), |
83 ), |
78 |
84 |
79 'caption' => array( |
85 'caption' => array( |
80 'type' => 'string', |
86 'type' => 'string', |
81 'default' => '', |
87 'default' => '', |
82 'sanitize_callback' => 'wp_kses_post', |
88 'sanitize_callback' => 'wp_kses_post', |
83 'description' => __( 'Caption' ), |
89 'description' => __( 'Caption' ), |
84 'should_preview_update' => false, |
90 'should_preview_update' => false, |
85 ), |
91 ), |
86 'alt' => array( |
92 'alt' => array( |
87 'type' => 'string', |
93 'type' => 'string', |
88 'default' => '', |
94 'default' => '', |
89 'sanitize_callback' => 'sanitize_text_field', |
95 'sanitize_callback' => 'sanitize_text_field', |
90 'description' => __( 'Alternative Text' ), |
96 'description' => __( 'Alternative Text' ), |
91 ), |
97 ), |
92 'link_type' => array( |
98 'link_type' => array( |
93 'type' => 'string', |
99 'type' => 'string', |
94 'enum' => array( 'none', 'file', 'post', 'custom' ), |
100 'enum' => array( 'none', 'file', 'post', 'custom' ), |
95 'default' => 'custom', |
101 'default' => 'custom', |
96 'media_prop' => 'link', |
102 'media_prop' => 'link', |
97 'description' => __( 'Link To' ), |
103 'description' => __( 'Link To' ), |
98 'should_preview_update' => true, |
104 'should_preview_update' => true, |
99 ), |
105 ), |
100 'link_url' => array( |
106 'link_url' => array( |
101 'type' => 'string', |
107 'type' => 'string', |
102 'default' => '', |
108 'default' => '', |
103 'format' => 'uri', |
109 'format' => 'uri', |
104 'media_prop' => 'linkUrl', |
110 'media_prop' => 'linkUrl', |
105 'description' => __( 'URL' ), |
111 'description' => __( 'URL' ), |
106 'should_preview_update' => true, |
112 'should_preview_update' => true, |
107 ), |
113 ), |
108 'image_classes' => array( |
114 'image_classes' => array( |
109 'type' => 'string', |
115 'type' => 'string', |
110 'default' => '', |
116 'default' => '', |
111 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
117 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
112 'media_prop' => 'extraClasses', |
118 'media_prop' => 'extraClasses', |
113 'description' => __( 'Image CSS Class' ), |
119 'description' => __( 'Image CSS Class' ), |
114 'should_preview_update' => false, |
120 'should_preview_update' => false, |
115 ), |
121 ), |
116 'link_classes' => array( |
122 'link_classes' => array( |
117 'type' => 'string', |
123 'type' => 'string', |
118 'default' => '', |
124 'default' => '', |
119 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
125 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
120 'media_prop' => 'linkClassName', |
126 'media_prop' => 'linkClassName', |
121 'should_preview_update' => false, |
127 'should_preview_update' => false, |
122 'description' => __( 'Link CSS Class' ), |
128 'description' => __( 'Link CSS Class' ), |
123 ), |
129 ), |
124 'link_rel' => array( |
130 'link_rel' => array( |
125 'type' => 'string', |
131 'type' => 'string', |
126 'default' => '', |
132 'default' => '', |
127 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
133 'sanitize_callback' => array( $this, 'sanitize_token_list' ), |
128 'media_prop' => 'linkRel', |
134 'media_prop' => 'linkRel', |
129 'description' => __( 'Link Rel' ), |
135 'description' => __( 'Link Rel' ), |
130 'should_preview_update' => false, |
136 'should_preview_update' => false, |
131 ), |
137 ), |
132 'link_target_blank' => array( |
138 'link_target_blank' => array( |
133 'type' => 'boolean', |
139 'type' => 'boolean', |
134 'default' => false, |
140 'default' => false, |
135 'media_prop' => 'linkTargetBlank', |
141 'media_prop' => 'linkTargetBlank', |
136 'description' => __( 'Open link in a new tab' ), |
142 'description' => __( 'Open link in a new tab' ), |
137 'should_preview_update' => false, |
143 'should_preview_update' => false, |
138 ), |
144 ), |
139 'image_title' => array( |
145 'image_title' => array( |
140 'type' => 'string', |
146 'type' => 'string', |
141 'default' => '', |
147 'default' => '', |
142 'sanitize_callback' => 'sanitize_text_field', |
148 'sanitize_callback' => 'sanitize_text_field', |
143 'media_prop' => 'title', |
149 'media_prop' => 'title', |
144 'description' => __( 'Image Title Attribute' ), |
150 'description' => __( 'Image Title Attribute' ), |
145 'should_preview_update' => false, |
151 'should_preview_update' => false, |
146 ), |
152 ), |
147 |
153 |
148 /* |
154 /* |
149 * There are two additional properties exposed by the PostImage modal |
155 * There are two additional properties exposed by the PostImage modal |
203 $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size ); |
213 $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size ); |
204 |
214 |
205 $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes ); |
215 $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes ); |
206 |
216 |
207 $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) ); |
217 $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) ); |
208 $width = empty( $caption_size[0] ) ? 0 : $caption_size[0]; |
218 $width = empty( $caption_size[0] ) ? 0 : $caption_size[0]; |
209 |
219 |
210 } else { |
220 } else { |
211 if ( empty( $instance['url'] ) ) { |
221 if ( empty( $instance['url'] ) ) { |
212 return; |
222 return; |
213 } |
223 } |
214 |
224 |
215 $instance['size'] = 'custom'; |
225 $instance['size'] = 'custom'; |
216 $caption = $instance['caption']; |
226 $caption = $instance['caption']; |
217 $width = $instance['width']; |
227 $width = $instance['width']; |
218 $classes = 'image ' . $instance['image_classes']; |
228 $classes = 'image ' . $instance['image_classes']; |
219 if ( 0 === $instance['width'] ) { |
229 if ( 0 === $instance['width'] ) { |
220 $instance['width'] = ''; |
230 $instance['width'] = ''; |
221 } |
231 } |
222 if ( 0 === $instance['height'] ) { |
232 if ( 0 === $instance['height'] ) { |
223 $instance['height'] = ''; |
233 $instance['height'] = ''; |
224 } |
234 } |
225 |
235 |
226 $image = sprintf( '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />', |
236 $image = sprintf( |
|
237 '<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />', |
227 esc_attr( $classes ), |
238 esc_attr( $classes ), |
228 esc_url( $instance['url'] ), |
239 esc_url( $instance['url'] ), |
229 esc_attr( $instance['alt'] ), |
240 esc_attr( $instance['alt'] ), |
230 esc_attr( $instance['width'] ), |
241 esc_attr( $instance['width'] ), |
231 esc_attr( $instance['height'] ) |
242 esc_attr( $instance['height'] ) |