|
306 | 306 | ), |
307 | 307 | 'youtube-id' => array( |
308 | 308 | 'default' => '', |
309 | | - 'description' => __( '11-character YouTube ID', 'ableplayer' ), |
| 309 | + 'description' => __( '11-character YouTube ID or a URL to a YouTube video page.', 'ableplayer' ), |
310 | 310 | ), |
311 | 311 | 'youtube-desc-id' => array( |
312 | 312 | 'default' => '', |
|
434 | 434 | $o .= ' data-skin="' . $all_atts['skin'] . '"'; |
435 | 435 | } |
436 | 436 | if ( ! empty( $all_atts['youtube-id'] ) ) { |
437 | | - $o .= ' data-youtube-id="' . $all_atts['youtube-id'] . '"'; |
| 437 | + $youtube_url = ableplayer_parse_youtube( $all_atts['youtube-id'] ); |
| 438 | + $o .= ' data-youtube-id="' . $all_atts['youtube-id'] . '"'; |
438 | 439 | } |
439 | 440 | if ( ! empty( $all_atts['youtube-desc-id'] ) ) { |
440 | | - $o .= ' data-youtube-desc-id="' . $all_atts['youtube-desc-id'] . '"'; |
| 441 | + $youtube_desc_url = ableplayer_parse_youtube( $all_atts['youtube-id'] ); |
| 442 | + $o .= ' data-youtube-desc-id="' . $all_atts['youtube-desc-id'] . '"'; |
441 | 443 | } |
442 | 444 | if ( ! empty( $all_atts['youtube-nocookie'] ) ) { |
443 | 445 | $o .= ' data-youtube-nocookie="' . $all_atts['youtube-nocookie'] . '"'; |
|
464 | 466 | } |
465 | 467 | add_shortcode( 'ableplayer', 'ableplayer_shortcode' ); |
466 | 468 |
|
| 469 | +/** |
| 470 | + * Parse the value of a YouTube shortcode attribute to extract YouTube ID if necessary. |
| 471 | + * |
| 472 | + * @param string $attr Value of the attribute. |
| 473 | + * |
| 474 | + * @param string YouTube video ID. |
| 475 | + */ |
| 476 | +function ableplayer_parse_youtube( $attr ) { |
| 477 | + $query = parse_url( $attr ); |
| 478 | + if ( ! isset( $query['query'] ) ) { |
| 479 | + $replace = array( 'http://youtu.be/', 'https://youtu.be/' ); |
| 480 | + |
| 481 | + return str_replace( $replace, '', $attr ); |
| 482 | + } else { |
| 483 | + parse_str( $query['query'], $results ); |
| 484 | + |
| 485 | + return $results['v']; |
| 486 | + } |
| 487 | + |
| 488 | + return $attr; |
| 489 | +} |
| 490 | + |
467 | 491 | /** |
468 | 492 | * Get unique ID for a specific Ableplayer instance. |
469 | 493 | * |
|
0 commit comments