Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Top_Posts_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Top_Posts_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Jetpack_Top_Posts_Widget extends WP_Widget { |
||
| 29 | public $alt_option_name = 'widget_stats_topposts'; |
||
| 30 | public $default_title = ''; |
||
| 31 | |||
| 32 | function __construct() { |
||
| 33 | parent::__construct( |
||
| 34 | 'top-posts', |
||
| 35 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 36 | apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ), |
||
| 37 | array( |
||
| 38 | 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ), |
||
| 39 | 'customize_selective_refresh' => true, |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); |
||
| 44 | |||
| 45 | View Code Duplication | if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
|
| 46 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Add explanation about how the statistics are calculated. |
||
| 51 | * |
||
| 52 | * @module widgets |
||
| 53 | * |
||
| 54 | * @since 3.9.3 |
||
| 55 | */ |
||
| 56 | add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) ); |
||
| 57 | } |
||
| 58 | |||
| 59 | function enqueue_style() { |
||
| 63 | |||
| 64 | function form( $instance ) { |
||
| 65 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
| 66 | |||
| 67 | $title = stripslashes( $instance['title'] ); |
||
| 68 | |||
| 69 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : 10; |
||
| 70 | if ( $count < 1 || 10 < $count ) { |
||
| 71 | $count = 10; |
||
| 72 | } |
||
| 73 | |||
| 74 | $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 75 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
| 76 | |||
| 77 | // 'likes' are not available in Jetpack |
||
| 78 | $ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views'; |
||
| 79 | |||
| 80 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
| 81 | $display = $instance['display']; |
||
| 82 | } else { |
||
| 83 | $display = 'text'; |
||
| 84 | } |
||
| 85 | |||
| 86 | ?> |
||
| 87 | |||
| 88 | <p> |
||
| 89 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
||
| 90 | <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 ); ?>" /> |
||
| 91 | </p> |
||
| 92 | |||
| 93 | <p> |
||
| 94 | <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label> |
||
| 95 | <input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" /> |
||
| 96 | </p> |
||
| 97 | |||
| 98 | <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?> |
||
| 99 | <p> |
||
| 100 | <label><?php esc_html_e( 'Order Top Posts & Pages By:', 'jetpack' ); ?></label> |
||
| 101 | <ul> |
||
| 102 | <li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-likes" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="likes" <?php checked( 'likes', $ordering ); ?> /> <?php esc_html_e( 'Likes', 'jetpack' ); ?></label></li> |
||
| 103 | <li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-views" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="views" <?php checked( 'views', $ordering ); ?> /> <?php esc_html_e( 'Views', 'jetpack' ); ?></label></li> |
||
| 104 | </ul> |
||
| 105 | </p> |
||
| 106 | <?php endif; ?> |
||
| 107 | |||
| 108 | <p> |
||
| 109 | <label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label> |
||
| 110 | <ul> |
||
| 111 | <?php foreach( $allowed_post_types as $type ) { |
||
| 112 | // Get the Post Type name to display next to the checkbox |
||
| 113 | $post_type_object = get_post_type_object( $type ); |
||
| 114 | $label = $post_type_object->labels->name; |
||
| 115 | |||
| 116 | $checked = ''; |
||
| 117 | if ( in_array( $type, $types ) ) { |
||
| 118 | $checked = 'checked="checked" '; |
||
| 119 | } ?> |
||
| 120 | |||
| 121 | <li><label> |
||
| 122 | <input value="<?php echo esc_attr( $type ); ?>" name="<?php echo $this->get_field_name( 'types' ); ?>[]" id="<?php echo $this->get_field_id( 'types' ); ?>-<?php echo $type; ?>" type="checkbox" <?php echo $checked; ?>> |
||
| 123 | <?php echo esc_html( $label ); ?> |
||
| 124 | </label></li> |
||
| 125 | |||
| 126 | <?php } // End foreach ?> |
||
| 127 | </ul> |
||
| 128 | </p> |
||
| 129 | |||
| 130 | <p> |
||
| 131 | <label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label> |
||
| 132 | <ul> |
||
| 133 | <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-text" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li> |
||
| 134 | <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-list" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li> |
||
| 135 | <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-grid" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li> |
||
| 136 | </ul> |
||
| 137 | </p><?php |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Fires after the fields are displayed in the Top Posts Widget settings in wp-admin. |
||
| 141 | * |
||
| 142 | * Allow adding extra content after the fields are displayed. |
||
| 143 | * |
||
| 144 | * @module widgets |
||
| 145 | * |
||
| 146 | * @since 3.9.3 |
||
| 147 | * |
||
| 148 | * @param array $args { |
||
| 149 | * @param array $instance The widget instance. |
||
| 150 | * @param object $this The class object. |
||
| 151 | * } |
||
| 152 | */ |
||
| 153 | do_action( 'jetpack_widget_top_posts_after_fields', array( $instance, $this ) ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Explains how the statics are calculated. |
||
| 158 | */ |
||
| 159 | function stats_explanation() { |
||
| 160 | ?> |
||
| 161 | |||
| 162 | <p><?php esc_html_e( 'Top Posts & Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' ); ?></p><?php |
||
| 163 | } |
||
| 164 | |||
| 165 | function update( $new_instance, $old_instance ) { |
||
| 166 | $instance = array(); |
||
| 167 | $instance['title'] = wp_kses( $new_instance['title'], array() ); |
||
| 168 | if ( $instance['title'] === $this->default_title ) { |
||
| 169 | $instance['title'] = false; // Store as false in case of language change |
||
| 170 | } |
||
| 171 | |||
| 172 | $instance['count'] = (int) $new_instance['count']; |
||
| 173 | if ( $instance['count'] < 1 || 10 < $instance['count'] ) { |
||
| 174 | $instance['count'] = 10; |
||
| 175 | } |
||
| 176 | |||
| 177 | // 'likes' are not available in Jetpack |
||
| 178 | $instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' == $new_instance['ordering'] ? 'likes' : 'views'; |
||
| 179 | |||
| 180 | $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); |
||
| 181 | $instance['types'] = $new_instance['types']; |
||
| 182 | foreach( $new_instance['types'] as $key => $type ) { |
||
| 183 | if ( ! in_array( $type, $allowed_post_types ) ) { |
||
| 184 | unset( $new_instance['types'][ $key ] ); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | View Code Duplication | if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
| 189 | $instance['display'] = $new_instance['display']; |
||
| 190 | } else { |
||
| 191 | $instance['display'] = 'text'; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Filters Top Posts Widget settings before they're saved. |
||
| 196 | * |
||
| 197 | * @module widgets |
||
| 198 | * |
||
| 199 | * @since 3.9.3 |
||
| 200 | * |
||
| 201 | * @param array $instance The santized widget instance. Only contains data processed by the current widget. |
||
| 202 | * @param array $new_instance The new widget instance before sanitization. |
||
| 203 | */ |
||
| 204 | $instance = apply_filters( 'jetpack_top_posts_saving', $instance, $new_instance ); |
||
| 205 | |||
| 206 | return $instance; |
||
| 207 | } |
||
| 208 | |||
| 209 | function widget( $args, $instance ) { |
||
| 412 | |||
| 413 | public static function defaults() { |
||
| 422 | |||
| 423 | /* |
||
| 424 | * Get most liked posts |
||
| 425 | * |
||
| 426 | * ONLY TO BE USED IN WPCOM |
||
| 427 | */ |
||
| 428 | function get_by_likes( $count ) { |
||
| 436 | |||
| 437 | function get_by_views( $count, $args ) { |
||
| 438 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 439 | global $wpdb; |
||
| 440 | |||
| 441 | $post_views = wp_cache_get( "get_top_posts_$count", 'stats' ); |
||
| 442 | if ( false === $post_views ) { |
||
| 443 | $post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) ); |
||
| 444 | unset( $post_views[0] ); |
||
| 445 | wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200); |
||
| 446 | } |
||
| 447 | |||
| 448 | return $this->get_posts( array_keys( $post_views ), $count ); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Filter the number of days used to calculate Top Posts for the Top Posts widget. |
||
| 453 | * |
||
| 454 | * @module widgets |
||
| 455 | * |
||
| 456 | * @since 3.9.3 |
||
| 457 | * |
||
| 458 | * @param int 2 Number of days. Default is 2. |
||
| 459 | * @param array $args The widget arguments. |
||
| 460 | */ |
||
| 461 | $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args ); |
||
| 462 | |||
| 463 | if ( $days < 1 ) { |
||
| 464 | $days = 2; |
||
| 465 | } |
||
| 466 | |||
| 467 | if ( $days > 10 ) { |
||
| 468 | $days = 10; |
||
| 469 | } |
||
| 470 | |||
| 471 | $post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) ); |
||
| 472 | if ( ! $post_view_posts ) { |
||
| 473 | return array(); |
||
| 474 | } |
||
| 475 | |||
| 476 | $post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) ); |
||
| 477 | if ( ! $post_view_ids ) { |
||
| 478 | return array(); |
||
| 479 | } |
||
| 480 | |||
| 481 | return $this->get_posts( $post_view_ids, $count ); |
||
| 482 | } |
||
| 483 | |||
| 484 | function get_fallback_posts() { |
||
| 506 | |||
| 507 | function get_posts( $post_ids, $count ) { |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Create a shortcode to display the widget anywhere. |
||
| 559 | * |
||
| 560 | * @since 3.9.2 |
||
| 586 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: