facebook-likebox.php 11 KB

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