暫無描述

medium.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Embed support for Medium
  4. *
  5. * Supported formats:
  6. * - Profiles: https://medium.com/@jeherve
  7. * - Stories: https://medium.com/@jeherve/this-is-a-story-19f582daaf5b
  8. * - And all the above in shortcode formats:
  9. * [medium url="https://medium.com/@jeherve/this-is-a-story-19f582daaf5b" width="100%" border="false" collapsed="true"]
  10. *
  11. * @package automattic/jetpack
  12. */
  13. // Faux-oembed support for Medium permalinks.
  14. wp_embed_register_handler( 'medium', '#^https?://medium.com/([a-zA-z0-9-_@]+)#', 'jetpack_embed_medium_oembed' );
  15. /**
  16. * Callback to modify output of embedded Medium posts.
  17. *
  18. * @param array $matches Regex partial matches against the URL passed.
  19. * @param array $attr Attributes received in embed response.
  20. * @param array $url Requested URL to be embedded.
  21. */
  22. function jetpack_embed_medium_oembed( $matches, $attr, $url ) {
  23. $attr = jetpack_embed_medium_args( $attr );
  24. $attr['url'] = $url;
  25. return jetpack_embed_medium_embed_html( $attr );
  26. }
  27. /**
  28. * Return custom markup to display a Medium profile, collection, or story.
  29. *
  30. * @param array $args Attributes received in embed response.
  31. */
  32. function jetpack_embed_medium_embed_html( $args ) {
  33. $args = jetpack_embed_medium_args( $args );
  34. if ( empty( $args['url'] ) ) {
  35. return;
  36. }
  37. $args['type'] = jetpack_embed_medium_get_embed_type( $args['url'] );
  38. if ( 'collection' === $args['type'] ) {
  39. return sprintf(
  40. '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
  41. esc_url( $args['url'] ),
  42. esc_html__( 'View this collection on Medium.com', 'jetpack' )
  43. );
  44. }
  45. wp_enqueue_script(
  46. 'medium-embed',
  47. 'https://static.medium.com/embed.js',
  48. array(),
  49. JETPACK__VERSION,
  50. true
  51. );
  52. return sprintf(
  53. '<a class="m-%1$s" href="%2$s" target="_blank" data-width="%3$s" data-border="%4$s" data-collapsed="%5$s">%6$s</a>',
  54. esc_attr( $args['type'] ),
  55. esc_url( $args['url'] ),
  56. esc_attr( $args['width'] ),
  57. esc_attr( $args['border'] ),
  58. esc_attr( $args['collapsed'] ),
  59. esc_html__( 'View at Medium.com', 'jetpack' )
  60. );
  61. }
  62. /**
  63. * Shortcode support that allows passing in URL
  64. *
  65. * @param array $atts Shortcode attributes.
  66. */
  67. function jetpack_embed_medium_shortcode( $atts ) {
  68. $atts = jetpack_embed_medium_args( $atts );
  69. if ( ! empty( $atts['url'] ) ) {
  70. global $wp_embed;
  71. return $wp_embed->shortcode( $atts, $atts['url'] );
  72. } else {
  73. if ( current_user_can( 'edit_posts' ) ) {
  74. return esc_html__( 'You did not provide a valid Medium URL.', 'jetpack' );
  75. } else {
  76. return '<!-- Missing Medium URL -->';
  77. }
  78. }
  79. }
  80. add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' );
  81. /**
  82. * Get embed type (profile, collection, or story) based on Medium URL.
  83. *
  84. * @param string $url Medium URL.
  85. */
  86. function jetpack_embed_medium_get_embed_type( $url ) {
  87. $url_path = wp_parse_url( $url, PHP_URL_PATH );
  88. if ( preg_match( '/^\/@[\.\w]+$/', $url_path ) ) {
  89. return 'profile';
  90. } elseif ( preg_match( '/^\/(?:s)\/(.+)$/', $url_path ) ) {
  91. return 'collection';
  92. }
  93. return 'story';
  94. }
  95. /**
  96. * Process Medium shortcode attributes.
  97. *
  98. * @param array $atts Shortcode attributes.
  99. */
  100. function jetpack_embed_medium_args( $atts ) {
  101. return shortcode_atts(
  102. array(
  103. 'url' => '',
  104. 'width' => '400',
  105. 'border' => true,
  106. 'collapsed' => false,
  107. ),
  108. $atts,
  109. 'medium'
  110. );
  111. }