PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.4
Jetpack – WP Security, Backup, Speed, & Growth v12.4
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 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / medium.php

medium.php in Jetpack – WP Security, Backup, Speed, & Growth 12.4, at modules/shortcodes/medium.php

124 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // 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 } 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 }
88 }
89 add_shortcode( 'medium', 'jetpack_embed_medium_shortcode' );
90
91 /**
92 * Get embed type (profile, collection, or story) based on Medium URL.
93 *
94 * @param string $url Medium URL.
95 */
96 function jetpack_embed_medium_get_embed_type( $url ) {
97 $url_path = wp_parse_url( $url, PHP_URL_PATH );
98 if ( preg_match( '/^\/@[\.\w]+$/', $url_path ) ) {
99 return 'profile';
100 } elseif ( preg_match( '/^\/(?:s)\/(.+)$/', $url_path ) ) {
101 return 'collection';
102 }
103
104 return 'story';
105 }
106
107 /**
108 * Process Medium shortcode attributes.
109 *
110 * @param array $atts Shortcode attributes.
111 */
112 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 );
123 }
124