PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.3
Jetpack – WP Security, Backup, Speed, & Growth v7.2.3
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 7.2.3, at modules/shortcodes/instagram.php

244 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 /**
5 * Embed Reversal for Instagram
6 *
7 * Hooked to pre_kses, converts an embed code from Instagram.com to an oEmbeddable URL.
8 *
9 * @return string The filtered or the original content.
10 **/
11 function jetpack_instagram_embed_reversal( $content ) {
12 if ( ! is_string( $content ) || false === stripos( $content, 'instagram.com' ) ) {
13 return $content;
14 }
15
16 /*
17 Sample embed code:
18 <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>
19 <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
20 */
21
22 $regexes = array();
23
24 // new style js.
25 $regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>].+?>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix';
26
27 // Let's play nice with the visual editor too.
28 $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';
29
30 // old style iframe.
31 $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?instagram\.com/p/([^"\'/]++)[^"\']*?)"[^>]*+>\s*?</iframe>#i';
32
33 // Let's play nice with the visual editor too.
34 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//(?:www\.)instagram\.com/p/([^"\'/]++)[^"\']*?)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
35
36 foreach ( $regexes as $regex ) {
37 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
38 continue;
39 }
40
41 foreach ( $matches as $match ) {
42 if ( ! preg_match( '#(https?:)?//(?:www\.)?instagr(\.am|am\.com)/p/([^/]*)#i', $match[1], $url_matches ) ) {
43 continue;
44 }
45
46 // Since we support Instagram via oEmbed, we simply leave a link on a line by itself.
47 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
48 $url = esc_url( $url_matches[0] );
49
50 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
51 /** This action is documented in modules/shortcodes/youtube.php */
52 do_action( 'jetpack_embed_to_shortcode', 'instagram', $url );
53 }
54 }
55
56 return $content;
57 }
58
59 add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' );
60
61 /**
62 * Instagram's custom Embed provider.
63 * We first remove 2 different embed providers, both registered by Core.
64 * - The first is the original provider,that only supports images.
65 * - 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
66 *
67 * Once the core embed provider is removed (one or the other, depending on your version of Core), we declare our own.
68 */
69 wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i' );
70 wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' );
71 wp_embed_register_handler(
72 'jetpack_instagram',
73 '#http(s?)://(www\.)?instagr(\.am|am\.com)/(p|tv)/([^\/]*)#i',
74 'jetpack_instagram_handler'
75 );
76
77 /**
78 * Handle Instagram embeds (build embed from regex).
79 *
80 * @param array $matches Array of matches from the regex.
81 * @param array $atts The original unmodified attributes.
82 * @param string $url The original URL that was matched by the regex.
83 */
84 function jetpack_instagram_handler( $matches, $atts, $url ) {
85 global $content_width;
86
87 // keep a copy of the passed-in URL since it's modified below
88 $passed_url = $url;
89
90 $max_width = 698;
91 $min_width = 320;
92
93 if ( is_feed() ) {
94 // Instagram offers direct links to images, but not to videos.
95 if ( 'p' === $matches[1] ) {
96 $media_url = sprintf( 'http://instagr.am/p/%1$s/media/?size=l', $matches[2] );
97 return sprintf(
98 '<a href="%1$s" title="%2$s" target="_blank"><img src="%3$s" alt="%4$s" /></a>',
99 esc_url( $url ),
100 esc_attr__( 'View on Instagram', 'jetpack' ),
101 esc_url( $media_url ),
102 esc_html__( 'Instagram Photo', 'jetpack' )
103 );
104 } elseif ( 'tv' === $matches[1] ) {
105 return sprintf(
106 '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>',
107 esc_url( $url ),
108 esc_attr__( 'View on Instagram', 'jetpack' ),
109 esc_html__( 'Instagram Video', 'jetpack' )
110 );
111 }
112 }
113
114 $atts = shortcode_atts(
115 array(
116 'width' => isset( $content_width ) ? $content_width : $max_width,
117 'hidecaption' => false,
118 ),
119 $atts
120 );
121
122 $atts['width'] = absint( $atts['width'] );
123 if ( $atts['width'] > $max_width ) {
124 $atts['width'] = $max_width;
125 } elseif ( $atts['width'] < $min_width ) {
126 $atts['width'] = $min_width;
127 }
128
129 // remove the modal param from the URL
130 $url = remove_query_arg( 'modal', $url );
131
132 // force .com instead of .am for https support
133 $url = str_replace( 'instagr.am', 'instagram.com', $url );
134
135 // The oembed endpoint expects HTTP, but HTTP requests 301 to HTTPS
136 $instagram_http_url = str_replace( 'https://', 'http://', $url );
137 $instagram_https_url = str_replace( 'http://', 'https://', $url );
138
139 $url_args = array(
140 'url' => $instagram_http_url,
141 'maxwidth' => $atts['width'],
142 );
143
144 if ( $atts['hidecaption'] ) {
145 $url_args['hidecaption'] = 'true';
146 }
147
148 $url = esc_url_raw( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) );
149
150 /**
151 * Filter Object Caching for response from Instagram.
152 *
153 * Allow enabling of object caching for the response sent by Instagram when querying for Instagram image HTML.
154 *
155 * @module shortcodes
156 *
157 * @since 3.3.0
158 *
159 * @param bool false Object caching is off by default.
160 * @param array $matches Array of Instagram URLs found in the post.
161 * @param array $atts Instagram Shortcode attributes.
162 * @param string $passed_url Instagram API URL.
163 */
164 $response_body_use_cache = apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url );
165 $response_body = false;
166 if ( $response_body_use_cache ) {
167 $cache_key = 'oembed_response_body_' . md5( $url );
168 $response_body = wp_cache_get( $cache_key, 'instagram_embeds' );
169 }
170
171 if ( ! $response_body ) {
172 // Not using cache (default case) or cache miss
173 $instagram_response = wp_remote_get( $url, array( 'redirection' => 0 ) );
174 if ( is_wp_error( $instagram_response ) || 200 != $instagram_response['response']['code'] || empty( $instagram_response['body'] ) ) {
175 return '<!-- instagram error: invalid instagram resource -->';
176 }
177
178 $response_body = json_decode( $instagram_response['body'] );
179 if ( $response_body_use_cache ) {
180 // if caching it is short-lived since this is a "Cache-Control: no-cache" resource
181 wp_cache_set( $cache_key, $response_body, 'instagram_embeds', HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) );
182 }
183 }
184
185 if ( ! empty( $response_body->html ) ) {
186 wp_enqueue_script(
187 'jetpack-instagram-embed',
188 Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/instagram.min.js', 'modules/shortcodes/js/instagram.js' ),
189 array( 'jquery' ),
190 false,
191 true
192 );
193 // there's a script in the response, which we strip on purpose since it's added by this ^ script
194 $ig_embed = preg_replace( '@<(script)[^>]*?>.*?</\\1>@si', '', $response_body->html );
195
196 return $ig_embed;
197 }
198
199 return '<!-- instagram error: no embed found -->';
200 }
201
202 /**
203 * Handle an alternate Instagram URL format, where the username is also part of the URL.
204 * We do not actually need that username for the embed.
205 */
206 wp_embed_register_handler(
207 'jetpack_instagram_alternate_format',
208 '#https?://(?:www\.)?instagr(?:\.am|am\.com)/(?:[^/]*)/(p|tv)/([^\/]*)#i',
209 'jetpack_instagram_alternate_format_handler'
210 );
211
212 /**
213 * Handle alternate Instagram embeds (build embed from regex).
214 *
215 * @param array $matches Array of matches from the regex.
216 * @param array $atts The original unmodified attributes.
217 * @param string $url The original URL that was matched by the regex.
218 */
219 function jetpack_instagram_alternate_format_handler( $matches, $atts, $url ) {
220 // Replace URL saved by original Instagram URL (no username).
221 $matches[0] = esc_url_raw(
222 sprintf(
223 'https://www.instagram.com/%1$s/%2$s',
224 $matches[1],
225 $matches[2]
226 )
227 );
228
229 return jetpack_instagram_handler( $matches, $atts, $url );
230 }
231
232 // [instagram url="http://instagram.com/p/PSbF9sEIGP/"]
233 // [instagram url="http://instagram.com/p/PSbF9sEIGP/" width="300"]
234 add_shortcode( 'instagram', 'jetpack_shortcode_instagram' );
235 function jetpack_shortcode_instagram( $atts ) {
236 global $wp_embed;
237
238 if ( empty( $atts['url'] ) ) {
239 return '';
240 }
241
242 return $wp_embed->shortcode( $atts, $atts['url'] );
243 }
244