Brak opisu

facebook.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Facebook embeds
  4. *
  5. * @package automattic/jetpack
  6. */
  7. define( 'JETPACK_FACEBOOK_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/(posts|photos)/([^/]+)?#' );
  8. define( 'JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/permalink.php\?([^\s]+)#' );
  9. define( 'JETPACK_FACEBOOK_PHOTO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/photo.php\?([^\s]+)#' );
  10. define( 'JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/photos/([^/]+)?#' );
  11. define( 'JETPACK_FACEBOOK_VIDEO_EMBED_REGEX', '#^https?://(www.)?facebook\.com/(?:video.php|watch\/?)\?([^\s]+)#' );
  12. define( 'JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX', '#^https?://(www.)?facebook\.com/([^/]+)/videos/([^/]+)?#' );
  13. /*
  14. * Example URL: https://www.facebook.com/VenusWilliams/posts/10151647007373076
  15. */
  16. wp_embed_register_handler( 'facebook', JETPACK_FACEBOOK_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  17. /*
  18. * Example URL: https://www.facebook.com/permalink.php?id=222622504529111&story_fbid=559431180743788
  19. */
  20. wp_embed_register_handler( 'facebook-alternate', JETPACK_FACEBOOK_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  21. /*
  22. * Photos are handled on a different endpoint; e.g. https://www.facebook.com/photo.php?fbid=10151609960150073&set=a.398410140072.163165.106666030072&type=1
  23. */
  24. wp_embed_register_handler( 'facebook-photo', JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  25. /*
  26. * Photos (from pages for example) can be at
  27. */
  28. wp_embed_register_handler( 'facebook-alternate-photo', JETPACK_FACEBOOK_PHOTO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  29. /*
  30. * Videos
  31. *
  32. * Formats:
  33. * https://www.facebook.com/video.php?v=2836814009877992
  34. * https://www.facebook.com/watch/?v=2836814009877992
  35. */
  36. wp_embed_register_handler( 'facebook-video', JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  37. /*
  38. * Videos https://www.facebook.com/WhiteHouse/videos/10153398464269238/
  39. */
  40. wp_embed_register_handler( 'facebook-alternate-video', JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, 'jetpack_facebook_embed_handler' );
  41. /**
  42. * Callback to modify output of embedded Facebook posts.
  43. *
  44. * @param array $matches Regex partial matches against the URL passed.
  45. * @param array $attr Attributes received in embed response.
  46. * @param string $url Requested URL to be embedded.
  47. * @return string Facebook embed markup.
  48. */
  49. function jetpack_facebook_embed_handler( $matches, $attr, $url ) {
  50. if (
  51. false !== strpos( $url, 'video.php' )
  52. || false !== strpos( $url, '/videos/' )
  53. || false !== strpos( $url, '/watch' )
  54. ) {
  55. $embed = sprintf( '<div class="fb-video" data-allowfullscreen="true" data-href="%s"></div>', esc_url( $url ) );
  56. } else {
  57. $width = 552; // As of 01/2017, the default width of Facebook embeds when no width attribute provided.
  58. global $content_width;
  59. if ( isset( $content_width ) ) {
  60. $width = min( $width, $content_width );
  61. }
  62. $embed = sprintf( '<div class="fb-post" data-href="%s" data-width="%s"></div>', esc_url( $url ), esc_attr( $width ) );
  63. }
  64. // Skip rendering scripts in an AMP context.
  65. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  66. return $embed;
  67. }
  68. // since Facebook is a faux embed, we need to load the JS SDK in the wpview embed iframe.
  69. if (
  70. defined( 'DOING_AJAX' )
  71. && DOING_AJAX
  72. // No need to check for a nonce here, that's already handled by Core further up.
  73. && ! empty( $_POST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
  74. && 'parse-embed' === $_POST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Missing
  75. ) {
  76. ob_start();
  77. wp_scripts()->do_items( array( 'jetpack-facebook-embed' ) );
  78. $scripts = ob_get_clean();
  79. return $embed . $scripts;
  80. } else {
  81. wp_enqueue_script( 'jetpack-facebook-embed' );
  82. return $embed;
  83. }
  84. }
  85. /**
  86. * Shortcode handler.
  87. *
  88. * @param array $atts Shortcode attributes.
  89. */
  90. function jetpack_facebook_shortcode_handler( $atts ) {
  91. global $wp_embed;
  92. if ( empty( $atts['url'] ) ) {
  93. return;
  94. }
  95. if ( ! preg_match( JETPACK_FACEBOOK_EMBED_REGEX, $atts['url'] )
  96. && ! preg_match( JETPACK_FACEBOOK_PHOTO_EMBED_REGEX, $atts['url'] )
  97. && ! preg_match( JETPACK_FACEBOOK_VIDEO_EMBED_REGEX, $atts['url'] )
  98. && ! preg_match( JETPACK_FACEBOOK_VIDEO_ALTERNATE_EMBED_REGEX, $atts['url'] ) ) {
  99. return;
  100. }
  101. return $wp_embed->shortcode( $atts, $atts['url'] );
  102. }
  103. add_shortcode( 'facebook', 'jetpack_facebook_shortcode_handler' );