vine.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Vine shortcode
  4. */
  5. /**
  6. * Vine embed code:
  7. * <iframe class="vine-embed" src="https://vine.co/v/bjHh0zHdgZT" width="600" height="600" frameborder="0"></iframe>
  8. * <script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>
  9. *
  10. * URL example:
  11. * https://vine.co/v/bjHh0zHdgZT/
  12. *
  13. * Embed shortcode examples:
  14. * [embed]https://vine.co/v/bjHh0zHdgZT[/embed]
  15. * [embed width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
  16. * [embed type="postcard" width="300"]https://vine.co/v/bjHh0zHdgZT[/embed]
  17. **/
  18. function vine_embed_video( $matches, $attr, $url, $rawattr ) {
  19. static $vine_flag_embedded_script;
  20. $max_height = 300;
  21. $type = 'simple';
  22. // Only allow 'postcard' or 'simple' types
  23. if ( isset( $rawattr['type'] ) && $rawattr['type'] === 'postcard' )
  24. $type = 'postcard';
  25. $vine_size = Jetpack::get_content_width();
  26. // If the user enters a value for width or height, we ignore the Jetpack::get_content_width()
  27. if ( isset( $rawattr['width'] ) || isset( $rawattr['height'] ) ) {
  28. // 300 is the minimum size that Vine provides for embeds. Lower than that, the postcard embeds looks weird.
  29. $vine_size = max( $max_height, min( $attr['width'], $attr['height'] ) );
  30. }
  31. if ( empty( $vine_size ) ) {
  32. $vine_size = $max_height;
  33. }
  34. $url = 'https://vine.co/v/' . $matches[1] . '/embed/' . $type;
  35. $vine_html = sprintf( '<span class="embed-vine" style="display: block;"><iframe class="vine-embed" src="%s" width="%s" height="%s" frameborder="0"></iframe></span>', esc_url( $url ), (int) $vine_size, (int) $vine_size );
  36. if ( $vine_flag_embedded_script !== true ) {
  37. $vine_html .= '<script async src="//platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>';
  38. $vine_flag_embedded_script = true;
  39. }
  40. return $vine_html;
  41. }
  42. wp_embed_register_handler( 'jetpack_vine', '#https?://vine.co/v/([a-z0-9]+).*#i', 'vine_embed_video' );
  43. function vine_shortcode( $atts ) {
  44. global $wp_embed;
  45. if ( empty( $atts['url'] ) )
  46. return '';
  47. if ( ! preg_match( '#https?://vine.co/v/([a-z0-9]+).*#i', $atts['url'] ) )
  48. return '';
  49. return $wp_embed->shortcode( $atts, $atts['url'] );
  50. }
  51. add_shortcode( 'vine', 'vine_shortcode' );