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. // Set up widget values 
  97. $instance['like_args'] = array( 
  98. 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),  
  99. 'height' => (int) $new_instance['height'],  
  100. 'show_faces' => isset( $new_instance['show_faces'] ),  
  101. 'stream' => isset( $new_instance['stream'] ),  
  102. 'cover' => isset( $new_instance['cover'] ),  
  103. ); 
  104.  
  105. $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] ); 
  106.  
  107. return $instance; 
  108.  
  109. function form( $instance ) { 
  110. $instance = wp_parse_args( (array) $instance, array( 
  111. 'title' => '',  
  112. 'like_args' => $this->get_default_args() 
  113. ) ); 
  114. $like_args = $this->normalize_facebook_args( $instance['like_args'] ); 
  115. ?> 
  116.  
  117. <p> 
  118. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  119. <?php _e( 'Title', 'jetpack' ); ?> 
  120. <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" /> 
  121. </label> 
  122. </p> 
  123.  
  124. <p> 
  125. <label for="<?php echo $this->get_field_id( 'href' ); ?>"> 
  126. <?php _e( 'Facebook Page URL', 'jetpack' ); ?> 
  127. <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" /> 
  128. <br /> 
  129. <small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small> 
  130. </label> 
  131. </p> 
  132.  
  133. <p> 
  134. <label for="<?php echo $this->get_field_id( 'height' ); ?>"> 
  135. <?php _e( 'Height', 'jetpack' ); ?> 
  136. <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 
  137. </label> 
  138. </p> 
  139.  
  140. <p> 
  141. <label for="<?php echo $this->get_field_id( 'show_faces' ); ?>"> 
  142. <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'] ); ?> /> 
  143. <?php _e( 'Show Faces', 'jetpack' ); ?> 
  144. <br /> 
  145. <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small> 
  146. </label> 
  147. </p> 
  148.  
  149. <p> 
  150. <label for="<?php echo $this->get_field_id( 'stream' ); ?>"> 
  151. <input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> /> 
  152. <?php _e( 'Show Stream', 'jetpack' ); ?> 
  153. <br /> 
  154. <small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small> 
  155. </label> 
  156. </p> 
  157.  
  158. <p> 
  159. <label for="<?php echo $this->get_field_id( 'cover' ); ?>"> 
  160. <input type="checkbox" name="<?php echo $this->get_field_name( 'cover' ); ?>" id="<?php echo $this->get_field_id( 'cover' ); ?>" <?php checked( $like_args['cover'] ); ?> /> 
  161. <?php _e( 'Show Cover Photo', 'jetpack' ); ?> 
  162. <br /> 
  163. </label> 
  164. </p> 
  165.  
  166. <?php 
  167.  
  168. function get_default_args() { 
  169. $defaults = array( 
  170. 'href' => '',  
  171. 'height' => $this->default_height,  
  172. 'show_faces' => 'true',  
  173. 'stream' => '',  
  174. 'cover' => 'true',  
  175. ); 
  176.  
  177. /** 
  178. * Filter Facebook Likebox default options. 
  179. * @module widgets 
  180. * @since 1.3.1 
  181. * @param array $defaults Array of default options. 
  182. */ 
  183. return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults ); 
  184.  
  185. function normalize_facebook_args( $args ) { 
  186. $args = wp_parse_args( (array) $args, $this->get_default_args() ); 
  187.  
  188. // Validate the Facebook Page URL 
  189. if ( $this->is_valid_facebook_url( $args['href'] ) ) { 
  190. $temp = explode( '?', $args['href'] ); 
  191. $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); 
  192. } else { 
  193. $args['href'] = ''; 
  194.  
  195. $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height ); 
  196. $args['show_faces'] = (bool) $args['show_faces']; 
  197. $args['stream'] = (bool) $args['stream']; 
  198. $args['cover'] = (bool) $args['cover']; 
  199.  
  200. // The height used to be dependent on other widget settings 
  201. // If the user changes those settings but doesn't customize the height,  
  202. // let's intelligently assign a new height. 
  203. if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) { 
  204. if ( $args['show_faces'] && $args['stream'] ) { 
  205. $args['height'] = 580; 
  206. } else if ( ! $args['show_faces'] && ! $args['stream'] ) { 
  207. $args['height'] = 130; 
  208. } else { 
  209. $args['height'] = 432; 
  210.  
  211. return $args; 
  212.  
  213. function is_valid_facebook_url( $url ) { 
  214. return ( FALSE !== strpos( $url, 'facebook.com' ) ) ? TRUE : FALSE; 
  215.  
  216. function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { 
  217. $value = (int) $value; 
  218.  
  219. if ( $max < $value || $min > $value ) 
  220. $value = $default; 
  221.  
  222. return (int) $value; 
  223.  
  224. function normalize_text_value( $value, $default = '', $allowed = array() ) { 
  225. $allowed = (array) $allowed; 
  226.  
  227. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) 
  228. $value = $default; 
  229.  
  230. return $value; 
  231.  
  232. function guess_locale_from_lang( $lang ) { 
  233. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) { 
  234. return 'en_US'; 
  235.  
  236. if ( !class_exists( 'GP_Locales' ) ) { 
  237. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { 
  238. return false; 
  239.  
  240. require JETPACK__GLOTPRESS_LOCALES_PATH; 
  241.  
  242. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  243. // WP.com: get_locale() returns 'it' 
  244. $locale = GP_Locales::by_slug( $lang ); 
  245. } else { 
  246. // Jetpack: get_locale() returns 'it_IT'; 
  247. $locale = GP_Locales::by_field( 'facebook_locale', $lang ); 
  248.  
  249. if ( ! $locale || empty( $locale->facebook_locale ) ) { 
  250. return 'en_US'; // Facebook requires a locale when pulling their SDK. 
  251.  
  252. return $locale->facebook_locale; 
  253.  
  254. function get_locale() { 
  255. return $this->guess_locale_from_lang( get_locale() );