| Conditions | 14 |
| Paths | 108 |
| Total Lines | 110 |
| Code Lines | 50 |
| Lines | 3 |
| Ratio | 2.73 % |
| 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 |
||
| 120 | public function widget( $args, $instance ) { |
||
| 121 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
||
| 122 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 123 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
||
| 124 | if ( ! $this->check_genericons() ) { |
||
| 125 | wp_enqueue_style( 'genericons' ); |
||
| 126 | } |
||
| 127 | $index = 10; |
||
| 128 | $html = array(); |
||
| 129 | $alt_text = esc_attr__( 'View %1$s’s profile on %2$s', 'jetpack' ); |
||
| 130 | foreach ( $this->services as $service => $data ) { |
||
| 131 | list( $service_name, $url ) = $data; |
||
| 132 | if ( ! isset( $instance[ $service . '_username' ] ) ) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | $username = $link_username = $instance[ $service . '_username' ]; |
||
| 136 | if ( empty( $username ) ) { |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | $index += 10; |
||
| 140 | $predefined_url = false; |
||
| 141 | |||
| 142 | /** Check if full URL entered in configuration, use it instead of tinkering **/ |
||
| 143 | if ( |
||
| 144 | in_array( |
||
| 145 | parse_url( $username, PHP_URL_SCHEME ), |
||
| 146 | array( 'http', 'https' ) |
||
| 147 | ) |
||
| 148 | ) { |
||
| 149 | $predefined_url = $username; |
||
| 150 | |||
| 151 | // In case of a predefined link we only display the service name |
||
| 152 | // for screen readers |
||
| 153 | $alt_text = '%2$s'; |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | if ( 'googleplus' === $service |
||
| 158 | && ! is_numeric( $username ) |
||
| 159 | && substr( $username, 0, 1 ) !== '+' |
||
| 160 | ) { |
||
| 161 | $link_username = '+' . $username; |
||
| 162 | } |
||
| 163 | if ( 'youtube' === $service && 'UC' === substr( $username, 0, 2 ) ) { |
||
| 164 | $link_username = 'channel/' . $username; |
||
| 165 | } else if ( 'youtube' === $service ) { |
||
| 166 | $link_username = 'user/' . $username; |
||
| 167 | } |
||
| 168 | |||
| 169 | if ( ! $predefined_url ) { |
||
| 170 | $predefined_url = sprintf( $url, $link_username ); |
||
| 171 | } |
||
| 172 | /** |
||
| 173 | * Fires for each profile link in the social icons widget. Can be used |
||
| 174 | * to change the links for certain social networks if needed. All URLs |
||
| 175 | * will be passed through `esc_attr` on output. |
||
| 176 | * |
||
| 177 | * @module widgets |
||
| 178 | * |
||
| 179 | * @since 3.8.0 |
||
| 180 | * |
||
| 181 | * @param string $url the currently processed URL |
||
| 182 | * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc. |
||
| 183 | */ |
||
| 184 | $link = apply_filters( |
||
| 185 | 'jetpack_social_media_icons_widget_profile_link', |
||
| 186 | $predefined_url, |
||
| 187 | $service |
||
| 188 | ); |
||
| 189 | $html[ $index ] = sprintf( |
||
| 190 | '<a href="%1$s" class="genericon genericon-%2$s" target="_blank"><span class="screen-reader-text">%3$s</span></a>', |
||
| 191 | esc_attr( $link ), |
||
| 192 | esc_attr( $service ), |
||
| 193 | sprintf( $alt_text, esc_html( $username ), $service_name ) |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | /** |
||
| 197 | * Fires at the end of the list of Social Media accounts. |
||
| 198 | * Can be used to add a new Social Media Site to the Social Media Icons Widget. |
||
| 199 | * The filter function passed the array of HTML entries that will be sorted |
||
| 200 | * by key, each wrapped in a list item element and output as an unsorted list. |
||
| 201 | * |
||
| 202 | * @module widgets |
||
| 203 | * |
||
| 204 | * @since 3.8.0 |
||
| 205 | * |
||
| 206 | * @param array $html Associative array of HTML snippets per each icon. |
||
| 207 | */ |
||
| 208 | $html = apply_filters( 'jetpack_social_media_icons_widget_array', $html ); |
||
| 209 | ksort( $html ); |
||
| 210 | $html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>'; |
||
| 211 | View Code Duplication | if ( ! empty( $instance['title'] ) ) { |
|
| 212 | $html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html; |
||
| 213 | } |
||
| 214 | $html = $args['before_widget'] . $html . $args['after_widget']; |
||
| 215 | |||
| 216 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 217 | do_action( 'jetpack_stats_extra', 'widget_view', 'social_media_icons' ); |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Filters the Social Media Icons widget output. |
||
| 221 | * |
||
| 222 | * @module widgets |
||
| 223 | * |
||
| 224 | * @since 3.6.0 |
||
| 225 | * |
||
| 226 | * @param string $html Social Media Icons widget html output. |
||
| 227 | */ |
||
| 228 | echo apply_filters( 'jetpack_social_media_icons_widget_output', $html ); |
||
| 229 | } |
||
| 230 | |||
| 331 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.