1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php /** * Class AMP_Imgur_Embed_Handler * * @package AMP * @since 1.0 */
/** * Class AMP_Imgur_Embed_Handler * * @internal */ class AMP_Imgur_Embed_Handler extends AMP_Base_Embed_Handler { /** * Regex matched to produce output amp-imgur. * * @var string */ const URL_PATTERN = '#https?://(www\.)?imgur\.com/.*#i';
/** * Register embed. */ public function register_embed() { add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10, 3 ); }
/** * Unregister embed. */ public function unregister_embed() { remove_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10 ); }
/** * Filter oEmbed HTML for Imgur to prepare it for AMP. * * @param mixed $return The oEmbed HTML. * @param string $url The attempted embed URL. * @param array $attr Attributes. * @return string Embed. */ public function filter_embed_oembed_html( $return, $url, $attr ) { $parsed_url = wp_parse_url( $url );
if ( ! isset( $parsed_url['host'], $parsed_url['path'] ) ) { return $return; }
if ( false !== strpos( $parsed_url['host'], 'imgur.com' ) ) { if ( empty( $attr['height'] ) ) { return $return; }
$attributes = wp_array_slice_assoc( $attr, [ 'width', 'height' ] );
if ( empty( $attr['width'] ) ) { $attributes['layout'] = 'fixed-height'; $attributes['width'] = 'auto'; }
$attributes['data-imgur-id'] = $this->get_imgur_id_from_url( $parsed_url ); if ( false === $attributes['data-imgur-id'] ) { return $return; }
$return = AMP_HTML_Utils::build_tag( 'amp-imgur', $attributes ); } return $return; }
/** * Get Imgur ID from URL. * * @param array $parsed_url Parsed URL. * @return bool|string ID / false. */ protected function get_imgur_id_from_url( $parsed_url ) {
if ( ! preg_match( '#^(?:/(a|gallery))?/([A-Za-z0-9]+)#', $parsed_url['path'], $matches ) ) { return false; }
return ! empty( $matches[1] ) ? "a/{$matches[2]}" : $matches[2]; } }
|