Completed
Push — add/sync-paused-themes-plugins ( 77c036...7d071a )
by
unknown
09:34
created

WPCOM_Widget_GooglePlus_Badge::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
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 View Code Duplication
		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 View Code Duplication
		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 View Code Duplication
			case 'portrait':
163
				$instance['width'] = filter_var(
164
					$new_instance['width'], FILTER_VALIDATE_INT, array(
165
						'options' => array(
166
							'min_range' => $this->min_width_portrait,
167
							'max_range' => $this->max_width,
168
							'default'   => $this->default_width,
169
						),
170
					)
171
				);
172
				break;
173 View Code Duplication
			case 'landscape':
174
				$instance['width'] = filter_var(
175
					$new_instance['width'], FILTER_VALIDATE_INT, array(
176
						'options' => array(
177
							'min_range' => $this->min_width_landscape,
178
							'max_range' => $this->max_width,
179
							'default'   => $this->default_width,
180
						),
181
					)
182
				);
183
				break;
184
		}
185
186
		if ( array_key_exists( $new_instance['type'], $this->allowed_types ) ) {
187
			$instance['type'] = $new_instance['type'];
188
		} else {
189
			$instance['type'] = $this->default_type;
190
		}
191
192
		switch ( $instance['type'] ) {
193
			case 'person':
194
			case 'page':
195
				$instance['show_coverphoto'] = isset( $new_instance['show_coverphoto'] );
196
				break;
197
			case 'community':
198
				$instance['show_photo']  = isset( $new_instance['show_photo'] );
199
				$instance['show_owners'] = isset( $new_instance['show_owners'] );
200
				break;
201
		}
202
203
		$instance['show_tagline'] = isset( $new_instance['show_tagline'] );
204
205
		return $instance;
206
	}
207
208
	function form( $instance ) {
209
		$defaults = array(
210
			'title'           => '',
211
			'href'            => '',
212
			'width'           => $this->default_width,
213
			'layout'          => $this->default_layout,
214
			'theme'           => $this->default_theme,
215
			'show_coverphoto' => true,
216
			'show_photo'      => true,
217
			'show_owners'     => false,
218
			'show_tagline'    => true,
219
			'type'            => $this->default_type,
220
		);
221
222
		$instance = wp_parse_args( $instance, $defaults );
223
		?>
224
225
		<p>
226
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
227
				<?php _e( 'Title', 'jetpack' ); ?>
228
				<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" />
229
			</label>
230
		</p>
231
232
		<p>
233
			<label for="<?php echo $this->get_field_id( 'type' ); ?>">
234
				<?php _e( 'Type of Widget', 'jetpack' ); ?>
235
				<select name="<?php echo $this->get_field_name( 'type' ); ?>" id="<?php echo $this->get_field_id( 'type' ); ?>" class="widefat googleplus-badge-choose-type">
236
					<?php
237
					foreach ( $this->allowed_types as $type_value => $type_display ) {
238
						printf(
239
							'<option value="%s"%s>%s</option>',
240
							esc_attr( $type_value ),
241
							selected( $type_value, $instance['type'], false ),
242
							esc_attr( $type_display )
243
						);
244
					}
245
					?>
246
				</select>
247
			</label>
248
		</p>
249
250
		<p>
251
			<label for="<?php echo $this->get_field_id( 'href' ); ?>">
252
				<?php _e( 'Google+ URL', 'jetpack' ); ?>
253
				<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" />
254
			</label>
255
		</p>
256
257
		<p>
258
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
259
				<?php _e( 'Width', 'jetpack' ); ?>
260
				<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
261
			</label>
262
		</p>
263
264
		<p>
265
			<label for="<?php echo $this->get_field_id( 'layout' ); ?>">
266
				<?php _e( 'Layout', 'jetpack' ); ?>
267
				<select name="<?php echo $this->get_field_name( 'layout' ); ?>" id="<?php echo $this->get_field_id( 'layout' ); ?>">
268
					<option value="landscape" <?php selected( $instance['layout'], 'landscape' ); ?>><?php _e( 'Landscape', 'jetpack' ); ?></option>
269
					<option value="portrait" <?php selected( $instance['layout'], 'portrait' ); ?>><?php _e( 'Portrait', 'jetpack' ); ?></option>
270
				</select>
271
			</label>
272
		</p>
273
274
		<p>
275
			<label for="<?php echo $this->get_field_id( 'theme' ); ?>">
276
				<?php _e( 'Theme', 'jetpack' ); ?>
277
				<select name="<?php echo $this->get_field_name( 'theme' ); ?>" id="<?php echo $this->get_field_id( 'theme' ); ?>">
278
					<option value="light" <?php selected( $instance['theme'], 'light' ); ?>><?php _e( 'Light', 'jetpack' ); ?></option>
279
					<option value="dark" <?php selected( $instance['theme'], 'dark' ); ?>><?php _e( 'Dark', 'jetpack' ); ?></option>
280
				</select>
281
			</label>
282
		</p>
283
284
		<p>
285
			<label for="<?php echo $this->get_field_id( 'show_coverphoto' ); ?>">
286
				<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" />
287
				<?php _e( 'Show Cover Photo', 'jetpack' ); ?>
288
			</label>
289
		</p>
290
291
		<p>
292
			<label for="<?php echo $this->get_field_id( 'show_photo' ); ?>">
293
				<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" />
294
				<?php _e( 'Show Photo', 'jetpack' ); ?>
295
			</label>
296
		</p>
297
298
		<p>
299
			<label for="<?php echo $this->get_field_id( 'show_owners' ); ?>">
300
				<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" />
301
				<?php _e( 'Show Owners', 'jetpack' ); ?>
302
			</label>
303
		</p>
304
305
		<p>
306
			<label for="<?php echo $this->get_field_id( 'show_tagline' ); ?>">
307
				<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'] ); ?> />
308
				<?php _e( 'Show Tag Line', 'jetpack' ); ?>
309
			</label>
310
		</p>
311
312
		<?php
313
	}
314
315
	function is_valid_googleplus_url( $url ) {
316
		return ( false !== strpos( $url, 'plus.google.com' ) ) ? true : false;
317
	}
318
319 View Code Duplication
	function filter_text( $value, $default = '', $allowed = array() ) {
320
		$allowed = (array) $allowed;
321
322
		if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
323
			$value = $default;
324
		}
325
326
		return $value;
327
	}
328
}
329
330
// END
331