Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

Jetpack_Gravatar_Profile_Widget::form()   C

Complexity

Conditions 11
Paths 96

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 41
nc 96
nop 1
dl 0
loc 65
rs 5.9999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Register the widget for use in Appearance -> Widgets
5
 */
6
add_action( 'widgets_init', 'jetpack_gravatar_profile_widget_init' );
7
8
function jetpack_gravatar_profile_widget_init() {
9
	register_widget( 'Jetpack_Gravatar_Profile_Widget' );
10
}
11
12
/**
13
 * Display a widgetized version of your Gravatar Profile
14
 * http://blog.gravatar.com/2010/03/26/gravatar-profiles/
15
 */
16
class Jetpack_Gravatar_Profile_Widget extends WP_Widget {
17
18
	function __construct() {
19
		parent::__construct(
20
			'grofile',
21
			/** This filter is documented in modules/widgets/facebook-likebox.php */
22
			apply_filters( 'jetpack_widget_name', __( 'Gravatar Profile', 'jetpack' ) ),
23
			array(
24
				'classname'   => 'widget-grofile grofile',
25
				'description' => __( 'Display a mini version of your Gravatar Profile', 'jetpack' ),
26
				'customize_selective_refresh' => true,
27
			)
28
		);
29
30
		if ( is_admin() ) {
31
			add_action( 'admin_footer-widgets.php', array( $this, 'admin_script' ) );
32
		}
33
34
		if ( is_customize_preview() ) {
35
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
36
		}
37
	}
38
39
	function widget( $args, $instance ) {
40
		/**
41
		 * Fires when an item is displayed on the front end.
42
		 *
43
		 * Can be used to track stats about the number of displays for a specific item
44
		 *
45
		 * @module widgets, shortcodes
46
		 *
47
		 * @since 1.6.0
48
		 *
49
		 * @param string widget_view Item type (e.g. widget, or embed).
50
		 * @param string grofile     Item description (e.g. grofile, goodreads).
51
		 */
52
		do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' );
53
54
		$instance = wp_parse_args( $instance, array(
55
			'title' => '',
56
			'email' => ''
57
		) );
58
59
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
60
		$title = apply_filters( 'widget_title', $instance['title'] );
61
62
		if ( !$instance['email'] ) {
63
			if ( current_user_can( 'edit_theme_options' ) ) {
64
				echo $args['before_widget'];
65
				if ( ! empty( $title ) )
66
					echo $args['before_title'] . $title . $args['after_title'];
67
				echo '<p>' . sprintf( __( 'You need to select what to show in this <a href="%s">Gravatar Profile widget</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>';
68
				echo $args['after_widget'];
69
			}
70
			return;
71
		}
72
73
		echo $args['before_widget'];
74
		if ( ! empty( $title ) )
75
			echo $args['before_title'] . $title . $args['after_title'];
76
77
		$profile = $this->get_profile( $instance['email'] );
78
79
		if( ! empty( $profile ) ) {
80
			$profile = wp_parse_args( $profile, array(
81
				'thumbnailUrl' => '',
82
				'profileUrl'   => '',
83
				'displayName'  => '',
84
				'aboutMe'      => '',
85
				'urls'         => array(),
86
				'accounts'     => array(),
87
			) );
88
			$gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // the default grav returned by grofiles is super small
89
90
			// Enqueue front end assets.
91
			$this->enqueue_scripts();
92
93
			?>
94
			<img src="<?php echo esc_url( $gravatar_url ); ?>" class="grofile-thumbnail no-grav" alt="<?php echo esc_attr( $profile['displayName'] ); ?>" />
95
			<div class="grofile-meta">
96
				<h4><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>"><?php echo esc_html( $profile['displayName'] ); ?></a></h4>
97
				<p><?php echo wp_kses_post( $profile['aboutMe'] ); ?></p>
98
			</div>
99
100
			<?php
101
102
			if( $instance['show_personal_links'] )
103
				$this->display_personal_links( (array) $profile['urls'] );
104
105
			if( $instance['show_account_links'] )
106
				$this->display_accounts( (array) $profile['accounts'] );
107
108
			?>
109
110
			<p><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>" class="grofile-full-link">
111
				<?php echo esc_html(
112
					/**
113
					 * Filter the Gravatar Profile widget's profile link title.
114
					 *
115
					 * @module widgets
116
					 *
117
					 * @since 2.8.0
118
					 *
119
					 * @param string $str Profile link title.
120
					 */
121
					apply_filters(
122
						'jetpack_gravatar_full_profile_title',
123
						__( 'View Full Profile &rarr;', 'jetpack' )
124
					)
125
				); ?>
126
			</a></p>
127
128
			<?php
129
		} else {
130
			if ( current_user_can( 'edit_theme_options' ) ) {
131
				echo '<p>' . esc_html__( 'Error loading profile', 'jetpack' ) . '</p>';
132
			}
133
		}
134
135
		echo $args['after_widget'];
136
	}
137
138
	function display_personal_links( $personal_links = array() ) {
139
		if ( empty( $personal_links ) )
140
			return;
141
		?>
142
143
			<h4><?php echo esc_html(
144
				apply_filters(
145
					/**
146
					 * Filter the Gravatar Profile widget's "Personal Links" section title.
147
					 *
148
					 * @module widgets
149
					 *
150
					 * @since 2.8.0
151
					 *
152
					 * @param string $str "Personal Links" section title.
153
					 */
154
					'jetpack_gravatar_personal_links_title',
155
					__( 'Personal Links', 'jetpack' )
156
					)
157
				); ?></h4>
158
			<ul class="grofile-urls grofile-links">
159
160
			<?php foreach( $personal_links as $personal_link ) : ?>
161
				<li>
162
					<a href="<?php echo esc_url( $personal_link['value'] ); ?>">
163
						<?php
164
							$link_title = ( ! empty( $personal_link['title'] ) ) ? $personal_link['title'] : $personal_link['value'];
165
							echo esc_html( $link_title );
166
						?>
167
					</a>
168
				</li>
169
			<?php endforeach; ?>
170
			</ul>
171
172
		<?php
173
	}
174
175
	function display_accounts( $accounts = array() ) {
176
		if ( empty( $accounts ) )
177
			return;
178
		?>
179
180
		<h4><?php echo esc_html(
181
				/**
182
				 * Filter the Gravatar Profile widget's "Verified Services" section title.
183
				 *
184
				 * @module widgets
185
				 *
186
				 * @since 2.8.0
187
				 *
188
				 * @param string $str "Verified Services" section title.
189
				 */
190
				apply_filters(
191
					'jetpack_gravatar_verified_services_title',
192
					__( 'Verified Services', 'jetpack' )
193
				)
194
			); ?></h4>
195
		<ul class="grofile-urls grofile-accounts">
196
197
		<?php foreach( $accounts as $account ) :
198
			if( $account['verified'] != 'true' )
199
				continue;
200
201
			$sanitized_service_name = $this->get_sanitized_service_name( $account['shortname'] );
202
			?>
203
204
			<li>
205
				<a href="<?php echo esc_url( $account['url'] ); ?>" title="<?php echo sprintf( _x( '%1$s on %2$s', '1: User Name, 2: Service Name (Facebook, Twitter, ...)', 'jetpack' ), esc_html( $account['display'] ), esc_html( $sanitized_service_name ) ); ?>">
206
					<span class="grofile-accounts-logo grofile-accounts-<?php echo esc_attr( $account['shortname'] ); ?> accounts_<?php echo esc_attr( $account['shortname'] ); ?>"></span>
207
				</a>
208
			</li>
209
210
		<?php endforeach; ?>
211
		</ul>
212
213
		<?php
214
	}
215
216
	/**
217
	 * Enqueue CSS and JavaScript.
218
	 *
219
	 * @since 4.0.0
220
	 */
221
	function enqueue_scripts() {
222
		wp_enqueue_style(
223
			'gravatar-profile-widget',
224
			plugins_url( 'gravatar-profile.css', __FILE__ ),
225
			array(),
226
			'20120711'
227
		);
228
229
		wp_enqueue_style(
230
			'gravatar-card-services',
231
			'https://secure.gravatar.com/css/services.css',
232
			array(),
233
			defined( 'GROFILES__CACHE_BUSTER' ) ? GROFILES__CACHE_BUSTER : gmdate( 'YW' )
234
		);
235
	}
236
237
	function form( $instance ) {
238
		$title               = isset( $instance['title'] ) ? $instance['title'] : '';
239
		$email               = isset( $instance['email'] ) ? $instance['email'] : '';
240
		$email_user          = isset( $instance['email_user'] ) ? $instance['email_user'] : get_current_user_id();
241
		$show_personal_links = isset( $instance['show_personal_links'] ) ? (bool) $instance['show_personal_links'] : '';
242
		$show_account_links  = isset( $instance['show_account_links'] ) ? (bool) $instance['show_account_links'] : '';
243
		$profile_url         = 'https://gravatar.com/profile/edit';
244
245
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
246
			$profile_url = admin_url( 'profile.php' );
247
248
			if ( isset( $_REQUEST['calypso'] ) ) {
249
				$profile_url = 'https://wordpress.com/me';
250
			}
251
		}
252
		?>
253
		<p>
254
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
255
				<?php esc_html_e( 'Title', 'jetpack' ); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
256
			</label>
257
		</p>
258
259
		<p>
260
			<label for="<?php echo $this->get_field_id( 'email_user' ); ?>">
261
				<?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'jetpack' ); ?>
262
				<br />
263
264
				<?php wp_dropdown_users( array(
265
					'show_option_none' => __( 'Custom', 'jetpack' ),
266
					'selected'         => $email_user,
267
					'name'             => $this->get_field_name( 'email_user' ),
268
					'id'               => $this->get_field_id( 'email_user' ),
269
					'class'            => 'gravatar-profile-user-select',
270
				) );?>
271
			</label>
272
		</p>
273
274
		<p class="gprofile-email-container <?php echo empty( $email_user ) || $email_user == -1 ? '' : 'hidden'; ?>">
275
			<label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?>
276
				<input class="widefat" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" />
277
			</label>
278
		</p>
279
280
		<p>
281
			<label for="<?php echo $this->get_field_id( 'show_personal_links' ); ?>">
282
				<input type="checkbox" name="<?php echo $this->get_field_name( 'show_personal_links' ); ?>" id="<?php echo $this->get_field_id( 'show_personal_links' ); ?>" <?php checked( $show_personal_links ); ?> />
283
				<?php esc_html_e( 'Show Personal Links', 'jetpack' ); ?>
284
				<br />
285
				<small><?php esc_html_e( 'Links to your websites, blogs, or any other sites that help describe who you are.', 'jetpack' ); ?></small>
286
			</label>
287
		</p>
288
289
		<p>
290
			<label for="<?php echo $this->get_field_id( 'show_account_links' ); ?>">
291
				<input type="checkbox" name="<?php echo $this->get_field_name( 'show_account_links' ); ?>" id="<?php echo $this->get_field_id( 'show_account_links' ); ?>" <?php checked( $show_account_links ); ?> />
292
				<?php esc_html_e( 'Show Account Links', 'jetpack' ); ?>
293
				<br />
294
				<small><?php esc_html_e( 'Links to services that you use across the web.', 'jetpack' ); ?></small>
295
			</label>
296
		</p>
297
298
		<p><a href="<?php echo esc_url( $profile_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Opens in new window', 'jetpack' ); ?>"><?php esc_html_e( 'Edit Your Profile', 'jetpack' )?></a> | <a href="https://gravatar.com" target="_blank" title="<?php esc_attr_e( 'Opens in new window', 'jetpack' ); ?>"><?php esc_html_e( "What's a Gravatar?", 'jetpack' ); ?></a></p>
299
300
		<?php
301
	}
302
303
	function admin_script() {
304
		?>
305
		<script>
306
		jQuery( function( $ ) {
307
			$( '.wrap' ).on( 'change', '.gravatar-profile-user-select', function() {
308
				var $input = $(this).closest('.widget-inside').find('.gprofile-email-container');
309
				if ( '-1' === this.value.toLowerCase() ) {
310
					$input.show();
311
				} else {
312
					$input.hide();
313
				}
314
			});
315
		} );
316
		</script>
317
		<?php
318
	}
319
320
	function update( $new_instance, $old_instance ) {
321
322
		$instance = array();
323
324
		$instance['title']               = isset( $new_instance['title'] ) ? wp_kses( $new_instance['title'], array() ) : '';
325
		$instance['email']               = isset( $new_instance['email'] ) ? wp_kses( $new_instance['email'], array() ) : '';
326
		$instance['email_user']          = isset( $new_instance['email_user'] ) ? intval( $new_instance['email_user'] ) : -1;
327
		$instance['show_personal_links'] = isset( $new_instance['show_personal_links'] ) ? (bool) $new_instance['show_personal_links'] : false;
328
		$instance['show_account_links']  = isset( $new_instance['show_account_links'] ) ? (bool) $new_instance['show_account_links'] : false;
329
330
		if ( $instance['email_user'] > 0 ) {
331
			$user = get_userdata( $instance['email_user'] );
332
			$instance['email'] = $user->user_email;
333
		}
334
335
		$hashed_email = md5( strtolower( trim( $instance['email'] ) ) );
336
		$cache_key = 'grofile-' . $hashed_email;
337
		delete_transient( $cache_key );
338
339
		return $instance;
340
	}
341
342
	private function get_profile( $email ) {
343
		$hashed_email = md5( strtolower( trim( $email ) ) );
344
		$cache_key = 'grofile-' . $hashed_email;
345
346
		if( ! $profile = get_transient( $cache_key ) ) {
347
			$profile_url = esc_url_raw( sprintf( '%s.gravatar.com/%s.json', ( is_ssl() ? 'https://secure' : 'http://www' ), $hashed_email ), array( 'http', 'https' ) );
348
349
			$expire = 300;
350
			$response = wp_remote_get( $profile_url, array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' ) );
351
			$response_code = wp_remote_retrieve_response_code( $response );
352
			if ( 200 == $response_code ) {
353
				$profile = wp_remote_retrieve_body( $response );
354
				$profile = json_decode( $profile, true );
355
356
				if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) {
357
					$expire = 900; // cache for 15 minutes
358
					$profile = $profile['entry'][0];
359
				} else {
360
					// Something strange happened.  Cache for 5 minutes.
361
					$profile = array();
362
				}
363
364
			} else {
365
				$expire = 900; // cache for 15 minutes
366
				$profile = array();
367
			}
368
369
			set_transient( $cache_key, $profile, $expire );
370
		}
371
		return $profile;
372
	}
373
374
	private function get_sanitized_service_name( $shortname ) {
375
		// Some services have stylized or mixed cap names *cough* WP *cough*
376
		switch( $shortname ) {
377
			case 'friendfeed':
378
				return 'FriendFeed';
379
			case 'linkedin':
380
				return 'LinkedIn';
381
			case 'yahoo':
382
				return 'Yahoo!';
383
			case 'youtube':
384
				return 'YouTube';
385
			case 'wordpress':
386
				return 'WordPress';
387
			case 'tripit':
388
				return 'TripIt';
389
			case 'myspace':
390
				return 'MySpace';
391
			case 'foursquare':
392
				return 'foursquare';
393
			case 'google':
394
				return 'Google+';
395
			default:
396
				// Others don't
397
				$shortname = ucwords( $shortname );
398
		}
399
		return $shortname;
400
	}
401
}
402
403
// END
404