PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.6
Jetpack – WP Security, Backup, Speed, & Growth v8.6
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 / instagram.php

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

289 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Instagram Embeds.
4 *
5 * Full links: https://www.instagram.com/p/BnMOk_FFsxg/
6 * https://www.instagram.com/tv/BkQjCfsBIzi/
7 * [instagram url=https://www.instagram.com/p/BnMOk_FFsxg/]
8 * [instagram url=https://www.instagram.com/p/BZoonmAHvHf/ width=320]
9 * Embeds can be converted to a shortcode when the author does not have unfiltered_html caps:
10 * <blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="2" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"><div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding-bottom:55%; padding-top:45%; text-align:center; width:100%;"><div style="position:relative;"><div style=" -webkit-animation:dkaXkpbBxI 1s ease-out infinite; animation:dkaXkpbBxI 1s ease-out infinite; background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-44px; width:44px;"></div><span style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:12px; font-style:normal; font-weight:bold; position:relative; top:15px;">Loading</span></div></div><p style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin:8px 0 0 0; padding:0 4px; word-wrap:break-word;"> Balloons</p><p style=" line-height:32px; margin-bottom:0; margin-top:8px; padding:0; text-align:center;"> <a href="https://instagram.com/p/r9vfPrmjeB/" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; text-decoration:none;" target="_top"> View on Instagram</a></p></div><style>@-webkit-keyframes"dkaXkpbBxI"{ 0%{opacity:0.5;} 50%{opacity:1;} 100%{opacity:0.5;} } @keyframes"dkaXkpbBxI"{ 0%{opacity:0.5;} 50%{opacity:1;} 100%{opacity:0.5;} }</style></blockquote>
11 * <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
12 *
13 * @package Jetpack
14 */
15
16 use Automattic\Jetpack\Assets;
17
18 /**
19 * Embed Reversal for Instagram
20 *
21 * Hooked to pre_kses, converts an embed code from Instagram.com to an oEmbeddable URL.
22 *
23 * @param string $content Post content.
24 *
25 * @return string The filtered or the original content.
26 **/
27 function jetpack_instagram_embed_reversal( $content ) {
28 if ( ! is_string( $content ) || false === stripos( $content, 'instagram.com' ) ) {
29 return $content;
30 }
31
32 /*
33 * Sample embed code:
34 * <blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="2" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"><div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding-bottom:55%; padding-top:45%; text-align:center; width:100%;"><div style="position:relative;"><div style=" -webkit-animation:dkaXkpbBxI 1s ease-out infinite; animation:dkaXkpbBxI 1s ease-out infinite; background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-44px; width:44px;"></div><span style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:12px; font-style:normal; font-weight:bold; position:relative; top:15px;">Loading</span></div></div><p style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin:8px 0 0 0; padding:0 4px; word-wrap:break-word;"> Balloons</p><p style=" line-height:32px; margin-bottom:0; margin-top:8px; padding:0; text-align:center;"> <a href="https://instagram.com/p/r9vfPrmjeB/" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; text-decoration:none;" target="_top"> View on Instagram</a></p></div><style>@-webkit-keyframes"dkaXkpbBxI"{ 0%{opacity:0.5;} 50%{opacity:1;} 100%{opacity:0.5;} } @keyframes"dkaXkpbBxI"{ 0%{opacity:0.5;} 50%{opacity:1;} 100%{opacity:0.5;} }</style></blockquote>
35 * <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
36 */
37
38 $regexes = array();
39
40 // new style js.
41 $regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>].+?>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix';
42
43 // Let's play nice with the visual editor too.
44 $regexes[] = '#&lt;blockquote(?:[^&]|&(?!gt;))+?class="instagram-media"(?:[^&]|&(?!gt;)).+?&gt;(.+?)&lt;/blockquote&gt;&lt;script(?:[^&]|&(?!gt;))+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"(?:[^&]|&(?!gt;))*+&gt;&lt;/script&gt;#ix';
45
46 // old style iframe.
47 $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?instagram\.com/p/([^"\'/]++)[^"\']*?)"[^>]*+>\s*?</iframe>#i';
48
49 // Let's play nice with the visual editor too.
50 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//(?:www\.)instagram\.com/p/([^"\'/]++)[^"\']*?)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
51
52 foreach ( $regexes as $regex ) {
53 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
54 continue;
55 }
56
57 foreach ( $matches as $match ) {
58 if ( ! preg_match( '#(https?:)?//(?:www\.)?instagr(\.am|am\.com)/p/([^/]*)#i', $match[1], $url_matches ) ) {
59 continue;
60 }
61
62 // Since we support Instagram via oEmbed, we simply leave a link on a line by itself.
63 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
64 $url = esc_url( $url_matches[0] );
65
66 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
67 /** This action is documented in modules/shortcodes/youtube.php */
68 do_action( 'jetpack_embed_to_shortcode', 'instagram', $url );
69 }
70 }
71
72 return $content;
73 }
74
75 add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' );
76
77 /**
78 * Instagram's custom Embed provider.
79 * We first remove 2 different embed providers, both registered by Core.
80 * - The first is the original provider,that only supports images.
81 * - The second is tne new provider that replaced the first one in Core when Core added support for videos. https://core.trac.wordpress.org/changeset/44486
82 *
83 * Once the core embed provider is removed (one or the other, depending on your version of Core), we declare our own.
84 */
85 wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i' );
86 wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' );
87 wp_embed_register_handler(
88 'jetpack_instagram',
89 '#http(s?)://(www\.)?instagr(\.am|am\.com)/(p|tv)/([^\/]*)#i',
90 'jetpack_instagram_handler'
91 );
92
93 /**
94 * Handle Instagram embeds (build embed from regex).
95 *
96 * @param array $matches Array of matches from the regex.
97 * @param array $atts The original unmodified attributes.
98 * @param string $url The original URL that was matched by the regex.
99 */
100 function jetpack_instagram_handler( $matches, $atts, $url ) {
101 global $content_width;
102
103 // keep a copy of the passed-in URL since it's modified below.
104 $passed_url = $url;
105
106 $max_width = 698;
107 $min_width = 320;
108
109 if ( is_feed() ) {
110 // Instagram offers direct links to images, but not to videos.
111 if ( 'p' === $matches[1] ) {
112 $media_url = sprintf( 'https://instagr.am/p/%1$s/media/?size=l', $matches[2] );
113 return sprintf(
114 '<a href="%1$s" title="%2$s" target="_blank"><img src="%3$s" alt="%4$s" /></a>',
115 esc_url( $url ),
116 esc_attr__( 'View on Instagram', 'jetpack' ),
117 esc_url( $media_url ),
118 esc_html__( 'Instagram Photo', 'jetpack' )
119 );
120 } elseif ( 'tv' === $matches[1] ) {
121 return sprintf(
122 '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>',
123 esc_url( $url ),
124 esc_attr__( 'View on Instagram', 'jetpack' ),
125 esc_html__( 'Instagram Video', 'jetpack' )
126 );
127 }
128 }
129
130 $atts = shortcode_atts(
131 array(
132 'width' => isset( $content_width ) ? $content_width : $max_width,
133 'hidecaption' => false,
134 ),
135 $atts
136 );
137
138 $atts['width'] = absint( $atts['width'] );
139 if ( $atts['width'] > $max_width ) {
140 $atts['width'] = $max_width;
141 } elseif ( $atts['width'] < $min_width ) {
142 $atts['width'] = $min_width;
143 }
144
145 // remove the modal param from the URL.
146 $url = remove_query_arg( 'modal', $url );
147
148 // force .com instead of .am for https support.
149 $url = str_replace( 'instagr.am', 'instagram.com', $url );
150
151 // The oembed endpoint expects HTTP, but HTTP requests 301 to HTTPS.
152 $instagram_http_url = str_replace( 'https://', 'http://', $url );
153
154 $url_args = array(
155 'url' => $instagram_http_url,
156 'maxwidth' => $atts['width'],
157 'omitscript' => 1,
158 );
159
160 if ( $atts['hidecaption'] ) {
161 $url_args['hidecaption'] = 'true';
162 }
163
164 $url = esc_url_raw( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) );
165
166 /**
167 * Filter Object Caching for response from Instagram.
168 *
169 * Allow enabling of object caching for the response sent by Instagram when querying for Instagram image HTML.
170 *
171 * @module shortcodes
172 *
173 * @since 3.3.0
174 *
175 * @param bool false Object caching is off by default.
176 * @param array $matches Array of Instagram URLs found in the post.
177 * @param array $atts Instagram Shortcode attributes.
178 * @param string $passed_url Instagram API URL.
179 */
180 $response_body_use_cache = apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url );
181 $response_body = false;
182 if ( $response_body_use_cache ) {
183 $cache_key = 'oembed_response_body_' . md5( $url );
184 $response_body = wp_cache_get( $cache_key, 'instagram_embeds' );
185 }
186
187 if ( ! $response_body ) {
188 // Not using cache (default case) or cache miss.
189 $instagram_response = wp_remote_get( $url, array( 'redirection' => 0 ) );
190 if (
191 is_wp_error( $instagram_response )
192 || 200 !== $instagram_response['response']['code']
193 || empty( $instagram_response['body'] ) ) {
194 return '<!-- instagram error: invalid instagram resource -->';
195 }
196
197 $response_body = json_decode( $instagram_response['body'] );
198 if ( $response_body_use_cache ) {
199 // if caching it is short-lived since this is a "Cache-Control: no-cache" resource.
200 wp_cache_set(
201 $cache_key,
202 $response_body,
203 'instagram_embeds',
204 HOUR_IN_SECONDS + wp_rand( 0, HOUR_IN_SECONDS )
205 );
206 }
207 }
208
209 if ( ! empty( $response_body->html ) ) {
210 wp_enqueue_script(
211 'jetpack-instagram-embed',
212 Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/instagram.min.js', 'modules/shortcodes/js/instagram.js' ),
213 array( 'jquery' ),
214 JETPACK__VERSION,
215 true
216 );
217 return $response_body->html;
218 }
219
220 return '<!-- instagram error: no embed found -->';
221 }
222
223 /**
224 * Handle an alternate Instagram URL format, where the username is also part of the URL.
225 * We do not actually need that username for the embed.
226 */
227 wp_embed_register_handler(
228 'jetpack_instagram_alternate_format',
229 '#https?://(?:www\.)?instagr(?:\.am|am\.com)/(?:[^/]*)/(p|tv)/([^\/]*)#i',
230 'jetpack_instagram_alternate_format_handler'
231 );
232
233 /**
234 * Handle alternate Instagram embeds (build embed from regex).
235 *
236 * @param array $matches Array of matches from the regex.
237 * @param array $atts The original unmodified attributes.
238 * @param string $url The original URL that was matched by the regex.
239 */
240 function jetpack_instagram_alternate_format_handler( $matches, $atts, $url ) {
241 // Replace URL saved by original Instagram URL (no username).
242 $matches[0] = esc_url_raw(
243 sprintf(
244 'https://www.instagram.com/%1$s/%2$s',
245 $matches[1],
246 $matches[2]
247 )
248 );
249
250 return jetpack_instagram_handler( $matches, $atts, $url );
251 }
252
253 /**
254 * Display the Instagram shortcode.
255 *
256 * @param array $atts Shortcode attributes.
257 */
258 function jetpack_shortcode_instagram( $atts ) {
259 global $wp_embed;
260
261 if ( empty( $atts['url'] ) ) {
262 return '';
263 }
264
265 if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
266 $url_pattern = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i';
267 preg_match( $url_pattern, $atts['url'], $matches );
268 if ( ! $matches ) {
269 return sprintf(
270 '<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>',
271 esc_url( $atts['url'] )
272 );
273 }
274
275 $shortcode_id = end( $matches );
276 $width = ! empty( $atts['width'] ) ? $atts['width'] : 600;
277 $height = ! empty( $atts['height'] ) ? $atts['height'] : 600;
278 return sprintf(
279 '<amp-instagram data-shortcode="%1$s" layout="responsive" width="%2$d" height="%3$d" data-captioned></amp-instagram>',
280 esc_attr( $shortcode_id ),
281 absint( $width ),
282 absint( $height )
283 );
284 }
285
286 return $wp_embed->shortcode( $atts, $atts['url'] );
287 }
288 add_shortcode( 'instagram', 'jetpack_shortcode_instagram' );
289