1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This is the base class for the Popular Posts output functionality. |
4
|
|
|
* Each actual Popular Posts option extends this class (inline, widget, products). |
5
|
|
|
* |
6
|
|
|
* @package MonsterInsights |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MonsterInsights_Popular_Posts |
11
|
|
|
*/ |
12
|
|
|
class MonsterInsights_Popular_Posts { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The key prefix used to store the settings for the magic __get method. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $settings_key; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Name of the shortcode |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $shortcode_key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The popular posts object type, by default inline, widget or products. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $type; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* An array of posts used in the query process. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $posts = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* An array of posts already displayed. Used to avoid duplicate posts on the same page. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
public $shown_posts = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The inline styles string with theme specifics from the Vue settings. |
51
|
|
|
* Each instance should append to this variable so we print styles for all the instances in the same place. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public static $inline_styles = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Stores the option to use ajax to display the popular posts widgets on the frontend. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public $ajaxify; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Stores the cache instance, specific to the plugin version. |
66
|
|
|
* |
67
|
|
|
* @var MonsterInsights_Popular_Posts_Cache |
68
|
|
|
*/ |
69
|
|
|
public $cache; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Holds the class object. |
73
|
|
|
* |
74
|
|
|
* @since 7.13.0 |
75
|
|
|
* @access public |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
public static $instances = array(); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var MonsterInsights_Popular_Posts_Themes |
82
|
|
|
*/ |
83
|
|
|
protected $theme_props; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Indicator that inline styles have been printed to avoid duplicates. |
87
|
|
|
* |
88
|
|
|
* @var bool |
89
|
|
|
*/ |
90
|
|
|
private static $styles_printed = false; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Number of posts to query from the db. Not all queried posts are used for display in the same widget. |
94
|
|
|
* |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
public $posts_count = 15; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* MonsterInsights_Popular_Posts constructor. |
101
|
|
|
*/ |
102
|
|
|
public function __construct() { |
103
|
|
|
|
104
|
|
|
$this->hooks(); |
105
|
|
|
$this->register_shortcode(); |
106
|
|
|
|
107
|
|
|
$this->ajaxify = monsterinsights_get_option( 'popular_posts_ajaxify', false ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Magic get for different types of popular posts. |
112
|
|
|
* |
113
|
|
|
* @param $name |
114
|
|
|
* |
115
|
|
|
* @return string|array|mixed |
116
|
|
|
*/ |
117
|
|
|
public function __get( $name ) { |
118
|
|
|
return monsterinsights_get_option( $this->settings_key . '_' . $name ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add hooks needed for the output. |
123
|
|
|
*/ |
124
|
|
|
public function hooks() { |
125
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_styles' ) ); |
126
|
|
|
|
127
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_load_ajaxify_script' ) ); |
128
|
|
|
|
129
|
|
|
$this->add_inline_styles(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add inline styles for each widget type to a single variable for printing. |
134
|
|
|
*/ |
135
|
|
|
protected function add_inline_styles() { |
136
|
|
|
if ( 'no_styles' !== $this->styling ) { |
|
|
|
|
137
|
|
|
self::$inline_styles .= $this->build_inline_styles(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Should return object-specific inline styles. |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function build_inline_styles() { |
147
|
|
|
return ''; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Register the shortcode for the specific class. |
152
|
|
|
*/ |
153
|
|
|
public function register_shortcode() { |
154
|
|
|
|
155
|
|
|
if ( ! empty( $this->shortcode_key ) ) { |
156
|
|
|
add_shortcode( $this->shortcode_key, array( $this, 'render_shortcode' ) ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Load the frontend styles if they are enabled. |
163
|
|
|
*/ |
164
|
|
|
public function load_frontend_styles() { |
165
|
|
|
|
166
|
|
|
// Only load our styles if enabled. |
167
|
|
|
if ( apply_filters( 'monsterinsights_popular_posts_styles_output', 'no_styles' === $this->styling, $this ) ) { |
|
|
|
|
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
171
|
|
|
|
172
|
|
|
// Load Popular Posts styles. |
173
|
|
|
wp_register_style( 'monsterinsights-popular-posts-style', plugins_url( 'assets/css/frontend' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() ); |
174
|
|
|
wp_enqueue_style( 'monsterinsights-popular-posts-style' ); |
175
|
|
|
|
176
|
|
|
$this->add_theme_specific_styles(); |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* If the Ajaxify option is enabled, print needed scripts. |
182
|
|
|
*/ |
183
|
|
|
public function maybe_load_ajaxify_script() { |
184
|
|
|
if ( ! $this->ajaxify ) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
188
|
|
|
|
189
|
|
|
wp_register_script( 'monsterinsights-popular-posts-js', plugins_url( 'assets/js/popular-posts' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true ); |
190
|
|
|
|
191
|
|
|
wp_enqueue_script( 'monsterinsights-popular-posts-js' ); |
192
|
|
|
|
193
|
|
|
wp_localize_script( 'monsterinsights-popular-posts-js', 'monsterinsights_pp', array( |
194
|
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
195
|
|
|
'post_id' => get_the_ID(), |
196
|
|
|
) ); |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Add inline styles based on customizations from the vue panel. |
202
|
|
|
*/ |
203
|
|
|
public function add_theme_specific_styles() { |
204
|
|
|
|
205
|
|
|
if ( ! self::$styles_printed ) { |
206
|
|
|
wp_add_inline_style( 'monsterinsights-popular-posts-style', $this->get_inline_styles() ); |
207
|
|
|
self::$styles_printed = true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* We have a single static variable for inline styles shared by all instances so we print just once. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function get_inline_styles() { |
218
|
|
|
return self::$inline_styles; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Rendering the shortcode. |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function render_shortcode( $args ) { |
227
|
|
|
|
228
|
|
|
return apply_filters( 'monsterinsights_popular_posts_shortcode_output', $this->shortcode_output( $args ), $args, $this ); |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Output of shortcode based on settings. |
234
|
|
|
* |
235
|
|
|
* @param array $args Arguments from shortcode/block. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function shortcode_output( $args ) { |
240
|
|
|
if ( $this->ajaxify ) { |
241
|
|
|
return $this->get_ajax_json_data( $args ); |
242
|
|
|
} else { |
243
|
|
|
return $this->get_rendered_html( $args ); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Print inline JSON data that with settings that get processed using an AJAX call. Acts similar to printing out |
249
|
|
|
* a shortcode with its settings but actually loading the output for that after the page was loaded, with AJAX. |
250
|
|
|
* |
251
|
|
|
* @param array $args Arguments from shortcode/block. |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function get_ajax_json_data( $args ) { |
256
|
|
|
|
257
|
|
|
$args['type'] = $this->type; |
258
|
|
|
|
259
|
|
|
$data = '<div><script type="application/json" class="monsterinsights-popular-posts-widget-json">'; |
260
|
|
|
$data .= wp_json_encode( $args ); |
261
|
|
|
$data .= '</script></div>'; |
262
|
|
|
|
263
|
|
|
return $data; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* This is replaced with actual HTML output in child classes. |
268
|
|
|
* |
269
|
|
|
* @param array $args Arguments used to build specific html. |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function get_rendered_html( $args ) { |
274
|
|
|
return ''; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the cache instance for the set type. |
279
|
|
|
* |
280
|
|
|
* @return MonsterInsights_Popular_Posts_Cache |
281
|
|
|
*/ |
282
|
|
|
public function get_cache() { |
283
|
|
|
if ( ! isset( $this->cache ) ) { |
284
|
|
|
$this->cache = new MonsterInsights_Popular_Posts_Cache( $this->type ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this->cache; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Use the query args to grab posts from the database. |
292
|
|
|
*/ |
293
|
|
|
public function get_posts() { |
294
|
|
|
|
295
|
|
|
$posts_args = $this->get_query_args(); |
296
|
|
|
|
297
|
|
|
$posts = $this->get_cache()->get_cached_posts( $posts_args ); |
298
|
|
|
|
299
|
|
|
if ( empty( $posts ) ) { |
300
|
|
|
|
301
|
|
|
if ( isset( $posts_args['post__in'] ) && empty( $posts_args['post__in'] ) ) { |
302
|
|
|
$this->posts = array(); |
303
|
|
|
|
304
|
|
|
return $this->posts; |
305
|
|
|
} |
306
|
|
|
$posts = get_posts( $posts_args ); |
307
|
|
|
|
308
|
|
|
$posts = $this->process_posts( $posts ); |
309
|
|
|
|
310
|
|
|
$this->get_cache()->save_posts_to_cache( $posts_args, $posts ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return apply_filters( 'monsterinsights_popular_posts_posts', $posts ); |
314
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Go through posts from a WP Query and prepare them for output. |
319
|
|
|
* |
320
|
|
|
* @param array $posts Array of posts from WP Query or similar, also supports array of ids. |
321
|
|
|
* |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
private function process_posts( $posts ) { |
325
|
|
|
$processed_posts = array(); |
326
|
|
|
foreach ( $posts as $post ) { |
327
|
|
|
if ( is_int( $post ) ) { |
328
|
|
|
$post = get_post( $post ); |
329
|
|
|
} |
330
|
|
|
$post_thumbnail = get_post_thumbnail_id( $post->ID ); |
331
|
|
|
$post_image = ''; |
332
|
|
|
$post_image_srcset = ''; |
333
|
|
|
if ( ! empty( $post_thumbnail ) ) { |
334
|
|
|
$post_image = wp_get_attachment_image_src( $post_thumbnail, 'small' ); |
335
|
|
|
if ( is_array( $post_image ) && ! empty( $post_image[0] ) ) { |
336
|
|
|
$post_image = $post_image[0]; |
337
|
|
|
} |
338
|
|
|
$post_image_srcset = wp_get_attachment_image_srcset( $post_thumbnail, 'small' ); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$author_data = get_userdata( $post->post_author ); |
|
|
|
|
342
|
|
|
|
343
|
|
|
$processed_posts[] = array( |
344
|
|
|
'id' => $post->ID, |
345
|
|
|
'title' => get_the_title( $post->ID ), |
346
|
|
|
'link' => get_permalink( $post->ID ), |
347
|
|
|
'image' => $post_image, |
348
|
|
|
'srcset' => $post_image_srcset, |
349
|
|
|
'image_id' => $post_thumbnail, |
350
|
|
|
'author' => $post->post_author, |
351
|
|
|
'author_name' => $author_data->display_name, |
352
|
|
|
'date' => get_the_date( '', $post->ID ), |
353
|
|
|
'comments' => get_comments_number( $post->ID ), |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return $processed_posts; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get the query args for grabbing the posts. This should probably get overwritten in child classes. |
362
|
|
|
* |
363
|
|
|
* @return mixed|void |
364
|
|
|
*/ |
365
|
|
|
private function get_query_args() { |
366
|
|
|
|
367
|
|
|
$args = array( |
368
|
|
|
'numberposts' => $this->posts_count, |
369
|
|
|
'ignore_sticky_posts' => true, |
370
|
|
|
); |
371
|
|
|
$args = wp_parse_args( $this->query_args(), $args ); |
372
|
|
|
|
373
|
|
|
return apply_filters( 'monsterinsights_popular_posts_query_args', $args ); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Set the query args specific to this instance. |
378
|
|
|
* |
379
|
|
|
* @return array |
380
|
|
|
*/ |
381
|
|
|
protected function query_args() { |
382
|
|
|
|
383
|
|
|
if ( 'comments' === $this->sort ) { |
|
|
|
|
384
|
|
|
return $this->get_query_args_comments(); |
385
|
|
|
} elseif ( 'sharedcount' === $this->sort ) { |
386
|
|
|
return $this->get_query_args_sharedcount(); |
387
|
|
|
} elseif ( 'curated' === $this->sort ) { |
388
|
|
|
return $this->get_query_args_curated(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get the query args for ordering by comments. |
396
|
|
|
* |
397
|
|
|
* @return array |
398
|
|
|
*/ |
399
|
|
|
protected function get_query_args_comments() { |
400
|
|
|
|
401
|
|
|
$query_args = array( |
402
|
|
|
'orderby' => 'comment_count', |
403
|
|
|
'order' => 'DESC', |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
return $query_args; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Get the query args for ordering by sharedcount. |
411
|
|
|
* |
412
|
|
|
* @return array |
413
|
|
|
*/ |
414
|
|
|
protected function get_query_args_sharedcount() { |
415
|
|
|
|
416
|
|
|
$query_args = array( |
417
|
|
|
'orderby' => 'meta_value_num', |
418
|
|
|
'order' => 'DESC', |
419
|
|
|
'meta_key' => '_monsterinsights_sharedcount_total', |
420
|
|
|
); |
421
|
|
|
|
422
|
|
|
return $query_args; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Build the query args for the curated option from the settings in the panel. |
428
|
|
|
* |
429
|
|
|
* @return array |
430
|
|
|
*/ |
431
|
|
|
protected function get_query_args_curated() { |
432
|
|
|
|
433
|
|
|
$posts = $this->curated; |
|
|
|
|
434
|
|
|
$post_in = array(); |
435
|
|
|
|
436
|
|
|
if ( ! empty( $posts ) && is_array( $posts ) ) { |
|
|
|
|
437
|
|
|
foreach ( $posts as $post ) { |
438
|
|
|
if ( ! empty( $post['id'] ) ) { |
439
|
|
|
$post_in[] = intval( $post['id'] ); |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
$query_args = array( |
445
|
|
|
'post__in' => $post_in, |
446
|
|
|
); |
447
|
|
|
|
448
|
|
|
return $query_args; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Load theme props for the specific instance. |
453
|
|
|
* |
454
|
|
|
* @param string $theme Theme key. |
455
|
|
|
* |
456
|
|
|
* @return MonsterInsights_Popular_Posts_Themes |
457
|
|
|
*/ |
458
|
|
|
public function get_theme_props( $theme = '' ) { |
459
|
|
|
|
460
|
|
|
if ( empty( $theme ) ) { |
461
|
|
|
$theme = $this->theme; |
|
|
|
|
462
|
|
|
} |
463
|
|
|
$theme_props = new MonsterInsights_Popular_Posts_Themes( $this->type, $theme ); |
464
|
|
|
|
465
|
|
|
return $theme_props; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Marks a post as already displayed, by id. |
470
|
|
|
* |
471
|
|
|
* @param $id |
472
|
|
|
*/ |
473
|
|
|
public function set_post_shown( $id ) { |
474
|
|
|
if ( ! in_array( $id, $this->shown_posts, true ) ) { |
475
|
|
|
$this->shown_posts[] = $id; |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Returns an array of posts that were already displayed on the current page. |
481
|
|
|
* |
482
|
|
|
* @return array |
483
|
|
|
*/ |
484
|
|
|
public function get_shown_posts() { |
485
|
|
|
|
486
|
|
|
return $this->shown_posts; |
487
|
|
|
|
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* Generic helper function to build style attributes for elements based on shortcode/block parameters. |
492
|
|
|
* |
493
|
|
|
* @param string $theme The theme for which we're building the style. |
494
|
|
|
* @param string $object Object we're styling like title, label, background, etc. |
495
|
|
|
* @param array $atts Attributes passed from shortcode/block. |
496
|
|
|
* @param string $key The key of the style we're going to output. |
497
|
|
|
* |
498
|
|
|
* @return string |
499
|
|
|
*/ |
500
|
|
|
public function get_element_style( $theme = '', $object, $atts, $key = '' ) { |
501
|
|
|
|
502
|
|
|
if ( empty( $theme ) ) { |
503
|
|
|
$theme = $this->theme; |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
// Find theme-specific available options and check if our attributes have those set. |
507
|
|
|
$theme_styles = $this->get_theme_props( $theme )->get_theme(); |
508
|
|
|
$style_output = ''; |
509
|
|
|
$style_css = ''; |
510
|
|
|
|
511
|
|
|
if ( ! empty( $theme_styles['styles'] ) ) { |
512
|
|
|
foreach ( $theme_styles['styles'] as $element => $options ) { |
513
|
|
|
if ( $object !== $element ) { |
514
|
|
|
continue; |
515
|
|
|
} |
516
|
|
|
foreach ( $options as $style_key => $value ) { |
517
|
|
|
$atts_key = $element . '_' . $style_key; |
518
|
|
|
|
519
|
|
|
if ( ! empty( $key ) && $key !== $style_key ) { |
520
|
|
|
// Allow output for just a specific key. |
521
|
|
|
continue; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
if ( ! empty( $atts[ $atts_key ] ) ) { |
525
|
|
|
if ( is_bool( $atts[ $atts_key ] ) || 'on' === $atts[ $atts_key ] ) { |
526
|
|
|
continue; |
527
|
|
|
} |
528
|
|
|
if ( 'size' === $style_key ) { |
529
|
|
|
$style_key = 'font-size'; |
530
|
|
|
$atts[ $atts_key ] .= 'px'; |
531
|
|
|
} |
532
|
|
|
if ( 'background' === $style_key || 'background' === $element && 'color' === $style_key ) { |
533
|
|
|
$style_key = 'background-color'; |
534
|
|
|
} |
535
|
|
|
if ( 'border' === $element || 'border' === $style_key ) { |
536
|
|
|
$style_key = 'border-color'; |
537
|
|
|
} |
538
|
|
|
$style_css .= $style_key . ':' . $atts[ $atts_key ] . ';'; |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
if ( ! empty( $style_css ) ) { |
|
|
|
|
545
|
|
|
$style_output = 'style="' . $style_css . '"'; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
return $style_output; |
549
|
|
|
|
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Get the current instance based on the called class. |
554
|
|
|
* |
555
|
|
|
* @return mixed |
556
|
|
|
*/ |
557
|
|
|
public static function get_instance() { |
558
|
|
|
|
559
|
|
|
$class = get_called_class(); |
560
|
|
|
|
561
|
|
|
if ( ! isset( self::$instances[ $class ] ) ) { |
562
|
|
|
self::$instances[ $class ] = new $class; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return self::$instances[ $class ]; |
566
|
|
|
|
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Check if the post is excluded from loading the widget. |
571
|
|
|
* |
572
|
|
|
* @param null|WP_Post $post The post to check if it's excluded. |
573
|
|
|
* |
574
|
|
|
* @return bool |
575
|
|
|
*/ |
576
|
|
|
public function is_post_excluded( $post = null ) { |
577
|
|
|
if ( is_null( $post ) ) { |
578
|
|
|
$post = get_post( get_the_ID() ); |
|
|
|
|
579
|
|
|
} |
580
|
|
|
$excluded = false; |
581
|
|
|
|
582
|
|
|
$posts_to_exclude = $this->exclude_posts; |
|
|
|
|
583
|
|
|
if ( ! empty( $posts_to_exclude ) ) { |
584
|
|
|
$post_ids = array(); |
585
|
|
|
foreach ( $posts_to_exclude as $exclude_post ) { |
|
|
|
|
586
|
|
|
if ( ! empty( $exclude_post['id'] ) ) { |
587
|
|
|
$post_ids[] = intval( $exclude_post['id'] ); |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
if ( in_array( $post->ID, $post_ids, true ) ) { |
592
|
|
|
$excluded = true; |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
return $excluded; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* Build a wrapper class based on theme, instance and some settings. |
601
|
|
|
* |
602
|
|
|
* @param array $atts Attributes of the shortcode/instance to process for output. |
603
|
|
|
* |
604
|
|
|
* @return string |
605
|
|
|
*/ |
606
|
|
|
public function get_wrapper_class( $atts ) { |
607
|
|
|
$theme = $this->theme; |
|
|
|
|
608
|
|
|
if ( ! empty( $atts['theme'] ) ) { |
609
|
|
|
$theme = $atts['theme']; |
610
|
|
|
} |
611
|
|
|
$columns = ! empty( $atts['columns'] ) ? $atts['columns'] : $this->theme_columns; |
|
|
|
|
612
|
|
|
$classes = array( |
613
|
|
|
'monsterinsights-' . $this->type . '-popular-posts', |
614
|
|
|
'monsterinsights-' . $this->type . '-popular-posts-' . $theme, |
615
|
|
|
'no_styles' !== $this->styling ? 'monsterinsights-popular-posts-styled' : '', |
|
|
|
|
616
|
|
|
); |
617
|
|
|
|
618
|
|
|
if ( $columns ) { |
619
|
|
|
$classes[] = 'monsterinsights-' . $this->type . '-popular-posts-columns-' . $columns; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
if ( isset( $atts['className'] ) ) { |
623
|
|
|
$classes[] = $atts['className']; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
$classname = implode( ' ', $classes ); |
627
|
|
|
|
628
|
|
|
return $classname; |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Check if the id is of the currently displayed post. Compatible with the Ajaxify functionality. |
633
|
|
|
* |
634
|
|
|
* @param $id |
635
|
|
|
* |
636
|
|
|
* @return bool |
637
|
|
|
*/ |
638
|
|
|
public function is_current_post( $id ) { |
639
|
|
|
|
640
|
|
|
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
641
|
|
|
$current_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false; |
642
|
|
|
|
643
|
|
|
return $id === $current_id; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
// Only run this check for singular pages. |
647
|
|
|
if ( ! is_singular() ) { |
648
|
|
|
return false; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
return get_the_ID() === absint( $id ); |
652
|
|
|
|
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Helper function that checks if a post should be displayed on the current page. |
657
|
|
|
* |
658
|
|
|
* @param int $id |
659
|
|
|
* |
660
|
|
|
* @return bool |
661
|
|
|
*/ |
662
|
|
|
public function should_display_post( $id ) { |
663
|
|
|
$shown = $this->get_shown_posts(); |
664
|
|
|
if ( in_array( $id, $shown, true ) ) { |
665
|
|
|
return false; |
666
|
|
|
} |
667
|
|
|
if ( $this->is_current_post( $id ) ) { |
668
|
|
|
return false; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
return true; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* This function grabs the posts from the cache or a fresh query and runs them through a check if they should be |
676
|
|
|
* displayed on the current page to avoid duplicates. |
677
|
|
|
* |
678
|
|
|
* @return array |
679
|
|
|
*/ |
680
|
|
|
public function get_posts_to_display() { |
681
|
|
|
$posts = $this->get_posts(); |
682
|
|
|
|
683
|
|
|
$returned_posts = array(); |
684
|
|
|
|
685
|
|
|
foreach ( $posts as $post ) { |
686
|
|
|
if ( $this->should_display_post( $post['id'] ) ) { |
687
|
|
|
$returned_posts[] = $post; |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
if ( apply_filters( 'monsterinsights_popular_posts_show_duplicates', true ) && count( $posts ) > 0 && count( $returned_posts ) === 0 ) { |
692
|
|
|
$this->shown_posts = array(); // Reset shown posts. |
693
|
|
|
return $this->get_posts_to_display(); // Run the function to grab the same posts again. |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
return $returned_posts; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* Check if the current instance has any posts available to display. |
701
|
|
|
* |
702
|
|
|
* @param array $posts Posts array to check if still available for display. |
703
|
|
|
* |
704
|
|
|
* @return bool |
705
|
|
|
*/ |
706
|
|
|
public function has_posts_to_show( $posts ) { |
707
|
|
|
|
708
|
|
|
foreach ( $posts as $post ) { |
709
|
|
|
if ( $this->should_display_post( $post['id'] ) ) { |
710
|
|
|
return true; |
711
|
|
|
} |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
return false; |
715
|
|
|
|
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Only inline styles that were customized for the specific instance. |
720
|
|
|
* |
721
|
|
|
* @return array |
722
|
|
|
*/ |
723
|
|
|
public function get_themes_styles_for_output() { |
724
|
|
|
|
725
|
|
|
$stored_styles = $this->get_theme_props()->get_theme_stored_styles(); |
726
|
|
|
$themes = ! empty( $stored_styles[ $this->type ] ) && is_array( $stored_styles[ $this->type ] ) ? $stored_styles[ $this->type ] : array(); |
727
|
|
|
|
728
|
|
|
return $themes; |
729
|
|
|
|
730
|
|
|
} |
731
|
|
|
} |
732
|
|
|
|