jetpack_instagram_handler

The Jetpack by WordPress.com jetpack instagram handler function.

Description

jetpack_instagram_handler( $matches, $atts, $url ); 

Parameters (3)

0. $matches
The matches.
1. $atts
The atts.
2. $url
The url.

Usage

  1. if ( !function_exists( 'jetpack_instagram_handler' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/shortcodes/instagram.php'; 
  3.  
  4. // The matches. 
  5. $matches = null; 
  6.  
  7. // The atts. 
  8. $atts = null; 
  9.  
  10. // The url. 
  11. $url = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = jetpack_instagram_handler($matches, $atts, $url); 
  15.  

Defined (1)

The function is defined in the following location(s).

/modules/shortcodes/instagram.php  
  1. function jetpack_instagram_handler( $matches, $atts, $url ) { 
  2. global $content_width; 
  3.  
  4. // keep a copy of the passed-in URL since it's modified below 
  5. $passed_url = $url; 
  6.  
  7. $max_width = 698; 
  8. $min_width = 320; 
  9.  
  10. if ( is_feed() ) { 
  11. $media_url = sprintf( 'http://instagr.am/p/%s/media/?size=l', $matches[3] ); 
  12. 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 ) ); 
  13.  
  14. $atts = shortcode_atts( array( 
  15. 'width' => isset( $content_width ) ? $content_width : $max_width,  
  16. 'hidecaption' => false,  
  17. ), $atts ); 
  18.  
  19. $atts['width'] = absint( $atts['width'] ); 
  20. if ( $atts['width'] > $max_width || $min_width > $atts['width'] ) 
  21. $atts['width'] = $max_width; 
  22.  
  23.  
  24. // remove the modal param from the URL 
  25. $url = remove_query_arg( 'modal', $url ); 
  26.  
  27. // force .com instead of .am for https support 
  28. $url = str_replace( 'instagr.am', 'instagram.com', $url ); 
  29.  
  30. // The oembed endpoint expects HTTP, but HTTPrequests301 to HTTPS 
  31. $instagram_http_url = str_replace( 'https://', 'http://', $url ); 
  32. $instagram_https_url = str_replace( 'http://', 'https://', $url ); 
  33.  
  34. $url_args = array( 
  35. 'url' => $instagram_http_url,  
  36. 'maxwidth' => $atts['width'],  
  37. ); 
  38.  
  39. if ( $atts['hidecaption'] ) { 
  40. $url_args['hidecaption'] = 'true'; 
  41.  
  42. $url = esc_url_raw( add_query_arg( $url_args, 'https://api.instagram.com/oembed/' ) ); 
  43.  
  44. /** 
  45. * Filter Object Caching for response from Instagram. 
  46. * Allow enabling of object caching for the response sent by Instagram when querying for Instagram image HTML. 
  47. * @module shortcodes 
  48. * @since 3.3.0 
  49. * @param bool false Object caching is off by default. 
  50. * @param array $matches Array of Instagram URLs found in the post. 
  51. * @param array $atts Instagram Shortcode attributes. 
  52. * @param string $passed_url Instagram API URL. 
  53. */ 
  54. $response_body_use_cache = apply_filters( 'instagram_cache_oembed_api_response_body', false, $matches, $atts, $passed_url ); 
  55. $response_body = false; 
  56. if ( $response_body_use_cache ) { 
  57. $cache_key = 'oembed_response_body_' . md5( $url ); 
  58. $response_body = wp_cache_get( $cache_key, 'instagram_embeds' ); 
  59.  
  60. if ( ! $response_body ) { 
  61. // Not using cache (default case) or cache miss 
  62. $instagram_response = wp_remote_get( $url, array( 'redirection' => 0 ) ); 
  63. if ( is_wp_error( $instagram_response ) || 200 != $instagram_response['response']['code'] || empty( $instagram_response['body'] ) ) { 
  64. return "<!-- instagramerror invalid instagram resource -->"; 
  65.  
  66. $response_body = json_decode( $instagram_response['body'] ); 
  67. if ( $response_body_use_cache ) { 
  68. // if caching it is short-lived since this is a "Cache-Control: no-cache" resource 
  69. wp_cache_set( $cache_key, $response_body, 'instagram_embeds', HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) ); 
  70.  
  71. if ( ! empty( $response_body->html ) ) { 
  72. wp_enqueue_script( 'jetpack-instagram-embed', plugins_url( 'js/instagram.js', __FILE__ ), array( 'jquery' ), false, true ); 
  73. // there's a script in the response, which we strip on purpose since it's added by this ^ script 
  74. $ig_embed = preg_replace( '@<(script)[^>]*?>.*?</\\1>@si', '', $response_body->html ); 
  75. return $ig_embed; 
  76.  
  77. return '<!-- instagramerror no embed found -->';