Widgets */ add_action( 'widgets_init', 'jetpack_googleplus_badge_init' ); function jetpack_googleplus_badge_init() { register_widget( 'WPCOM_Widget_GooglePlus_Badge' ); } /** * Google+ Badge widget class * Display a Google+ Badge as a widget * https://developers.google.com/+/web/badge/ */ class WPCOM_Widget_GooglePlus_Badge extends WP_Widget { private $default_width = 220; private $max_width = 450; private $min_width_portrait = 180; private $min_width_landscape = 273; private $min_width; private $default_theme = 'light'; private $allowed_themes = array( 'light', 'dark' ); private $default_layout = 'portrait'; private $allowed_layouts = array( 'landscape', 'portrait' ); private $default_type = 'person'; private $allowed_types = array(); function __construct() { $this->min_width = min( $this->min_width_portrait, $this->min_width_landscape ); $this->allowed_types = array( 'person' => __( 'Person Widget', 'jetpack' ), 'page' => __( 'Page Widget', 'jetpack' ), 'community' => __( 'Community Widget', 'jetpack' ), ); parent::__construct( 'googleplus-badge', /** This filter is documented in modules/widgets/facebook-likebox.php */ apply_filters( 'jetpack_widget_name', __( 'Google+ Badge', 'jetpack' ) ), array( 'classname' => 'widget_googleplus_badge', 'description' => __( 'Display a Google+ Badge to connect visitors to your Google+', 'jetpack' ), 'customize_selective_refresh' => true, ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); if ( is_active_widget( '', '', 'googleplus-badge' ) || is_customize_preview() ) { add_action( 'wp_print_styles', array( $this, 'enqueue_script' ) ); add_filter( 'script_loader_tag', array( $this, 'replace_script_tag' ), 10, 2 ); } } function enqueue_script() { wp_enqueue_script( 'googleplus-widget', 'https://apis.google.com/js/platform.js' ); } function replace_script_tag( $tag, $handle ) { if ( 'googleplus-widget' !== $handle ) { return $tag; } return str_replace( ' src', ' async defer src', $tag ); } function enqueue_admin_scripts() { global $pagenow; if ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) { wp_enqueue_script( 'googleplus-widget-admin', Jetpack::get_file_url_for_environment( '_inc/build/widgets/google-plus/js/admin.min.js', 'modules/widgets/google-plus/js/admin.js' ), array( 'jquery' ) ); } } function widget( $args, $instance ) { /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'googleplus-badge' ); if ( empty( $instance['href'] ) || ! $this->is_valid_googleplus_url( $instance['href'] ) ) { if ( current_user_can( 'edit_theme_options' ) ) { echo $args['before_widget']; echo '

' . sprintf( __( 'It looks like your Google+ URL is incorrectly configured. Please check it in your widget settings.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '

'; echo $args['after_widget']; } echo ''; return; } /** This filter is documented in core/src/wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $instance['title'] ); echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . esc_html( $title ) . $args['after_title']; } switch( $instance['type'] ) { case 'person': case 'page': printf( '
', $instance['type'], esc_url( $instance['href'] ), esc_attr( $instance['layout'] ), esc_attr( $instance['theme'] ), esc_attr( $instance['show_coverphoto'] ? 'true' : 'false' ), esc_attr( $instance['show_tagline'] ? 'true' : 'false' ), esc_attr( $instance['width'] ) ); break; case 'community': printf( '
', $instance['type'], esc_url( $instance['href'] ), esc_attr( $instance['layout'] ), esc_attr( $instance['theme'] ), esc_attr( $instance['show_photo'] ? 'true' : 'false' ), esc_attr( $instance['show_owners'] ? 'true' : 'false' ), esc_attr( $instance['show_tagline'] ? 'true' : 'false' ), esc_attr( $instance['width'] ) ); break; } echo $args['after_widget']; } function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) ); // Validate the Google+ URL $instance['href'] = trim( strip_tags( stripslashes( $new_instance['href'] ) ) ); if ( $this->is_valid_googleplus_url( $instance['href'] ) ) { $temp = explode( '?', $instance['href'] ); $instance['href'] = str_replace( array( 'http://plus.google.com', 'https://plus.google.com' ), 'https://plus.google.com', $temp[0] ); } else { $instance['href'] = ''; } $instance['theme'] = $this->filter_text( $new_instance['theme'], $this->default_theme, $this->allowed_themes ); $instance['layout'] = $this->filter_text( $new_instance['layout'], $this->default_layout, $this->allowed_layouts ); switch( $instance['layout'] ) { case 'portrait': $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => $this->min_width_portrait, 'max_range' => $this->max_width, 'default' => $this->default_width, ) ) ); break; case 'landscape': $instance['width'] = filter_var( $new_instance['width'], FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => $this->min_width_landscape, 'max_range' => $this->max_width, 'default' => $this->default_width, ) ) ); break; } if ( array_key_exists( $new_instance['type'], $this->allowed_types ) ) { $instance['type'] = $new_instance['type']; } else { $instance['type'] = $this->default_type; } switch( $instance['type'] ) { case 'person': case 'page': $instance['show_coverphoto'] = isset( $new_instance['show_coverphoto'] ); break; case 'community': $instance['show_photo'] = isset( $new_instance['show_photo'] ); $instance['show_owners'] = isset( $new_instance['show_owners'] ); break; } $instance['show_tagline'] = isset( $new_instance['show_tagline'] ); return $instance; } function form( $instance ) { $defaults = array( 'title' => '', 'href' => '', 'width' => $this->default_width, 'layout' => $this->default_layout, 'theme' => $this->default_theme, 'show_coverphoto' => true, 'show_photo' => true, 'show_owners' => false, 'show_tagline' => true, 'type' => $this->default_type, ); $instance = wp_parse_args( $instance, $defaults ); ?>