/modules/widgets/facebook-likebox.php

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