No Description

vine.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Vine shortcode
  4. * The service is now archived, but existing embeds are still accessible.
  5. *
  6. * Examples:
  7. * Vine embed code:
  8. * <iframe class="vine-embed" src="https://vine.co/v/bjHh0zHdgZT" width="600" height="600" frameborder="0"></iframe>
  9. * <script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>
  10. *
  11. * URL example:
  12. * https://vine.co/v/bjHh0zHdgZT/
  13. *
  14. * Embed shortcode examples:
  15. * [embed]https://vine.co/v/bjHh0zHdgZT[/embed]
  16. * [embed width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
  17. * [embed type="postcard" width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
  18. *
  19. * @package automattic/jetpack
  20. */
  21. /**
  22. * Handle Vine embeds.
  23. *
  24. * @param array $matches Results after parsing the URL using the regex in wp_embed_register_handler().
  25. * @param array $attr Embed attributes.
  26. * @param string $url The original URL that was matched by the regex.
  27. * @param array $rawattr The original unmodified attributes.
  28. * @return string The embed HTML.
  29. */
  30. function vine_embed_video( $matches, $attr, $url, $rawattr ) {
  31. $max_height = 300;
  32. $type = 'simple';
  33. // Only allow 'postcard' or 'simple' types.
  34. if (
  35. isset( $rawattr['type'] )
  36. && 'postcard' === $rawattr['type']
  37. ) {
  38. $type = 'postcard';
  39. }
  40. $vine_size = Jetpack::get_content_width();
  41. // If the user enters a value for width or height, we ignore the Jetpack::get_content_width().
  42. if ( isset( $rawattr['width'] ) || isset( $rawattr['height'] ) ) {
  43. // 300 is the minimum size that Vine provides for embeds. Lower than that, the postcard embeds looks weird.
  44. $vine_size = max( $max_height, min( $attr['width'], $attr['height'] ) );
  45. }
  46. if ( empty( $vine_size ) ) {
  47. $vine_size = $max_height;
  48. }
  49. $url = 'https://vine.co/v/' . $matches[1] . '/embed/' . $type;
  50. $vine_html = sprintf(
  51. '<span class="embed-vine" style="display: block;"><iframe class="vine-embed" src="%1$s" width="%2$d" height="%3$d" frameborder="0"></iframe></span>',
  52. esc_url( $url ),
  53. (int) $vine_size,
  54. (int) $vine_size
  55. );
  56. wp_enqueue_script(
  57. 'vine-embed',
  58. 'https://platform.vine.co/static/scripts/embed.js',
  59. array(),
  60. JETPACK__VERSION,
  61. true
  62. );
  63. return $vine_html;
  64. }
  65. wp_embed_register_handler( 'jetpack_vine', '#https?://vine.co/v/([a-z0-9]+).*#i', 'vine_embed_video' );
  66. /**
  67. * Display the Vine shortcode.
  68. *
  69. * @param array $atts Shortcode attributes.
  70. */
  71. function vine_shortcode( $atts ) {
  72. global $wp_embed;
  73. if ( empty( $atts['url'] ) ) {
  74. return '';
  75. }
  76. if ( ! preg_match( '#https?://vine.co/v/([a-z0-9]+).*#i', $atts['url'] ) ) {
  77. return '';
  78. }
  79. return $wp_embed->shortcode( $atts, $atts['url'] );
  80. }
  81. add_shortcode( 'vine', 'vine_shortcode' );