Conditions | 36 |
Paths | > 20000 |
Total Lines | 277 |
Lines | 5 |
Ratio | 1.81 % |
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 |
||
218 | function widget( $args, $instance ) { |
||
219 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
220 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
||
221 | |||
222 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
223 | |||
224 | $title = isset( $instance['title'] ) ? $instance['title'] : false; |
||
225 | if ( false === $title ) { |
||
226 | $title = $this->default_title; |
||
227 | } |
||
228 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
229 | $title = apply_filters( 'widget_title', $title ); |
||
230 | |||
231 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
||
232 | if ( $count < 1 || 10 < $count ) { |
||
233 | $count = 10; |
||
234 | } |
||
235 | /** |
||
236 | * Control the number of displayed posts. |
||
237 | * |
||
238 | * @module widgets |
||
239 | * |
||
240 | * @since 3.3.0 |
||
241 | * |
||
242 | * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
||
243 | */ |
||
244 | $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
||
245 | |||
246 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
247 | |||
248 | // 'likes' are not available in Jetpack |
||
249 | $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
||
250 | |||
251 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
252 | $display = $instance['display']; |
||
253 | } else { |
||
254 | $display = 'text'; |
||
255 | } |
||
256 | |||
257 | if ( 'text' != $display ) { |
||
258 | $get_image_options = array( |
||
259 | 'fallback_to_avatars' => true, |
||
260 | /** This filter is documented in modules/stats.php */ |
||
261 | 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
||
262 | 'avatar_size' => 40, |
||
263 | 'width' => null, |
||
264 | 'height' => null, |
||
265 | ); |
||
266 | if ( 'grid' == $display ) { |
||
267 | $get_image_options['avatar_size'] = 200; |
||
268 | } |
||
269 | /** |
||
270 | * Top Posts Widget Image options. |
||
271 | * |
||
272 | * @module widgets |
||
273 | * |
||
274 | * @since 1.8.0 |
||
275 | * |
||
276 | * @param array $get_image_options { |
||
277 | * Array of Image options. |
||
278 | * @type bool true Should we default to Gravatars when no image is found? Default is true. |
||
279 | * @type string $gravatar_default Default Image URL if no Gravatar is found. |
||
280 | * @type int $avatar_size Default Image size. |
||
281 | * @type mixed $width Image width, not set by default and $avatar_size is used instead. |
||
282 | * @type mixed $height Image height, not set by default and $avatar_size is used instead. |
||
283 | * } |
||
284 | */ |
||
285 | $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
||
286 | } |
||
287 | |||
288 | if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
||
289 | $posts = $this->get_by_likes( $count, $types ); |
||
290 | } else { |
||
291 | $posts = $this->get_by_views( $count, $args, $types ); |
||
292 | } |
||
293 | |||
294 | if ( ! $posts ) { |
||
295 | $posts = $this->get_fallback_posts( $count, $types ); |
||
296 | } |
||
297 | |||
298 | echo $args['before_widget']; |
||
299 | if ( ! empty( $title ) ) { |
||
300 | echo $args['before_title'] . $title . $args['after_title']; |
||
301 | } |
||
302 | |||
303 | if ( ! $posts ) { |
||
304 | $link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) ); |
||
305 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
306 | $link = 'https://en.support.wordpress.com/getting-more-site-traffic/'; |
||
307 | } |
||
308 | |||
309 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
310 | echo '<p>' . sprintf( |
||
311 | __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), |
||
312 | esc_url( $link ) |
||
313 | ) . '</p>'; |
||
314 | } |
||
315 | |||
316 | echo $args['after_widget']; |
||
317 | return; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Filter the layout of the Top Posts Widget |
||
322 | * |
||
323 | * @module widgets |
||
324 | * |
||
325 | * @since 6.4.0 |
||
326 | * |
||
327 | * @param string $layout layout of the Top Posts Widget (empty string) |
||
328 | * @param array $posts IDs of the posts to be displayed |
||
329 | * @param array $display Display option from widget form |
||
330 | */ |
||
331 | $layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display ); |
||
332 | if ( ! empty( $layout ) ) { |
||
333 | echo $layout; |
||
334 | echo $args['after_widget']; |
||
335 | return; |
||
336 | } |
||
337 | |||
338 | switch ( $display ) { |
||
339 | case 'list': |
||
340 | case 'grid': |
||
341 | // Keep the avatar_size as default dimensions for backward compatibility. |
||
342 | $width = (int) $get_image_options['avatar_size']; |
||
343 | $height = (int) $get_image_options['avatar_size']; |
||
344 | |||
345 | // Check if the user has changed the width. |
||
346 | if ( ! empty( $get_image_options['width'] ) ) { |
||
347 | $width = (int) $get_image_options['width']; |
||
348 | } |
||
349 | |||
350 | // Check if the user has changed the height. |
||
351 | if ( ! empty( $get_image_options['height'] ) ) { |
||
352 | $height = (int) $get_image_options['height']; |
||
353 | } |
||
354 | |||
355 | foreach ( $posts as &$post ) { |
||
356 | $image = Jetpack_PostImages::get_image( |
||
357 | $post['post_id'], |
||
358 | array( |
||
359 | 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
||
360 | 'width' => (int) $width, |
||
361 | 'height' => (int) $height, |
||
362 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
||
363 | ) |
||
364 | ); |
||
365 | $post['image'] = $image['src']; |
||
366 | if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
||
367 | $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) ); |
||
368 | } |
||
369 | } |
||
370 | |||
371 | unset( $post ); |
||
372 | |||
373 | if ( 'grid' == $display ) { |
||
374 | echo "<div class='widgets-grid-layout no-grav'>\n"; |
||
375 | foreach ( $posts as $post ) : |
||
376 | ?> |
||
377 | <div class="widget-grid-view-image"> |
||
378 | <?php |
||
379 | /** |
||
380 | * Fires before each Top Post result, inside <li>. |
||
381 | * |
||
382 | * @module widgets |
||
383 | * |
||
384 | * @since 3.2.0 |
||
385 | * |
||
386 | * @param string $post['post_id'] Post ID. |
||
387 | */ |
||
388 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
389 | |||
390 | /** |
||
391 | * Filter the permalink of items in the Top Posts widget. |
||
392 | * |
||
393 | * @module widgets |
||
394 | * |
||
395 | * @since 4.4.0 |
||
396 | * |
||
397 | * @param string $post['permalink'] Post permalink. |
||
398 | * @param array $post Post array. |
||
399 | */ |
||
400 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
401 | |||
402 | printf( |
||
403 | '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true"/></a>', |
||
404 | esc_url( $filtered_permalink ), |
||
405 | esc_attr( wp_kses( $post['title'], array() ) ), |
||
406 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
407 | absint( $width ), |
||
408 | absint( $height ), |
||
409 | esc_url( $post['image'] ) |
||
410 | ); |
||
411 | |||
412 | /** |
||
413 | * Fires after each Top Post result, inside <li>. |
||
414 | * |
||
415 | * @module widgets |
||
416 | * |
||
417 | * @since 3.2.0 |
||
418 | * |
||
419 | * @param string $post['post_id'] Post ID. |
||
420 | */ |
||
421 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
422 | ?> |
||
423 | </div> |
||
424 | <?php |
||
425 | endforeach; |
||
426 | echo "</div>\n"; |
||
427 | } else { |
||
428 | echo "<ul class='widgets-list-layout no-grav'>\n"; |
||
429 | foreach ( $posts as $post ) : |
||
430 | ?> |
||
431 | <li> |
||
432 | <?php |
||
433 | /** This action is documented in modules/widgets/top-posts.php */ |
||
434 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
435 | |||
436 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
437 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
438 | |||
439 | printf( |
||
440 | '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s> |
||
441 | <img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar"/> |
||
442 | </a> |
||
443 | <div class="widgets-list-layout-links"> |
||
444 | <a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s>%7$s</a> |
||
445 | </div> |
||
446 | ', |
||
447 | esc_url( $filtered_permalink ), |
||
448 | esc_attr( wp_kses( $post['title'], array() ) ), |
||
449 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
450 | absint( $width ), |
||
451 | absint( $height ), |
||
452 | esc_url( $post['image'] ), |
||
453 | esc_html( wp_kses( $post['title'], array() ) ) |
||
454 | ); |
||
455 | |||
456 | /** This action is documented in modules/widgets/top-posts.php */ |
||
457 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
458 | ?> |
||
459 | </li> |
||
460 | <?php |
||
461 | endforeach; |
||
462 | echo "</ul>\n"; |
||
463 | } |
||
464 | break; |
||
465 | default: |
||
466 | echo '<ul>'; |
||
467 | foreach ( $posts as $post ) : |
||
468 | ?> |
||
469 | <li> |
||
470 | <?php |
||
471 | /** This action is documented in modules/widgets/top-posts.php */ |
||
472 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
473 | |||
474 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
475 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
476 | |||
477 | printf( |
||
478 | '<a href="%1$s" class="bump-view" data-bump-view="tp"%2$s>%3$s</a>', |
||
479 | esc_url( $filtered_permalink ), |
||
480 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
481 | esc_html( wp_kses( $post['title'], array() ) ) |
||
482 | ); |
||
483 | |||
484 | /** This action is documented in modules/widgets/top-posts.php */ |
||
485 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
486 | ?> |
||
487 | </li> |
||
488 | <?php |
||
489 | endforeach; |
||
490 | echo '</ul>'; |
||
491 | } |
||
492 | |||
493 | echo $args['after_widget']; |
||
494 | } |
||
495 | |||
733 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: