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