PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.9.2
Jetpack – WP Security, Backup, Speed, & Growth v13.9.2
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 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / shortcodes / instagram.php
jetpack / modules / shortcodes Last commit date
css 2 years ago images 2 years ago img 2 years ago js 2 years ago archiveorg-book.php 5 years ago archiveorg.php 5 years ago archives.php 5 years ago bandcamp.php 3 years ago brightcove.php 5 years ago cartodb.php 5 years ago class.filter-embedded-html-objects.php 2 years ago codepen.php 5 years ago crowdsignal.php 2 years ago dailymotion.php 3 years ago descript.php 4 years ago facebook.php 2 years ago flatio.php 5 years ago flickr.php 2 years ago getty.php 2 years ago gist.php 3 years ago googleapps.php 2 years ago googlemaps.php 2 years ago googleplus.php 5 years ago gravatar.php 2 years ago houzz.php 5 years ago inline-pdfs.php 4 years ago instagram.php 3 years ago kickstarter.php 3 years ago mailchimp.php 3 years ago medium.php 3 years ago mixcloud.php 2 years ago others.php 2 years ago pinterest.php 2 years ago presentations.php 2 years ago quiz.php 4 years ago recipe.php 2 years ago scribd.php 5 years ago sitemap.php 5 years ago slideshare.php 5 years ago slideshow.php 2 years ago smartframe.php 3 years ago soundcloud.php 3 years ago spotify.php 2 years ago ted.php 5 years ago tweet.php 2 years ago twitchtv.php 5 years ago twitter-timeline.php 5 years ago unavailable.php 3 years ago untappd-menu.php 3 years ago upcoming-events.php 3 years ago ustream.php 5 years ago videopress.php 4 years ago vimeo.php 2 years ago vine.php 5 years ago vr.php 2 years ago wordads.php 5 years ago wufoo.php 3 years ago youtube.php 1 year ago
instagram.php
334 lines
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 automattic/jetpack
14 */
15
16 use Automattic\Jetpack\Connection\Client;
17 use Automattic\Jetpack\Constants;
18 use Automattic\Jetpack\Status;
19
20 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
21 add_action( 'init', 'jetpack_instagram_enable_embeds' );
22 } else {
23 jetpack_instagram_enable_embeds();
24 }
25
26 /**
27 * Register Instagram as oembed provider, and add required filters for the API request.
28 * Add filter to reverse iframes to shortcode. Register [instagram] shortcode.
29 *
30 * @since 9.1.0
31 */
32 function jetpack_instagram_enable_embeds() {
33 wp_oembed_add_provider(
34 '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv|reel)/.*#i',
35 'https://graph.facebook.com/v5.0/instagram_oembed/',
36 true
37 );
38
39 /**
40 * Handle an alternate Instagram URL format, where the username is also part of the URL.
41 */
42 wp_oembed_add_provider(
43 '#https?://(?:www\.)?instagr(?:\.am|am\.com)/(?:[^/]*)/(p|tv|reel)/([^\/]*)#i',
44 'https://graph.facebook.com/v5.0/instagram_oembed/',
45 true
46 );
47
48 /**
49 * Add auth token required by Instagram's oEmbed REST API, or proxy through WP.com.
50 */
51 add_filter( 'oembed_fetch_url', 'jetpack_instagram_oembed_fetch_url', 10, 3 );
52
53 /**
54 * Add JP auth headers if we're proxying through WP.com.
55 */
56 add_filter( 'oembed_remote_get_args', 'jetpack_instagram_oembed_remote_get_args', 10, 2 );
57
58 /**
59 * Embed reversal: Convert an embed code from Instagram.com to an oEmbeddable URL.
60 */
61 add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' );
62
63 /**
64 * Add the shortcode.
65 */
66 add_shortcode( 'instagram', 'jetpack_shortcode_instagram' );
67 }
68
69 /**
70 * Embed Reversal for Instagram
71 *
72 * Hooked to pre_kses, converts an embed code from Instagram.com to an oEmbeddable URL.
73 *
74 * @param string $content Post content.
75 *
76 * @return string The filtered or the original content.
77 **/
78 function jetpack_instagram_embed_reversal( $content ) {
79 if ( ! is_string( $content ) || false === stripos( $content, 'instagram.com' ) ) {
80 return $content;
81 }
82
83 /*
84 * Sample embed code:
85 * <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>
86 * <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script>
87 */
88
89 $regexes = array();
90
91 // new style js.
92 $regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>].+?>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix';
93
94 // Let's play nice with the visual editor too.
95 $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';
96
97 // old style iframe.
98 $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?instagram\.com/p/([^"\'/]++)[^"\']*?)"[^>]*+>\s*?</iframe>#i';
99
100 // Let's play nice with the visual editor too.
101 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//(?:www\.)instagram\.com/p/([^"\'/]++)[^"\']*?)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
102
103 foreach ( $regexes as $regex ) {
104 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
105 continue;
106 }
107
108 foreach ( $matches as $match ) {
109 if ( ! preg_match( '#(https?:)?//(?:www\.)?instagr(\.am|am\.com)/p/([^/]*)#i', $match[1], $url_matches ) ) {
110 continue;
111 }
112
113 // Since we support Instagram via oEmbed, we simply leave a link on a line by itself.
114 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
115 $url = esc_url( $url_matches[0] );
116
117 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
118 /** This action is documented in modules/shortcodes/youtube.php */
119 do_action( 'jetpack_embed_to_shortcode', 'instagram', $url );
120 }
121 }
122
123 return $content;
124 }
125
126 /**
127 * List of allowed and sanitized parameters
128 * that can be used with the Instagram oEmbed endpoint.
129 *
130 * Those parameters can be provided via the Instagram URL, or via shortcode parameters.
131 *
132 * @see https://developers.facebook.com/docs/graph-api/reference/instagram-oembed#parameters
133 *
134 * @since 9.1.0
135 *
136 * @param string $url URL of the content to be embedded.
137 * @param array $atts Shortcode attributes.
138 *
139 * @return array $params Array of parameters to be used in Instagram query.
140 */
141 function jetpack_instagram_get_allowed_parameters( $url, $atts = array() ) {
142 global $content_width;
143
144 // Any URL passed via a shortcode attribute takes precedence.
145 if ( ! empty( $atts['url'] ) ) {
146 $url = $atts['url'];
147 unset( $atts['url'] );
148 }
149
150 /*
151 * Get URL and parameters from the URL if possible.
152 *
153 * We'll also clean any other query params from the URL since Facebook's new API for Instagram
154 * embeds does not like query parameters. See p7H4VZ-2DU-p2.
155 */
156 $parsed_url = wp_parse_url( $url );
157 if ( $parsed_url && isset( $parsed_url['host'] ) && isset( $parsed_url['path'] ) ) {
158 // Bail early if this is not an Instagram URL.
159 if ( ! preg_match( '/(?:^|\.)instagr(?:\.am|am\.com)$/', $parsed_url['host'] ) ) {
160 return array();
161 }
162
163 $url = 'https://www.instagram.com' . $parsed_url['path'];
164
165 // If we have any parameters as part of the URL, we merge them with our attributes.
166 if ( ! empty( $parsed_url['query'] ) ) {
167 $query_args = array();
168 wp_parse_str( $parsed_url['query'], $query_args );
169
170 $atts = array_merge( $atts, $query_args );
171 }
172 } else {
173 return array();
174 }
175
176 $max_width = 698;
177 $min_width = 320;
178
179 $params = shortcode_atts(
180 array(
181 'url' => $url,
182 'width' => ( is_numeric( $content_width ) && $content_width > 0 ) ? $content_width : $max_width,
183 'height' => '',
184 'hidecaption' => false,
185 ),
186 $atts,
187 'instagram'
188 );
189
190 // Ensure width is within bounds.
191 $params['width'] = absint( $params['width'] );
192 if ( $params['width'] > $max_width ) {
193 $params['width'] = $max_width;
194 } elseif ( $params['width'] < $min_width ) {
195 $params['width'] = $min_width;
196 }
197
198 return $params;
199 }
200
201 /**
202 * Add auth token required by Instagram's oEmbed REST API, or proxy through WP.com.
203 *
204 * @since 9.1.0
205 *
206 * @param string $provider URL of the oEmbed provider.
207 * @param string $url URL of the content to be embedded.
208 * @param array $args Additional arguments for retrieving embed HTML.
209 *
210 * @return string
211 */
212 function jetpack_instagram_oembed_fetch_url( $provider, $url, $args ) {
213 if ( ! wp_startswith( $provider, 'https://graph.facebook.com/v5.0/instagram_oembed/' ) ) {
214 return $provider;
215 }
216
217 // Get a set of URL and parameters supported by Facebook.
218 $clean_parameters = jetpack_instagram_get_allowed_parameters( $url, $args );
219
220 // Replace existing URL by our clean version.
221 if ( ! empty( $clean_parameters['url'] ) ) {
222 $provider = add_query_arg( 'url', rawurlencode( $clean_parameters['url'] ), $provider );
223 }
224
225 // Our shortcode supports the width param, but the API expects maxwidth.
226 if ( ! empty( $clean_parameters['width'] ) ) {
227 $provider = add_query_arg( 'maxwidth', $clean_parameters['width'], $provider );
228 }
229
230 if ( ! empty( $clean_parameters['hidecaption'] ) ) {
231 $provider = add_query_arg( 'hidecaption', true, $provider );
232 }
233
234 $access_token = jetpack_instagram_get_access_token();
235
236 if ( ! empty( $access_token ) ) {
237 return add_query_arg( 'access_token', $access_token, $provider );
238 }
239
240 // If we don't have an access token, we go through the WP.com proxy instead.
241 // To that end, we need to make sure that we're connected to WP.com.
242 if ( ! Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode() ) {
243 return $provider;
244 }
245
246 // @TODO Use Core's /oembed/1.0/proxy endpoint on WP.com
247 // (Currently not global but per-site, i.e. /oembed/1.0/sites/1234567/proxy)
248 // and deprecate /oembed-proxy/instagram endpoint.
249 $wpcom_oembed_proxy = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/oembed-proxy/instagram/';
250 return str_replace( 'https://graph.facebook.com/v5.0/instagram_oembed/', $wpcom_oembed_proxy, $provider );
251 }
252
253 /**
254 * Add JP auth headers if we're proxying through WP.com.
255 *
256 * @param array $args oEmbed remote get arguments.
257 * @param string $url URL to be inspected.
258 */
259 function jetpack_instagram_oembed_remote_get_args( $args, $url ) {
260 if ( ! wp_startswith( $url, Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/oembed-proxy/instagram/' ) ) {
261 return $args;
262 }
263
264 $method = 'GET';
265 $signed_request = Client::build_signed_request(
266 compact( 'url', 'method' )
267 );
268
269 return $signed_request['request'];
270 }
271
272 /**
273 * Fetches a Facebook API access token used for query for Instagram embed information, if one is set.
274 *
275 * @return string The access token or ''
276 */
277 function jetpack_instagram_get_access_token() {
278 /**
279 * Filters the Instagram embed token that is used for querying the Facebook API.
280 *
281 * When this token is set, requests are not proxied through the WordPress.com API. Instead, a request is made directly to the
282 * Facebook API to query for information about the embed which should provide a performance benefit.
283 *
284 * @module shortcodes
285 *
286 * @since 9.0.0
287 *
288 * @param string string The access token set via the JETPACK_INSTAGRAM_EMBED_TOKEN constant.
289 */
290 return (string) apply_filters( 'jetpack_instagram_embed_token', (string) Constants::get_constant( 'JETPACK_INSTAGRAM_EMBED_TOKEN' ) );
291 }
292
293 /**
294 * Display the Instagram shortcode.
295 *
296 * @param array $atts Shortcode attributes.
297 */
298 function jetpack_shortcode_instagram( $atts ) {
299 global $wp_embed;
300
301 if ( empty( $atts['url'] ) ) {
302 return '';
303 }
304
305 $atts = jetpack_instagram_get_allowed_parameters( $atts['url'], $atts );
306
307 if ( empty( $atts['url'] ) ) {
308 return '';
309 }
310
311 if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
312 $url_pattern = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i';
313 preg_match( $url_pattern, $atts['url'], $matches );
314 if ( ! $matches ) {
315 return sprintf(
316 '<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>',
317 esc_url( $atts['url'] )
318 );
319 }
320
321 $shortcode_id = end( $matches );
322 $width = ! empty( $atts['width'] ) ? $atts['width'] : 600;
323 $height = ! empty( $atts['height'] ) ? $atts['height'] : 600;
324 return sprintf(
325 '<amp-instagram data-shortcode="%1$s" layout="responsive" width="%2$d" height="%3$d" data-captioned></amp-instagram>',
326 esc_attr( $shortcode_id ),
327 absint( $width ),
328 absint( $height )
329 );
330 }
331
332 return $wp_embed->shortcode( $atts, $atts['url'] );
333 }
334