get_rendered_html()   F
last analyzed

Complexity

Conditions 21
Paths 10370

Size

Total Lines 77
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 21
eloc 51
c 3
b 0
f 0
nc 10370
nop 1
dl 0
loc 77
rs 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Code specific to the inline Popular Posts widget type.
4
 */
5
6
/**
7
 * Class MonsterInsights_Popular_Posts_Inline
8
 */
9
class MonsterInsights_Popular_Posts_Inline extends MonsterInsights_Popular_Posts {
10
11
	/**
12
	 * Used to load the setting specific for this class.
13
	 *
14
	 * @var string
15
	 */
16
	protected $settings_key = 'popular_posts_inline';
17
18
	/**
19
	 * Used for registering the shortcode specific to this class.
20
	 *
21
	 * @var string
22
	 */
23
	protected $shortcode_key = 'monsterinsights_popular_posts_inline';
24
25
	/**
26
	 * The instance type. Used for loading specific settings.
27
	 *
28
	 * @var string
29
	 */
30
	protected $type = 'inline';
31
32
	/**
33
	 * Inline-specific hooks.
34
	 */
35
	public function hooks() {
36
		parent::hooks();
37
38
		add_action( 'wp', array( $this, 'maybe_auto_insert' ) );
39
	}
40
41
	/**
42
	 * Get the rendered HTML for output.
43
	 *
44
	 * @param array $atts These are attributes used to build the specific instance, they can be either shortcode
45
	 * attributes or Gutenberg block props.
46
	 *
47
	 * @return string
48
	 */
49
	public function get_rendered_html( $atts ) {
50
51
		$theme = $this->theme;
0 ignored issues
show
Bug Best Practice introduced by
The property theme does not exist on MonsterInsights_Popular_Posts_Inline. Since you implemented __get, consider adding a @property annotation.
Loading history...
52
		if ( ! empty( $atts['theme'] ) ) {
53
			$theme = $atts['theme'];
54
		}
55
56
		$theme = $this->is_theme_available( $theme );
57
58
		$posts = $this->get_posts_to_display();
59
60
		if ( empty( $posts ) ) {
61
			return '';
62
		}
63
64
		if ( 'curated' === $this->sort && apply_filters( 'monsterinsights_popular_posts_inline_curated_shuffle', true ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The property sort does not exist on MonsterInsights_Popular_Posts_Inline. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
			// Randomize the order.
66
			shuffle( $posts );
67
		}
68
69
		$theme_styles = $this->get_theme_props( $theme )->get_theme();
70
		$limit        = ! empty( $theme_styles['posts'] ) ? $theme_styles['posts'] : 1;
71
72
		$label_text = '';
73
		if ( isset( $theme_styles['styles']['label'] ) ) {
74
			$label_text = isset( $atts['labelText'] ) ? $atts['labelText'] : $theme_styles['styles']['label']['text'];
75
		}
76
77
		// Wrap in a P tag to keep the same spacing.
78
		$html  = '<div class="' . esc_attr($this->get_wrapper_class( $atts )) . '" ';
79
		$html .= ! empty( $this->get_element_style( $theme, 'background', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'background', $atts ) ) . '"' : '';
80
		$html .= '>';
81
82
		if ( ! empty( $theme_styles['image'] ) && ! empty( $posts[0]['image'] ) ) {
83
			$html .= '<div class="monsterinsights-inline-popular-posts-image">';
84
			$html .= '<img src="' . esc_url($posts[0]['image']) . '" srcset=" ' . esc_attr($posts[0]['srcset']) . ' " alt="' . esc_attr( $posts[0]['title'] ) . '" />';
85
			$html .= '</div>';
86
		}
87
		$html .= '<div class="monsterinsights-inline-popular-posts-text">';
88
		if ( ! empty( $theme_styles['styles']['icon'] ) ) {
89
			$html .= '<span class="monsterinsights-inline-popular-posts-icon" style=""><svg width="14" height="19" viewBox="0 0 14 19" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7.875 0.899463C7.875 1.59183 8.0816 2.24711 8.49479 2.8653C8.93229 3.48349 9.44271 4.06458 10.026 4.60859C10.6337 5.15259 11.2292 5.73369 11.8125 6.35188C12.4201 6.97007 12.9306 7.76135 13.3438 8.72572C13.7812 9.66537 14 10.7163 14 11.8785C14 13.832 13.3073 15.5011 11.9219 16.8858C10.5608 18.2953 8.92014 19 7 19C5.07986 19 3.42708 18.2953 2.04167 16.8858C0.680556 15.5011 0 13.832 0 11.8785C0 9.94973 0.668403 8.28062 2.00521 6.87116C2.27257 6.57443 2.58854 6.50024 2.95312 6.64861C3.31771 6.79697 3.5 7.08134 3.5 7.50171V10.6545C3.5 11.3221 3.71875 11.8908 4.15625 12.3607C4.61806 12.8305 5.16493 13.0654 5.79688 13.0654C6.45312 13.0654 7.01215 12.8428 7.47396 12.3978C7.93576 11.9279 8.16667 11.3592 8.16667 10.6916C8.16667 10.2712 8.04514 9.86318 7.80208 9.46754C7.58333 9.0719 7.31597 8.71336 7 8.3919C6.68403 8.07044 6.34375 7.73662 5.97917 7.39043C5.63889 7.04425 5.34722 6.66097 5.10417 6.2406C4.88542 5.82024 4.73958 5.35041 4.66667 4.83114C4.59375 4.31186 4.67882 3.68131 4.92188 2.93948C5.18924 2.17293 5.63889 1.33219 6.27083 0.417277C6.51389 0.0463641 6.84201 -0.0772735 7.25521 0.0463641C7.6684 0.170002 7.875 0.454368 7.875 0.899463Z" fill="#EB5757"></path></svg></span>';
90
		}
91
		if ( ! empty( $theme_styles['styles']['label'] ) ) {
92
			$html .= '<span class="monsterinsights-inline-popular-posts-label" ';
93
			$html .= ! empty( $this->get_element_style( $theme, 'label', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'label', $atts ) ) . '"' : '';
94
			$html .= '>' . esc_html( $label_text ) . '</span>';
95
		}
96
		if ( ! empty( $theme_styles['styles']['border'] ) ) {
97
			$html .= '<span class="monsterinsights-inline-popular-posts-border" ';
98
			$html .= ! empty( $this->get_element_style( $theme, 'border', $atts, 'color' ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'border', $atts, 'color' ) ) . '"' : '';
99
			$html .= '></span>';
100
		}
101
		if ( ! empty( $theme_styles['styles']['border']['color2'] ) ) {
102
			$html .= '<span class="monsterinsights-inline-popular-posts-border-2" ';
103
			$html .= ! empty( $this->get_element_style( $theme, 'border', $atts, 'color2' ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'border', $atts, 'color2' ) ) . '"' : '';
104
			$html .= '></span>';
105
		}
106
107
		$display_count = 0;
108
		foreach ( $posts as $post ) {
109
			$display_count ++;
110
			if ( $display_count > $limit ) {
111
				break;
112
			}
113
			$this->set_post_shown( $post['id'] );
114
			$html .= '<div class="monsterinsights-inline-popular-posts-post">';
115
			$html .= '<a class="monsterinsights-inline-popular-posts-title" ';
116
			$html .= ! empty( $this->get_element_style( $theme, 'title', $atts ) ) ? ' style="' . esc_attr( $this->get_element_style( $theme, 'title', $atts ) ) . '" ' : '';
117
			$html .= ' href="' . esc_url( $post['link'] ) . '">' . esc_html( $post['title'] );
118
			$html .= '</a>';
119
			$html .= '</div>';
120
		}
121
122
		$html .= '</div>';// Text div.
123
		$html .= '</div><p></p>';// Main div.
124
125
		return $html;
126
127
	}
128
129
	/**
130
	 * Specific inline styles based on theme settings.
131
	 *
132
	 * @return string
133
	 */
134
	public function build_inline_styles() {
135
136
		$themes = $this->get_themes_styles_for_output();
137
		$styles = '';
138
139
		foreach ( $themes as $theme_key => $theme_styles ) {
140
			if ( ! empty( $theme_styles['background'] ) ) {
141
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' {';
142
143
				if ( ! empty( $theme_styles['background']['color'] ) ) {
144
					$styles .= 'background-color:' . esc_attr($theme_styles['background']['color']) . ';';
145
				}
146
				if ( ! empty( $theme_styles['background']['border'] ) ) {
147
					$styles .= 'border-color:' . esc_attr($theme_styles['background']['border']) . ';';
148
				}
149
150
				$styles .= '}';
151
			}
152
153
			if ( ! empty( $theme_styles['label'] ) ) {
154
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-label {';
155
156
				if ( ! empty( $theme_styles['label']['color'] ) ) {
157
					$styles .= 'color:' . esc_attr($theme_styles['label']['color']) . ';';
158
				}
159
160
				if ( ! empty( $theme_styles['label']['background'] ) ) {
161
					$styles .= 'background-color:' . esc_attr($theme_styles['label']['background']) . ';';
162
				}
163
164
				$styles .= '}';
165
			}
166
167
			if ( ! empty( $theme_styles['title'] ) ) {
168
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-title {';
169
170
				if ( ! empty( $theme_styles['title']['color'] ) ) {
171
					$styles .= 'color:' . esc_attr($theme_styles['title']['color']) . ';';
172
				}
173
				if ( ! empty( $theme_styles['title']['size'] ) ) {
174
					$styles .= 'font-size:' . esc_attr($theme_styles['title']['size']) . 'px;';
175
				}
176
177
				$styles .= '}';
178
			}
179
180
			if ( ! empty( $theme_styles['border'] ) ) {
181
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-border {';
182
183
				if ( ! empty( $theme_styles['border']['color'] ) ) {
184
					$styles .= 'border-color:' . esc_attr($theme_styles['border']['color']) . ';';
185
				}
186
187
				$styles .= '}';
188
			}
189
		}
190
191
		return $styles;
192
	}
193
194
	/**
195
	 * Check if we should attempt to automatically insert the inline widget.
196
	 */
197
	public function maybe_auto_insert() {
198
199
		$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_Inline. Since you implemented __get, consider adding a @property annotation.
Loading history...
200
		if ( ! empty( $post_types ) && is_singular( $post_types ) && 'automatic' === $this->placement ) {
0 ignored issues
show
Bug Best Practice introduced by
The property placement does not exist on MonsterInsights_Popular_Posts_Inline. Since you implemented __get, consider adding a @property annotation.
Loading history...
201
			add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) );
202
		}
203
204
	}
205
206
	/**
207
	 * Insert the widget in the content.
208
	 *
209
	 * @param string $content The post content.
210
	 *
211
	 * @return string
212
	 */
213
	public function add_inline_posts_to_content(
214
		$content
215
	) {
216
217
		if ( $this->is_post_excluded() ) {
218
			return $content;
219
		}
220
221
		$words_count = str_word_count( $content );
222
		$after_count = intval( $this->after_count );
0 ignored issues
show
Bug Best Practice introduced by
The property after_count does not exist on MonsterInsights_Popular_Posts_Inline. Since you implemented __get, consider adding a @property annotation.
Loading history...
223
224
		// Insert only if there are more words then the insert after value.
225
		if ( $words_count > $after_count ) {
226
227
			$words = explode( ' ', $content );
228
			$count = 0;
229
230
			foreach ( $words as $index => $word ) {
231
				$count ++;
232
				if ( $count > $after_count ) {
233
					$p_index = strpos( $word, '</p>' );
234
					// Make sure the paragraph tag is not wrapped in another element like a blockquote.
235
					if ( false !== $p_index && false === strpos( $word, '</p></' ) ) {
236
						$words[ $index ] = substr_replace( $word, $this->shortcode_output( array() ), $p_index + 4, 0 );
237
						$this->posts     = array();
238
						break;
239
					}
240
				}
241
			}
242
243
			$content = implode( ' ', $words );
244
245
		}
246
247
		return $content;
248
	}
249
250
	/**
251
	 * Check if the selected theme is available with the current license to avoid showing a theme not available.
252
	 * Returns the default 'alpha' theme if not available.
253
	 *
254
	 * @param string $theme Theme slug for which we are checking.
255
	 *
256
	 * @return string
257
	 */
258
	public function is_theme_available( $theme ) {
259
260
		$theme_props = $this->get_theme_props( $theme )->get_theme();
261
262
		if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) {
263
			return $theme;
264
		}
265
266
		return 'alpha';
267
268
	}
269
270
}
271
272
/**
273
 * Get the current class in a function.
274
 *
275
 * @return MonsterInsights_Popular_Posts_Inline Instance of the current class.
276
 */
277
function MonsterInsights_Popular_Posts_Inline() {
278
	return MonsterInsights_Popular_Posts_Inline::get_instance();
279
}
280
281
MonsterInsights_Popular_Posts_Inline();
282