Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

medium.php ➔ jetpack_embed_medium_oembed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_PATH.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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