WPCOM_Widget_GooglePlus_Badge

Google+ Badge widget class Display a Google+ Badge as a widget https://developers.google.com/+/web/badge/.

Defined (1)

The class is defined in the following location(s).

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