/modules/shortcodes/instagram.php

  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. * @return (string) the filtered or the original content 
  9. **/ 
  10. function jetpack_instagram_embed_reversal( $content ) { 
  11. if ( false === stripos( $content, 'instagram.com' ) ) 
  12. return $content; 
  13.  
  14. /** Sample embed code: 
  15. <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> 
  16. <script async defer src="https://platform.instagram.com/en_US/embeds.js"></script> 
  17. */ 
  18.  
  19. $regexes = array(); 
  20.  
  21. // new style js 
  22. $regexes[] = '#<blockquote[^>]+?class="instagram-media"[^>](.+?)>(.+?)</blockquote><script[^>]+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"></script>#ix'; 
  23.  
  24. // Let's play nice with the visual editor too. 
  25. $regexes[] = '#<blockquote(?:[^&]|&(?!gt;))+?class="instagram-media"(?:[^&]|&(?!gt;))(.+?)>(.+?)</blockquote><script(?:[^&]|&(?!gt;))+?src="(https?:)?//platform\.instagram\.com/(.+?)/embeds\.js"(?:[^&]|&(?!gt;))*+></script>#ix'; 
  26.  
  27. // old style iframe 
  28. $regexes[] = '#<iframe[^>]+?src="(?:https?:)?//instagram\.com/p/([^"\'/]++)[^"\']*?"[^>]*+>\s*?</iframe>#i'; 
  29.  
  30. // Let's play nice with the visual editor too. 
  31. $regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src="(?:https?:)?//instagram\.com/p/([^"\'/]++)[^"\']*?"(?:[^&]|&(?!gt;))*+>\s*?</iframe>#i'; 
  32.  
  33. foreach ( $regexes as $regex ) { 
  34. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) 
  35. continue; 
  36.  
  37. foreach ( $matches as $match ) { 
  38. if ( ! preg_match( '#(https?:)?//instagr(\.am|am\.com)/p/([^/]*)#i', $match[2], $url_matches ) ) 
  39. continue; 
  40.  
  41. // Since we support Instagram via oEmbed, we simply leave a link on a line by itself. 
  42. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); 
  43. $url = esc_url( $url_matches[0] ); 
  44.  
  45. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); 
  46. /** This action is documented in modules/shortcodes/youtube.php */ 
  47. do_action( 'jetpack_embed_to_shortcode', 'instagram', $url ); 
  48.  
  49. return $content; 
  50. add_filter( 'pre_kses', 'jetpack_instagram_embed_reversal' ); 
  51.  
  52.  
  53. /** 
  54. * Instagram 
  55. */ 
  56. wp_oembed_remove_provider( '#http://instagr(\.am|am\.com)/p/.*#i' ); // remove core's oEmbed support so we can override 
  57. wp_embed_register_handler( 'jetpack_instagram', '#http(s?)://instagr(\.am|am\.com)/p/([^/]*)#i', 'jetpack_instagram_handler' ); 
  58.  
  59. function jetpack_instagram_handler( $matches, $atts, $url ) { 
  60. global $content_width; 
  61.  
  62. // keep a copy of the passed-in URL since it's modified below 
  63. $passed_url = $url; 
  64.  
  65. $max_width = 698; 
  66. $min_width = 320; 
  67.  
  68. if ( is_feed() ) { 
  69. $media_url = sprintf( 'http://instagr.am/p/%s/media/?size=l', $matches[3] ); 
  70. return sprintf( '<a href="%s" title="%s"><img src="%s" alt="Instagram Photo" /></a>', esc_url( $url ), esc_attr__( 'View on Instagram', 'jetpack' ), esc_url( $media_url ) ); 
  71.  
  72. $atts = shortcode_atts( array( 
  73. 'width' => isset( $content_width ) ? $content_width : $max_width,  
  74. 'hidecaption' => false,  
  75. ), $atts ); 
  76.  
  77. $atts['width'] = absint( $atts['width'] ); 
  78. if ( $atts['width'] > $max_width || $min_width > $atts['width'] ) 
  79. $atts['width'] = $max_width; 
  80.  
  81.  
  82. // remove the modal param from the URL 
  83. $url = remove_query_arg( 'modal', $url ); 
  84.  
  85. // force .com instead of .am for https support 
  86. $url = str_replace( 'instagr.am', 'instagram.com', $url ); 
  87.  
  88. // The oembed endpoint expects HTTP, but HTTP requests 301 to HTTPS 
  89. $instagram_http_url = str_replace( 'https://', 'http://', $url ); 
  90. $instagram_https_url = str_replace( 'http://', 'https://', $url ); 
  91.  
  92. $url_args = array( 
  93. 'url' => $instagram_http_url,  
  94. 'maxwidth' => $atts['width'],  
  95. ); 
  96.  
  97. if ( $atts['hidecaption'] ) { 
  98. $url_args['hidecaption'] = 'true'; 
  99.  
  100. $url = esc_url_raw( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) ); 
  101.  
  102. /** 
  103. * Filter Object Caching for response from Instagram. 
  104. * 
  105. * Allow enabling of object caching for the response sent by Instagram when querying for Instagram image HTML. 
  106. * 
  107. * @module shortcodes 
  108. * 
  109. * @since 3.3.0 
  110. * 
  111. * @param bool false Object caching is off by default. 
  112. * @param array $matches Array of Instagram URLs found in the post. 
  113. * @param array $atts Instagram Shortcode attributes. 
  114. * @param string $passed_url Instagram API URL. 
  115. */ 
  116. $response_body_use_cache = apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url ); 
  117. $response_body = false; 
  118. if ( $response_body_use_cache ) { 
  119. $cache_key = 'oembed_response_body_' . md5( $url ); 
  120. $response_body = wp_cache_get( $cache_key, 'instagram_embeds' ); 
  121.  
  122. if ( ! $response_body ) { 
  123. // Not using cache (default case) or cache miss 
  124. $instagram_response = wp_remote_get( $url, array( 'redirection' => 0 ) ); 
  125. if ( is_wp_error( $instagram_response ) || 200 != $instagram_response['response']['code'] || empty( $instagram_response['body'] ) ) { 
  126. return "<!-- instagram error: invalid instagram resource -->"; 
  127.  
  128. $response_body = json_decode( $instagram_response['body'] ); 
  129. if ( $response_body_use_cache ) { 
  130. // if caching it is short-lived since this is a "Cache-Control: no-cache" resource 
  131. wp_cache_set( $cache_key, $response_body, 'instagram_embeds', HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) ); 
  132.  
  133. if ( ! empty( $response_body->html ) ) { 
  134. wp_enqueue_script( 'jetpack-instagram-embed', plugins_url( 'js/instagram.js', __FILE__ ), array( 'jquery' ), false, true ); 
  135. // there's a script in the response, which we strip on purpose since it's added by this ^ script 
  136. $ig_embed = preg_replace( '@<(script)[^>]*?>.*?</\\1>@si', '', $response_body->html ); 
  137. return $ig_embed; 
  138.  
  139. return '<!-- instagram error: no embed found -->'; 
  140.  
  141.  
  142. // filters instagram's username format to the expected format that matches the embed handler 
  143. wp_embed_register_handler( 'jetpack_instagram_alternate_format', '#http(s?)://instagr(\.am|am\.com)/([^/]*)/p/([^/]*)#i', 'jetpack_instagram_alternate_format_handler' ); 
  144. function jetpack_instagram_alternate_format_handler( $matches, $atts, $url ) { 
  145. $url = esc_url_raw( 'https://instagram.com/p/' . $matches[4] ); 
  146. $matches[0] = $url; 
  147. $matches[3] = $matches[4]; 
  148. unset( $matches[4] ); 
  149. return jetpack_instagram_handler( $matches, $atts, $url ); 
  150.  
  151.  
  152. // [instagram url="http://instagram.com/p/PSbF9sEIGP/"] 
  153. // [instagram url="http://instagram.com/p/PSbF9sEIGP/" width="300"] 
  154. add_shortcode( 'instagram', 'jetpack_shortcode_instagram' ); 
  155. function jetpack_shortcode_instagram( $atts ) { 
  156. global $wp_embed; 
  157.  
  158. if ( empty( $atts['url'] ) ) { 
  159. return ''; 
  160.  
  161. return $wp_embed->shortcode( $atts, $atts['url'] ); 
.