70 'add_media' => _x( 'Add Media', 'label for button in the media widget' ), |
70 'add_media' => _x( 'Add Media', 'label for button in the media widget' ), |
71 'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ), |
71 'replace_media' => _x( 'Replace Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ), |
72 'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ), |
72 'edit_media' => _x( 'Edit Media', 'label for button in the media widget; should preferably not be longer than ~13 characters long' ), |
73 'add_to_widget' => __( 'Add to Widget' ), |
73 'add_to_widget' => __( 'Add to Widget' ), |
74 'missing_attachment' => sprintf( |
74 'missing_attachment' => sprintf( |
75 /* translators: %s: URL to media library */ |
75 /* translators: %s: URL to media library. */ |
76 __( 'We can’t find that file. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
76 __( 'We can’t find that file. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
77 esc_url( admin_url( 'upload.php' ) ) |
77 esc_url( admin_url( 'upload.php' ) ) |
78 ), |
78 ), |
79 /* translators: %d: widget count */ |
79 /* translators: %d: Widget count. */ |
80 'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ), |
80 'media_library_state_multi' => _n_noop( 'Media Widget (%d)', 'Media Widget (%d)' ), |
81 'media_library_state_single' => __( 'Media Widget' ), |
81 'media_library_state_single' => __( 'Media Widget' ), |
82 'unsupported_file_type' => __( 'Looks like this isn’t the correct kind of file. Please link to an appropriate file instead.' ), |
82 'unsupported_file_type' => __( 'Looks like this isn’t the correct kind of file. Please link to an appropriate file instead.' ), |
83 ); |
83 ); |
84 $this->l10n = array_merge( $l10n_defaults, array_filter( $this->l10n ) ); |
84 $this->l10n = array_merge( $l10n_defaults, array_filter( $this->l10n ) ); |
104 if ( $this->registered ) { |
104 if ( $this->registered ) { |
105 return; |
105 return; |
106 } |
106 } |
107 $this->registered = true; |
107 $this->registered = true; |
108 |
108 |
109 // Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). |
109 // Note that the widgets component in the customizer will also do |
|
110 // the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). |
110 add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); |
111 add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); |
111 |
112 |
112 if ( $this->is_preview() ) { |
113 if ( $this->is_preview() ) { |
113 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) ); |
114 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) ); |
114 } |
115 } |
115 |
116 |
116 // Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts(). |
117 // Note that the widgets component in the customizer will also do |
|
118 // the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts(). |
117 add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) ); |
119 add_action( 'admin_footer-widgets.php', array( $this, 'render_control_template_scripts' ) ); |
118 |
120 |
119 add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 ); |
121 add_filter( 'display_media_states', array( $this, 'display_media_state' ), 10, 2 ); |
120 } |
122 } |
121 |
123 |
125 * @since 4.8.0 |
127 * @since 4.8.0 |
126 * |
128 * |
127 * @see WP_REST_Controller::get_item_schema() |
129 * @see WP_REST_Controller::get_item_schema() |
128 * @see WP_REST_Controller::get_additional_fields() |
130 * @see WP_REST_Controller::get_additional_fields() |
129 * @link https://core.trac.wordpress.org/ticket/35574 |
131 * @link https://core.trac.wordpress.org/ticket/35574 |
|
132 * |
130 * @return array Schema for properties. |
133 * @return array Schema for properties. |
131 */ |
134 */ |
132 public function get_instance_schema() { |
135 public function get_instance_schema() { |
133 $schema = array( |
136 $schema = array( |
134 'attachment_id' => array( |
137 'attachment_id' => array( |
271 if ( ! array_key_exists( $field, $new_instance ) ) { |
274 if ( ! array_key_exists( $field, $new_instance ) ) { |
272 continue; |
275 continue; |
273 } |
276 } |
274 $value = $new_instance[ $field ]; |
277 $value = $new_instance[ $field ]; |
275 |
278 |
276 // Workaround for rest_validate_value_from_schema() due to the fact that rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true. |
279 /* |
|
280 * Workaround for rest_validate_value_from_schema() due to the fact that |
|
281 * rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true. |
|
282 */ |
277 if ( 'boolean' === $field_schema['type'] && '' === $value ) { |
283 if ( 'boolean' === $field_schema['type'] && '' === $value ) { |
278 $value = false; |
284 $value = false; |
279 } |
285 } |
280 |
286 |
281 if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) { |
287 if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) { |
318 * Note that the widget UI itself is rendered with JavaScript via `MediaWidgetControl#render()`. |
324 * Note that the widget UI itself is rendered with JavaScript via `MediaWidgetControl#render()`. |
319 * |
325 * |
320 * @since 4.8.0 |
326 * @since 4.8.0 |
321 * |
327 * |
322 * @see \WP_Widget_Media::render_control_template_scripts() Where the JS template is located. |
328 * @see \WP_Widget_Media::render_control_template_scripts() Where the JS template is located. |
|
329 * |
323 * @param array $instance Current settings. |
330 * @param array $instance Current settings. |
324 * @return void |
|
325 */ |
331 */ |
326 final public function form( $instance ) { |
332 final public function form( $instance ) { |
327 $instance_schema = $this->get_instance_schema(); |
333 $instance_schema = $this->get_instance_schema(); |
328 $instance = wp_array_slice_assoc( |
334 $instance = wp_array_slice_assoc( |
329 wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ), |
335 wp_parse_args( (array) $instance, wp_list_pluck( $instance_schema, 'default' ) ), |