medium.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // Embed support for Medium https://medium.com/p/3eaed64aed8a
  3. /**
  4. * Faux-oembed support for Medium permalinks
  5. *
  6. * e.g.
  7. * https://medium.com/help-center
  8. * https://medium.com/@richroll
  9. */
  10. wp_embed_register_handler( 'medium', '#^https?://medium.com/([a-zA-z0-9-_@]+)#', 'jetpack_embed_medium_oembed' );
  11. function jetpack_embed_medium_oembed( $matches, $attr, $url ) {
  12. $attr = jetpack_embed_medium_args( $attr );
  13. $attr['url'] = $url;
  14. return jetpack_embed_medium_embed_html( $attr );
  15. }
  16. function jetpack_embed_medium_embed_html( $args ) {
  17. $args = jetpack_embed_medium_args( $args );
  18. if ( empty( $args['url'] ) ) {
  19. return;
  20. }
  21. $args['type'] = jetpack_embed_medium_get_embed_type( $args['url'] );
  22. return sprintf( '<script async src="https://static.medium.com/embed.js"></script><a class="m-%1$s" href="%2$s" target="_blank" data-width="%3$s" data-border="%4$s" data-collapsed="%5$s">View %1$s at Medium.com</a>', esc_attr( $args['type'] ), esc_url( $args['url'] ), esc_attr( $args['width'] ), esc_attr( $args['border'] ), esc_attr( $args['collapsed'] ) );
  23. }
  24. /**
  25. * Shortcode support that allows passing in URL
  26. *
  27. * [medium url="https://medium.com/help-center" width="100%" border="false" collapsed="true"]
  28. */
  29. add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' );
  30. function jetpack_embed_medium_shortcode( $atts ) {
  31. $atts = jetpack_embed_medium_args( $atts );
  32. if ( ! empty( $atts['url'] ) ) {
  33. global $wp_embed;
  34. return $wp_embed->shortcode( $atts, $atts['url'] );
  35. }
  36. }
  37. function jetpack_embed_medium_get_embed_type( $url ) {
  38. $url_path = parse_url( $url, PHP_URL_PATH );
  39. if ( preg_match( '/^\/@[\.\w]+$/', $url_path ) ) {
  40. return 'profile';
  41. } else if ( preg_match( '/^\/[\da-zA-Z-]+$/', $url_path ) ) {
  42. return 'collection';
  43. }
  44. return 'story';
  45. }
  46. function jetpack_embed_medium_args( $atts ) {
  47. return shortcode_atts( array(
  48. 'url' => '',
  49. 'width' => '400',
  50. 'border' => true,
  51. 'collapsed' => false,
  52. ), $atts, 'medium' );
  53. }