WPCOM_Widget_Facebook_LikeBox

Facebook Page Plugin (formerly known as the Like Box) Display a Facebook Page Plugin as a widget (replaces the old like box plugin) https://developers.facebook.com/docs/plugins/page-plugin.

Defined (1)

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

/modules/widgets/facebook-likebox.php  
  1. class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { 
  2.  
  3. private $default_height = 580; 
  4. private $max_height = 9999; 
  5. private $min_height = 130; 
  6.  
  7. function __construct() { 
  8. parent::__construct( 
  9. 'facebook-likebox',  
  10. /** 
  11. * Filter the name of a widget included in the Extra Sidebar Widgets module. 
  12. * @module widgets 
  13. * @since 2.1.2 
  14. * @param string $widget_name Widget title. 
  15. */ 
  16. apply_filters( 'jetpack_widget_name', __( 'Facebook Like Box', 'jetpack' ) ),  
  17. array( 
  18. 'classname' => 'widget_facebook_likebox',  
  19. 'description' => __( 'Display a Facebook Like Box to connect visitors to your Facebook Page', 'jetpack' ) 
  20. ); 
  21.  
  22. function widget( $args, $instance ) { 
  23.  
  24. extract( $args ); 
  25.  
  26. $like_args = $this->normalize_facebook_args( $instance['like_args'] ); 
  27.  
  28. wp_enqueue_style( 'jetpack_facebook_likebox', plugins_url( 'facebook-likebox/style.css', __FILE__ ) ); 
  29. wp_style_add_data( 'jetpack_facebook_likebox', 'jetpack-inline', true ); 
  30.  
  31. if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) { 
  32. if ( current_user_can('edit_theme_options') ) { 
  33. echo $before_widget; 
  34. echo '<p>' . sprintf( __( 'It looks like your Facebook URL is incorrectly configured. Please check it in your <a href="%s">widget settings</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; 
  35. echo $after_widget; 
  36. echo '<!-- Invalid Facebook Page URL -->'; 
  37. return; 
  38.  
  39. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  40. $title = apply_filters( 'widget_title', $instance['title'] ); 
  41. $page_url = set_url_scheme( $like_args['href'], 'https' ); 
  42.  
  43. $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false'; 
  44. $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false'; 
  45. $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true'; 
  46.  
  47. $locale = $this->get_locale(); 
  48.  
  49. /** This filter is documented in modules/sharedaddy/sharing-sources.php */ 
  50. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); 
  51. if ( is_numeric( $fb_app_id ) ) { 
  52. $fb_app_id = '&appId=' . $fb_app_id; 
  53. } else { 
  54. $fb_app_id = ''; 
  55.  
  56. echo $before_widget; 
  57.  
  58. if ( ! empty( $title ) ) : 
  59. echo $before_title; 
  60.  
  61. $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>'; 
  62.  
  63. /** 
  64. * Filter Facebook Likebox's widget title. 
  65. * @module widgets 
  66. * @since 3.3.0 
  67. * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL). 
  68. * @param string $title Widget title as set in the widget settings. 
  69. * @param string $page_url Facebook Page URL. 
  70. */ 
  71. echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url ); 
  72.  
  73. echo $after_title; 
  74. endif; 
  75.  
  76. ?> 
  77. <div id="fb-root"></div> 
  78. <div class="fb-page" data-href="<?php echo esc_url( $page_url ); ?>" data-height="<?php echo intval( $like_args['height'] ); ?>" data-hide-cover="<?php echo esc_attr( $like_args['cover'] ); ?>" data-show-facepile="<?php echo esc_attr( $like_args['show_faces'] ); ?>" data-show-posts="<?php echo esc_attr( $like_args['stream'] ); ?>"> 
  79. <div class="fb-xfbml-parse-ignore"><blockquote cite="<?php echo esc_url( $page_url ); ?>"><a href="<?php echo esc_url( $page_url ); ?>"><?php echo esc_html( $title ); ?></a></blockquote></div> 
  80. </div> 
  81. <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 esc_html( $locale ); ?>/sdk.js#xfbml=1<?php echo $fb_app_id; ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> 
  82. <?php 
  83. echo $after_widget; 
  84.  
  85. /** This action is already documented in modules/widgets/gravatar-profile.php */ 
  86. do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' ); 
  87.  
  88. function update( $new_instance, $old_instance ) { 
  89. $instance = array( 
  90. 'title' => '',  
  91. 'like_args' => $this->get_default_args(),  
  92. ); 
  93.  
  94. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) ); 
  95.  
  96. $new_instance = wp_parse_args( $new_instance, $this->get_default_args() ); 
  97.  
  98. // Set up widget values 
  99. $instance['like_args'] = array( 
  100. 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),  
  101. 'height' => (int) $new_instance['height'],  
  102. 'show_faces' => (bool) $new_instance['show_faces'],  
  103. 'stream' => (bool) $new_instance['stream'],  
  104. 'cover' => (bool) $new_instance['cover'],  
  105. ); 
  106.  
  107. $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] ); 
  108.  
  109. return $instance; 
  110.  
  111. function form( $instance ) { 
  112. $instance = wp_parse_args( (array) $instance, array( 
  113. 'title' => '',  
  114. 'like_args' => $this->get_default_args() 
  115. ) ); 
  116. $like_args = $this->normalize_facebook_args( $instance['like_args'] ); 
  117. ?> 
  118.  
  119. <p> 
  120. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  121. <?php _e( 'Title', 'jetpack' ); ?> 
  122. <input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" /> 
  123. </label> 
  124. </p> 
  125.  
  126. <p> 
  127. <label for="<?php echo $this->get_field_id( 'href' ); ?>"> 
  128. <?php _e( 'Facebook Page URL', 'jetpack' ); ?> 
  129. <input type="text" name="<?php echo $this->get_field_name( 'href' ); ?>" id="<?php echo $this->get_field_id( 'href' ); ?>" value="<?php echo esc_url( $like_args['href'] ); ?>" class="widefat" /> 
  130. <br /> 
  131. <small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small> 
  132. </label> 
  133. </p> 
  134.  
  135. <p> 
  136. <label for="<?php echo $this->get_field_id( 'height' ); ?>"> 
  137. <?php _e( 'Height', 'jetpack' ); ?> 
  138. <input type="number" class="smalltext" min="1" max="999" maxlength="3" name="<?php echo $this->get_field_name( 'height' ); ?>" id="<?php echo $this->get_field_id( 'height' ); ?>" value="<?php echo esc_attr( $like_args['height'] ); ?>" style="text-align: center;" />px 
  139. </label> 
  140. </p> 
  141.  
  142. <p> 
  143. <label for="<?php echo $this->get_field_id( 'show_faces' ); ?>"> 
  144. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_faces' ); ?>" id="<?php echo $this->get_field_id( 'show_faces' ); ?>" <?php checked( $like_args['show_faces'] ); ?> /> 
  145. <?php _e( 'Show Faces', 'jetpack' ); ?> 
  146. <br /> 
  147. <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small> 
  148. </label> 
  149. </p> 
  150.  
  151. <p> 
  152. <label for="<?php echo $this->get_field_id( 'stream' ); ?>"> 
  153. <input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> /> 
  154. <?php _e( 'Show Stream', 'jetpack' ); ?> 
  155. <br /> 
  156. <small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small> 
  157. </label> 
  158. </p> 
  159.  
  160. <p> 
  161. <label for="<?php echo $this->get_field_id( 'cover' ); ?>"> 
  162. <input type="checkbox" name="<?php echo $this->get_field_name( 'cover' ); ?>" id="<?php echo $this->get_field_id( 'cover' ); ?>" <?php checked( $like_args['cover'] ); ?> /> 
  163. <?php _e( 'Show Cover Photo', 'jetpack' ); ?> 
  164. <br /> 
  165. </label> 
  166. </p> 
  167.  
  168. <?php 
  169.  
  170. function get_default_args() { 
  171. $defaults = array( 
  172. 'href' => '',  
  173. 'height' => $this->default_height,  
  174. 'show_faces' => true,  
  175. 'stream' => false,  
  176. 'cover' => true,  
  177. ); 
  178.  
  179. /** 
  180. * Filter Facebook Likebox default options. 
  181. * @module widgets 
  182. * @since 1.3.1 
  183. * @param array $defaults Array of default options. 
  184. */ 
  185. return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults ); 
  186.  
  187. function normalize_facebook_args( $args ) { 
  188. $args = wp_parse_args( (array) $args, $this->get_default_args() ); 
  189.  
  190. // Validate the Facebook Page URL 
  191. if ( $this->is_valid_facebook_url( $args['href'] ) ) { 
  192. $temp = explode( '?', $args['href'] ); 
  193. $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); 
  194. } else { 
  195. $args['href'] = ''; 
  196.  
  197. $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height ); 
  198. $args['show_faces'] = (bool) $args['show_faces']; 
  199. $args['stream'] = (bool) $args['stream']; 
  200. $args['cover'] = (bool) $args['cover']; 
  201.  
  202. // The height used to be dependent on other widget settings 
  203. // If the user changes those settings but doesn't customize the height,  
  204. // let's intelligently assign a new height. 
  205. if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) { 
  206. if ( $args['show_faces'] && $args['stream'] ) { 
  207. $args['height'] = 580; 
  208. } else if ( ! $args['show_faces'] && ! $args['stream'] ) { 
  209. $args['height'] = 130; 
  210. } else { 
  211. $args['height'] = 432; 
  212.  
  213. return $args; 
  214.  
  215. function is_valid_facebook_url( $url ) { 
  216. return ( FALSE !== strpos( $url, 'facebook.com' ) ) ? TRUE : FALSE; 
  217.  
  218. function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { 
  219. $value = (int) $value; 
  220.  
  221. if ( $max < $value || $min > $value ) 
  222. $value = $default; 
  223.  
  224. return (int) $value; 
  225.  
  226. function normalize_text_value( $value, $default = '', $allowed = array() ) { 
  227. $allowed = (array) $allowed; 
  228.  
  229. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) 
  230. $value = $default; 
  231.  
  232. return $value; 
  233.  
  234. function guess_locale_from_lang( $lang ) { 
  235. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) { 
  236. return 'en_US'; 
  237.  
  238. if ( !class_exists( 'GP_Locales' ) ) { 
  239. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { 
  240. return false; 
  241.  
  242. require JETPACK__GLOTPRESS_LOCALES_PATH; 
  243.  
  244. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  245. // WP.com: get_locale() returns 'it' 
  246. $locale = GP_Locales::by_slug( $lang ); 
  247. } else { 
  248. // Jetpack: get_locale() returns 'it_IT'; 
  249. $locale = GP_Locales::by_field( 'facebook_locale', $lang ); 
  250.  
  251. if ( ! $locale || empty( $locale->facebook_locale ) ) { 
  252. return 'en_US'; // Facebook requires a locale when pulling their SDK. 
  253.  
  254. return $locale->facebook_locale; 
  255.  
  256. function get_locale() { 
  257. return $this->guess_locale_from_lang( get_locale() );