/modules/shortcodes/facebook.php

  1. <?php 
  2. /** 
  3. * Facebook embeds 
  4. */ 
  5.  
  6. define( 'JETPACK_FACEBOOK_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/(posts|photos)/([^/]+)?#' ); 
  7. define( 'JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/permalink.php\?([^\s]+)#' ); 
  8. define( 'JETPACK_FACEBOOK_PHOTO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/photo.php\?([^\s]+)#' ); 
  9. define( 'JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/photos/([^/]+)?#' ); 
  10. define( 'JETPACK_FACEBOOK_VIDEO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/video.php\?([^\s]+)#' ); 
  11. define( 'JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/videos/([^/]+)?#' ); 
  12.  
  13.  
  14. // Example URL: https://www.facebook.com/VenusWilliams/posts/10151647007373076 
  15. wp_embed_register_handler( 'facebook', JETPACK_FACEBOOK_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  16.  
  17. // Example URL: https://www.facebook.com/permalink.php?id=222622504529111&story_fbid=559431180743788 
  18. wp_embed_register_handler( 'facebook-alternate', JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  19.  
  20. // Photos are handled on a different endpoint; e.g. https://www.facebook.com/photo.php?fbid=10151609960150073&set=a.398410140072.163165.106666030072&type=1 
  21. wp_embed_register_handler( 'facebook-photo', JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  22.  
  23. // Photos (from pages for example) can be at 
  24. wp_embed_register_handler( 'facebook-alternate-photo', JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  25.  
  26. // Videos e.g. https://www.facebook.com/video.php?v=772471122790796 
  27. wp_embed_register_handler( 'facebook-video', JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  28. // Videos https://www.facebook.com/WhiteHouse/videos/10153398464269238/ 
  29. wp_embed_register_handler( 'facebook-alternate-video', JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' ); 
  30.  
  31. function jetpack_facebook_embed_handler( $matches, $attr, $url ) { 
  32. if ( false !== strpos( $url, 'video.php' ) || false !== strpos( $url, '/videos/' ) ) { 
  33. $embed = sprintf( '<div class="fb-video" data-allowfullscreen="true" data-href="%s"></div>', esc_url( $url ) ); 
  34. } else { 
  35. $embed = sprintf( '<fb:post href="%s"></fb:post>', esc_url( $url ) ); 
  36.  
  37. // since Facebook is a faux embed, we need to load the JS SDK in the wpview embed iframe 
  38. if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_POST['action'] ) && 'parse-embed' == $_POST['action'] ) { 
  39. return $embed . '<script src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>'; 
  40. } else { 
  41. wp_enqueue_script( 'jetpack-facebook-embed', plugins_url( 'js/facebook.js', __FILE__ ), array( 'jquery' ), null, true ); 
  42. /** This filter is documented in modules/sharedaddy/sharing-sources.php */ 
  43. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); 
  44. if ( ! is_numeric( $fb_app_id ) ) { 
  45. $fb_app_id = ''; 
  46. wp_localize_script( 
  47. 'jetpack-facebook-embed',  
  48. 'jpfbembed',  
  49. array( 
  50. 'appid' => $fb_app_id 
  51. ); 
  52. return $embed; 
  53.  
  54. add_shortcode( 'facebook', 'jetpack_facebook_shortcode_handler' ); 
  55.  
  56. function jetpack_facebook_shortcode_handler( $atts ) { 
  57. global $wp_embed; 
  58.  
  59. if ( empty( $atts['url'] ) ) 
  60. return; 
  61.  
  62. if ( ! preg_match( JETPACK_FACEBOOK_EMBED_REGEX, $atts['url'] ) 
  63. && ! preg_match( JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, $atts['url'] ) 
  64. && ! preg_match( JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, $atts['url'] ) 
  65. && ! preg_match( JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, $atts['url'] ) ) { 
  66. return; 
  67.  
  68. return $wp_embed->shortcode( $atts, $atts['url'] ); 
.