|
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
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Register Popular Posts widget. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function register_widget() { |
|
48
|
|
|
register_widget( 'MonsterInsights_Popular_Posts_Widget_Sidebar' ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the rendered HTML for output. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $atts These are attributes used to build the specific instance, they can be either shortcode |
|
55
|
|
|
* attributes or Gutenberg block props. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function get_rendered_html( $atts ) { |
|
60
|
|
|
|
|
61
|
|
|
$theme = $this->theme; |
|
|
|
|
|
|
62
|
|
|
if ( ! empty( $atts['theme'] ) ) { |
|
63
|
|
|
$theme = $atts['theme']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$theme = $this->is_theme_available( $theme ); |
|
67
|
|
|
|
|
68
|
|
|
if ( ! empty( $atts['post_count'] ) ) { |
|
69
|
|
|
$limit = intval( $atts['post_count'] ); |
|
70
|
|
|
} else { |
|
71
|
|
|
$limit = $this->count; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$posts = $this->get_posts_to_display(); |
|
75
|
|
|
|
|
76
|
|
|
if ( empty( $posts ) ) { |
|
77
|
|
|
return ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$theme_styles = $this->get_theme_props( $theme )->get_theme(); |
|
81
|
|
|
|
|
82
|
|
|
$label_text = ''; |
|
83
|
|
|
if ( isset( $theme_styles['styles']['label'] ) ) { |
|
84
|
|
|
$label_text = isset( $atts['label_text'] ) ? $atts['label_text'] : $theme_styles['styles']['label']['text']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ( isset( $atts['widget_title'] ) ) { |
|
88
|
|
|
$show_title = boolval( $atts['widget_title'] ); |
|
89
|
|
|
$title_text = empty( $atts['widget_title_text'] ) ? '' : $atts['widget_title_text']; |
|
90
|
|
|
} else { |
|
91
|
|
|
$show_title = $this->title; |
|
|
|
|
|
|
92
|
|
|
$title_text = $this->title_text; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$html = '<div class="' . $this->get_wrapper_class( $atts ) . '">'; |
|
96
|
|
|
if ( $show_title ) { |
|
97
|
|
|
$html .= '<h2 class="monsterinsights-widget-popular-posts-widget-title">' . wp_kses_post( $title_text ) . '</h2>'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$html .= '<ul class="monsterinsights-widget-popular-posts-list">'; |
|
101
|
|
|
|
|
102
|
|
|
$display_count = 0; |
|
103
|
|
|
foreach ( $posts as $post ) { |
|
104
|
|
|
$display_count ++; |
|
105
|
|
|
if ( $display_count > $limit ) { |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
$this->set_post_shown( $post['id'] ); |
|
109
|
|
|
$html .= '<li ' . $this->get_element_style( $theme, 'background', $atts ) . '>'; |
|
110
|
|
|
$html .= '<a href="' . $post['link'] . '">'; |
|
111
|
|
|
if ( ! empty( $theme_styles['image'] ) && ! empty( $post['image'] ) ) { |
|
112
|
|
|
$html .= '<div class="monsterinsights-widget-popular-posts-image">'; |
|
113
|
|
|
$html .= '<img src="' . $post['image'] . '" srcset=" ' . $post['srcset'] . ' " />'; |
|
114
|
|
|
$html .= '</div>'; |
|
115
|
|
|
} |
|
116
|
|
|
$html .= '<div class="monsterinsights-widget-popular-posts-text">'; |
|
117
|
|
|
if ( isset( $theme_styles['styles']['label'] ) ) { |
|
118
|
|
|
$html .= '<span class="monsterinsights-widget-popular-posts-label" ' . $this->get_element_style( $theme, 'label', $atts ) . '>' . $label_text . '</span>'; |
|
119
|
|
|
} |
|
120
|
|
|
$html .= '<span class="monsterinsights-widget-popular-posts-title" ' . $this->get_element_style( $theme, 'title', $atts ) . '>' . $post['title'] . '</span>'; |
|
121
|
|
|
$html .= '</div>';// monsterinsights-widget-popular-posts-text. |
|
122
|
|
|
$html .= '</a>'; |
|
123
|
|
|
$html .= '</li>'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$html .= '</ul></div><p></p>';// Main div. |
|
127
|
|
|
|
|
128
|
|
|
return $html; |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Add widget-specific styles based on theme settings. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function build_inline_styles() { |
|
136
|
|
|
|
|
137
|
|
|
$themes = $this->get_themes_styles_for_output(); |
|
138
|
|
|
$styles = ''; |
|
139
|
|
|
|
|
140
|
|
|
foreach ( $themes as $theme_key => $theme_styles ) { |
|
141
|
|
|
|
|
142
|
|
|
if ( ! empty( $theme_styles['background'] ) ) { |
|
143
|
|
|
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . $theme_key . ' .monsterinsights-widget-popular-posts-list li {'; |
|
144
|
|
|
|
|
145
|
|
|
if ( ! empty( $theme_styles['background']['color'] ) ) { |
|
146
|
|
|
$styles .= 'background-color:' . $theme_styles['background']['color'] . ';'; |
|
147
|
|
|
} |
|
148
|
|
|
if ( ! empty( $theme_styles['background']['border'] ) ) { |
|
149
|
|
|
$styles .= 'border-color:' . $theme_styles['background']['border'] . ';'; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$styles .= '}'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ( ! empty( $theme_styles['label'] ) ) { |
|
156
|
|
|
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . $theme_key . ' .monsterinsights-widget-popular-posts-label {'; |
|
157
|
|
|
|
|
158
|
|
|
if ( ! empty( $theme_styles['label']['color'] ) ) { |
|
159
|
|
|
$styles .= 'color:' . $theme_styles['label']['color'] . ';'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if ( ! empty( $theme_styles['label']['background'] ) ) { |
|
163
|
|
|
$styles .= 'background-color:' . $theme_styles['label']['background'] . ';'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$styles .= '}'; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if ( ! empty( $theme_styles['title'] ) ) { |
|
170
|
|
|
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . $theme_key . ' .monsterinsights-widget-popular-posts-list li .monsterinsights-widget-popular-posts-title {'; |
|
171
|
|
|
|
|
172
|
|
|
if ( ! empty( $theme_styles['title']['color'] ) ) { |
|
173
|
|
|
$styles .= 'color:' . $theme_styles['title']['color'] . ';'; |
|
174
|
|
|
} |
|
175
|
|
|
if ( ! empty( $theme_styles['title']['size'] ) ) { |
|
176
|
|
|
$styles .= 'font-size:' . $theme_styles['title']['size'] . 'px;'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$styles .= '}'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if ( ! empty( $theme_styles['border'] ) ) { |
|
183
|
|
|
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-border {'; |
|
184
|
|
|
|
|
185
|
|
|
if ( ! empty( $theme_styles['border']['color'] ) ) { |
|
186
|
|
|
$styles .= 'border-color:' . $theme_styles['border']['color'] . ';'; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$styles .= '}'; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $styles; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Check if we should attempt to automatically insert the inline widget. |
|
198
|
|
|
*/ |
|
199
|
|
|
public function maybe_auto_insert() { |
|
200
|
|
|
|
|
201
|
|
|
$post_types = $this->post_types; |
|
|
|
|
|
|
202
|
|
|
if ( ! empty( $post_types ) && is_singular( $post_types ) && $this->automatic ) { |
|
|
|
|
|
|
203
|
|
|
add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) ); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Insert the widget in the content. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $content The post content. |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
public function add_inline_posts_to_content( $content ) { |
|
216
|
|
|
|
|
217
|
|
|
if ( $this->is_post_excluded() ) { |
|
218
|
|
|
return $content; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$content .= $this->shortcode_output( array() ); |
|
222
|
|
|
|
|
223
|
|
|
return $content; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Check if the selected theme is available with the current license to avoid showing a theme not available. |
|
228
|
|
|
* Returns the default 'alpha' theme if not available. |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $theme Theme slug for which we are checking. |
|
231
|
|
|
* |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function is_theme_available( $theme ) { |
|
235
|
|
|
|
|
236
|
|
|
$theme_props = $this->get_theme_props( $theme )->get_theme(); |
|
237
|
|
|
|
|
238
|
|
|
if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) { |
|
239
|
|
|
return $theme; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return 'alpha'; |
|
243
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get the current class in a function. |
|
250
|
|
|
* |
|
251
|
|
|
* @return MonsterInsights_Popular_Posts_Widget Instance of the current class. |
|
252
|
|
|
*/ |
|
253
|
|
|
function MonsterInsights_Popular_Posts_Widget() { |
|
254
|
|
|
return MonsterInsights_Popular_Posts_Widget::get_instance(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
MonsterInsights_Popular_Posts_Widget(); |
|
258
|
|
|
|