MonsterInsights_Popular_Posts_Widget   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 107
c 3
b 0
f 0
dl 0
loc 263
rs 8.8798
wmc 44

8 Methods

Rating   Name   Duplication   Size   Complexity  
F build_inline_styles() 0 59 13
A maybe_auto_insert() 0 5 4
A is_theme_available() 0 9 3
A remove_widget_from_legacy_widgets() 0 4 1
F get_rendered_html() 0 81 19
A hooks() 0 9 1
A add_inline_posts_to_content() 0 9 2
A register_widget() 0 2 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * Code specific to the widget Popular Posts widget type.
4
 */
5
6
/**
7
 * Class MonsterInsights_Popular_Posts_Widget
8
 */
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;
0 ignored issues
show
Bug Best Practice introduced by
The property theme does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property count does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
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 ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The property sort does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
			$title_text = $this->title_text;
0 ignored issues
show
Bug Best Practice introduced by
The property title_text does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
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() {
151
152
		$themes = $this->get_themes_styles_for_output();
153
		$styles = '';
154
155
		foreach ( $themes as $theme_key => $theme_styles ) {
156
157
			if ( ! empty( $theme_styles['background'] ) ) {
158
				$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-list li {';
159
160
				if ( ! empty( $theme_styles['background']['color'] ) ) {
161
					$styles .= 'background-color:' . esc_attr($theme_styles['background']['color']) . ';';
162
				}
163
				if ( ! empty( $theme_styles['background']['border'] ) ) {
164
					$styles .= 'border-color:' . esc_attr($theme_styles['background']['border']) . ';';
165
				}
166
167
				$styles .= '}';
168
			}
169
170
			if ( ! empty( $theme_styles['label'] ) ) {
171
				$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-label {';
172
173
				if ( ! empty( $theme_styles['label']['color'] ) ) {
174
					$styles .= 'color:' . esc_attr($theme_styles['label']['color']) . ';';
175
				}
176
177
				if ( ! empty( $theme_styles['label']['background'] ) ) {
178
					$styles .= 'background-color:' . esc_attr($theme_styles['label']['background']) . ';';
179
				}
180
181
				$styles .= '}';
182
			}
183
184
			if ( ! empty( $theme_styles['title'] ) ) {
185
				$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-list li .monsterinsights-widget-popular-posts-title {';
186
187
				if ( ! empty( $theme_styles['title']['color'] ) ) {
188
					$styles .= 'color:' . esc_attr($theme_styles['title']['color']) . ';';
189
				}
190
				if ( ! empty( $theme_styles['title']['size'] ) ) {
191
					$styles .= 'font-size:' . esc_attr($theme_styles['title']['size']) . 'px;';
192
				}
193
194
				$styles .= '}';
195
			}
196
197
			if ( ! empty( $theme_styles['border'] ) ) {
198
				$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-inline-popular-posts-border {';
199
200
				if ( ! empty( $theme_styles['border']['color'] ) ) {
201
					$styles .= 'border-color:' . esc_attr($theme_styles['border']['color']) . ';';
202
				}
203
204
				$styles .= '}';
205
			}
206
		}
207
208
		return $styles;
209
	}
210
211
	/**
212
	 * Check if we should attempt to automatically insert the inline widget.
213
	 */
214
	public function maybe_auto_insert() {
215
216
		$post_types = $this->post_types;
0 ignored issues
show
Bug Best Practice introduced by
The property post_types does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
217
		if ( ! empty( $post_types ) && is_singular( $post_types ) && $this->automatic ) {
0 ignored issues
show
Bug Best Practice introduced by
The property automatic does not exist on MonsterInsights_Popular_Posts_Widget. Since you implemented __get, consider adding a @property annotation.
Loading history...
218
			add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) );
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 ) {
250
251
		$theme_props = $this->get_theme_props( $theme )->get_theme();
252
253
		if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) {
254
			return $theme;
255
		}
256
257
		return 'alpha';
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 ) {
269
		// $widgets[] = 'monsterinsights-popular-posts-widget';
270
271
		return $widgets;
272
	}
273
274
}
275
276
/**
277
 * Get the current class in a function.
278
 *
279
 * @return MonsterInsights_Popular_Posts_Widget Instance of the current class.
280
 */
281
function MonsterInsights_Popular_Posts_Widget() {
282
	return MonsterInsights_Popular_Posts_Widget::get_instance();
283
}
284
285
MonsterInsights_Popular_Posts_Widget();
286