Share_Facebook

The Jetpack by WordPress.com Share Facebook class.

Defined (1)

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

/modules/sharedaddy/sharing-sources.php  
  1. class Share_Facebook extends Sharing_Source { 
  2. public $shortname = 'facebook'; 
  3. public $genericon = '\f204'; 
  4. private $share_type = 'default'; 
  5.  
  6. public function __construct( $id, array $settings ) { 
  7. parent::__construct( $id, $settings ); 
  8.  
  9. if ( isset( $settings['share_type'] ) ) 
  10. $this->share_type = $settings['share_type']; 
  11.  
  12. if ( 'official' == $this->button_style ) 
  13. $this->smart = true; 
  14. else 
  15. $this->smart = false; 
  16.  
  17. public function get_name() { 
  18. return __( 'Facebook', 'jetpack' ); 
  19.  
  20. public function display_header() { 
  21.  
  22. function guess_locale_from_lang( $lang ) { 
  23. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) { 
  24. return 'en_US'; 
  25.  
  26. if ( !class_exists( 'GP_Locales' ) ) { 
  27. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { 
  28. return false; 
  29.  
  30. require JETPACK__GLOTPRESS_LOCALES_PATH; 
  31.  
  32. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  33. // WP.com: get_locale() returns 'it' 
  34. $locale = GP_Locales::by_slug( $lang ); 
  35. } else { 
  36. // Jetpack: get_locale() returns 'it_IT'; 
  37. $locale = GP_Locales::by_field( 'wp_locale', $lang ); 
  38.  
  39. if ( ! $locale ) { 
  40. return false; 
  41.  
  42. if ( empty( $locale->facebook_locale ) ) { 
  43. if ( empty( $locale->wp_locale ) ) { 
  44. return false; 
  45. } else { 
  46. // Facebook SDK is smart enough to fall back to en_US if a 
  47. // locale isn't supported. Since supported Facebook locales 
  48. // can fall out of sync, we'll attempt to use the known 
  49. // wp_locale value and rely on said fallback. 
  50. return $locale->wp_locale; 
  51.  
  52. return $locale->facebook_locale; 
  53.  
  54. public function get_display( $post ) { 
  55. if ( $this->smart ) { 
  56. $share_url = $this->get_share_url( $post->ID ); 
  57. $fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>'; 
  58. /** 
  59. * Filter the output of the Facebook Sharing button. 
  60. * @module sharedaddy 
  61. * @since 3.6.0 
  62. * @param string $fb_share_html Facebook Sharing button HTML. 
  63. * @param string $share_url URL of the post to share. 
  64. */ 
  65. return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url ); 
  66.  
  67. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */ 
  68. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) { 
  69. sharing_register_post_for_share_counts( $post->ID ); 
  70. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID ); 
  71.  
  72. public function process_request( $post, array $post_data ) { 
  73. $fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) ); 
  74.  
  75. // Record stats 
  76. parent::process_request( $post, $post_data ); 
  77.  
  78. // Redirect to Facebook 
  79. wp_redirect( $fb_url ); 
  80. die(); 
  81.  
  82. public function display_footer() { 
  83. $this->js_dialog( $this->shortname ); 
  84. if ( $this->smart ) { 
  85. $locale = $this->guess_locale_from_lang( get_locale() ); 
  86. if ( ! $locale ) { 
  87. $locale = 'en_US'; 
  88. /** 
  89. * Filter the App ID used in the official Facebook Share button. 
  90. * @since 3.8.0 
  91. * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID). 
  92. */ 
  93. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); 
  94. if ( is_numeric( $fb_app_id ) ) { 
  95. $fb_app_id = '&appId=' . $fb_app_id; 
  96. } else { 
  97. $fb_app_id = ''; 
  98. ?><div id="fb-root"></div> 
  99. <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = '//connect.facebook.net/<?php echo $locale; ?>/sdk.js#xfbml=1<?php echo $fb_app_id; ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> 
  100. <script> 
  101. jQuery( document.body ).on( 'post-load', function() { 
  102. if ( 'undefined' !== typeof FB ) { 
  103. FB.XFBML.parse(); 
  104. } ); 
  105. </script> 
  106. <?php