Loading

Balloons

View on Instagram

*/ $regexes = array(); // new style js $regexes[] = '#]+?class="instagram-media"[^>](.+?)>(.+?)]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js">#ix'; // Let's play nice with the visual editor too. $regexes[] = '#<blockquote(?:[^&]|&(?!gt;))+?class="instagram-media"(?:[^&]|&(?!gt;))(.+?)>(.+?)</blockquote><script(?:[^&]|&(?!gt;))+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"(?:[^&]|&(?!gt;))*+></script>#ix'; // old style iframe $regexes[] = '#]+?src="(?:https?:)?//instagram\.com/p/([^"\'/]++)[^"\']*?"[^>]*+>\s*?#i'; // Let's play nice with the visual editor too. $regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src="(?:https?:)?//instagram\.com/p/([^"\'/]++)[^"\']*?"(?:[^&]|&(?!gt;))*+>\s*?</iframe>#i'; foreach ( $regexes as $regex ) { if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) continue; foreach ( $matches as $match ) { if ( ! preg_match( '#(https?:)?//instagr(\.am|am\.com)/p/([^/]*)#i', $match[2], $url_matches ) ) continue; // Since we support Instagram via oEmbed, we simply leave a link on a line by itself. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); $url = esc_url( $url_matches[0] ); $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); do_action( 'jetpack_embed_to_shortcode', 'instagram', $url ); } } return $content; } add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' ); /** * Instagram */ wp_oembed_remove_provider( '#http://instagr(\.am|am\.com)/p/.*#i' ); // remove core's oEmbed support so we can override wp_embed_register_handler( 'jetpack_instagram', '#http(s?)://instagr(\.am|am\.com)/p/([^/]*)#i', 'jetpack_instagram_handler' ); function jetpack_instagram_handler( $matches, $atts, $url ) { global $content_width; static $did_script; // keep a copy of the passed-in URL since it's modified below $passed_url = $url; $max_width = 698; $min_width = 320; if ( is_feed() ) { $media_url = sprintf( 'http://instagr.am/p/%s/media/?size=l', $matches[2] ); return sprintf( 'Instagram Photo', esc_url( $url ), esc_attr__( 'View on Instagram', 'jetpack' ), esc_url( $media_url ) ); } $atts = shortcode_atts( array( 'width' => isset( $content_width ) ? $content_width : $max_width, 'hidecaption' => false, ), $atts ); $atts['width'] = absint( $atts['width'] ); if ( $atts['width'] > $max_width || $min_width > $atts['width'] ) $atts['width'] = $max_width; // remove the modal param from the URL $url = remove_query_arg( 'modal', $url ); // force .com instead of .am for https support $url = str_replace( 'instagr.am', 'instagram.com', $url ); // The oembed endpoint expects HTTP, but HTTP requests 301 to HTTPS $instagram_http_url = str_replace( 'https://', 'http://', $url ); $instagram_https_url = str_replace( 'http://', 'https://', $url ); $url_args = array( 'url' => $instagram_http_url, 'maxwidth' => $atts['width'], ); if ( $atts['hidecaption'] ) { $url_args['hidecaption'] = 'true'; } $url = esc_url_raw( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) ); // Don't use object caching here by default, but give themes ability to turn it on. $response_body_use_cache = apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url ); $response_body = false; if ( $response_body_use_cache ) { $cache_key = 'oembed_response_body_' . md5( $url ); $response_body = wp_cache_get( $cache_key, 'instagram_embeds' ); } if ( ! $response_body ) { // Not using cache (default case) or cache miss $instagram_response = wp_remote_get( $url, array( 'redirection' => 0 ) ); if ( is_wp_error( $instagram_response ) || 200 != $instagram_response['response']['code'] || empty( $instagram_response['body'] ) ) { return ""; } $response_body = json_decode( $instagram_response['body'] ); if ( $response_body_use_cache ) { // if caching it is short-lived since this is a "Cache-Control: no-cache" resource wp_cache_set( $cache_key, $response_body, 'instagram_embeds', HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) ); } } if ( ! empty( $response_body->html ) ) { if ( ! $did_script ) { $did_script = true; add_action( 'wp_footer', 'jetpack_instagram_add_script' ); } // there's a script in the response, which we strip on purpose since it's added above $ig_embed = preg_replace( '@<(script)[^>]*?>.*?@si', '', $response_body->html ); } else { $ig_embed = jetpack_instagram_iframe_embed( $instagram_https_url, $atts ); } return $ig_embed; } function jetpack_instagram_add_script() { ?> shortcode( $atts, $atts['url'] ); } function jetpack_instagram_iframe_embed( $url, $atts ) { $atts['height'] = intval( $atts['width'] ) + 98; // http://www.niemanlab.org/2013/07/instagram-embeds-are-here-but-not-quite-perfect-for-publishers/ $url = trailingslashit( $url ) . 'embed/'; return sprintf( '', esc_url( $url ), esc_attr( $atts['width'] ), esc_attr( $atts['height'] ) ); }