googleplus-badge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Register the widget for use in Appearance -> Widgets
  4. */
  5. add_action( 'widgets_init', 'jetpack_googleplus_badge_init' );
  6. function jetpack_googleplus_badge_init() {
  7. register_widget( 'WPCOM_Widget_GooglePlus_Badge' );
  8. }
  9. /**
  10. * Google+ Badge widget class
  11. * Display a Google+ Badge as a widget
  12. * https://developers.google.com/+/web/badge/
  13. */
  14. class WPCOM_Widget_GooglePlus_Badge extends WP_Widget {
  15. private $default_width = 220;
  16. private $max_width = 450;
  17. private $min_width_portrait = 180;
  18. private $min_width_landscape = 273;
  19. private $min_width;
  20. private $default_theme = 'light';
  21. private $allowed_themes = array( 'light', 'dark' );
  22. private $default_layout = 'portrait';
  23. private $allowed_layouts = array( 'landscape', 'portrait' );
  24. private $default_type = 'person';
  25. private $allowed_types = array();
  26. function __construct() {
  27. $this->min_width = min( $this->min_width_portrait, $this->min_width_landscape );
  28. $this->allowed_types = array(
  29. 'person' => __( 'Person Widget', 'jetpack' ),
  30. 'page' => __( 'Page Widget', 'jetpack' ),
  31. 'community' => __( 'Community Widget', 'jetpack' ),
  32. );
  33. parent::__construct(
  34. 'googleplus-badge',
  35. /** This filter is documented in modules/widgets/facebook-likebox.php */
  36. apply_filters( 'jetpack_widget_name', __( 'Google+ Badge', 'jetpack' ) ),
  37. array(
  38. 'classname' => 'widget_googleplus_badge',
  39. 'description' => __( 'Display a Google+ Badge to connect visitors to your Google+', 'jetpack' ),
  40. 'customize_selective_refresh' => true,
  41. )
  42. );
  43. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
  44. if ( is_active_widget( '', '', 'googleplus-badge' ) || is_customize_preview() ) {
  45. add_action( 'wp_print_styles', array( $this, 'enqueue_script' ) );
  46. add_filter( 'script_loader_tag', array( $this, 'replace_script_tag' ), 10, 2 );
  47. }
  48. }
  49. function enqueue_script() {
  50. wp_enqueue_script( 'googleplus-widget', 'https://apis.google.com/js/platform.js' );
  51. }
  52. function replace_script_tag( $tag, $handle ) {
  53. if ( 'googleplus-widget' !== $handle ) {
  54. return $tag;
  55. }
  56. return str_replace( ' src', ' async defer src', $tag );
  57. }
  58. function enqueue_admin_scripts() {
  59. global $pagenow;
  60. if ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) {
  61. wp_enqueue_script( 'googleplus-widget-admin', plugins_url( '/google-plus/js/admin.js', __FILE__ ), array( 'jquery' ) );
  62. }
  63. }
  64. function widget( $args, $instance ) {
  65. if ( empty( $instance['href'] ) || ! $this->is_valid_googleplus_url( $instance['href'] ) ) {
  66. if ( current_user_can( 'edit_theme_options' ) ) {
  67. echo $args['before_widget'];
  68. echo '<p>' . sprintf(
  69. __( 'It looks like your Google+ URL is incorrectly configured. Please check it in your <a href="%s">widget settings</a>.', 'jetpack' ),
  70. admin_url( 'widgets.php' )
  71. ) . '</p>';
  72. echo $args['after_widget'];
  73. }
  74. echo '<!-- Invalid Google+ URL -->';
  75. return;
  76. }
  77. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  78. $title = apply_filters( 'widget_title', $instance['title'] );
  79. echo $args['before_widget'];
  80. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  81. switch( $instance['type'] ) {
  82. case 'person':
  83. case 'page':
  84. printf(
  85. '<div class="g-%s" data-href="%s" data-layout="%s" data-theme="%s" data-showcoverphoto="%s" data-showtagline="%s" data-width="%s"></div>',
  86. $instance['type'],
  87. esc_url( $instance['href'] ),
  88. esc_attr( $instance['layout'] ),
  89. esc_attr( $instance['theme'] ),
  90. esc_attr( $instance['show_coverphoto'] ? 'true' : 'false' ),
  91. esc_attr( $instance['show_tagline'] ? 'true' : 'false' ),
  92. esc_attr( $instance['width'] )
  93. );
  94. break;
  95. case 'community':
  96. printf(
  97. '<div class="g-%s" data-href="%s" data-layout="%s" data-theme="%s" data-showphoto="%s" data-showowners="%s" data-showtagline="%s" data-width="%s"></div>',
  98. $instance['type'],
  99. esc_url( $instance['href'] ),
  100. esc_attr( $instance['layout'] ),
  101. esc_attr( $instance['theme'] ),
  102. esc_attr( $instance['show_photo'] ? 'true' : 'false' ),
  103. esc_attr( $instance['show_owners'] ? 'true' : 'false' ),
  104. esc_attr( $instance['show_tagline'] ? 'true' : 'false' ),
  105. esc_attr( $instance['width'] )
  106. );
  107. break;
  108. }
  109. echo $args['after_widget'];
  110. /** This action is already documented in modules/widgets/gravatar-profile.php */
  111. do_action( 'jetpack_stats_extra', 'widget', 'googleplus-badge' );
  112. }
  113. function update( $new_instance, $old_instance ) {
  114. $instance = array();
  115. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
  116. // Validate the Google+ URL
  117. $instance['href'] = trim( strip_tags( stripslashes( $new_instance['href'] ) ) );
  118. if ( $this->is_valid_googleplus_url( $instance['href'] ) ) {
  119. $temp = explode( '?', $instance['href'] );
  120. $instance['href'] = str_replace( array( 'http://plus.google.com', 'https://plus.google.com' ), 'https://plus.google.com', $temp[0] );
  121. } else {
  122. $instance['href'] = '';
  123. }
  124. $instance['theme'] = $this->filter_text( $new_instance['theme'], $this->default_theme, $this->allowed_themes );
  125. $instance['layout'] = $this->filter_text( $new_instance['layout'], $this->default_layout, $this->allowed_layouts );
  126. switch( $instance['layout'] ) {
  127. case 'portrait':
  128. $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array(
  129. 'min_range' => $this->min_width_portrait,
  130. 'max_range' => $this->max_width,
  131. 'default' => $this->default_width,
  132. ) ) );
  133. break;
  134. case 'landscape':
  135. $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array(
  136. 'min_range' => $this->min_width_landscape,
  137. 'max_range' => $this->max_width,
  138. 'default' => $this->default_width,
  139. ) ) );
  140. break;
  141. }
  142. if ( array_key_exists( $new_instance['type'], $this->allowed_types ) ) {
  143. $instance['type'] = $new_instance['type'];
  144. } else {
  145. $instance['type'] = $this->default_type;
  146. }
  147. switch( $instance['type'] ) {
  148. case 'person':
  149. case 'page':
  150. $instance['show_coverphoto'] = isset( $new_instance['show_coverphoto'] );
  151. break;
  152. case 'community':
  153. $instance['show_photo'] = isset( $new_instance['show_photo'] );
  154. $instance['show_owners'] = isset( $new_instance['show_owners'] );
  155. break;
  156. }
  157. $instance['show_tagline'] = isset( $new_instance['show_tagline'] );
  158. return $instance;
  159. }
  160. function form( $instance ) {
  161. $defaults = array(
  162. 'title' => '',
  163. 'href' => '',
  164. 'width' => $this->default_width,
  165. 'layout' => $this->default_layout,
  166. 'theme' => $this->default_theme,
  167. 'show_coverphoto' => true,
  168. 'show_photo' => true,
  169. 'show_owners' => false,
  170. 'show_tagline' => true,
  171. 'type' => $this->default_type,
  172. );
  173. $instance = wp_parse_args( $instance, $defaults );
  174. ?>
  175. <p>
  176. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  177. <?php _e( 'Title', 'jetpack' ); ?>
  178. <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" />
  179. </label>
  180. </p>
  181. <p>
  182. <label for="<?php echo $this->get_field_id( 'type' ); ?>">
  183. <?php _e( 'Type of Widget', 'jetpack' ); ?>
  184. <select name="<?php echo $this->get_field_name( 'type' ); ?>" id="<?php echo $this->get_field_id( 'type' ); ?>" class="widefat googleplus-badge-choose-type">
  185. <?php
  186. foreach( $this->allowed_types as $type_value => $type_display ) {
  187. printf(
  188. '<option value="%s"%s>%s</option>',
  189. esc_attr( $type_value ),
  190. selected( $type_value, $instance['type'], false ),
  191. esc_attr( $type_display )
  192. );
  193. }
  194. ?>
  195. </select>
  196. </label>
  197. </p>
  198. <p>
  199. <label for="<?php echo $this->get_field_id( 'href' ); ?>">
  200. <?php _e( 'Google+ URL', 'jetpack' ); ?>
  201. <input type="text" name="<?php echo $this->get_field_name( 'href' ); ?>" id="<?php echo $this->get_field_id( 'href' ); ?>" value="<?php echo esc_url( $instance['href'] ); ?>" class="widefat" />
  202. </label>
  203. </p>
  204. <p>
  205. <label for="<?php echo $this->get_field_id( 'width' ); ?>">
  206. <?php _e( 'Width', 'jetpack' ); ?>
  207. <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 $this->get_field_name( 'width' ); ?>" id="<?php echo $this->get_field_id( 'width' ); ?>" value="<?php echo esc_attr( $instance['width'] ); ?>" style="text-align: center;" />px
  208. </label>
  209. </p>
  210. <p>
  211. <label for="<?php echo $this->get_field_id( 'layout' ); ?>">
  212. <?php _e( 'Layout', 'jetpack' ); ?>
  213. <select name="<?php echo $this->get_field_name( 'layout' ); ?>" id="<?php echo $this->get_field_id( 'layout' ); ?>">
  214. <option value="landscape" <?php selected( $instance['layout'], 'landscape' ); ?>><?php _e( 'Landscape', 'jetpack' ); ?></option>
  215. <option value="portrait" <?php selected( $instance['layout'], 'portrait' ); ?>><?php _e( 'Portrait', 'jetpack' ); ?></option>
  216. </select>
  217. </label>
  218. </p>
  219. <p>
  220. <label for="<?php echo $this->get_field_id( 'theme' ); ?>">
  221. <?php _e( 'Theme', 'jetpack' ); ?>
  222. <select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>">
  223. <option value="light" <?php selected( $instance['theme'], 'light' ); ?>><?php _e( 'Light', 'jetpack' ); ?></option>
  224. <option value="dark" <?php selected( $instance['theme'], 'dark' ); ?>><?php _e( 'Dark', 'jetpack' ); ?></option>
  225. </select>
  226. </label>
  227. </p>
  228. <p>
  229. <label for="<?php echo $this->get_field_id( 'show_coverphoto' ); ?>">
  230. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_coverphoto' ); ?>" id="<?php echo $this->get_field_id( 'show_coverphoto' ); ?>" <?php checked( $instance['show_coverphoto'] ); ?> class="googleplus-badge-only-person googleplus-badge-only-page" />
  231. <?php _e( 'Show Cover Photo', 'jetpack' ); ?>
  232. </label>
  233. </p>
  234. <p>
  235. <label for="<?php echo $this->get_field_id( 'show_photo' ); ?>">
  236. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_photo' ); ?>" id="<?php echo $this->get_field_id( 'show_photo' ); ?>" <?php checked( $instance['show_photo'] ); ?> class="googleplus-badge-only-community" />
  237. <?php _e( 'Show Photo', 'jetpack' ); ?>
  238. </label>
  239. </p>
  240. <p>
  241. <label for="<?php echo $this->get_field_id( 'show_owners' ); ?>">
  242. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_owners' ); ?>" id="<?php echo $this->get_field_id( 'show_owners' ); ?>" <?php checked( $instance['show_owners'] ); ?> class="googleplus-badge-only-community" />
  243. <?php _e( 'Show Owners', 'jetpack' ); ?>
  244. </label>
  245. </p>
  246. <p>
  247. <label for="<?php echo $this->get_field_id( 'show_tagline' ); ?>">
  248. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_tagline' ); ?>" id="<?php echo $this->get_field_id( 'show_tagline' ); ?>" <?php checked( $instance['show_tagline'] ); ?> />
  249. <?php _e( 'Show Tag Line', 'jetpack' ); ?>
  250. </label>
  251. </p>
  252. <?php
  253. }
  254. function is_valid_googleplus_url( $url ) {
  255. return ( FALSE !== strpos( $url, 'plus.google.com' ) ) ? TRUE : FALSE;
  256. }
  257. function filter_text( $value, $default = '', $allowed = array() ) {
  258. $allowed = (array) $allowed;
  259. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) )
  260. $value = $default;
  261. return $value;
  262. }
  263. }
  264. // END