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