Geen omschrijving

facebook-likebox.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /**
  53. * Display the widget.
  54. *
  55. * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  56. * @param array $instance The settings for the particular instance of the widget.
  57. */
  58. public function widget( $args, $instance ) {
  59. $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
  60. $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
  61. $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
  62. $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
  63. $like_args = $this->get_default_args();
  64. if ( isset( $instance['like_args'] ) ) {
  65. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  66. }
  67. if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) {
  68. if ( current_user_can( 'edit_theme_options' ) ) {
  69. echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  70. $error_link = wp_kses(
  71. sprintf(
  72. /* translators: %s: link to widgets administration screen. */
  73. __( 'It looks like your Facebook URL is incorrectly configured. Please check it in your <a href="%1$s">widget settings</a>.', 'jetpack' ),
  74. esc_url( admin_url( 'widgets.php' ) )
  75. ),
  76. array( 'a' => array( 'href' => array() ) )
  77. );
  78. printf(
  79. '<p>%s</p>',
  80. $error_link // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  81. );
  82. echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  83. }
  84. echo '<!-- Invalid Facebook Page URL -->';
  85. return;
  86. }
  87. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  88. $title = apply_filters( 'widget_title', $instance['title'] );
  89. $page_url = set_url_scheme( $like_args['href'], 'https' );
  90. $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false';
  91. $like_args['stream'] = (bool) $like_args['stream'] ? 'timeline' : 'false';
  92. $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true';
  93. $like_args['small_header'] = (bool) $like_args['small_header'] ? 'true' : 'false';
  94. /**
  95. * Filter Facebook Likebox's widget call to action button
  96. *
  97. * @module widgets
  98. *
  99. * @since 8.4.0
  100. *
  101. * @param bool True value hides the call to action button
  102. */
  103. $hide_cta = apply_filters( 'jetpack_facebook_likebox_hide_cta', false );
  104. echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  105. if ( ! empty( $title ) ) :
  106. echo $before_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  107. $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>';
  108. /**
  109. * Filter Facebook Likebox's widget title.
  110. *
  111. * @module widgets
  112. *
  113. * @since 3.3.0
  114. *
  115. * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL).
  116. * @param string $title Widget title as set in the widget settings.
  117. * @param string $page_url Facebook Page URL.
  118. */
  119. $likebox_widget_title = apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
  120. echo wp_kses(
  121. $likebox_widget_title,
  122. array( 'a' => array( 'href' => array() ) )
  123. );
  124. echo $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  125. endif;
  126. ?>
  127. <div id="fb-root"></div>
  128. <div class="fb-page" data-href="<?php echo esc_url( $page_url ); ?>" data-width="<?php echo (int) $like_args['width']; ?>" data-height="<?php echo (int) $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-tabs="<?php echo esc_attr( $like_args['stream'] ); ?>" data-hide-cta="<?php echo esc_attr( $hide_cta ? 'true' : 'false' ); ?>" data-small-header="<?php echo esc_attr( $like_args['small_header'] ); ?>">
  129. <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>
  130. </div>
  131. <?php
  132. wp_enqueue_script( 'jetpack-facebook-embed' );
  133. echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  134. /** This action is documented in modules/widgets/gravatar-profile.php */
  135. do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' );
  136. }
  137. function update( $new_instance, $old_instance ) {
  138. $instance = array();
  139. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
  140. // Set up widget values.
  141. $instance['like_args'] = array(
  142. 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),
  143. 'width' => (int) $new_instance['width'],
  144. 'height' => (int) $new_instance['height'],
  145. 'show_faces' => isset( $new_instance['show_faces'] ),
  146. 'stream' => isset( $new_instance['stream'] ),
  147. 'cover' => isset( $new_instance['cover'] ),
  148. 'small_header' => isset( $new_instance['small_header'] ),
  149. );
  150. $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
  151. // Include the new instance's args in the array's top level to support updating from the Widgets page.
  152. $instance = array_merge( $instance, array_intersect_key( $instance['like_args'], $new_instance ) );
  153. return $instance;
  154. }
  155. function form( $instance ) {
  156. $instance = wp_parse_args(
  157. (array) $instance, array(
  158. 'title' => '',
  159. 'like_args' => $this->get_default_args(),
  160. )
  161. );
  162. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  163. ?>
  164. <p>
  165. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
  166. <?php esc_html_e( 'Title', 'jetpack' ); ?>
  167. <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" />
  168. </label>
  169. </p>
  170. <p>
  171. <label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>">
  172. <?php esc_html_e( 'Facebook Page URL', 'jetpack' ); ?>
  173. <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" />
  174. <br />
  175. <small><?php esc_html_e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
  176. </label>
  177. </p>
  178. <p>
  179. <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>">
  180. <?php esc_html_e( 'Width in pixels', 'jetpack' ); ?>
  181. <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;" />
  182. <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_width ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_width ); ?></small>
  183. </label>
  184. </p>
  185. <p>
  186. <label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>">
  187. <?php esc_html_e( 'Height in pixels', 'jetpack' ); ?>
  188. <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;" />
  189. <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_height ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_height ); ?></small>
  190. </label>
  191. </p>
  192. <p>
  193. <label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>">
  194. <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'] ); ?> />
  195. <?php esc_html_e( 'Show Faces', 'jetpack' ); ?>
  196. <br />
  197. <small><?php esc_html_e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
  198. </label>
  199. </p>
  200. <p>
  201. <label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>">
  202. <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'] ); ?> />
  203. <?php esc_html_e( 'Show Timeline', 'jetpack' ); ?>
  204. <br />
  205. <small><?php esc_html_e( 'Show Page Posts.', 'jetpack' ); ?></small>
  206. </label>
  207. </p>
  208. <p>
  209. <label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>">
  210. <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'] ); ?> />
  211. <?php esc_html_e( 'Show Cover Photo', 'jetpack' ); ?>
  212. <br />
  213. </label>
  214. </p>
  215. <p>
  216. <label for="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>">
  217. <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'small_header' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>" <?php checked( $like_args['small_header'] ); ?> />
  218. <?php esc_html_e( 'Use Small Header', 'jetpack' ); ?>
  219. <br />
  220. </label>
  221. </p>
  222. <?php
  223. }
  224. function get_default_args() {
  225. $defaults = array(
  226. 'href' => '',
  227. 'width' => $this->default_width,
  228. 'height' => $this->default_height,
  229. 'show_faces' => 'true',
  230. 'stream' => '',
  231. 'cover' => 'true',
  232. 'small_header' => '',
  233. );
  234. /**
  235. * Filter Facebook Likebox default options.
  236. *
  237. * @module widgets
  238. *
  239. * @since 1.3.1
  240. *
  241. * @param array $defaults Array of default options.
  242. */
  243. return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
  244. }
  245. function normalize_facebook_args( $args ) {
  246. $args = wp_parse_args( (array) $args, $this->get_default_args() );
  247. // Validate the Facebook Page URL
  248. if ( $this->is_valid_facebook_url( $args['href'] ) ) {
  249. $temp = explode( '?', $args['href'] );
  250. $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
  251. } else {
  252. $args['href'] = '';
  253. }
  254. $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width );
  255. $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
  256. $args['show_faces'] = (bool) $args['show_faces'];
  257. $args['stream'] = (bool) $args['stream'];
  258. $args['cover'] = (bool) $args['cover'];
  259. $args['small_header'] = (bool) $args['small_header'];
  260. // The height used to be dependent on other widget settings
  261. // If the user changes those settings but doesn't customize the height,
  262. // let's intelligently assign a new height.
  263. if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
  264. if ( $args['show_faces'] && $args['stream'] ) {
  265. $args['height'] = 580;
  266. } elseif ( ! $args['show_faces'] && ! $args['stream'] ) {
  267. $args['height'] = 130;
  268. } else {
  269. $args['height'] = 432;
  270. }
  271. }
  272. return $args;
  273. }
  274. function is_valid_facebook_url( $url ) {
  275. return ( false !== strpos( $url, 'facebook.com' ) ) ? true : false;
  276. }
  277. function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) {
  278. $value = (int) $value;
  279. if ( $value > $max ) {
  280. $value = $max;
  281. } elseif ( $value < $min ) {
  282. $value = $min;
  283. }
  284. return (int) $value;
  285. }
  286. function normalize_text_value( $value, $default = '', $allowed = array() ) {
  287. $allowed = (array) $allowed;
  288. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
  289. $value = $default;
  290. }
  291. return $value;
  292. }
  293. /**
  294. * @deprecated
  295. */
  296. function guess_locale_from_lang( $lang ) {
  297. _deprecated_function( __METHOD__, '4.0.0', 'Jetpack::guess_locale_from_lang()' );
  298. Jetpack::$instance->guess_locale_from_lang( $lang );
  299. }
  300. /**
  301. * @deprecated
  302. */
  303. function get_locale() {
  304. _deprecated_function( __METHOD__, '4.0.0', 'Jetpack::get_locale()' );
  305. Jetpack::$instance->get_locale();
  306. }
  307. }