| @@ -1,39 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 3 | +// Embed support for Medium https://medium.com/p/3eaed64aed8a | |
| 4 | + | |
| 2 | 5 | /** |
| 3 | - * Embed support for Medium | |
| 6 | + * Faux-oembed support for Medium permalinks | |
| 4 | 7 | * |
| 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 | |
| 8 | + * e.g. | |
| 9 | + * https://medium.com/help-center | |
| 10 | + * https://medium.com/@richroll | |
| 12 | 11 | */ |
| 13 | - | |
| 14 | -// Faux-oembed support for Medium permalinks. | |
| 15 | 12 | wp_embed_register_handler( 'medium', '#^https?://medium.com/([a-zA-z0-9-_@]+)#', 'jetpack_embed_medium_oembed' ); |
| 16 | 13 | |
| 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 | 14 | function jetpack_embed_medium_oembed( $matches, $attr, $url ) { |
| 25 | - $attr = jetpack_embed_medium_args( $attr ); | |
| 15 | + $attr = jetpack_embed_medium_args( $attr ); | |
| 26 | 16 | $attr['url'] = $url; |
| 27 | 17 | |
| 28 | 18 | return jetpack_embed_medium_embed_html( $attr ); |
| 29 | 19 | } |
| 30 | 20 | |
| 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 | 21 | function jetpack_embed_medium_embed_html( $args ) { |
| 37 | 22 | $args = jetpack_embed_medium_args( $args ); |
| 38 | 23 | |
| 39 | 24 | if ( empty( $args['url'] ) ) { |
| @@ -41,40 +26,18 @@ | ||
| 41 | 26 | } |
| 42 | 27 | |
| 43 | 28 | $args['type'] = jetpack_embed_medium_get_embed_type( $args['url'] ); |
| 44 | 29 | |
| 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 | - ); | |
| 30 | + return sprintf( '<script async src="https://static.medium.com/embed.js"></script><a class="m-%1$s" href="%2$s" target="_blank" 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'] ) ); | |
| 70 | 31 | } |
| 71 | 32 | |
| 72 | 33 | /** |
| 73 | 34 | * Shortcode support that allows passing in URL |
| 74 | 35 | * |
| 75 | - * @param array $atts Shortcode attributes. | |
| 36 | + * [medium url="https://medium.com/help-center" width="100%" border="false" collapsed="true"] | |
| 76 | 37 | */ |
| 38 | +add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' ); | |
| 39 | + | |
| 77 | 40 | function jetpack_embed_medium_shortcode( $atts ) { |
| 78 | 41 | $atts = jetpack_embed_medium_args( $atts ); |
| 79 | 42 | |
| 80 | 43 | if ( ! empty( $atts['url'] ) ) { |
| @@ -79,26 +42,16 @@ | ||
| 79 | 42 | |
| 80 | 43 | if ( ! empty( $atts['url'] ) ) { |
| 81 | 44 | global $wp_embed; |
| 82 | 45 | return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 83 | - } elseif ( current_user_can( 'edit_posts' ) ) { | |
| 84 | - return esc_html__( 'You did not provide a valid Medium URL.', 'jetpack' ); | |
| 85 | - } else { | |
| 86 | - return '<!-- Missing Medium URL -->'; | |
| 87 | 46 | } |
| 88 | 47 | } |
| 89 | -add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' ); | |
| 90 | 48 | |
| 91 | -/** | |
| 92 | - * Get embed type (profile, collection, or story) based on Medium URL. | |
| 93 | - * | |
| 94 | - * @param string $url Medium URL. | |
| 95 | - */ | |
| 96 | 49 | function jetpack_embed_medium_get_embed_type( $url ) { |
| 97 | - $url_path = wp_parse_url( $url, PHP_URL_PATH ); | |
| 50 | + $url_path = parse_url( $url, PHP_URL_PATH ); | |
| 98 | 51 | if ( preg_match( '/^\/@[\.\w]+$/', $url_path ) ) { |
| 99 | 52 | return 'profile'; |
| 100 | - } elseif ( preg_match( '/^\/(?:s)\/(.+)$/', $url_path ) ) { | |
| 53 | + } else if ( preg_match( '/^\/[\da-zA-Z-]+$/', $url_path ) ) { | |
| 101 | 54 | return 'collection'; |
| 102 | 55 | } |
| 103 | 56 | |
| 104 | 57 | return 'story'; |
| @@ -103,21 +56,12 @@ | ||
| 103 | 56 | |
| 104 | 57 | return 'story'; |
| 105 | 58 | } |
| 106 | 59 | |
| 107 | -/** | |
| 108 | - * Process Medium shortcode attributes. | |
| 109 | - * | |
| 110 | - * @param array $atts Shortcode attributes. | |
| 111 | - */ | |
| 112 | 60 | function jetpack_embed_medium_args( $atts ) { |
| 113 | - return shortcode_atts( | |
| 114 | - array( | |
| 115 | - 'url' => '', | |
| 116 | - 'width' => '400', | |
| 117 | - 'border' => true, | |
| 118 | - 'collapsed' => false, | |
| 119 | - ), | |
| 120 | - $atts, | |
| 121 | - 'medium' | |
| 122 | - ); | |
| 61 | + return shortcode_atts( array( | |
| 62 | + 'url' => '', | |
| 63 | + 'width' => '400', | |
| 64 | + 'border' => true, | |
| 65 | + 'collapsed' => false, | |
| 66 | + ), $atts, 'medium' ); | |
| 123 | 67 | } |