PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | modules/shortcodes/medium.php +127 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,127 @@
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 +}