/modules/shortcodes/medium.php

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