PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/facebook-likebox.php

https://gitlab.com/Gashler/sg
PHP | 268 lines | 196 code | 56 blank | 16 comment | 32 complexity | 4c818a0a35f8e4e265085cac1f75a93f MD5 | raw file
  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 Like Box widget class
  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 $max_height = 9999;
  17. private $min_height = 130;
  18. function __construct() {
  19. parent::__construct(
  20. 'facebook-likebox',
  21. apply_filters( 'jetpack_widget_name', __( 'Facebook Like Box', 'jetpack' ) ),
  22. array(
  23. 'classname' => 'widget_facebook_likebox',
  24. 'description' => __( 'Display a Facebook Like Box to connect visitors to your Facebook Page', 'jetpack' )
  25. )
  26. );
  27. }
  28. function widget( $args, $instance ) {
  29. extract( $args );
  30. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  31. wp_enqueue_style( 'jetpack_facebook_likebox', plugins_url( 'facebook-likebox/style.css', __FILE__ ) );
  32. wp_style_add_data( 'jetpack_facebook_likebox', 'jetpack-inline', true );
  33. if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) {
  34. if ( current_user_can('edit_theme_options') ) {
  35. echo $before_widget;
  36. 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>';
  37. echo $after_widget;
  38. }
  39. echo '<!-- Invalid Facebook Page URL -->';
  40. return;
  41. }
  42. $title = apply_filters( 'widget_title', $instance['title'] );
  43. $page_url = set_url_scheme( $like_args['href'], 'https' );
  44. $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false';
  45. $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false';
  46. $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true';
  47. $locale = $this->get_locale();
  48. echo $before_widget;
  49. if ( ! empty( $title ) ) :
  50. echo $before_title;
  51. $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>';
  52. echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
  53. echo $after_title;
  54. endif;
  55. ?>
  56. <div id="fb-root"></div>
  57. <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'] ); ?>">
  58. <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>
  59. </div>
  60. <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&appId=249643311490&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
  61. <?php
  62. echo $after_widget;
  63. do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' );
  64. }
  65. function update( $new_instance, $old_instance ) {
  66. $instance = array(
  67. 'title' => '',
  68. 'like_args' => $this->get_default_args(),
  69. );
  70. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
  71. // Set up widget values
  72. $instance['like_args'] = array(
  73. 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),
  74. 'height' => (int) $new_instance['height'],
  75. 'show_faces' => (bool) $new_instance['show_faces'],
  76. 'stream' => (bool) $new_instance['stream'],
  77. 'cover' => (bool) $new_instance['cover'],
  78. );
  79. $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
  80. return $instance;
  81. }
  82. function form( $instance ) {
  83. $instance = wp_parse_args( (array) $instance, array(
  84. 'title' => '',
  85. 'like_args' => $this->get_default_args()
  86. ) );
  87. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  88. ?>
  89. <p>
  90. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  91. <?php _e( 'Title', 'jetpack' ); ?>
  92. <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" />
  93. </label>
  94. </p>
  95. <p>
  96. <label for="<?php echo $this->get_field_id( 'href' ); ?>">
  97. <?php _e( 'Facebook Page URL', 'jetpack' ); ?>
  98. <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" />
  99. <br />
  100. <small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
  101. </label>
  102. </p>
  103. <p>
  104. <label for="<?php echo $this->get_field_id( 'height' ); ?>">
  105. <?php _e( 'Height', 'jetpack' ); ?>
  106. <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
  107. </label>
  108. </p>
  109. <p>
  110. <label for="<?php echo $this->get_field_id( 'show_faces' ); ?>">
  111. <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'] ); ?> />
  112. <?php _e( 'Show Faces', 'jetpack' ); ?>
  113. <br />
  114. <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
  115. </label>
  116. </p>
  117. <p>
  118. <label for="<?php echo $this->get_field_id( 'stream' ); ?>">
  119. <input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> />
  120. <?php _e( 'Show Stream', 'jetpack' ); ?>
  121. <br />
  122. <small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small>
  123. </label>
  124. </p>
  125. <p>
  126. <label for="<?php echo $this->get_field_id( 'cover' ); ?>">
  127. <input type="checkbox" name="<?php echo $this->get_field_name( 'cover' ); ?>" id="<?php echo $this->get_field_id( 'cover' ); ?>" <?php checked( $like_args['cover'] ); ?> />
  128. <?php _e( 'Show Cover Photo', 'jetpack' ); ?>
  129. <br />
  130. </label>
  131. </p>
  132. <?php
  133. }
  134. function get_default_args() {
  135. $defaults = array(
  136. 'href' => '',
  137. 'height' => $this->default_height,
  138. 'show_faces' => true,
  139. 'stream' => false,
  140. 'cover' => true,
  141. );
  142. return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
  143. }
  144. function normalize_facebook_args( $args ) {
  145. $args = wp_parse_args( (array) $args, $this->get_default_args() );
  146. // Validate the Facebook Page URL
  147. if ( $this->is_valid_facebook_url( $args['href'] ) ) {
  148. $temp = explode( '?', $args['href'] );
  149. $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
  150. } else {
  151. $args['href'] = '';
  152. }
  153. $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
  154. $args['show_faces'] = (bool) $args['show_faces'];
  155. $args['stream'] = (bool) $args['stream'];
  156. $args['cover'] = (bool) $args['cover'];
  157. // The height used to be dependent on other widget settings
  158. // If the user changes those settings but doesn't customize the height,
  159. // let's intelligently assign a new height.
  160. if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
  161. if ( $args['show_faces'] && $args['stream'] ) {
  162. $args['height'] = 580;
  163. } else if ( ! $args['show_faces'] && ! $args['stream'] ) {
  164. $args['height'] = 130;
  165. } else {
  166. $args['height'] = 432;
  167. }
  168. }
  169. return $args;
  170. }
  171. function is_valid_facebook_url( $url ) {
  172. return ( FALSE !== strpos( $url, 'facebook.com' ) ) ? TRUE : FALSE;
  173. }
  174. function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) {
  175. $value = (int) $value;
  176. if ( $max < $value || $min > $value )
  177. $value = $default;
  178. return (int) $value;
  179. }
  180. function normalize_text_value( $value, $default = '', $allowed = array() ) {
  181. $allowed = (array) $allowed;
  182. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) )
  183. $value = $default;
  184. return $value;
  185. }
  186. function guess_locale_from_lang( $lang ) {
  187. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) {
  188. return 'en_US';
  189. }
  190. if ( !class_exists( 'GP_Locales' ) ) {
  191. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  192. return false;
  193. }
  194. require JETPACK__GLOTPRESS_LOCALES_PATH;
  195. }
  196. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  197. // WP.com: get_locale() returns 'it'
  198. $locale = GP_Locales::by_slug( $lang );
  199. } else {
  200. // Jetpack: get_locale() returns 'it_IT';
  201. $locale = GP_Locales::by_field( 'wp_locale', $lang );
  202. }
  203. if ( !$locale || empty( $locale->facebook_locale ) ) {
  204. return false;
  205. }
  206. return $locale->facebook_locale;
  207. }
  208. function get_locale() {
  209. return $this->guess_locale_from_lang( get_locale() );
  210. }
  211. }
  212. // END