Passed
Push — master ( 6f0a6d...90a9ca )
by
unknown
09:41
created

MonsterInsights_Popular_Posts_Inline::hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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 ) {
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="' . $this->get_wrapper_class( $atts ) . '" ' . $this->get_element_style( $theme, 'background', $atts ) . '>';
79
80
		if ( ! empty( $theme_styles['image'] ) && ! empty( $posts[0]['image'] ) ) {
81
			$html .= '<div class="monsterinsights-inline-popular-posts-image">';
82
			$html .= '<img src="' . $posts[0]['image'] . '" srcset=" ' . $posts[0]['srcset'] . ' " />';
83
			$html .= '</div>';
84
		}
85
		$html .= '<div class="monsterinsights-inline-popular-posts-text">';
86
		if ( ! empty( $theme_styles['styles']['icon'] ) ) {
87
			$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>';
88
		}
89
		if ( ! empty( $theme_styles['styles']['label'] ) ) {
90
			$html .= '<span class="monsterinsights-inline-popular-posts-label" ' . $this->get_element_style( $theme, 'label', $atts ) . '>' . $label_text . '</span>';
91
		}
92
		if ( ! empty( $theme_styles['styles']['border'] ) ) {
93
			$html .= '<span class="monsterinsights-inline-popular-posts-border" ' . $this->get_element_style( $theme, 'border', $atts, 'color' ) . '></span>';
94
		}
95
		if ( ! empty( $theme_styles['styles']['border']['color2'] ) ) {
96
			$html .= '<span class="monsterinsights-inline-popular-posts-border-2" ' . $this->get_element_style( $theme, 'border', $atts, 'color2' ) . '></span>';
97
		}
98
99
		$display_count = 0;
100
		foreach ( $posts as $post ) {
101
			$display_count ++;
102
			if ( $display_count > $limit ) {
103
				break;
104
			}
105
			$this->set_post_shown( $post['id'] );
106
			$html .= '<div class="monsterinsights-inline-popular-posts-post">';
107
			$html .= '<a class="monsterinsights-inline-popular-posts-title" ' . $this->get_element_style( $theme, 'title', $atts ) . ' href="' . $post['link'] . '">' . $post['title'] . '</a>';
108
			$html .= '</div>';
109
		}
110
111
		$html .= '</div>';// Text div.
112
		$html .= '</div><p></p>';// Main div.
113
114
		return $html;
115
116
	}
117
118
	/**
119
	 * Specific inline styles based on theme settings.
120
	 *
121
	 * @return string
122
	 */
123
	public function build_inline_styles() {
124
125
		$themes = $this->get_themes_styles_for_output();
126
		$styles = '';
127
128
		foreach ( $themes as $theme_key => $theme_styles ) {
129
			if ( ! empty( $theme_styles['background'] ) ) {
130
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' {';
131
132
				if ( ! empty( $theme_styles['background']['color'] ) ) {
133
					$styles .= 'background-color:' . $theme_styles['background']['color'] . ';';
134
				}
135
				if ( ! empty( $theme_styles['background']['border'] ) ) {
136
					$styles .= 'border-color:' . $theme_styles['background']['border'] . ';';
137
				}
138
139
				$styles .= '}';
140
			}
141
142
			if ( ! empty( $theme_styles['label'] ) ) {
143
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-label {';
144
145
				if ( ! empty( $theme_styles['label']['color'] ) ) {
146
					$styles .= 'color:' . $theme_styles['label']['color'] . ';';
147
				}
148
149
				if ( ! empty( $theme_styles['label']['background'] ) ) {
150
					$styles .= 'background-color:' . $theme_styles['label']['background'] . ';';
151
				}
152
153
				$styles .= '}';
154
			}
155
156
			if ( ! empty( $theme_styles['title'] ) ) {
157
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-title {';
158
159
				if ( ! empty( $theme_styles['title']['color'] ) ) {
160
					$styles .= 'color:' . $theme_styles['title']['color'] . ';';
161
				}
162
				if ( ! empty( $theme_styles['title']['size'] ) ) {
163
					$styles .= 'font-size:' . $theme_styles['title']['size'] . 'px;';
164
				}
165
166
				$styles .= '}';
167
			}
168
169
			if ( ! empty( $theme_styles['border'] ) ) {
170
				$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-border {';
171
172
				if ( ! empty( $theme_styles['border']['color'] ) ) {
173
					$styles .= 'border-color:' . $theme_styles['border']['color'] . ';';
174
				}
175
176
				$styles .= '}';
177
			}
178
		}
179
180
		return $styles;
181
	}
182
183
	/**
184
	 * Check if we should attempt to automatically insert the inline widget.
185
	 */
186
	public function maybe_auto_insert() {
187
188
		$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...
189
		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...
190
			add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) );
191
		}
192
193
	}
194
195
	/**
196
	 * Insert the widget in the content.
197
	 *
198
	 * @param string $content The post content.
199
	 *
200
	 * @return string
201
	 */
202
	public function add_inline_posts_to_content(
203
		$content
204
	) {
205
206
		if ( $this->is_post_excluded() ) {
207
			return $content;
208
		}
209
210
		$words_count = str_word_count( $content );
211
		$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...
212
213
		// Insert only if there are more words then the insert after value.
214
		if ( $words_count > $after_count ) {
215
216
			$words = explode( ' ', $content );
217
			$count = 0;
218
219
			foreach ( $words as $index => $word ) {
220
				$count ++;
221
				if ( $count > $after_count ) {
222
					$p_index = mb_strpos( $word, '</p>' );
223
					// Make sure the paragraph tag is not wrapped in another element like a blockquote.
224
					if ( false !== $p_index && false === mb_strpos( $word, '</p></' ) ) {
225
						$words[ $index ] = substr_replace( $word, $this->shortcode_output( array() ), $p_index + 4, 0 );
226
						$this->posts     = array();
227
						break;
228
					}
229
				}
230
			}
231
232
			$content = implode( ' ', $words );
233
234
		}
235
236
		return $content;
237
	}
238
239
	/**
240
	 * Check if the selected theme is available with the current license to avoid showing a theme not available.
241
	 * Returns the default 'alpha' theme if not available.
242
	 *
243
	 * @param string $theme Theme slug for which we are checking.
244
	 *
245
	 * @return string
246
	 */
247
	public function is_theme_available( $theme ) {
248
249
		$theme_props = $this->get_theme_props( $theme )->get_theme();
250
251
		if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) {
252
			return $theme;
253
		}
254
255
		return 'alpha';
256
257
	}
258
259
}
260
261
/**
262
 * Get the current class in a function.
263
 *
264
 * @return MonsterInsights_Popular_Posts_Inline Instance of the current class.
265
 */
266
function MonsterInsights_Popular_Posts_Inline() {
267
	return MonsterInsights_Popular_Posts_Inline::get_instance();
268
}
269
270
MonsterInsights_Popular_Posts_Inline();
271