17 class WP_Widget_Media_Video extends WP_Widget_Media { |
17 class WP_Widget_Media_Video 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_video', __( 'Video' ), array( |
25 parent::__construct( |
26 'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ), |
26 'media_video', |
27 'mime_type' => 'video', |
27 __( 'Video' ), |
28 ) ); |
28 array( |
29 |
29 'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ), |
30 $this->l10n = array_merge( $this->l10n, array( |
30 'mime_type' => 'video', |
31 'no_media_selected' => __( 'No video selected' ), |
31 ) |
32 'add_media' => _x( 'Add Video', 'label for button in the video widget' ), |
32 ); |
33 'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ), |
33 |
34 'edit_media' => _x( 'Edit Video', 'label for button in the video 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 video. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
37 'no_media_selected' => __( 'No video selected' ), |
38 esc_url( admin_url( 'upload.php' ) ) |
38 'add_media' => _x( 'Add Video', 'label for button in the video widget' ), |
39 ), |
39 'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ), |
40 /* translators: %d: widget count */ |
40 'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ), |
41 'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ), |
41 'missing_attachment' => sprintf( |
42 'media_library_state_single' => __( 'Video Widget' ), |
42 /* translators: %s: URL to media library */ |
43 /* translators: %s: a list of valid video file extensions */ |
43 __( 'We can’t find that video. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
44 'unsupported_file_type' => sprintf( __( 'Sorry, we can’t load the video at the supplied URL. Please check that the URL is for a supported video file (%s) or stream (e.g. YouTube and Vimeo).' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ), |
44 esc_url( admin_url( 'upload.php' ) ) |
45 ) ); |
45 ), |
|
46 /* translators: %d: widget count */ |
|
47 'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ), |
|
48 'media_library_state_single' => __( 'Video Widget' ), |
|
49 /* translators: %s: a list of valid video file extensions */ |
|
50 'unsupported_file_type' => sprintf( __( 'Sorry, we can’t load the video at the supplied URL. Please check that the URL is for a supported video file (%s) or stream (e.g. YouTube and Vimeo).' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ), |
|
51 ) |
|
52 ); |
46 } |
53 } |
47 |
54 |
48 /** |
55 /** |
49 * Get schema for properties of a widget instance (item). |
56 * Get schema for properties of a widget instance (item). |
50 * |
57 * |
51 * @since 4.8.0 |
58 * @since 4.8.0 |
52 * |
59 * |
53 * @see WP_REST_Controller::get_item_schema() |
60 * @see WP_REST_Controller::get_item_schema() |
54 * @see WP_REST_Controller::get_additional_fields() |
61 * @see WP_REST_Controller::get_additional_fields() |
55 * @link https://core.trac.wordpress.org/ticket/35574 |
62 * @link https://core.trac.wordpress.org/ticket/35574 |
56 * @return array Schema for properties. |
63 * @return array Schema for properties. |
57 */ |
64 */ |
58 public function get_instance_schema() { |
65 public function get_instance_schema() { |
59 $schema = array_merge( |
66 |
60 parent::get_instance_schema(), |
67 $schema = array( |
61 array( |
68 'preload' => array( |
62 'preload' => array( |
69 'type' => 'string', |
63 'type' => 'string', |
70 'enum' => array( 'none', 'auto', 'metadata' ), |
64 'enum' => array( 'none', 'auto', 'metadata' ), |
71 'default' => 'metadata', |
65 'default' => 'metadata', |
72 'description' => __( 'Preload' ), |
66 'description' => __( 'Preload' ), |
73 'should_preview_update' => false, |
67 'should_preview_update' => false, |
74 ), |
68 ), |
75 'loop' => array( |
69 'loop' => array( |
76 'type' => 'boolean', |
70 'type' => 'boolean', |
77 'default' => false, |
71 'default' => false, |
78 'description' => __( 'Loop' ), |
72 'description' => __( 'Loop' ), |
79 'should_preview_update' => false, |
73 'should_preview_update' => false, |
80 ), |
74 ), |
81 'content' => array( |
75 'content' => array( |
82 'type' => 'string', |
76 'type' => 'string', |
83 'default' => '', |
77 'default' => '', |
84 'sanitize_callback' => 'wp_kses_post', |
78 'sanitize_callback' => 'wp_kses_post', |
85 'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ), |
79 'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ), |
86 'should_preview_update' => false, |
80 'should_preview_update' => false, |
87 ), |
81 ), |
|
82 ) |
|
83 ); |
88 ); |
84 |
89 |
85 foreach ( wp_get_video_extensions() as $video_extension ) { |
90 foreach ( wp_get_video_extensions() as $video_extension ) { |
86 $schema[ $video_extension ] = array( |
91 $schema[ $video_extension ] = array( |
87 'type' => 'string', |
92 'type' => 'string', |
88 'default' => '', |
93 'default' => '', |
89 'format' => 'uri', |
94 'format' => 'uri', |
90 /* translators: %s: video extension */ |
95 /* translators: %s: video extension */ |
91 'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ), |
96 'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ), |
92 ); |
97 ); |
93 } |
98 } |
94 |
99 |
95 return $schema; |
100 return array_merge( $schema, parent::get_instance_schema() ); |
96 } |
101 } |
97 |
102 |
98 /** |
103 /** |
99 * Render the media on the frontend. |
104 * Render the media on the frontend. |
100 * |
105 * |
101 * @since 4.8.0 |
106 * @since 4.8.0 |
102 * |
107 * |
103 * @param array $instance Widget instance props. |
108 * @param array $instance Widget instance props. |
104 * |
109 * |
105 * @return void |
110 * @return void |
106 */ |
111 */ |
107 public function render_media( $instance ) { |
112 public function render_media( $instance ) { |
108 $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); |
113 $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); |
109 $attachment = null; |
114 $attachment = null; |
110 |
115 |
111 if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) { |
116 if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) { |
112 $attachment = get_post( $instance['attachment_id'] ); |
117 $attachment = get_post( $instance['attachment_id'] ); |
113 } |
118 } |