css
2 weeks ago
images
1 year ago
img
4 weeks ago
js
6 months ago
archiveorg-book.php
6 months ago
archiveorg.php
6 months ago
archives.php
2 weeks ago
bandcamp.php
6 months ago
brightcove.php
5 months ago
cartodb.php
6 months ago
class.filter-embedded-html-objects.php
6 months ago
codepen.php
6 months ago
crowdsignal.php
5 months ago
dailymotion.php
6 months ago
descript.php
6 months ago
facebook.php
6 months ago
flatio.php
6 months ago
flickr.php
5 months ago
getty.php
6 months ago
gist.php
6 months ago
googleapps.php
6 months ago
googlemaps.php
3 weeks ago
googleplus.php
6 months ago
gravatar.php
6 months ago
houzz.php
6 months ago
inline-pdfs.php
6 months ago
instagram.php
6 months ago
kickstarter.php
6 months ago
mailchimp.php
5 months ago
medium.php
6 months ago
mixcloud.php
6 months ago
others.php
6 months ago
pinterest.php
6 months ago
presentations.php
6 months ago
quiz.php
6 months ago
recipe.php
6 months ago
scribd.php
6 months ago
shortcode-utils.php
6 months ago
sitemap.php
6 months ago
slideshare.php
6 months ago
slideshow.php
4 weeks ago
smartframe.php
6 months ago
soundcloud.php
6 months ago
spotify.php
6 months ago
ted.php
6 months ago
tweet.php
6 months ago
twitchtv.php
6 months ago
twitter-timeline.php
6 months ago
twitter.php
6 months ago
unavailable.php
6 months ago
untappd-menu.php
6 months ago
upcoming-events.php
6 months ago
ustream.php
6 months ago
videopress.php
6 months ago
vimeo.php
1 week ago
vine.php
6 months ago
vr.php
1 week ago
wufoo.php
6 months ago
youtube.php
3 months ago
medium.php
128 lines
| 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 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | // Faux-oembed support for Medium permalinks. |
| 19 | wp_embed_register_handler( 'medium', '#^https?://medium.com/([a-zA-z0-9-_@]+)#', 'jetpack_embed_medium_oembed' ); |
| 20 | |
| 21 | /** |
| 22 | * Callback to modify output of embedded Medium posts. |
| 23 | * |
| 24 | * @param array $matches Regex partial matches against the URL passed. |
| 25 | * @param array $attr Attributes received in embed response. |
| 26 | * @param array $url Requested URL to be embedded. |
| 27 | */ |
| 28 | function jetpack_embed_medium_oembed( $matches, $attr, $url ) { |
| 29 | $attr = jetpack_embed_medium_args( $attr ); |
| 30 | $attr['url'] = $url; |
| 31 | |
| 32 | return jetpack_embed_medium_embed_html( $attr ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Return custom markup to display a Medium profile, collection, or story. |
| 37 | * |
| 38 | * @param array $args Attributes received in embed response. |
| 39 | */ |
| 40 | function jetpack_embed_medium_embed_html( $args ) { |
| 41 | $args = jetpack_embed_medium_args( $args ); |
| 42 | |
| 43 | if ( empty( $args['url'] ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $args['type'] = jetpack_embed_medium_get_embed_type( $args['url'] ); |
| 48 | |
| 49 | if ( 'collection' === $args['type'] ) { |
| 50 | return sprintf( |
| 51 | '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>', |
| 52 | esc_url( $args['url'] ), |
| 53 | esc_html__( 'View this collection on Medium.com', 'jetpack' ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | wp_enqueue_script( |
| 58 | 'medium-embed', |
| 59 | 'https://static.medium.com/embed.js', |
| 60 | array(), |
| 61 | JETPACK__VERSION, |
| 62 | true |
| 63 | ); |
| 64 | |
| 65 | return sprintf( |
| 66 | '<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>', |
| 67 | esc_attr( $args['type'] ), |
| 68 | esc_url( $args['url'] ), |
| 69 | esc_attr( $args['width'] ), |
| 70 | esc_attr( $args['border'] ), |
| 71 | esc_attr( $args['collapsed'] ), |
| 72 | esc_html__( 'View at Medium.com', 'jetpack' ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Shortcode support that allows passing in URL |
| 78 | * |
| 79 | * @param array $atts Shortcode attributes. |
| 80 | */ |
| 81 | function jetpack_embed_medium_shortcode( $atts ) { |
| 82 | $atts = jetpack_embed_medium_args( $atts ); |
| 83 | |
| 84 | if ( ! empty( $atts['url'] ) ) { |
| 85 | global $wp_embed; |
| 86 | return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 87 | } elseif ( current_user_can( 'edit_posts' ) ) { |
| 88 | return esc_html__( 'You did not provide a valid Medium URL.', 'jetpack' ); |
| 89 | } else { |
| 90 | return '<!-- Missing Medium URL -->'; |
| 91 | } |
| 92 | } |
| 93 | add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' ); |
| 94 | |
| 95 | /** |
| 96 | * Get embed type (profile, collection, or story) based on Medium URL. |
| 97 | * |
| 98 | * @param string $url Medium URL. |
| 99 | */ |
| 100 | function jetpack_embed_medium_get_embed_type( $url ) { |
| 101 | $url_path = wp_parse_url( $url, PHP_URL_PATH ); |
| 102 | if ( preg_match( '/^\/@[\.\w]+$/', $url_path ) ) { |
| 103 | return 'profile'; |
| 104 | } elseif ( preg_match( '/^\/(?:s)\/(.+)$/', $url_path ) ) { |
| 105 | return 'collection'; |
| 106 | } |
| 107 | |
| 108 | return 'story'; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Process Medium shortcode attributes. |
| 113 | * |
| 114 | * @param array $atts Shortcode attributes. |
| 115 | */ |
| 116 | function jetpack_embed_medium_args( $atts ) { |
| 117 | return shortcode_atts( |
| 118 | array( |
| 119 | 'url' => '', |
| 120 | 'width' => '400', |
| 121 | 'border' => true, |
| 122 | 'collapsed' => false, |
| 123 | ), |
| 124 | $atts, |
| 125 | 'medium' |
| 126 | ); |
| 127 | } |
| 128 |