Skip to content

Commit b3e13c4

Browse files
committed
Add support to generate Able Player shortcodes with direct URLs
Fixes #25
1 parent c6fb7d6 commit b3e13c4

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/ableplayer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,31 +476,34 @@ function ableplayer_shortcode( $atts, $content = null ) {
476476
} else {
477477
$type = false;
478478
if ( $all_atts['media-id'] ) {
479+
$is_num = is_numeric( $all_atts['media-id'] );
479480
// If Video ID is set but is not a valid URL, return.
480-
$media_id = ( is_numeric( $all_atts['media-id'] ) ) ? wp_get_attachment_url( $all_atts['media-id'] ) : $all_atts['media-id'];
481+
$media_id = ( $is_num ) ? wp_get_attachment_url( $all_atts['media-id'] ) : $all_atts['media-id'];
481482
if ( ! $media_id ) {
482483
return false;
483484
} else {
484-
$type = get_post_mime_type( $all_atts['media-id'] );
485+
$type = ( $is_num ) ? get_post_mime_type( $all_atts['media-id'] ) : 'video/mp4';
485486
$type = ( 'video/quicktime' === $type ) ? 'video/mp4' : $type;
486487
$element = ( wp_attachment_is( 'audio', $all_atts['media-id'] ) ) ? 'audio' : 'video';
487488
$source = '<source type="' . esc_attr( $type ) . '" src="' . esc_url( $media_id ) . '"%datasrc%>' . PHP_EOL;
488489
}
489490
}
490491
if ( $all_atts['media-desc-id'] ) {
492+
$is_num = is_numeric( $all_atts['media-desc-id'] );
491493
// If Video ID is set but is not a valid URL, return.
492-
$media_desc_id = ( is_numeric( $all_atts['media-desc-id'] ) ) ? wp_get_attachment_url( $all_atts['media-desc-id'] ) : $all_atts['media-desc-id'];
494+
$media_desc_id = ( $is_num ) ? wp_get_attachment_url( $all_atts['media-desc-id'] ) : $all_atts['media-desc-id'];
493495
if ( $media_desc_id ) {
494-
$datatype = get_post_mime_type( $all_atts['media-desc-id'] );
496+
$datatype = ( $is_num ) ? get_post_mime_type( $all_atts['media-desc-id'] ) : 'video/mp4';
495497
$datatype = ( 'video/quicktime' ) ? 'video/mp4' : $datatype;
496498
$datasource .= ( $type === $datatype ) ? ' data-desc-src="' . esc_url( $media_desc_id ) . '"' : '';
497499
}
498500
}
499501
if ( $all_atts['media-asl-id'] ) {
502+
$is_num = is_numeric( $all_atts['media-asl-id'] );
500503
// If Video ID is set but is not a valid URL, return.
501-
$media_asl_id = ( is_numeric( $all_atts['media-asl-id'] ) ) ? wp_get_attachment_url( $all_atts['media-asl-id'] ) : $all_atts['media-desc-id'];
504+
$media_asl_id = ( $is_num ) ? wp_get_attachment_url( $all_atts['media-asl-id'] ) : $all_atts['media-asl-id'];
502505
if ( $media_asl_id ) {
503-
$datatype = get_post_mime_type( $all_atts['media-asl-id'] );
506+
$datatype = ( $is_num ) ? get_post_mime_type( $all_atts['media-asl-id'] ) : 'video/mp4';
504507
$datatype = ( 'video/quicktime' ) ? 'video/mp4' : $datatype;
505508
$datasource .= ( $type === $datatype ) ? ' data-sign-src="' . esc_url( $media_asl_id ) . '"' : '';
506509
}

src/inc/generator.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ function ableplayer_generate( $format = 'shortcode' ) {
2727
$string = '';
2828
$array = array();
2929
$shortcode = 'ableplayer';
30-
$keys = array( 'youtube-id', 'vimeo-id', 'media-id', 'youtube-desc-id', 'youtube-sign-src', 'vimeo-desc-id', 'media-desc-id', 'media-asl-id', 'poster', 'captions', 'subtitles', 'descriptions', 'chapters', 'autoplay', 'loop', 'playsinline', 'hidecontrols', 'heading', 'speed', 'start', 'volume', 'seekinterval' );
30+
$keys = array( 'youtube-id', 'vimeo-id', 'media-id', 'media-id-url', 'media-desc-id-url', 'media-asl-id-url', 'youtube-desc-id', 'youtube-sign-src', 'vimeo-desc-id', 'media-desc-id', 'media-asl-id', 'poster', 'captions', 'subtitles', 'descriptions', 'chapters', 'autoplay', 'loop', 'playsinline', 'hidecontrols', 'heading', 'speed', 'start', 'volume', 'seekinterval' );
3131
$post = map_deep( $_POST, 'sanitize_text_field' );
3232

33-
if ( empty( $post['youtube-id'] ) && empty( $post['vimeo-id'] ) && empty( $post['media-id'] ) ) {
33+
if ( empty( $post['youtube-id'] ) && empty( $post['vimeo-id'] ) && empty( $post['media-id'] ) && empty( $post['media-id-url'] ) ) {
3434
return array(
3535
'message' => __( 'You must specify at least one media source', 'ableplayer' ),
3636
'type' => 'error',
@@ -44,6 +44,9 @@ function ableplayer_generate( $format = 'shortcode' ) {
4444
if ( 'heading' === $key && ableplayer_get_settings( 'default_heading' ) === $v ) {
4545
continue;
4646
}
47+
if ( str_contains( $key, '-url' ) ) {
48+
$key = str_replace( '-url', '', $key );
49+
}
4750
if ( '' !== $v ) {
4851
if ( in_array( $key, array( 'captions', 'subtitles', 'descriptions', 'chapters' ), true ) ) {
4952
$v .= ableplayer_shortcode_track( $key, $post );
@@ -178,6 +181,7 @@ function ableplayer_generator_fields( $data ) {
178181
'youtube' => 'YouTube',
179182
'vimeo' => 'Vimeo',
180183
'local' => 'Local',
184+
'media' => 'URL',
181185
),
182186
),
183187
'generator'
@@ -221,6 +225,43 @@ function ableplayer_generator_fields( $data ) {
221225
);
222226
?>
223227
</p>
228+
<p class="media-sources media">
229+
<?php
230+
ableplayer_settings_field(
231+
array(
232+
'name' => 'media-id-url',
233+
'label' => __( 'URL Source', 'ableplayer' ),
234+
'type' => 'url',
235+
'atts' => array(
236+
'placeholder' => 'https://example.com',
237+
),
238+
),
239+
'generator'
240+
);
241+
ableplayer_settings_field(
242+
array(
243+
'name' => 'media-desc-id-url',
244+
'label' => __( 'URL for Audio Described Source', 'ableplayer' ),
245+
'type' => 'url',
246+
'atts' => array(
247+
'placeholder' => 'https://example.com',
248+
),
249+
),
250+
'generator'
251+
);
252+
ableplayer_settings_field(
253+
array(
254+
'name' => 'media-asl-id-url',
255+
'label' => __( 'URL for Sign Language Source', 'ableplayer' ),
256+
'type' => 'url',
257+
'atts' => array(
258+
'placeholder' => 'https://example.com',
259+
),
260+
),
261+
'generator'
262+
);
263+
?>
264+
</p>
224265
<p class="media-sources vimeo">
225266
<?php
226267
ableplayer_settings_field(

0 commit comments

Comments
 (0)