| 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 |
use Automattic\Jetpack\Connection\Client; |
| 18 |
use Automattic\Jetpack\Constants; |
| 19 |
|
| 20 |
/** |
| 21 |
* Embed Reversal for Instagram |
| 22 |
* |
| 23 |
* Hooked to pre_kses, converts an embed code from Instagram.com to an oEmbeddable URL. |
| 24 |
* |
| 25 |
* @param string $content Post content. |
| 26 |
* |
| 27 |
* @return string The filtered or the original content. |
| 28 |
**/ |
| 29 |
function jetpack_instagram_embed_reversal( $content ) { |
| 30 |
if ( ! is_string( $content ) || false === stripos( $content, 'instagram.com' ) ) { |
| 31 |
return $content; |
| 32 |
} |
| 33 |
|
| 34 |
/* |
| 35 |
* Sample embed code: |
| 36 |
* <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> |
| 37 |
* <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script> |
| 38 |
*/ |
| 39 |
|
| 40 |
$regexes = array(); |
| 41 |
|
| 42 |
// new style js. |
| 43 |
$regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>].+?>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix'; |
| 44 |
|
| 45 |
// Let's play nice with the visual editor too. |
| 46 |
$regexes[] = '#<blockquote(?:[^&]|&(?!gt;))+?class="instagram-media"(?:[^&]|&(?!gt;)).+?>(.+?)</blockquote><script(?:[^&]|&(?!gt;))+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"(?:[^&]|&(?!gt;))*+></script>#ix'; |
| 47 |
|
| 48 |
// old style iframe. |
| 49 |
$regexes[] = '#<iframe[^>]+?src="((?:https?:)?//(?:www\.)?instagram\.com/p/([^"\'/]++)[^"\']*?)"[^>]*+>\s*?</iframe>#i'; |
| 50 |
|
| 51 |
// Let's play nice with the visual editor too. |
| 52 |
$regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//(?:www\.)instagram\.com/p/([^"\'/]++)[^"\']*?)"(?:[^&]|&(?!gt;))*+>\s*?</iframe>#i'; |
| 53 |
|
| 54 |
foreach ( $regexes as $regex ) { |
| 55 |
if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
foreach ( $matches as $match ) { |
| 60 |
if ( ! preg_match( '#(https?:)?//(?:www\.)?instagr(\.am|am\.com)/p/([^/]*)#i', $match[1], $url_matches ) ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
// Since we support Instagram via oEmbed, we simply leave a link on a line by itself. |
| 65 |
$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); |
| 66 |
$url = esc_url( $url_matches[0] ); |
| 67 |
|
| 68 |
$content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); |
| 69 |
/** This action is documented in modules/shortcodes/youtube.php */ |
| 70 |
do_action( 'jetpack_embed_to_shortcode', 'instagram', $url ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $content; |
| 75 |
} |
| 76 |
|
| 77 |
add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Instagram's custom Embed provider. |
| 81 |
* We first remove 2 different embed providers, both registered by Core. |
| 82 |
* - The first is the original provider,that only supports images. |
| 83 |
* - 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 |
| 84 |
* |
| 85 |
* Once the core embed provider is removed (one or the other, depending on your version of Core), we declare our own. |
| 86 |
*/ |
| 87 |
wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/p/.*#i' ); |
| 88 |
wp_oembed_remove_provider( '#https?://(www\.)?instagr(\.am|am\.com)/(p|tv)/.*#i' ); |
| 89 |
wp_embed_register_handler( |
| 90 |
'jetpack_instagram', |
| 91 |
'#http(s?)://(www\.)?instagr(\.am|am\.com)/(p|tv)/([^\/]*)#i', |
| 92 |
'jetpack_instagram_handler' |
| 93 |
); |
| 94 |
|
| 95 |
/** |
| 96 |
* Handle Instagram embeds (build embed from regex). |
| 97 |
* |
| 98 |
* @param array $matches Array of matches from the regex. |
| 99 |
* @param array $atts The original unmodified attributes. |
| 100 |
* @param string $url The original URL that was matched by the regex. |
| 101 |
*/ |
| 102 |
function jetpack_instagram_handler( $matches, $atts, $url ) { |
| 103 |
global $content_width; |
| 104 |
|
| 105 |
// keep a copy of the passed-in URL since it's modified below. |
| 106 |
$passed_url = $url; |
| 107 |
|
| 108 |
$max_width = 698; |
| 109 |
$min_width = 320; |
| 110 |
|
| 111 |
if ( is_feed() ) { |
| 112 |
// Instagram offers direct links to images, but not to videos. |
| 113 |
if ( 'p' === $matches[1] ) { |
| 114 |
$media_url = sprintf( 'https://instagr.am/p/%1$s/media/?size=l', $matches[2] ); |
| 115 |
return sprintf( |
| 116 |
'<a href="%1$s" title="%2$s" target="_blank"><img src="%3$s" alt="%4$s" /></a>', |
| 117 |
esc_url( $url ), |
| 118 |
esc_attr__( 'View on Instagram', 'jetpack' ), |
| 119 |
esc_url( $media_url ), |
| 120 |
esc_html__( 'Instagram Photo', 'jetpack' ) |
| 121 |
); |
| 122 |
} elseif ( 'tv' === $matches[1] ) { |
| 123 |
return sprintf( |
| 124 |
'<a href="%1$s" title="%2$s" target="_blank">%3$s</a>', |
| 125 |
esc_url( $url ), |
| 126 |
esc_attr__( 'View on Instagram', 'jetpack' ), |
| 127 |
esc_html__( 'Instagram Video', 'jetpack' ) |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$atts = shortcode_atts( |
| 133 |
array( |
| 134 |
'width' => isset( $content_width ) ? $content_width : $max_width, |
| 135 |
'hidecaption' => false, |
| 136 |
), |
| 137 |
$atts |
| 138 |
); |
| 139 |
|
| 140 |
$atts['width'] = absint( $atts['width'] ); |
| 141 |
if ( $atts['width'] > $max_width ) { |
| 142 |
$atts['width'] = $max_width; |
| 143 |
} elseif ( $atts['width'] < $min_width ) { |
| 144 |
$atts['width'] = $min_width; |
| 145 |
} |
| 146 |
|
| 147 |
// remove the modal param from the URL. |
| 148 |
$url = remove_query_arg( 'modal', $url ); |
| 149 |
|
| 150 |
// force .com instead of .am for https support. |
| 151 |
$url = str_replace( 'instagr.am', 'instagram.com', $url ); |
| 152 |
|
| 153 |
// The oembed endpoint expects HTTP, but HTTP requests 301 to HTTPS. |
| 154 |
$instagram_http_url = str_replace( 'https://', 'http://', $url ); |
| 155 |
|
| 156 |
$url_args = array( |
| 157 |
'url' => $instagram_http_url, |
| 158 |
'maxwidth' => $atts['width'], |
| 159 |
'omitscript' => 1, |
| 160 |
); |
| 161 |
|
| 162 |
if ( $atts['hidecaption'] ) { |
| 163 |
$url_args['hidecaption'] = 'true'; |
| 164 |
} |
| 165 |
|
| 166 |
$use_cache = jetpack_instagram_use_cache( $matches, $atts, $passed_url ); |
| 167 |
$cache_key = 'oembed_response_body_' . md5( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) ); |
| 168 |
$response_body = $use_cache |
| 169 |
? wp_cache_get( $cache_key, 'instagram_embeds' ) |
| 170 |
: false; |
| 171 |
|
| 172 |
if ( ! $response_body ) { |
| 173 |
$response_body = jetpack_instagram_fetch_embed( $url_args ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( is_wp_error( $response_body ) || empty( $response_body->html ) ) { |
| 177 |
return jetpack_instagram_output_errored_embed( $url ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( $use_cache ) { |
| 181 |
wp_cache_set( |
| 182 |
$cache_key, |
| 183 |
$response_body, |
| 184 |
'instagram_embeds', |
| 185 |
HOUR_IN_SECONDS + wp_rand( 0, HOUR_IN_SECONDS ) |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
wp_enqueue_script( |
| 190 |
'jetpack-instagram-embed', |
| 191 |
Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/instagram.min.js', 'modules/shortcodes/js/instagram.js' ), |
| 192 |
array( 'jquery' ), |
| 193 |
JETPACK__VERSION, |
| 194 |
true |
| 195 |
); |
| 196 |
|
| 197 |
return $response_body->html; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Given a URL, will output an HTML comment and the linked URL. |
| 202 |
* |
| 203 |
* @param string $url The URL that was attempted to embed. |
| 204 |
* |
| 205 |
* @return string The linked URL to the Instagram item. |
| 206 |
*/ |
| 207 |
function jetpack_instagram_output_errored_embed( $url ) { |
| 208 |
return sprintf( |
| 209 |
'<a href="%s">%s</a>', |
| 210 |
esc_url( $url ), |
| 211 |
esc_url_raw( $url ) |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Should the request to fetch embed information be cached? |
| 217 |
* |
| 218 |
* @param array $matches Array of matches from the regex. |
| 219 |
* @param array $atts The original unmodified attributes. |
| 220 |
* @param string $passed_url The original URL that was matched by the regex. |
| 221 |
* |
| 222 |
* @return bool |
| 223 |
*/ |
| 224 |
function jetpack_instagram_use_cache( $matches, $atts, $passed_url ) { |
| 225 |
/** |
| 226 |
* Filter Object Caching for response from Instagram. |
| 227 |
* |
| 228 |
* Allow enabling of object caching for the response sent by Instagram when querying for Instagram image HTML. |
| 229 |
* |
| 230 |
* @module shortcodes |
| 231 |
* |
| 232 |
* @since 3.3.0 |
| 233 |
* |
| 234 |
* @param bool false Object caching is off by default. |
| 235 |
* @param array $matches Array of Instagram URLs found in the post. |
| 236 |
* @param array $atts Instagram Shortcode attributes. |
| 237 |
* @param string $passed_url Instagram API URL. |
| 238 |
*/ |
| 239 |
return apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Handles the logic for actually fetching information for an Instagram embed, conditionally proxying the |
| 244 |
* request through the WordPress.com API if this is not a WordPress.com site. |
| 245 |
* |
| 246 |
* @param array $args An array of arguments to pass to the embed API. |
| 247 |
* |
| 248 |
* @return mixed An object if successful or a WP_Error object |
| 249 |
*/ |
| 250 |
function jetpack_instagram_fetch_embed( $args ) { |
| 251 |
if ( Constants::is_defined( 'IS_WPCOM' ) && Constants::get_constant( 'IS_WPCOM' ) ) { |
| 252 |
$url = esc_url_raw( |
| 253 |
add_query_arg( |
| 254 |
$args, |
| 255 |
'https://api.instagram.com/oembed/' |
| 256 |
) |
| 257 |
); |
| 258 |
$response = wp_remote_get( $url, array( 'redirection' => 0 ) ); |
| 259 |
} else { |
| 260 |
if ( ! Jetpack::is_active_and_not_offline_mode() ) { |
| 261 |
return new WP_Error( |
| 262 |
'jetpack_not_active', |
| 263 |
esc_html__( 'Jetpack must be active to fetch Instagram embed', 'jetpack' ) |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
$response = Client::wpcom_json_api_request_as_blog( |
| 268 |
'/oembed-proxy/instagram?' . http_build_query( $args ), |
| 269 |
'2', |
| 270 |
array(), |
| 271 |
null, |
| 272 |
'wpcom' |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
$response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 277 |
if ( |
| 278 |
is_wp_error( $response ) |
| 279 |
|| 200 !== wp_remote_retrieve_response_code( $response ) |
| 280 |
|| empty( $response_body ) |
| 281 |
) { |
| 282 |
return new WP_Error( |
| 283 |
'instagram_error', |
| 284 |
esc_html__( 'Invalid Instagram resource', 'jetpack' ) |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
return $response_body; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Handle an alternate Instagram URL format, where the username is also part of the URL. |
| 293 |
* We do not actually need that username for the embed. |
| 294 |
*/ |
| 295 |
wp_embed_register_handler( |
| 296 |
'jetpack_instagram_alternate_format', |
| 297 |
'#https?://(?:www\.)?instagr(?:\.am|am\.com)/(?:[^/]*)/(p|tv)/([^\/]*)#i', |
| 298 |
'jetpack_instagram_alternate_format_handler' |
| 299 |
); |
| 300 |
|
| 301 |
/** |
| 302 |
* Handle alternate Instagram embeds (build embed from regex). |
| 303 |
* |
| 304 |
* @param array $matches Array of matches from the regex. |
| 305 |
* @param array $atts The original unmodified attributes. |
| 306 |
* @param string $url The original URL that was matched by the regex. |
| 307 |
*/ |
| 308 |
function jetpack_instagram_alternate_format_handler( $matches, $atts, $url ) { |
| 309 |
// Replace URL saved by original Instagram URL (no username). |
| 310 |
$matches[0] = esc_url_raw( |
| 311 |
sprintf( |
| 312 |
'https://www.instagram.com/%1$s/%2$s', |
| 313 |
$matches[1], |
| 314 |
$matches[2] |
| 315 |
) |
| 316 |
); |
| 317 |
|
| 318 |
return jetpack_instagram_handler( $matches, $atts, $url ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Display the Instagram shortcode. |
| 323 |
* |
| 324 |
* @param array $atts Shortcode attributes. |
| 325 |
*/ |
| 326 |
function jetpack_shortcode_instagram( $atts ) { |
| 327 |
global $wp_embed; |
| 328 |
|
| 329 |
if ( empty( $atts['url'] ) ) { |
| 330 |
return ''; |
| 331 |
} |
| 332 |
|
| 333 |
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 334 |
$url_pattern = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i'; |
| 335 |
preg_match( $url_pattern, $atts['url'], $matches ); |
| 336 |
if ( ! $matches ) { |
| 337 |
return sprintf( |
| 338 |
'<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>', |
| 339 |
esc_url( $atts['url'] ) |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
$shortcode_id = end( $matches ); |
| 344 |
$width = ! empty( $atts['width'] ) ? $atts['width'] : 600; |
| 345 |
$height = ! empty( $atts['height'] ) ? $atts['height'] : 600; |
| 346 |
return sprintf( |
| 347 |
'<amp-instagram data-shortcode="%1$s" layout="responsive" width="%2$d" height="%3$d" data-captioned></amp-instagram>', |
| 348 |
esc_attr( $shortcode_id ), |
| 349 |
absint( $width ), |
| 350 |
absint( $height ) |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
return $wp_embed->shortcode( $atts, $atts['url'] ); |
| 355 |
} |
| 356 |
add_shortcode( 'instagram', 'jetpack_shortcode_instagram' ); |
| 357 |
|