| 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\Connection\Client; |
| 17 |
use Automattic\Jetpack\Constants; |
| 18 |
|
| 19 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 20 |
add_action( 'init', 'jetpack_instagram_enable_embeds' ); |
| 21 |
} else { |
| 22 |
jetpack_instagram_enable_embeds(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register Instagram as oembed provider, and add required filters for the API request. |
| 27 |
* Add filter to reverse iframes to shortcode. Register [instagram] shortcode. |
| 28 |
* |
| 29 |
* @since 9.1.0 |
| 30 |
*/ |
| 31 |
function jetpack_instagram_enable_embeds() { |
| 32 |
|
| 33 |
/** |
| 34 |
* Instagram's custom Embed provider. |
| 35 |
* We first remove the embed provider that's registered by Core; then, we declare our own. |
| 36 |
* |
| 37 |
* We can drop the `wp_oembed_remove_provider` line once Core stops adding its own Instagram provider: |
| 38 |
* https://core.trac.wordpress.org/ticket/50861. |
| 39 |
*/ |
| 40 |
wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' ); |
| 41 |
|
| 42 |
wp_oembed_add_provider( |
| 43 |
'#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i', |
| 44 |
'https://graph.facebook.com/v5.0/instagram_oembed/', |
| 45 |
true |
| 46 |
); |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle an alternate Instagram URL format, where the username is also part of the URL. |
| 50 |
*/ |
| 51 |
wp_oembed_add_provider( |
| 52 |
'#https?://(?:www\.)?instagr(?:\.am|am\.com)/(?:[^/]*)/(p|tv)/([^\/]*)#i', |
| 53 |
'https://graph.facebook.com/v5.0/instagram_oembed/', |
| 54 |
true |
| 55 |
); |
| 56 |
|
| 57 |
/** |
| 58 |
* Add auth token required by Instagram's oEmbed REST API, or proxy through WP.com. |
| 59 |
*/ |
| 60 |
add_filter( 'oembed_fetch_url', 'jetpack_instagram_oembed_fetch_url', 10, 2 ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Add JP auth headers if we're proxying through WP.com. |
| 64 |
*/ |
| 65 |
add_filter( 'oembed_remote_get_args', 'jetpack_instagram_oembed_remote_get_args', 10, 2 ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Embed reversal: Convert an embed code from Instagram.com to an oEmbeddable URL. |
| 69 |
*/ |
| 70 |
add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' ); |
| 71 |
|
| 72 |
/** |
| 73 |
* Add the shortcode. |
| 74 |
*/ |
| 75 |
add_shortcode( 'instagram', 'jetpack_shortcode_instagram' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Embed Reversal for Instagram |
| 80 |
* |
| 81 |
* Hooked to pre_kses, converts an embed code from Instagram.com to an oEmbeddable URL. |
| 82 |
* |
| 83 |
* @param string $content Post content. |
| 84 |
* |
| 85 |
* @return string The filtered or the original content. |
| 86 |
**/ |
| 87 |
function jetpack_instagram_embed_reversal( $content ) { |
| 88 |
if ( ! is_string( $content ) || false === stripos( $content, 'instagram.com' ) ) { |
| 89 |
return $content; |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* Sample embed code: |
| 94 |
* <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> |
| 95 |
* <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script> |
| 96 |
*/ |
| 97 |
|
| 98 |
$regexes = array(); |
| 99 |
|
| 100 |
// new style js. |
| 101 |
$regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>].+?>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix'; |
| 102 |
|
| 103 |
// Let's play nice with the visual editor too. |
| 104 |
$regexes[] = '#<blockquote(?:[^&]|&(?!gt;))+?class="instagram-media"(?:[^&]|&(?!gt;)).+?>(.+?)</blockquote><script(?:[^&]|&(?!gt;))+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"(?:[^&]|&(?!gt;))*+></script>#ix'; |
| 105 |
|
| 106 |
// old style iframe. |
| 107 |
$regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?instagram\.com/p/([^"\'/]++)[^"\']*?)"[^>]*+>\s*?</iframe>#i'; |
| 108 |
|
| 109 |
// Let's play nice with the visual editor too. |
| 110 |
$regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//(?:www\.)instagram\.com/p/([^"\'/]++)[^"\']*?)"(?:[^&]|&(?!gt;))*+>\s*?</iframe>#i'; |
| 111 |
|
| 112 |
foreach ( $regexes as $regex ) { |
| 113 |
if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
foreach ( $matches as $match ) { |
| 118 |
if ( ! preg_match( '#(https?:)?//(?:www\.)?instagr(\.am|am\.com)/p/([^/]*)#i', $match[1], $url_matches ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
// Since we support Instagram via oEmbed, we simply leave a link on a line by itself. |
| 123 |
$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); |
| 124 |
$url = esc_url( $url_matches[0] ); |
| 125 |
|
| 126 |
$content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); |
| 127 |
/** This action is documented in modules/shortcodes/youtube.php */ |
| 128 |
do_action( 'jetpack_embed_to_shortcode', 'instagram', $url ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $content; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* List of allowed and sanitized parameters |
| 137 |
* that can be used with the Instagram oEmbed endpoint. |
| 138 |
* |
| 139 |
* Those parameters can be provided via the Instagram URL, or via shortcode parameters. |
| 140 |
* |
| 141 |
* @see https://developers.facebook.com/docs/graph-api/reference/instagram-oembed#parameters |
| 142 |
* |
| 143 |
* @since 9.1.0 |
| 144 |
* |
| 145 |
* @param string $url URL of the content to be embedded. |
| 146 |
* @param array $atts Shortcode attributes. |
| 147 |
* |
| 148 |
* @return array $params Array of parameters to be used in Instagram query. |
| 149 |
*/ |
| 150 |
function jetpack_instagram_get_allowed_parameters( $url, $atts = array() ) { |
| 151 |
global $content_width; |
| 152 |
|
| 153 |
// Any URL passed via a shortcode attribute takes precedence. |
| 154 |
if ( ! empty( $atts['url'] ) ) { |
| 155 |
$url = $atts['url']; |
| 156 |
unset( $atts['url'] ); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Get URL and parameters from the URL if possible. |
| 161 |
* |
| 162 |
* We'll also clean any other query params from the URL since Facebook's new API for Instagram |
| 163 |
* embeds does not like query parameters. See p7H4VZ-2DU-p2. |
| 164 |
*/ |
| 165 |
$parsed_url = wp_parse_url( $url ); |
| 166 |
if ( $parsed_url && isset( $parsed_url['host'] ) && isset( $parsed_url['path'] ) ) { |
| 167 |
// Bail early if this is not an Instagram URL. |
| 168 |
if ( ! preg_match( '/(?:^|\.)instagr(?:\.am|am\.com)$/', $parsed_url['host'] ) ) { |
| 169 |
return array(); |
| 170 |
} |
| 171 |
|
| 172 |
$url = 'https://www.instagram.com' . $parsed_url['path']; |
| 173 |
|
| 174 |
// If we have any parameters as part of the URL, we merge them with our attributes. |
| 175 |
if ( ! empty( $parsed_url['query'] ) ) { |
| 176 |
$query_args = array(); |
| 177 |
wp_parse_str( $parsed_url['query'], $query_args ); |
| 178 |
|
| 179 |
$atts = array_merge( $atts, $query_args ); |
| 180 |
} |
| 181 |
} else { |
| 182 |
return array(); |
| 183 |
} |
| 184 |
|
| 185 |
$max_width = 698; |
| 186 |
$min_width = 320; |
| 187 |
|
| 188 |
$params = shortcode_atts( |
| 189 |
array( |
| 190 |
'url' => $url, |
| 191 |
'width' => isset( $content_width ) ? $content_width : $max_width, |
| 192 |
'height' => '', |
| 193 |
'hidecaption' => false, |
| 194 |
), |
| 195 |
$atts, |
| 196 |
'instagram' |
| 197 |
); |
| 198 |
|
| 199 |
// Ensure width is within bounds. |
| 200 |
$params['width'] = absint( $params['width'] ); |
| 201 |
if ( $params['width'] > $max_width ) { |
| 202 |
$params['width'] = $max_width; |
| 203 |
} elseif ( $params['width'] < $min_width ) { |
| 204 |
$params['width'] = $min_width; |
| 205 |
} |
| 206 |
|
| 207 |
return $params; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Add auth token required by Instagram's oEmbed REST API, or proxy through WP.com. |
| 212 |
* |
| 213 |
* @since 9.1.0 |
| 214 |
* |
| 215 |
* @param string $provider URL of the oEmbed provider. |
| 216 |
* @param string $url URL of the content to be embedded. |
| 217 |
* |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
function jetpack_instagram_oembed_fetch_url( $provider, $url ) { |
| 221 |
if ( ! wp_startswith( $provider, 'https://graph.facebook.com/v5.0/instagram_oembed/' ) ) { |
| 222 |
return $provider; |
| 223 |
} |
| 224 |
|
| 225 |
// Get a set of URL and parameters supported by Facebook. |
| 226 |
$clean_parameters = jetpack_instagram_get_allowed_parameters( $url ); |
| 227 |
|
| 228 |
// Replace existing URL by our clean version. |
| 229 |
if ( ! empty( $clean_parameters['url'] ) ) { |
| 230 |
$provider = add_query_arg( 'url', rawurlencode( $clean_parameters['url'] ), $provider ); |
| 231 |
} |
| 232 |
|
| 233 |
// Our shortcode supports the width param, but the API expects maxwidth. |
| 234 |
if ( ! empty( $clean_parameters['width'] ) ) { |
| 235 |
$provider = add_query_arg( 'maxwidth', $clean_parameters['width'], $provider ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( ! empty( $clean_parameters['hidecaption'] ) ) { |
| 239 |
$provider = add_query_arg( 'hidecaption', true, $provider ); |
| 240 |
} |
| 241 |
|
| 242 |
$access_token = jetpack_instagram_get_access_token(); |
| 243 |
|
| 244 |
if ( ! empty( $access_token ) ) { |
| 245 |
return add_query_arg( 'access_token', $access_token, $provider ); |
| 246 |
} |
| 247 |
|
| 248 |
// If we don't have an access token, we go through the WP.com proxy instead. |
| 249 |
// To that end, we need to make sure that we're connected to WP.com. |
| 250 |
if ( ! Jetpack::is_active_and_not_offline_mode() ) { |
| 251 |
return $provider; |
| 252 |
} |
| 253 |
|
| 254 |
// @TODO Use Core's /oembed/1.0/proxy endpoint on WP.com |
| 255 |
// (Currently not global but per-site, i.e. /oembed/1.0/sites/1234567/proxy) |
| 256 |
// and deprecate /oembed-proxy/instagram endpoint. |
| 257 |
$wpcom_oembed_proxy = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/oembed-proxy/instagram/'; |
| 258 |
return str_replace( 'https://graph.facebook.com/v5.0/instagram_oembed/', $wpcom_oembed_proxy, $provider ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Add JP auth headers if we're proxying through WP.com. |
| 263 |
* |
| 264 |
* @param array $args oEmbed remote get arguments. |
| 265 |
* @param string $url URL to be inspected. |
| 266 |
*/ |
| 267 |
function jetpack_instagram_oembed_remote_get_args( $args, $url ) { |
| 268 |
if ( ! wp_startswith( $url, Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/oembed-proxy/instagram/' ) ) { |
| 269 |
return $args; |
| 270 |
} |
| 271 |
|
| 272 |
$method = 'GET'; |
| 273 |
$signed_request = Client::build_signed_request( |
| 274 |
compact( 'url', 'method' ) |
| 275 |
); |
| 276 |
|
| 277 |
return $signed_request['request']; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Fetches a Facebook API access token used for query for Instagram embed information, if one is set. |
| 282 |
* |
| 283 |
* @return string The access token or '' |
| 284 |
*/ |
| 285 |
function jetpack_instagram_get_access_token() { |
| 286 |
/** |
| 287 |
* Filters the Instagram embed token that is used for querying the Facebook API. |
| 288 |
* |
| 289 |
* When this token is set, requests are not proxied through the WordPress.com API. Instead, a request is made directly to the |
| 290 |
* Facebook API to query for information about the embed which should provide a performance benefit. |
| 291 |
* |
| 292 |
* @module shortcodes |
| 293 |
* |
| 294 |
* @since 9.0.0 |
| 295 |
* |
| 296 |
* @param string string The access token set via the JETPACK_INSTAGRAM_EMBED_TOKEN constant. |
| 297 |
*/ |
| 298 |
return (string) apply_filters( 'jetpack_instagram_embed_token', (string) Constants::get_constant( 'JETPACK_INSTAGRAM_EMBED_TOKEN' ) ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Display the Instagram shortcode. |
| 303 |
* |
| 304 |
* @param array $atts Shortcode attributes. |
| 305 |
*/ |
| 306 |
function jetpack_shortcode_instagram( $atts ) { |
| 307 |
global $wp_embed; |
| 308 |
|
| 309 |
if ( empty( $atts['url'] ) ) { |
| 310 |
return ''; |
| 311 |
} |
| 312 |
|
| 313 |
$atts = jetpack_instagram_get_allowed_parameters( $atts['url'], $atts ); |
| 314 |
|
| 315 |
if ( empty( $atts['url'] ) ) { |
| 316 |
return ''; |
| 317 |
} |
| 318 |
|
| 319 |
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 320 |
$url_pattern = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i'; |
| 321 |
preg_match( $url_pattern, $atts['url'], $matches ); |
| 322 |
if ( ! $matches ) { |
| 323 |
return sprintf( |
| 324 |
'<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>', |
| 325 |
esc_url( $atts['url'] ) |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
$shortcode_id = end( $matches ); |
| 330 |
$width = ! empty( $atts['width'] ) ? $atts['width'] : 600; |
| 331 |
$height = ! empty( $atts['height'] ) ? $atts['height'] : 600; |
| 332 |
return sprintf( |
| 333 |
'<amp-instagram data-shortcode="%1$s" layout="responsive" width="%2$d" height="%3$d" data-captioned></amp-instagram>', |
| 334 |
esc_attr( $shortcode_id ), |
| 335 |
absint( $width ), |
| 336 |
absint( $height ) |
| 337 |
); |
| 338 |
} |
| 339 |
|
| 340 |
return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 341 |
} |
| 342 |
|