| Conditions | 8 |
| Paths | 20 |
| Total Lines | 89 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 ); |
||
|
|
|||
| 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"> </span> |
||
| 143 | </div> |
||
| 144 | </div><?php |
||
| 145 | |||
| 146 | // Increment and return the rendered profile |
||
| 147 | $instance++; |
||
| 148 | |||
| 149 | return ob_get_clean(); |
||
| 150 | } |
||
| 151 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.