|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Getty shortcode |
|
4
|
|
|
* |
|
5
|
|
|
* [getty src="82278805" width="$width" height="$height"] |
|
6
|
|
|
* <div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:462px;"><div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/82278805" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div><div style="overflow:hidden;position:relative;height:0;padding:80.086580% 0 0 0;width:100%;"><iframe src="//embed.gettyimages.com/embed/82278805?et=jGiu6FXXSpJDGf1SnwLV2g&sig=TFVNFtqghwNw5iJQ1MFWnI8f4Y40_sfogfZLhai6SfA=" width="462" height="370" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;"></iframe></div><p style="margin:0;"></p></div> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
10
|
|
|
add_action( 'init', 'jetpack_getty_enable_embeds' ); |
|
11
|
|
|
} else { |
|
12
|
|
|
jetpack_getty_enable_embeds(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Register Getty as oembed provider. Add filter to reverse iframes to shortcode. Register [getty] shortcode. |
|
17
|
|
|
* |
|
18
|
|
|
* @since 4.5.0 |
|
19
|
|
|
* @since 5.8.0 removed string parameter. |
|
20
|
|
|
*/ |
|
21
|
|
|
function jetpack_getty_enable_embeds() { |
|
22
|
|
|
|
|
23
|
|
|
// Support their oEmbed Endpoint |
|
24
|
|
|
wp_oembed_add_provider( '#https?://www\.gettyimages\.com/detail/.*#i', "https://embed.gettyimages.com/oembed/", true ); |
|
25
|
|
|
wp_oembed_add_provider( '#https?://(www\.)?gty\.im/.*#i', "https://embed.gettyimages.com/oembed/", true ); |
|
26
|
|
|
|
|
27
|
|
|
// Allow iframes to be filtered to short code (so direct copy+paste can be done) |
|
28
|
|
|
add_filter( 'pre_kses', 'wpcom_shortcodereverse_getty' ); |
|
29
|
|
|
|
|
30
|
|
|
// Actually display the Getty Embed |
|
31
|
|
|
add_shortcode( 'getty', 'jetpack_getty_shortcode' ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Filters the oEmbed provider URL for Getty URLs to include site URL host as |
|
36
|
|
|
* caller if available, falling back to "wordpress.com". Must be applied at |
|
37
|
|
|
* time of embed in case that `init` is too early (WP.com REST API). |
|
38
|
|
|
* |
|
39
|
|
|
* @module shortcodes |
|
40
|
|
|
* |
|
41
|
|
|
* @since 5.8.0 |
|
42
|
|
|
* |
|
43
|
|
|
* @see WP_oEmbed::fetch |
|
44
|
|
|
* |
|
45
|
|
|
* @return string oEmbed provider URL |
|
46
|
|
|
*/ |
|
47
|
|
|
add_filter( 'oembed_fetch_url', 'getty_add_oembed_endpoint_caller' ); |
|
48
|
|
|
|
|
49
|
|
|
function getty_add_oembed_endpoint_caller( $provider ) { |
|
50
|
|
|
// By time filter is called, original provider URL has had url, maxwidth, |
|
51
|
|
|
// maxheight query parameters added. |
|
52
|
|
|
if ( 0 !== strpos( $provider, 'https://embed.gettyimages.com/oembed/' ) ) { |
|
53
|
|
|
return $provider; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Set the caller argument to pass to Getty's oembed provider. |
|
57
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
58
|
|
|
|
|
59
|
|
|
// Only include caller for non-private sites |
|
60
|
|
|
if ( ! function_exists( 'is_private_blog' ) || ! is_private_blog() ) { |
|
61
|
|
|
$host = parse_url( get_bloginfo( 'url' ), PHP_URL_HOST ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Fall back to WordPress.com |
|
65
|
|
|
if ( empty( $host ) ) { |
|
66
|
|
|
$host = 'wordpress.com'; |
|
67
|
|
|
} |
|
68
|
|
|
} else { |
|
69
|
|
|
$host = parse_url( get_home_url(), PHP_URL_HOST ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return add_query_arg( 'caller', $host, $provider ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Compose shortcode based on Getty iframes. |
|
77
|
|
|
* |
|
78
|
|
|
* @since 4.5.0 |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $content |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
function wpcom_shortcodereverse_getty( $content ) { |
|
85
|
|
|
if ( ! is_string( $content ) || false === stripos( $content, '.gettyimages.com/' ) ) { |
|
86
|
|
|
return $content; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)([a-z0-9_-]+(,[a-z0-9_-]+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i'; |
|
90
|
|
|
$regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// Markup pattern for 2017 embed syntax with significant differences from |
|
94
|
|
|
// the prior pattern: |
|
95
|
|
|
$regexp_2017 = '!<a.+?class=\'gie-(single|slideshow)\'.+?gie\.widgets\.load\({([^}]+)}\).+?embed-cdn\.gettyimages\.com/widgets\.js.+?</script>!'; |
|
96
|
|
|
$regexp_2017_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp_2017, ENT_NOQUOTES ) ); |
|
97
|
|
|
|
|
98
|
|
|
foreach ( array( 'regexp_2017', 'regexp_2017_ent', 'regexp', 'regexp_ent' ) as $reg ) { |
|
99
|
|
|
if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
foreach ( $matches as $match ) { |
|
104
|
|
|
if ( 'regexp_2017' === $reg || 'regexp_2017_ent' === $reg ) { |
|
105
|
|
|
// Extract individual keys from the matched JavaScript object |
|
106
|
|
|
$params = $match[2]; |
|
107
|
|
|
if ( ! preg_match_all( '!(?P<key>\w+)\s*:\s*([\'"](?P<value>[^\'"]*?)(px)?[\'"])!', $params, $key_matches, PREG_SET_ORDER ) ) { |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ( $key_matches as $key_match ) { |
|
112
|
|
|
switch ( $key_match['key'] ) { |
|
113
|
|
|
case 'items': $ids = $key_match['value']; break; |
|
|
|
|
|
|
114
|
|
|
case 'w': $width = (int) $key_match['value']; break; |
|
|
|
|
|
|
115
|
|
|
case 'h': $height = (int) $key_match['value']; break; |
|
|
|
|
|
|
116
|
|
|
case 'tld': $tld = $key_match['value']; break; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
|
|
$params = $match[5]; |
|
121
|
|
|
if ( 'regexp_ent' === $reg ) { |
|
122
|
|
|
$params = html_entity_decode( $params ); |
|
123
|
|
|
} |
|
124
|
|
|
$params = wp_kses_hair( $params, array( 'http' ) ); |
|
125
|
|
|
|
|
126
|
|
|
$ids = esc_html( $match[3] ); |
|
127
|
|
|
$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
|
128
|
|
|
$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ( empty( $ids ) ) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$shortcode = '[getty src="' . esc_attr( $ids ) . '"'; |
|
136
|
|
|
if ( ! empty( $width ) ) { |
|
137
|
|
|
$shortcode .= ' width="' . esc_attr( $width ) . '"'; |
|
138
|
|
|
} |
|
139
|
|
|
if ( ! empty( $height ) ) { |
|
140
|
|
|
$shortcode .= ' height="' . esc_attr( $height ) . '"'; |
|
141
|
|
|
} |
|
142
|
|
|
// While it does not appear to have any practical impact, Getty has |
|
143
|
|
|
// requested that we include TLD in the embed request |
|
144
|
|
|
if ( ! empty( $tld ) ) { |
|
145
|
|
|
$shortcode .= ' tld="' . esc_attr( $tld ). '"'; |
|
146
|
|
|
} |
|
147
|
|
|
$shortcode .= ']'; |
|
148
|
|
|
|
|
149
|
|
|
$content = str_replace( $match[0], $shortcode, $content ); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// strip out enclosing div and any other markup |
|
154
|
|
|
$regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is'; |
|
155
|
|
|
$regexp_ent = str_replace( array( '&#0*58;', '[^>]' ), array( '&#0*58;|�*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
|
156
|
|
|
|
|
157
|
|
|
foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { |
|
158
|
|
|
if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) { |
|
159
|
|
|
continue; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ( $matches as $match ) { |
|
163
|
|
|
$content = str_replace( $match[0], $match[1], $content ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** This action is documented in modules/widgets/social-media-icons.php */ |
|
168
|
|
|
do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' ); |
|
169
|
|
|
|
|
170
|
|
|
return $content; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Parse shortcode arguments and render its output. |
|
175
|
|
|
* |
|
176
|
|
|
* @since 4.5.0 |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $atts Shortcode parameters. |
|
179
|
|
|
* @param string $content Content enclosed by shortcode tags. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
function jetpack_getty_shortcode( $atts, $content = '' ) { |
|
184
|
|
|
|
|
185
|
|
View Code Duplication |
if ( ! empty( $content ) ) { |
|
186
|
|
|
$src = $content; |
|
187
|
|
|
} elseif ( ! empty( $atts['src'] ) ) { |
|
188
|
|
|
$src = $atts['src']; |
|
189
|
|
|
} elseif ( ! empty( $atts[0] ) ) { |
|
190
|
|
|
$src = $atts[0]; |
|
191
|
|
|
} else { |
|
192
|
|
|
return '<!-- Missing Getty Source ID -->'; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$src = preg_replace( '/^([\da-z-]+(,[\da-z-]+)*).*$/', '$1', $src ); |
|
196
|
|
|
|
|
197
|
|
|
$params = array( |
|
198
|
|
|
'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null, |
|
199
|
|
|
'height' => isset( $atts['height'] ) ? (int) $atts['height'] : null |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
if ( ! empty( $atts['tld'] ) ) { |
|
203
|
|
|
$params['tld'] = $atts['tld']; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return wp_oembed_get( 'https://gty.im/' . $src, array_filter( $params ) ); |
|
207
|
|
|
} |
|
208
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.