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