17 class WP_Widget_Media_Audio extends WP_Widget_Media { |
17 class WP_Widget_Media_Audio 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_audio', __( 'Audio' ), array( |
25 parent::__construct( |
26 'description' => __( 'Displays an audio player.' ), |
26 'media_audio', |
27 'mime_type' => 'audio', |
27 __( 'Audio' ), |
28 ) ); |
28 array( |
29 |
29 'description' => __( 'Displays an audio player.' ), |
30 $this->l10n = array_merge( $this->l10n, array( |
30 'mime_type' => 'audio', |
31 'no_media_selected' => __( 'No audio selected' ), |
31 ) |
32 'add_media' => _x( 'Add Audio', 'label for button in the audio widget' ), |
32 ); |
33 'replace_media' => _x( 'Replace Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ), |
33 |
34 'edit_media' => _x( 'Edit Audio', 'label for button in the audio 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 audio file. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
37 'no_media_selected' => __( 'No audio selected' ), |
38 esc_url( admin_url( 'upload.php' ) ) |
38 'add_media' => _x( 'Add Audio', 'label for button in the audio widget' ), |
39 ), |
39 'replace_media' => _x( 'Replace Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ), |
40 /* translators: %d: widget count */ |
40 'edit_media' => _x( 'Edit Audio', 'label for button in the audio widget; should preferably not be longer than ~13 characters long' ), |
41 'media_library_state_multi' => _n_noop( 'Audio Widget (%d)', 'Audio Widget (%d)' ), |
41 'missing_attachment' => sprintf( |
42 'media_library_state_single' => __( 'Audio Widget' ), |
42 /* translators: %s: URL to media library */ |
43 'unsupported_file_type' => __( 'Looks like this isn’t the correct kind of file. Please link to an audio file instead.' ), |
43 __( 'We can’t find that audio file. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ), |
44 ) ); |
44 esc_url( admin_url( 'upload.php' ) ) |
|
45 ), |
|
46 /* translators: %d: widget count */ |
|
47 'media_library_state_multi' => _n_noop( 'Audio Widget (%d)', 'Audio Widget (%d)' ), |
|
48 'media_library_state_single' => __( 'Audio Widget' ), |
|
49 'unsupported_file_type' => __( 'Looks like this isn’t the correct kind of file. Please link to an audio file instead.' ), |
|
50 ) |
|
51 ); |
45 } |
52 } |
46 |
53 |
47 /** |
54 /** |
48 * Get schema for properties of a widget instance (item). |
55 * Get schema for properties of a widget instance (item). |
49 * |
56 * |
50 * @since 4.8.0 |
57 * @since 4.8.0 |
51 * |
58 * |
52 * @see WP_REST_Controller::get_item_schema() |
59 * @see WP_REST_Controller::get_item_schema() |
53 * @see WP_REST_Controller::get_additional_fields() |
60 * @see WP_REST_Controller::get_additional_fields() |
54 * @link https://core.trac.wordpress.org/ticket/35574 |
61 * @link https://core.trac.wordpress.org/ticket/35574 |
55 * @return array Schema for properties. |
62 * @return array Schema for properties. |
56 */ |
63 */ |
57 public function get_instance_schema() { |
64 public function get_instance_schema() { |
58 $schema = array_merge( |
65 $schema = array( |
59 parent::get_instance_schema(), |
66 'preload' => array( |
60 array( |
67 'type' => 'string', |
61 'preload' => array( |
68 'enum' => array( 'none', 'auto', 'metadata' ), |
62 'type' => 'string', |
69 'default' => 'none', |
63 'enum' => array( 'none', 'auto', 'metadata' ), |
70 'description' => __( 'Preload' ), |
64 'default' => 'none', |
71 ), |
65 'description' => __( 'Preload' ), |
72 'loop' => array( |
66 ), |
73 'type' => 'boolean', |
67 'loop' => array( |
74 'default' => false, |
68 'type' => 'boolean', |
75 'description' => __( 'Loop' ), |
69 'default' => false, |
76 ), |
70 'description' => __( 'Loop' ), |
|
71 ), |
|
72 ) |
|
73 ); |
77 ); |
74 |
78 |
75 foreach ( wp_get_audio_extensions() as $audio_extension ) { |
79 foreach ( wp_get_audio_extensions() as $audio_extension ) { |
76 $schema[ $audio_extension ] = array( |
80 $schema[ $audio_extension ] = array( |
77 'type' => 'string', |
81 'type' => 'string', |
78 'default' => '', |
82 'default' => '', |
79 'format' => 'uri', |
83 'format' => 'uri', |
80 /* translators: %s: audio extension */ |
84 /* translators: %s: audio extension */ |
81 'description' => sprintf( __( 'URL to the %s audio source file' ), $audio_extension ), |
85 'description' => sprintf( __( 'URL to the %s audio source file' ), $audio_extension ), |
82 ); |
86 ); |
83 } |
87 } |
84 |
88 |
85 return $schema; |
89 return array_merge( $schema, parent::get_instance_schema() ); |
86 } |
90 } |
87 |
91 |
88 /** |
92 /** |
89 * Render the media on the frontend. |
93 * Render the media on the frontend. |
90 * |
94 * |
91 * @since 4.8.0 |
95 * @since 4.8.0 |
92 * |
96 * |
93 * @param array $instance Widget instance props. |
97 * @param array $instance Widget instance props. |
94 * @return void |
98 * @return void |
95 */ |
99 */ |
96 public function render_media( $instance ) { |
100 public function render_media( $instance ) { |
97 $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); |
101 $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance ); |
98 $attachment = null; |
102 $attachment = null; |
99 |
103 |
100 if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) { |
104 if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) { |
101 $attachment = get_post( $instance['attachment_id'] ); |
105 $attachment = get_post( $instance['attachment_id'] ); |
102 } |
106 } |