Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.27% |
48 / 55 |
|
60.00% |
3 / 5 |
CRAP | n/a |
0 / 0 |
|
| jetpack_embed_medium_oembed | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| jetpack_embed_medium_embed_html | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
3 | |||
| jetpack_embed_medium_shortcode | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| jetpack_embed_medium_get_embed_type | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| jetpack_embed_medium_args | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | } |