PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/juanito.abelo/nlmobile
PHP | 314 lines | 238 code | 60 blank | 16 comment | 36 complexity | ef5faf97ae448c00a90048f06b469fae 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 Like Box as a widget
  12. * http://developers.facebook.com/docs/reference/plugins/like-box/
  13. */
  14. class WPCOM_Widget_Facebook_LikeBox extends WP_Widget {
  15. private $default_height = 432;
  16. private $default_width = 200;
  17. private $max_width = 9999;
  18. private $min_width = 0;
  19. private $max_height = 9999;
  20. private $min_height = 100;
  21. private $default_colorscheme = 'light';
  22. private $allowed_colorschemes = array( 'light', 'dark' );
  23. function __construct() {
  24. parent::__construct(
  25. 'facebook-likebox',
  26. apply_filters( 'jetpack_widget_name', __( 'Facebook Like Box', 'jetpack' ) ),
  27. array(
  28. 'classname' => 'widget_facebook_likebox',
  29. 'description' => __( 'Display a Facebook Like Box to connect visitors to your Facebook Page', 'jetpack' )
  30. )
  31. );
  32. }
  33. function widget( $args, $instance ) {
  34. extract( $args );
  35. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  36. if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) {
  37. if ( current_user_can('edit_theme_options') ) {
  38. echo $before_widget;
  39. 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>';
  40. echo $after_widget;
  41. }
  42. echo '<!-- Invalid Facebook Page URL -->';
  43. return;
  44. }
  45. $title = apply_filters( 'widget_title', $instance['title'] );
  46. $page_url = set_url_scheme( $like_args['href'], 'https' );
  47. $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false';
  48. $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false';
  49. $like_args['force_wall'] = (bool) $like_args['force_wall'] ? 'true' : 'false';
  50. $like_args['show_border']= (bool) $like_args['show_border'] ? 'true' : 'false';
  51. $like_args['header'] = (bool) $like_args['header'] ? 'true' : 'false';
  52. $like_bg_colour = apply_filters( 'jetpack_fb_likebox_bg', ( 'dark' == $like_args['colorscheme'] ? '#000' : '#fff' ), $like_args['colorscheme'] );
  53. $locale = $this->get_locale();
  54. if ( $locale && 'en_US' != $locale )
  55. $like_args['locale'] = $locale;
  56. $like_args = urlencode_deep( $like_args );
  57. $like_url = add_query_arg(
  58. $like_args,
  59. 'https://www.facebook.com/plugins/likebox.php'
  60. );
  61. echo $before_widget;
  62. if ( ! empty( $title ) ) :
  63. echo $before_title;
  64. $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>';
  65. echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
  66. echo $after_title;
  67. endif;
  68. ?><iframe src="<?php echo esc_url( $like_url ); ?>" scrolling="no" frameborder="0" style="border: none; overflow: hidden;<?php echo 0 != $like_args['width'] ? ' width: ' . (int) $like_args['width'] . 'px; ' : ''; ?> height: <?php echo (int) $like_args['height']; ?>px; background: <?php echo esc_attr( $like_bg_colour ); ?>"></iframe><?php
  69. echo $after_widget;
  70. do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' );
  71. }
  72. function update( $new_instance, $old_instance ) {
  73. $instance = array(
  74. 'title' => '',
  75. 'like_args' => $this->get_default_args(),
  76. );
  77. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
  78. // Set up widget values
  79. $instance['like_args'] = array(
  80. 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),
  81. 'width' => (int) $new_instance['width'],
  82. 'height' => (int) $new_instance['height'],
  83. 'colorscheme' => $new_instance['colorscheme'],
  84. 'show_faces' => (bool) $new_instance['show_faces'],
  85. 'stream' => (bool) $new_instance['stream'],
  86. 'show_border' => (bool) $new_instance['show_border'],
  87. 'header' => false, // The header just displays "Find us on Facebook"; it's redundant with the title
  88. 'force_wall' => (bool) $new_instance['force_wall'],
  89. );
  90. $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
  91. return $instance;
  92. }
  93. function form( $instance ) {
  94. $instance = wp_parse_args( (array) $instance, array(
  95. 'title' => '',
  96. 'like_args' => $this->get_default_args()
  97. ) );
  98. $like_args = $this->normalize_facebook_args( $instance['like_args'] );
  99. ?>
  100. <p>
  101. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  102. <?php _e( 'Title', 'jetpack' ); ?>
  103. <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" />
  104. </label>
  105. </p>
  106. <p>
  107. <label for="<?php echo $this->get_field_id( 'href' ); ?>">
  108. <?php _e( 'Facebook Page URL', 'jetpack' ); ?>
  109. <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" />
  110. <br />
  111. <small><?php _e( 'The Like Box only works with <a href="http://www.facebook.com/help/?faq=174987089221178">Facebook Pages</a>.', 'jetpack' ); ?></small>
  112. </label>
  113. </p>
  114. <p>
  115. <label for="<?php echo $this->get_field_id( 'width' ); ?>">
  116. <?php _e( 'Width', 'jetpack' ); ?>
  117. <input type="number" class="smalltext" min="1" max="999" maxlength="3" name="<?php echo $this->get_field_name( 'width' ); ?>" id="<?php echo $this->get_field_id( 'width' ); ?>" value="<?php echo esc_attr( $like_args['width'] ); ?>" style="text-align: center;" />px
  118. </label>
  119. </p>
  120. <p>
  121. <label for="<?php echo $this->get_field_id( 'height' ); ?>">
  122. <?php _e( 'Height', 'jetpack' ); ?>
  123. <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
  124. </label>
  125. </p>
  126. <p>
  127. <label for="<?php echo $this->get_field_id( 'colorscheme' ); ?>">
  128. <?php _e( 'Color Scheme', 'jetpack' ); ?>
  129. <select name="<?php echo $this->get_field_name( 'colorscheme' ); ?>" id="<?php echo $this->get_field_id( 'colorscheme' ); ?>">
  130. <option value="light" <?php selected( $like_args['colorscheme'], 'light' ); ?>><?php _e( 'Light', 'jetpack' ); ?></option>
  131. <option value="dark" <?php selected( $like_args['colorscheme'], 'dark' ); ?>><?php _e( 'Dark', 'jetpack' ); ?></option>
  132. </select>
  133. </label>
  134. </p>
  135. <p>
  136. <label for="<?php echo $this->get_field_id( 'show_faces' ); ?>">
  137. <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'] ); ?> />
  138. <?php _e( 'Show Faces', 'jetpack' ); ?>
  139. <br />
  140. <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
  141. </label>
  142. </p>
  143. <p>
  144. <label for="<?php echo $this->get_field_id( 'stream' ); ?>">
  145. <input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> />
  146. <?php _e( 'Show Stream', 'jetpack' ); ?>
  147. <br />
  148. <small><?php _e( 'Show the profile stream for the public profile.', 'jetpack' ); ?></small>
  149. </label>
  150. </p>
  151. <p>
  152. <label for="<?php echo $this->get_field_id( 'show_border' ); ?>">
  153. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_border' ); ?>" id="<?php echo $this->get_field_id( 'show_border' ); ?>" <?php checked( $like_args['show_border'] ); ?> />
  154. <?php _e( 'Show Border', 'jetpack' ); ?>
  155. <br />
  156. <small><?php _e( 'Show a border around the plugin.', 'jetpack' ); ?></small>
  157. </label>
  158. </p>
  159. <p>
  160. <label for="<?php echo $this->get_field_id( 'force_wall' ); ?>">
  161. <input type="checkbox" name="<?php echo $this->get_field_name( 'force_wall' ); ?>" id="<?php echo $this->get_field_id( 'force_wall' ); ?>" <?php checked( $like_args['force_wall'] ); ?> />
  162. <?php _e( 'Show Wall', 'jetpack' ); ?>
  163. <br />
  164. <small><?php _e( 'Show the wall for a Places page rather than friend activity.', 'jetpack' ); ?></small>
  165. </label>
  166. </p>
  167. <?php
  168. }
  169. function get_default_args() {
  170. $defaults = array(
  171. 'href' => '',
  172. 'width' => $this->default_width,
  173. 'height' => $this->default_height,
  174. 'colorscheme' => $this->default_colorscheme,
  175. 'show_faces' => true,
  176. 'stream' => false,
  177. 'show_border' => true,
  178. 'header' => false,
  179. 'force_wall' => false,
  180. );
  181. return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
  182. }
  183. function normalize_facebook_args( $args ) {
  184. $args = wp_parse_args( (array) $args, $this->get_default_args() );
  185. // Validate the Facebook Page URL
  186. if ( $this->is_valid_facebook_url( $args['href'] ) ) {
  187. $temp = explode( '?', $args['href'] );
  188. $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
  189. } else {
  190. $args['href'] = '';
  191. }
  192. $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width );
  193. $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
  194. $args['colorscheme'] = $this->normalize_text_value( $args['colorscheme'], $this->default_colorscheme, $this->allowed_colorschemes );
  195. $args['show_faces'] = (bool) $args['show_faces'];
  196. $args['stream'] = (bool) $args['stream'];
  197. $args['show_border'] = (bool) $args['show_border'];
  198. $args['force_wall'] = (bool) $args['force_wall'];
  199. // The height used to be dependent on other widget settings
  200. // If the user changes those settings but doesn't customize the height,
  201. // let's intelligently assign a new height.
  202. if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
  203. if( $args['show_faces'] && $args['stream'] ) {
  204. $args['height'] = 580;
  205. } else if( ! $args['show_faces'] && ! $args['stream'] ) {
  206. $args['height'] = 110;
  207. } else {
  208. $args['height'] = 432;
  209. }
  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. if ( $max < $value || $min > $value )
  219. $value = $default;
  220. return (int) $value;
  221. }
  222. function normalize_text_value( $value, $default = '', $allowed = array() ) {
  223. $allowed = (array) $allowed;
  224. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) )
  225. $value = $default;
  226. return $value;
  227. }
  228. function guess_locale_from_lang( $lang ) {
  229. if ( 'en' == $lang || 'en_US' == $lang || !$lang ) {
  230. return 'en_US';
  231. }
  232. if ( !class_exists( 'GP_Locales' ) ) {
  233. if ( !defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || !file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  234. return false;
  235. }
  236. require JETPACK__GLOTPRESS_LOCALES_PATH;
  237. }
  238. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  239. // WP.com: get_locale() returns 'it'
  240. $locale = GP_Locales::by_slug( $lang );
  241. } else {
  242. // Jetpack: get_locale() returns 'it_IT';
  243. $locale = GP_Locales::by_field( 'wp_locale', $lang );
  244. }
  245. if ( !$locale || empty( $locale->facebook_locale ) ) {
  246. return false;
  247. }
  248. return $locale->facebook_locale;
  249. }
  250. function get_locale() {
  251. return $this->guess_locale_from_lang( get_locale() );
  252. }
  253. }
  254. // END