Completed
Push — try/user_token-auth-for-rest-a... ( 422660...ea5d0d )
by
unknown
08:47
created

gravatar.php ➔ jetpack_gravatar_profile_shortcode()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 89
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 20
nop 1
dl 0
loc 89
rs 5.6482
c 0
b 0
f 0

How to fix   Long Method   

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
 * Gravatar shortcode for avatar and profile.
4
 *
5
 * Usage:
6
 *
7
 * [gravatar email="[email protected]" size="48"]
8
 * [gravatar_profile who="[email protected]"]
9
 */
10
11
add_shortcode( 'gravatar', 'jetpack_gravatar_shortcode' );
12
add_shortcode( 'gravatar_profile', 'jetpack_gravatar_profile_shortcode' );
13
14
/**
15
 * Get gravatar using the email provided at the specified size.
16
 *
17
 * @since 4.5.0
18
 *
19
 * @param array $atts Shortcode attributes.
20
 *
21
 * @return bool|string
22
 */
23
function jetpack_gravatar_shortcode( $atts ) {
24
	$atts = shortcode_atts( array(
25
		'email' => '',
26
		'size'  => 96,
27
	), $atts );
28
29
	if ( empty( $atts['email'] ) || ! is_email( $atts['email'] ) ) {
30
		return false;
31
	}
32
33
	$atts['size'] = intval( $atts['size'] );
34
	if ( 0 > $atts['size'] ) {
35
		$atts['size'] = 96;
36
	}
37
38
	return get_avatar( $atts['email'], $atts['size'] );
39
}
40
41
/**
42
 * Display Gravatar profile
43
 *
44
 * @since 4.5.0
45
 *
46
 * @param array $atts Shortcode attributes.
47
 *
48
 * @uses shortcode_atts()
49
 * @uses get_user_by()
50
 * @uses is_email()
51
 * @uses sanitize_email()
52
 * @uses sanitize_user()
53
 * @uses set_url_scheme()
54
 * @uses wpcom_get_avatar_url()
55
 * @uses get_user_attribute()
56
 * @uses esc_url()
57
 * @uses esc_html()
58
 * @uses _e()
59
 *
60
 * @return string
61
 */
62
function jetpack_gravatar_profile_shortcode( $atts ) {
63
	// Give each use of the shortcode a unique ID
64
	static $instance = 0;
65
66
	// Process passed attributes
67
	$atts = shortcode_atts( array(
68
		'who' => null,
69
	), $atts, 'jetpack_gravatar_profile' );
70
71
	// Can specify username, user ID, or email address
72
	if ( is_numeric( $atts['who'] ) ) {
73
		$user = get_user_by( 'id', (int) $atts['who'] );
74
	} elseif ( is_email( $atts['who'] ) ) {
75
		$user = get_user_by( 'email', sanitize_email( $atts['who'] ) );
76
	} elseif ( is_string( $atts['who'] ) ) {
77
		$user = get_user_by( 'login', sanitize_user( $atts['who'] ) );
78
	} else {
79
		$user = false;
80
	}
81
82
	// Bail if we don't have a user
83
	if ( false === $user ) {
84
		return false;
85
	}
86
87
	// Render the shortcode
88
	$gravatar_url  = set_url_scheme( 'http://gravatar.com/' . $user->user_login );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
89
90
	if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
91
		$avatar_url    = wpcom_get_avatar_url( $user->ID, 96 );
92
		$avatar_url    = $avatar_url[0];
93
		$user_location = get_user_attribute( $user->ID, 'location' );
94
	} else {
95
		$avatar_url    = get_avatar_url( $user->user_email, array( 'size' => 96 ) );
96
		$user_location = get_user_meta( $user->ID, 'location', true );
97
	}
98
99
	ob_start();
100
101
	?>
102
	<script type="text/javascript">
103
		( function() {
104
			if ( null === document.getElementById( 'gravatar-profile-embed-styles' ) ) {
105
				var headID = document.getElementsByTagName( 'head' )[0];
106
				var styleNode = document.createElement( 'style' );
107
				styleNode.type = 'text/css';
108
				styleNode.id = 'gravatar-profile-embed-styles';
109
110
				var gCSS = '.grofile-wrap { border: solid 1px #eee; padding: 10px; } .grofile { padding: 0 0 5px 0; }  .grofile-left { float: left; display: block; width: 96px; margin-right: 15px; } .grofile .gravatar { margin-bottom: 5px; } .grofile-clear { clear: left; font-size: 1px; height: 1px; } .grofile ul li a { text-indent: -99999px; } .grofile .grofile-left a:hover { text-decoration: none !important; border: none !important; } .grofile-name { margin-top: 0; }';
111
112
				if ( document.all ) {
113
					styleNode.innerText = gCSS;
114
				} else {
115
					styleNode.textContent = gCSS;
116
				}
117
118
				headID.appendChild( styleNode );
119
			}
120
		} )();
121
	</script>
122
123
	<div class="grofile vcard" id="grofile-embed-<?php echo esc_attr( $instance ); ?>">
124
		<div class="grofile-inner">
125
			<div class="grofile-left">
126
				<div class="grofile-img">
127
					<a href="<?php echo esc_url( $gravatar_url ); ?>">
128
						<img src="<?php echo esc_url( $avatar_url ); ?>" width="96" height="96" class="no-grav gravatar photo" />
129
					</a>
130
				</div>
131
			</div>
132
			<div class="grofile-right">
133
				<p class="grofile-name fn">
134
					<strong><?php echo esc_html( $user->display_name ); ?></strong>
135
					<?php if ( ! empty( $user_location ) ) : ?><br><span class="grofile-location adr"><?php echo esc_html( $user_location ); ?></span><?php endif; ?>
136
				</p>
137
				<p class="grofile-bio"><strong><?php esc_html_e( 'Bio:', 'jetpack' ); ?></strong> <?php echo wp_kses_post( $user->description ); ?></p>
138
				<p class="grofile-view">
139
					<a href="<?php echo esc_url( $gravatar_url ); ?>"><?php esc_html_e( 'View complete profile', 'jetpack' ); ?></a>
140
				</p>
141
			</div>
142
			<span class="grofile-clear">&nbsp;</span>
143
		</div>
144
	</div><?php
145
146
	// Increment and return the rendered profile
147
	$instance++;
148
149
	return ob_get_clean();
150
}
151