| Total Complexity | 44 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Popular_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.
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 MonsterInsights_Popular_Posts_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class MonsterInsights_Popular_Posts_Widget extends MonsterInsights_Popular_Posts { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * The instance type. Used for loading specific settings. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $type = 'widget'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Used to load the setting specific for this class. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $settings_key = 'popular_posts_widget'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Used for registering the shortcode specific to this class. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $shortcode_key = 'monsterinsights_popular_posts_widget'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Widget-specific hooks. |
||
| 34 | */ |
||
| 35 | public function hooks() { |
||
| 36 | parent::hooks(); |
||
| 37 | |||
| 38 | add_action( 'wp', array( $this, 'maybe_auto_insert' ) ); |
||
| 39 | |||
| 40 | add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
||
| 41 | add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( |
||
| 42 | $this, |
||
| 43 | 'remove_widget_from_legacy_widgets' |
||
| 44 | ) ); |
||
| 45 | } |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Register Popular Posts widget. |
||
| 50 | */ |
||
| 51 | public function register_widget() { |
||
| 52 | register_widget( 'MonsterInsights_Popular_Posts_Widget_Sidebar' ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the rendered HTML for output. |
||
| 57 | * |
||
| 58 | * @param array $atts These are attributes used to build the specific instance, they can be either shortcode |
||
| 59 | * attributes or Gutenberg block props. |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function get_rendered_html( $atts ) { |
||
| 64 | |||
| 65 | $theme = $this->theme; |
||
|
|
|||
| 66 | if ( ! empty( $atts['theme'] ) ) { |
||
| 67 | $theme = $atts['theme']; |
||
| 68 | } |
||
| 69 | |||
| 70 | $theme = $this->is_theme_available( $theme ); |
||
| 71 | |||
| 72 | if ( ! empty( $atts['post_count'] ) ) { |
||
| 73 | $limit = intval( $atts['post_count'] ); |
||
| 74 | } else { |
||
| 75 | $limit = $this->count; |
||
| 76 | } |
||
| 77 | |||
| 78 | $posts = $this->get_posts_to_display(); |
||
| 79 | |||
| 80 | if ( empty( $posts ) ) { |
||
| 81 | return ''; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( 'curated' === $this->sort && apply_filters( 'monsterinsights_popular_posts_widget_curated_shuffle', true ) ) { |
||
| 85 | // Randomize the order. |
||
| 86 | shuffle( $posts ); |
||
| 87 | } |
||
| 88 | |||
| 89 | $theme_styles = $this->get_theme_props( $theme )->get_theme(); |
||
| 90 | |||
| 91 | $label_text = ''; |
||
| 92 | if ( isset( $theme_styles['styles']['label'] ) ) { |
||
| 93 | $label_text = isset( $atts['label_text'] ) ? esc_html($atts['label_text']) : esc_html($theme_styles['styles']['label']['text']); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( isset( $atts['widget_title'] ) ) { |
||
| 97 | $show_title = (bool) $atts['widget_title']; |
||
| 98 | $title_text = empty( $atts['widget_title_text'] ) ? '' : $atts['widget_title_text']; |
||
| 99 | } else { |
||
| 100 | $show_title = $this->title; |
||
| 101 | $title_text = $this->title_text; |
||
| 102 | } |
||
| 103 | |||
| 104 | $html = '<div class="' . esc_attr($this->get_wrapper_class( $atts )) . '">'; |
||
| 105 | if ( $show_title ) { |
||
| 106 | $html .= '<h2 class="monsterinsights-widget-popular-posts-widget-title">' . esc_html( $title_text ) . '</h2>'; |
||
| 107 | } |
||
| 108 | |||
| 109 | $html .= '<ul class="monsterinsights-widget-popular-posts-list">'; |
||
| 110 | |||
| 111 | $display_count = 0; |
||
| 112 | foreach ( $posts as $post ) { |
||
| 113 | $display_count ++; |
||
| 114 | if ( $display_count > $limit ) { |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | $this->set_post_shown( $post['id'] ); |
||
| 118 | $html .= '<li '; |
||
| 119 | $html .= ! empty( $this->get_element_style( $theme, 'background', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'background', $atts ) ) . '"' : ''; |
||
| 120 | $html .= '>'; |
||
| 121 | $html .= '<a href="' . esc_url($post['link']) . '">'; |
||
| 122 | if ( ! empty( $theme_styles['image'] ) && ! empty( $post['image'] ) ) { |
||
| 123 | $html .= '<div class="monsterinsights-widget-popular-posts-image">'; |
||
| 124 | $html .= '<img src="' . esc_url($post['image']) . '" srcset=" ' . esc_attr($post['srcset']) . ' " alt="' . esc_attr( $post['title'] ) . '" />'; |
||
| 125 | $html .= '</div>'; |
||
| 126 | } |
||
| 127 | $html .= '<div class="monsterinsights-widget-popular-posts-text">'; |
||
| 128 | if ( isset( $theme_styles['styles']['label'] ) ) { |
||
| 129 | $html .= '<span class="monsterinsights-widget-popular-posts-label" '; |
||
| 130 | $html .= ! empty( $this->get_element_style( $theme, 'label', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'label', $atts ) ) . '"' : ''; |
||
| 131 | $html .= '>' . esc_html( $label_text ) . '</span>'; |
||
| 132 | } |
||
| 133 | $html .= '<span class="monsterinsights-widget-popular-posts-title" '; |
||
| 134 | $html .= ! empty( $this->get_element_style( $theme, 'title', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'title', $atts ) ) . '"' : ''; |
||
| 135 | $html .= '>' . esc_html( $post['title'] ) . '</span>'; |
||
| 136 | $html .= '</div>'; // monsterinsights-widget-popular-posts-text. |
||
| 137 | $html .= '</a>'; |
||
| 138 | $html .= '</li>'; |
||
| 139 | } |
||
| 140 | |||
| 141 | $html .= '</ul></div><p></p>';// Main div. |
||
| 142 | |||
| 143 | return $html; |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Add widget-specific styles based on theme settings. |
||
| 149 | */ |
||
| 150 | public function build_inline_styles() { |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Check if we should attempt to automatically insert the inline widget. |
||
| 213 | */ |
||
| 214 | public function maybe_auto_insert() { |
||
| 219 | } |
||
| 220 | |||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Insert the widget in the content. |
||
| 225 | * |
||
| 226 | * @param string $content The post content. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function add_inline_posts_to_content( $content ) { |
||
| 231 | |||
| 232 | if ( $this->is_post_excluded() ) { |
||
| 233 | return $content; |
||
| 234 | } |
||
| 235 | |||
| 236 | $content .= $this->shortcode_output( array() ); |
||
| 237 | |||
| 238 | return $content; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Check if the selected theme is available with the current license to avoid showing a theme not available. |
||
| 243 | * Returns the default 'alpha' theme if not available. |
||
| 244 | * |
||
| 245 | * @param string $theme Theme slug for which we are checking. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function is_theme_available( $theme ) { |
||
| 258 | |||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove this widget from legacy widgets not to have duplications. |
||
| 263 | * |
||
| 264 | * @param string[] $widgets An array of excluded widget-type IDs. |
||
| 265 | * |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | public function remove_widget_from_legacy_widgets( $widgets ) { |
||
| 272 | } |
||
| 273 | |||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 286 |