~canonical-sysadmins/wordpress/4.9.1

« back to all changes in this revision

Viewing changes to wp-includes/widgets/class-wp-widget-media-video.php

  • Committer: Barry Price
  • Date: 2017-06-09 02:09:58 UTC
  • mfrom: (1.1.26 upstream)
  • Revision ID: barry.price@canonical.com-20170609020958-838whhwt2196f2vk
Merge WP4.8 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Widget API: WP_Widget_Media_Video class
 
4
 *
 
5
 * @package WordPress
 
6
 * @subpackage Widgets
 
7
 * @since 4.8.0
 
8
 */
 
9
 
 
10
/**
 
11
 * Core class that implements a video widget.
 
12
 *
 
13
 * @since 4.8.0
 
14
 *
 
15
 * @see WP_Widget
 
16
 */
 
17
class WP_Widget_Media_Video extends WP_Widget_Media {
 
18
 
 
19
        /**
 
20
         * Constructor.
 
21
         *
 
22
         * @since  4.8.0
 
23
         * @access public
 
24
         */
 
25
        public function __construct() {
 
26
                parent::__construct( 'media_video', __( 'Video' ), array(
 
27
                        'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
 
28
                        'mime_type'   => 'video',
 
29
                ) );
 
30
 
 
31
                $this->l10n = array_merge( $this->l10n, array(
 
32
                        'no_media_selected' => __( 'No video selected' ),
 
33
                        'add_media' => _x( 'Add Video', 'label for button in the video widget' ),
 
34
                        'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
 
35
                        'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
 
36
                        'missing_attachment' => sprintf(
 
37
                                /* translators: placeholder is URL to media library */
 
38
                                __( 'We can&#8217;t find that video. Check your <a href="%s">media library</a> and make sure it wasn&#8217;t deleted.' ),
 
39
                                esc_url( admin_url( 'upload.php' ) )
 
40
                        ),
 
41
                        /* translators: %d is widget count */
 
42
                        'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
 
43
                        'media_library_state_single' => __( 'Video Widget' ),
 
44
                        /* translators: placeholder is a list of valid video file extensions */
 
45
                        'unsupported_file_type' => sprintf( __( 'Sorry, we can&#8217;t display the video file type selected. Please select a supported video file (%1$s) or stream (YouTube or Vimeo) instead.' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
 
46
                ) );
 
47
        }
 
48
 
 
49
        /**
 
50
         * Get schema for properties of a widget instance (item).
 
51
         *
 
52
         * @since  4.8.0
 
53
         * @access public
 
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
                $schema = array_merge(
 
62
                        parent::get_instance_schema(),
 
63
                        array(
 
64
                                'preload' => array(
 
65
                                        'type' => 'string',
 
66
                                        'enum' => array( 'none', 'auto', 'metadata' ),
 
67
                                        'default' => 'metadata',
 
68
                                        'description' => __( 'Preload' ),
 
69
                                        'should_preview_update' => false,
 
70
                                ),
 
71
                                'loop' => array(
 
72
                                        'type' => 'boolean',
 
73
                                        'default' => false,
 
74
                                        'description' => __( 'Loop' ),
 
75
                                        'should_preview_update' => false,
 
76
                                ),
 
77
                                'content' => array(
 
78
                                        'type' => 'string',
 
79
                                        'default' => '',
 
80
                                        'sanitize_callback' => 'wp_kses_post',
 
81
                                        'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
 
82
                                        'should_preview_update' => false,
 
83
                                ),
 
84
                        )
 
85
                );
 
86
 
 
87
                foreach ( wp_get_video_extensions() as $video_extension ) {
 
88
                        $schema[ $video_extension ] = array(
 
89
                                'type' => 'string',
 
90
                                'default' => '',
 
91
                                'format' => 'uri',
 
92
                                /* translators: placeholder is video extension */
 
93
                                'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
 
94
                        );
 
95
                }
 
96
 
 
97
                return $schema;
 
98
        }
 
99
 
 
100
        /**
 
101
         * Render the media on the frontend.
 
102
         *
 
103
         * @since  4.8.0
 
104
         * @access public
 
105
         *
 
106
         * @param array $instance Widget instance props.
 
107
         *
 
108
         * @return void
 
109
         */
 
110
        public function render_media( $instance ) {
 
111
                $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
 
112
                $attachment = null;
 
113
 
 
114
                if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
 
115
                        $attachment = get_post( $instance['attachment_id'] );
 
116
                }
 
117
 
 
118
                if ( $attachment ) {
 
119
                        $src = wp_get_attachment_url( $attachment->ID );
 
120
                } else {
 
121
 
 
122
                        // Manually add the loop query argument.
 
123
                        $loop = $instance['loop'] ? '1' : '0';
 
124
                        $src = empty( $instance['url'] ) ? $instance['url'] : add_query_arg( 'loop', $loop, $instance['url'] );
 
125
                }
 
