PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 15.0
Jetpack – WP Security, Backup, Speed, & Growth v15.0
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 / getty.php
getty.php
230 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @package automattic/jetpack
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit( 0 );
13 }
14
15 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
16 add_action( 'init', 'jetpack_getty_enable_embeds' );
17 } else {
18 jetpack_getty_enable_embeds();
19 }
20
21 /**
22 * Register Getty as oembed provider. Add filter to reverse iframes to shortcode. Register [getty] shortcode.
23 *
24 * @since 4.5.0
25 * @since 5.8.0 removed string parameter.
26 */
27 function jetpack_getty_enable_embeds() {
28
29 // Support their oEmbed Endpoint.
30 wp_oembed_add_provider( '#https?://www\.gettyimages\.com/detail/.*#i', 'https://embed.gettyimages.com/oembed/', true );
31 wp_oembed_add_provider( '#https?://(www\.)?gty\.im/.*#i', 'https://embed.gettyimages.com/oembed/', true );
32
33 if ( jetpack_shortcodes_should_hook_pre_kses() ) {
34 // Allow iframes to be filtered to short code (so direct copy+paste can be done).
35 add_filter( 'pre_kses', 'wpcom_shortcodereverse_getty' );
36 }
37
38 // Actually display the Getty Embed.
39 add_shortcode( 'getty', 'jetpack_getty_shortcode' );
40 }
41
42 /**
43 * Filters the oEmbed provider URL for Getty URLs to include site URL host as
44 * caller if available, falling back to "wordpress.com". Must be applied at
45 * time of embed in case that `init` is too early (WP.com REST API).
46 *
47 * @module shortcodes
48 *
49 * @since 5.8.0
50 *
51 * @see WP_oEmbed::fetch
52 *
53 * @return string oEmbed provider URL
54 */
55 add_filter( 'oembed_fetch_url', 'getty_add_oembed_endpoint_caller' );
56
57 /**
58 * Filter the embeds to add a caller parameter.
59 *
60 * @param string $provider URL of the oEmbed provider.
61 */
62 function getty_add_oembed_endpoint_caller( $provider ) {
63 // By time filter is called, original provider URL has had url, maxwidth,
64 // maxheight query parameters added.
65 if ( ! str_starts_with( $provider, 'https://embed.gettyimages.com/oembed/' ) ) {
66 return $provider;
67 }
68
69 // Set the caller argument to pass to Getty's oembed provider.
70 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
71
72 // Only include caller for non-private sites.
73 if ( ! function_exists( 'is_private_blog' ) || ! is_private_blog() ) {
74 $host = wp_parse_url( get_bloginfo( 'url' ), PHP_URL_HOST );
75 }
76
77 // Fall back to WordPress.com.
78 if ( empty( $host ) ) {
79 $host = 'wordpress.com';
80 }
81 } else {
82 $host = wp_parse_url( get_home_url(), PHP_URL_HOST );
83 }
84
85 return add_query_arg( 'caller', $host, $provider );
86 }
87
88 /**
89 * Compose shortcode based on Getty iframes.
90 *
91 * @since 4.5.0
92 *
93 * @param string $content Post content.
94 *
95 * @return mixed
96 */
97 function wpcom_shortcodereverse_getty( $content ) {
98 if ( ! is_string( $content ) || false === stripos( $content, '.gettyimages.com/' ) ) {
99 return $content;
100 }
101
102 $regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)([a-z0-9_-]+(,[a-z0-9_-]+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
103 $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
104
105 // Markup pattern for 2017 embed syntax with significant differences from the prior pattern.
106 $regexp_2017 = '!<a.+?class=\'gie-(single|slideshow)\'.+?gie\.widgets\.load\({([^}]+)}\).+?embed-cdn\.gettyimages\.com/widgets\.js.+?</script>!';
107 $regexp_2017_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp_2017, ENT_NOQUOTES ) );
108
109 foreach ( compact( 'regexp_2017', 'regexp_2017_ent', 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
110 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
111 continue;
112 }
113
114 foreach ( $matches as $match ) {
115 if ( 'regexp_2017' === $reg || 'regexp_2017_ent' === $reg ) {
116 // Extract individual keys from the matched JavaScript object.
117 $params = $match[2];
118 if ( ! preg_match_all( '!(?P<key>\w+)\s*:\s*([\'"](?P<value>[^\'"]*?)(px)?[\'"])!', $params, $key_matches, PREG_SET_ORDER ) ) {
119 continue;
120 }
121
122 foreach ( $key_matches as $key_match ) {
123 switch ( $key_match['key'] ) {
124 case 'items':
125 $ids = $key_match['value'];
126 break;
127 case 'w':
128 $width = (int) $key_match['value'];
129 break;
130 case 'h':
131 $height = (int) $key_match['value'];
132 break;
133 case 'tld':
134 $tld = $key_match['value'];
135 break;
136 }
137 }
138 } else {
139 $params = $match[5];
140 if ( 'regexp_ent' === $reg ) {
141 $params = html_entity_decode( $params, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
142 }
143 $params = wp_kses_hair( $params, array( 'http' ) );
144
145 $ids = esc_html( $match[3] );
146 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
147 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
148 }
149
150 if ( empty( $ids ) ) {
151 continue;
152 }
153
154 $shortcode = '[getty src="' . esc_attr( $ids ) . '"';
155 if ( ! empty( $width ) ) {
156 $shortcode .= ' width="' . esc_attr( $width ) . '"';
157 }
158 if ( ! empty( $height ) ) {
159 $shortcode .= ' height="' . esc_attr( $height ) . '"';
160 }
161
162 /*
163 * While it does not appear to have any practical impact, Getty has
164 * requested that we include TLD in the embed request
165 */
166 if ( ! empty( $tld ) ) {
167 $shortcode .= ' tld="' . esc_attr( $tld ) . '"';
168 }
169 $shortcode .= ']';
170
171 $content = str_replace( $match[0], $shortcode, $content );
172 }
173 }
174
175 // strip out enclosing div and any other markup.
176 $regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is';
177 $regexp_ent = str_replace( array( '&amp;#0*58;', '[^&gt;]' ), array( '&amp;#0*58;|&#0*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) );
178
179 foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
180 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
181 continue;
182 }
183
184 foreach ( $matches as $match ) {
185 $content = str_replace( $match[0], $match[1], $content );
186 }
187 }
188
189 /** This action is documented in modules/widgets/social-media-icons.php */
190 do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' );
191
192 return $content;
193 }
194
195 /**
196 * Parse shortcode arguments and render its output.
197 *
198 * @since 4.5.0
199 *
200 * @param array $atts Shortcode parameters.
201 * @param string $content Content enclosed by shortcode tags.
202 *
203 * @return string
204 */
205 function jetpack_getty_shortcode( $atts, $content = '' ) {
206
207 if ( ! empty( $content ) ) {
208 $src = $content;
209 } elseif ( ! empty( $atts['src'] ) ) {
210 $src = $atts['src'];
211 } elseif ( ! empty( $atts[0] ) ) {
212 $src = $atts[0];
213 } else {
214 return '<!-- Missing Getty Source ID -->';
215 }
216
217 $src = preg_replace( '/^([\da-z-]+(,[\da-z-]+)*).*$/', '$1', $src );
218
219 $params = array(
220 'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null,
221 'height' => isset( $atts['height'] ) ? (int) $atts['height'] : null,
222 );
223
224 if ( ! empty( $atts['tld'] ) ) {
225 $params['tld'] = $atts['tld'];
226 }
227
228 return wp_oembed_get( 'https://gty.im/' . $src, array_filter( $params ) );
229 }
230