googleplus-badge.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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(
  62. 'googleplus-widget-admin',
  63. Jetpack::get_file_url_for_environment(
  64. '_inc/build/widgets/google-plus/js/admin.min.js',
  65. 'modules/widgets/google-plus/js/admin.js'
  66. ),
  67. array( 'jquery' )
  68. );
  69. }
  70. }
  71. function widget( $args, $instance ) {
  72. /** This action is documented in modules/widgets/gravatar-profile.php */
  73. do_action( 'jetpack_stats_extra', 'widget_view', 'googleplus-badge' );
  74. if ( empty( $instance['href'] ) || ! $this->is_valid_googleplus_url( $instance['href'] ) ) {
  75. if ( current_user_can( 'edit_theme_options' ) ) {
  76. echo $args['before_widget'];
  77. echo '<p>' . sprintf(
  78. __( 'It looks like your Google+ URL is incorrectly configured. Please check it in your <a href="%s">widget settings</a>.', 'jetpack' ),
  79. admin_url( 'widgets.php' )
  80. ) . '</p>';
  81. echo $args['after_widget'];
  82. }
  83. echo '<!-- Invalid Google+ URL -->';
  84. return;
  85. }
  86. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  87. $title = apply_filters( 'widget_title', $instance['title'] );
  88. echo $args['before_widget'];
  89. if ( ! empty( $title ) ) {
  90. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  91. }
  92. switch( $instance['type'] ) {
  93. case 'person':
  94. case 'page':
  95. printf(
  96. '<div class="g-%s" data-href="%s" data-layout="%s" data-theme="%s" data-showcoverphoto="%s" data-showtagline="%s" data-width="%s"></div>',
  97. $instance['type'],
  98. esc_url( $instance['href'] ),
  99. esc_attr( $instance['layout'] ),
  100. esc_attr( $instance['theme'] ),
  101. esc_attr( $instance['show_coverphoto'] ? 'true' : 'false' ),
  102. esc_attr( $instance['show_tagline'] ? 'true' : 'false' ),
  103. esc_attr( $instance['width'] )
  104. );
  105. break;
  106. case 'community':
  107. printf(
  108. '<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>',
  109. $instance['type'],
  110. esc_url( $instance['href'] ),
  111. esc_attr( $instance['layout'] ),
  112. esc_attr( $instance['theme'] ),
  113. esc_attr( $instance['show_photo'] ? 'true' : 'false' ),
  114. esc_attr( $instance['show_owners'] ? 'true' : 'false' ),
  115. esc_attr( $instance['show_tagline'] ? 'true' : 'false' ),
  116. esc_attr( $instance['width'] )
  117. );
  118. break;
  119. }
  120. echo $args['after_widget'];
  121. }
  122. function update( $new_instance, $old_instance ) {
  123. $instance = array();
  124. $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
  125. // Validate the Google+ URL
  126. $instance['href'] = trim( strip_tags( stripslashes( $new_instance['href'] ) ) );
  127. if ( $this->is_valid_googleplus_url( $instance['href'] ) ) {
  128. $temp = explode( '?', $instance['href'] );
  129. $instance['href'] = str_replace( array( 'http://plus.google.com', 'https://plus.google.com' ), 'https://plus.google.com', $temp[0] );
  130. } else {
  131. $instance['href'] = '';
  132. }
  133. $instance['theme'] = $this->filter_text( $new_instance['theme'], $this->default_theme, $this->allowed_themes );
  134. $instance['layout'] = $this->filter_text( $new_instance['layout'], $this->default_layout, $this->allowed_layouts );
  135. switch( $instance['layout'] ) {
  136. case 'portrait':
  137. $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array(
  138. 'min_range' => $this->min_width_portrait,
  139. 'max_range' => $this->max_width,
  140. 'default' => $this->default_width,
  141. ) ) );
  142. break;
  143. case 'landscape':
  144. $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array(
  145. 'min_range' => $this->min_width_landscape,
  146. 'max_range' => $this->max_width,
  147. 'default' => $this->default_width,
  148. ) ) );
  149. break;
  150. }
  151. if ( array_key_exists( $new_instance['type'], $this->allowed_types ) ) {
  152. $instance['type'] = $new_instance['type'];
  153. } else {
  154. $instance['type'] = $this->default_type;
  155. }
  156. switch( $instance['type'] ) {
  157. case 'person':
  158. case 'page':
  159. $instance['show_coverphoto'] = isset( $new_instance['show_coverphoto'] );
  160. break;
  161. case 'community':
  162. $instance['show_photo'] = isset( $new_instance['show_photo'] );
  163. $instance['show_owners'] = isset( $new_instance['show_owners'] );
  164. break;
  165. }
  166. $instance['show_tagline'] = isset( $new_instance['show_tagline'] );
  167. return $instance;
  168. }
  169. function form( $instance ) {
  170. $defaults = array(
  171. 'title' => '',
  172. 'href' => '',
  173. 'width' => $this->default_width,
  174. 'layout' => $this->default_layout,
  175. 'theme' => $this->default_theme,
  176. 'show_coverphoto' => true,
  177. 'show_photo' => true,
  178. 'show_owners' => false,
  179. 'show_tagline' => true,
  180. 'type' => $this->default_type,
  181. );
  182. $instance = wp_parse_args( $instance, $defaults );
  183. ?>
  184. <p>
  185. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  186. <?php _e( 'Title', 'jetpack' ); ?>
  187. <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" />
  188. </label>
  189. </p>
  190. <p>
  191. <label for="<?php echo $this->get_field_id( 'type' ); ?>">
  192. <?php _e( 'Type of Widget', 'jetpack' ); ?>
  193. <select name="<?php echo $this->get_field_name( 'type' ); ?>" id="<?php echo $this->get_field_id( 'type' ); ?>" class="widefat googleplus-badge-choose-type">
  194. <?php
  195. foreach( $this->allowed_types as $type_value => $type_display ) {
  196. printf(
  197. '<option value="%s"%s>%s</option>',
  198. esc_attr( $type_value ),
  199. selected( $type_value, $instance['type'], false ),
  200. esc_attr( $type_display )
  201. );
  202. }
  203. ?>
  204. </select>
  205. </label>
  206. </p>
  207. <p>
  208. <label for="<?php echo $this->get_field_id( 'href' ); ?>">
  209. <?php _e( 'Google+ URL', 'jetpack' ); ?>
  210. <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" />
  211. </label>
  212. </p>
  213. <p>
  214. <label for="<?php echo $this->get_field_id( 'width' ); ?>">
  215. <?php _e( 'Width', 'jetpack' ); ?>
  216. <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
  217. </label>
  218. </p>
  219. <p>
  220. <label for="<?php echo $this->get_field_id( 'layout' ); ?>">
  221. <?php _e( 'Layout', 'jetpack' ); ?>
  222. <select name="<?php echo $this->get_field_name( 'layout' ); ?>" id="<?php echo $this->get_field_id( 'layout' ); ?>">
  223. <option value="landscape" <?php selected( $instance['layout'], 'landscape' ); ?>><?php _e( 'Landscape', 'jetpack' ); ?></option>
  224. <option value="portrait" <?php selected( $instance['layout'], 'portrait' ); ?>><?php _e( 'Portrait', 'jetpack' ); ?></option>
  225. </select>
  226. </label>
  227. </p>
  228. <p>
  229. <label for="<?php echo $this->get_field_id( 'theme' ); ?>">
  230. <?php _e( 'Theme', 'jetpack' ); ?>
  231. <select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>">
  232. <option value="light" <?php selected( $instance['theme'], 'light' ); ?>><?php _e( 'Light', 'jetpack' ); ?></option>
  233. <option value="dark" <?php selected( $instance['theme'], 'dark' ); ?>><?php _e( 'Dark', 'jetpack' ); ?></option>
  234. </select>
  235. </label>
  236. </p>
  237. <p>
  238. <label for="<?php echo $this->get_field_id( 'show_coverphoto' ); ?>">
  239. <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" />
  240. <?php _e( 'Show Cover Photo', 'jetpack' ); ?>
  241. </label>
  242. </p>
  243. <p>
  244. <label for="<?php echo $this->get_field_id( 'show_photo' ); ?>">
  245. <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" />
  246. <?php _e( 'Show Photo', 'jetpack' ); ?>
  247. </label>
  248. </p>
  249. <p>
  250. <label for="<?php echo $this->get_field_id( 'show_owners' ); ?>">
  251. <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" />
  252. <?php _e( 'Show Owners', 'jetpack' ); ?>
  253. </label>
  254. </p>
  255. <p>
  256. <label for="<?php echo $this->get_field_id( 'show_tagline' ); ?>">
  257. <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'] ); ?> />
  258. <?php _e( 'Show Tag Line', 'jetpack' ); ?>
  259. </label>
  260. </p>
  261. <?php
  262. }
  263. function is_valid_googleplus_url( $url ) {
  264. return ( FALSE !== strpos( $url, 'plus.google.com' ) ) ? TRUE : FALSE;
  265. }
  266. function filter_text( $value, $default = '', $allowed = array() ) {
  267. $allowed = (array) $allowed;
  268. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) )
  269. $value = $default;
  270. return $value;
  271. }
  272. }
  273. // END