126
 
 
127
                if ( empty( $src ) ) {
 
128
                        return;
 
129
                }
 
130
 
 
131
                add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
 
132
 
 
133
                echo wp_video_shortcode(
 
134
                        array_merge(
 
135
                                $instance,
 
136
                                compact( 'src' )
 
137
                        ),
 
138
                        $instance['content']
 
139
                );
 
140
 
 
141
                remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
 
142
        }
 
143
 
 
144
        /**
 
145
         * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
 
146
         *
 
147
         * @since 4.8.0
 
148
         * @access public
 
149
         *
 
150
         * @param string $html Video shortcode HTML output.
 
151
         * @return string HTML Output.
 
152
         */
 
153
        public function inject_video_max_width_style( $html ) {
 
154
                $html = preg_replace( '/\sheight="\d+"/', '', $html );
 
155
                $html = preg_replace( '/\swidth="\d+"/', '', $html );
 
156
                $html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
 
157
                return $html;
 
158
        }
 
159
 
 
160
        /**
 
161
         * Enqueue preview scripts.
 
162
         *
 
163
         * These scripts normally are enqueued just-in-time when a video shortcode is used.
 
164
         * In the customizer, however, widgets can be dynamically added and rendered via
 
165
         * selective refresh, and so it is important to unconditionally enqueue them in
 
166
         * case a widget does get added.
 
167
         *
 
168
         * @since 4.8.0
 
169
         * @access public
 
170
         */
 
171
        public function enqueue_preview_scripts() {
 
172
                /** This filter is documented in wp-includes/media.php */
 
173
                if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
 
174
                        wp_enqueue_style( 'wp-mediaelement' );
 
175
                        wp_enqueue_script( 'wp-mediaelement' );
 
176
                }
 
177
 
 
178
                // Enqueue script needed by Vimeo; see wp_video_shortcode().
 
179
                wp_enqueue_script( 'froogaloop' );
 
180
        }
 
181
 
 
182
        /**
 
183
         * Loads the required scripts and styles for the widget control.
 
184
         *
 
185
         * @since 4.8.0
 
186
         * @access public
 
187
         */
 
188
        public function enqueue_admin_scripts() {
 
189
                parent::enqueue_admin_scripts();
 
190
 
 
191
                $handle = 'media-video-widget';
 
192
                wp_enqueue_script( $handle );
 
193
 
 
194
                $exported_schema = array();
 
195
                foreach ( $this->get_instance_schema() as $field => $field_schema ) {
 
196
                        $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
 
197
                }
 
198
                wp_add_inline_script(
 
199
                        $handle,
 
200
                        sprintf(
 
201
                                'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
 
202
                                wp_json_encode( $this->id_base ),
 
203
                                wp_json_encode( $exported_schema )
 
204
                        )
 
205
                );
 
206
 
 
207
                wp_add_inline_script(
 
208
                        $handle,
 
209
                        sprintf(
 
210
                                '
 
211
                                        wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
 
212
                                        wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
 
213
                                ',
 
214
                                wp_json_encode( $this->id_base ),
 
215
                                wp_json_encode( $this->widget_options['mime_type'] ),
 
216
                                wp_json_encode( $this->l10n )
 
217
                        )
 
218
                );
 
219
        }
 
220
 
 
221
        /**
 
222
         * Render form template scripts.
 
223
         *
 
224
         * @since 4.8.0
 
225
         * @access public
 
226
         */
 
227
        public function render_control_template_scripts() {
 
228
                parent::render_control_template_scripts()
 
229
                ?>
 
230
                <script type="text/html" id="tmpl-wp-media-widget-video-preview">
 
231
                        <# if ( data.error && 'missing_attachment' === data.error ) { #>
 
232
                                <div class="notice notice-error notice-alt notice-missing-attachment">
 
233
                                        <p><?php echo $this->l10n['missing_attachment']; ?></p>
 
234
                                </div>
 
235
                        <# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
 
236
                                <div class="notice notice-error notice-alt notice-missing-attachment">
 
237
                                        <p><?php echo $this->l10n['unsupported_file_type']; ?></p>
 
238
                                </div>
 
239
                        <# } else if ( data.error ) { #>
 
240
                                <div class="notice notice-error notice-alt">
 
241
                                        <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
 
242
                                </div>
 
243
                        <# } else if ( data.is_hosted_embed && data.model.poster ) { #>
 
244
                                <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
 
245
                                        <img src="{{ data.model.poster }}" />
 
246
                                </a>
 
247
                        <# } else if ( data.is_hosted_embed ) { #>
 
248
                                <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
 
249
                                        <span class="dashicons dashicons-format-video"></span>
 
250
                                </a>
 
251
                        <# } else if ( data.model.src ) { #>
 
252
                                <?php wp_underscore_video_template() ?>
 
253
                        <# } #>
 
254
                </script>
 
255
                <?php
 
256
        }
 
257
}