PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.1.0
WP Popular Posts v5.1.0
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Output.php
wordpress-popular-posts / src Last commit date
Activation 6 years ago Admin 6 years ago Container 6 years ago Front 6 years ago Moment 6 years ago Rest 6 years ago Widget 6 years ago Bootstrap.php 6 years ago Cache.php 6 years ago Helper.php 6 years ago I18N.php 6 years ago Image.php 6 years ago Output.php 6 years ago Query.php 6 years ago Settings.php 6 years ago Themer.php 6 years ago Translate.php 6 years ago WordPressPopularPosts.php 6 years ago deprecated.php 6 years ago template-tags.php 6 years ago
Output.php
895 lines
1 <?php
2 /**
3 * This class formats the HTML output of every popular posts listing.
4 *
5 *
6 * @package WordPressPopularPosts
7 * @author Hector Cabrera <me@cabrerahector.com>
8 */
9
10 namespace WordPressPopularPosts;
11
12 class Output {
13
14 /**
15 * Popular posts data.
16 *
17 * @since 4.0.0
18 * @var string
19 */
20 private $data;
21
22 /**
23 * HTML output.
24 *
25 * @since 4.0.0
26 * @var string
27 */
28 private $output;
29
30 /**
31 * Widget / shortcode settings.
32 *
33 * @since 4.0.0
34 * @var array
35 */
36 private $public_options = [];
37
38 /**
39 * Administrative settings.
40 *
41 * @since 2.3.3
42 * @var array
43 */
44 private $admin_options = [];
45
46 /**
47 * Default excerpt 'more' string.
48 *
49 * @since 4.2.1
50 * @var string
51 */
52 private $more;
53
54 /**
55 * Image object
56 *
57 * @since 4.0.2
58 * @var WordPressPopularPosts\Image
59 */
60 private $thumbnail;
61
62 /**
63 * Translate object.
64 *
65 * @var \WordPressPopularPosts\Translate $translate
66 * @access private
67 */
68 private $translate;
69
70 /**
71 * Themer object.
72 *
73 * @var \WordPressPopularPosts\Themer $themer
74 * @access private
75 */
76 private $themer;
77
78 /**
79 * Constructor.
80 *
81 * @since 4.0.0
82 * @param array $public_options
83 * @param array $admin_options
84 * @param WordPressPopularPosts\Image $thumbnail
85 * @param WordPressPopularPosts\Translate $translate
86 * @param \WordPressPopularPosts\Themer $themer
87 */
88 public function __construct(array $public_options = [], array $admin_options = [], Image $thumbnail, Translate $translate, \WordPressPopularPosts\Themer $themer)
89 {
90 $this->public_options = $public_options;
91 $this->admin_options = $admin_options;
92 $this->thumbnail = $thumbnail;
93 $this->translate = $translate;
94 $this->themer = $themer;
95
96 $this->more = '...';
97
98 // Allow customization of the more (...) string
99 if ( has_filter('wpp_excerpt_more') )
100 $this->more = apply_filters('wpp_excerpt_more', $this->more);
101 }
102
103 /**
104 * Sets data.
105 *
106 * @since 5.0.0
107 * @param array
108 */
109 public function set_data(array $data = [])
110 {
111 $this->data = $data;
112 }
113
114 /**
115 * Sets public options.
116 *
117 * @since 5.0.0
118 * @param array
119 */
120 public function set_public_options(array $public_options = [])
121 {
122 $this->public_options = Helper::merge_array_r(
123 $this->public_options,
124 $public_options
125 );
126 }
127
128 /**
129 * Output the HTML.
130 *
131 * @since 4.0.0
132 */
133 public function output()
134 {
135 echo $this->output;
136 }
137
138 /**
139 * Return the HTML.
140 *
141 * @since 4.0.0
142 * @return string
143 */
144 public function get_output()
145 {
146 return $this->output;
147 }
148
149 /**
150 * Build the HTML output.
151 *
152 * @since 4.0.0
153 */
154 public function build_output()
155 {
156 // Got some posts, format 'em!
157 if ( ! empty($this->data) ) {
158
159 $this->output = "\n" . "<!-- WordPress Popular Posts" . ( WP_DEBUG ? ' v' . WPP_VERSION : '' ) . " -->" . "\n";
160
161 // Allow WP themers / coders access to raw data
162 // so they can build their own output
163 if ( has_filter('wpp_custom_html') ) {
164 $this->output .= apply_filters('wpp_custom_html', $this->data, $this->public_options);
165 return;
166 }
167
168 if ( $this->public_options['theme']['name'] ) {
169 $this->output .= '<div class="popular-posts-sr">';
170
171 $theme_stylesheet = $this->themer->get_theme($this->public_options['theme']['name'])['path'] . '/style.css';
172 $theme_css_rules = wp_strip_all_tags(file_get_contents($theme_stylesheet), true);
173 $additional_styles = '';
174
175 if ( has_filter('wpp_additional_theme_styles') ) {
176 $additional_styles = wp_strip_all_tags(apply_filters('wpp_additional_theme_styles', '', $this->public_options['theme']['name']), true);
177
178 if ( $additional_styles )
179 $additional_styles = ' /* additional rules */ ' . $additional_styles;
180 }
181
182 $this->output .= '<style>' . $theme_css_rules . $additional_styles . '</style>';
183 }
184
185 /* Open HTML wrapper */
186 // Output a custom wrapper
187 if (
188 isset($this->public_options['markup']['custom_html'])
189 && $this->public_options['markup']['custom_html']
190 && isset($this->public_options['markup']['wpp-start'])
191 && isset($this->public_options['markup']['wpp-end'])
192 ){
193 $this->output .= "\n" . htmlspecialchars_decode($this->public_options['markup']['wpp-start'], ENT_QUOTES) ."\n";
194 }
195 // Output the default wrapper
196 else {
197
198 $classes = "wpp-list";
199
200 if ( $this->public_options['thumbnail']['active'] )
201 $classes .= " wpp-list-with-thumbnails";
202
203 $this->output .= "\n" . "<ul class=\"{$classes}\">" . "\n";
204
205 }
206
207 $position = 0;
208
209 // Format each post
210 foreach( $this->data as $post_object ) {
211 $position++;
212 $this->output .= $this->render_post($post_object, $position);
213 }
214
215 /* Close HTML wrapper */
216 // Output a custom wrapper
217 if (
218 isset($this->public_options['markup']['custom_html'])
219 && $this->public_options['markup']['custom_html']
220 && isset($this->public_options['markup']['wpp-start'])
221 && isset($this->public_options['markup']['wpp-end'])
222 ){
223 $this->output .= "\n" . htmlspecialchars_decode($this->public_options['markup']['wpp-end'], ENT_QUOTES) ."\n";
224 }
225 // Output default wrapper
226 else {
227 $this->output .= "</ul>" . "\n";
228 }
229
230 if ( $this->public_options['theme']['name'] ) {
231 $this->output .= "</div>";
232 }
233
234 }
235 // Got nothing to show, give 'em the old "Sorry. No data so far." message!
236 else {
237 $this->output = apply_filters('wpp_no_data', "<p class=\"wpp-no-data\">" . __('Sorry. No data so far.', 'wordpress-popular-posts') . "</p>");
238 }
239 }
240
241 /**
242 * Build the HTML markup for a single post.
243 *
244 * @since 4.0.0
245 * @access private
246 * @param object $post_object
247 * @param integer $position
248 * @return string
249 */
250 private function render_post(\stdClass $post_object, $position = 1)
251 {
252 $is_single = $this->is_single();
253 $post = '';
254 $post_id = $post_object->id;
255 $trid = $this->translate->get_object_id(
256 $post_object->id,
257 get_post_type($post_object->id)
258 );
259
260 if ( $post_id != $trid ) {
261 $post_id = $trid;
262 }
263
264 $is_current_post = ( $is_single && ($is_single == $post_id || $is_single == $post_object->id) ) ? true : false;
265
266 // Permalink
267 $permalink = $this->get_permalink($post_object, $post_id);
268
269 // Post title (and title attribute)
270 $post_title_attr = esc_attr(wp_strip_all_tags($this->get_title($post_object, $post_id)));
271 $post_title = $this->get_title($post_object, $post_id);
272
273 if ( $this->public_options['shorten_title']['active'] ) {
274 $length = ( filter_var($this->public_options['shorten_title']['length'], FILTER_VALIDATE_INT) && $this->public_options['shorten_title']['length'] > 0 )
275 ? $this->public_options['shorten_title']['length']
276 : 25;
277
278 $post_title = Helper::truncate($post_title, $length, $this->public_options['shorten_title']['words'], $this->more);
279 }
280
281 // Thumbnail
282 $post_thumbnail = $this->get_thumbnail($post_object);
283
284 // Post excerpt
285 $post_excerpt = $this->get_excerpt($post_object, $post_id);
286
287 // Post rating
288 $post_rating = $this->get_rating($post_object);
289
290 /**
291 * Post meta
292 */
293
294 // Post date
295 $post_date = $this->get_date($post_object);
296
297 // Post taxonomies
298 $post_taxonomies = $this->get_taxonomies($post_id);
299
300 // Post author
301 $post_author = $this->get_author($post_object, $post_id);
302
303 // Post views count
304 $post_views = $this->get_pageviews($post_object);
305
306 // Post comments count
307 $post_comments = $this->get_comments($post_object);
308
309 // Post meta
310 $post_meta = join(' | ', $this->get_metadata($post_object, $post_id));
311
312 // Build custom HTML output
313 if ( $this->public_options['markup']['custom_html'] ) {
314 $data = [
315 'id' => $post_id,
316 'title' => '<a href="' . $permalink . '" ' . ($post_title_attr !== $post_title ? 'title="' . $post_title_attr . '" ' : '' ) . 'class="wpp-post-title" target="' . $this->admin_options['tools']['link']['target'] . '">' . $post_title . '</a>',
317 'title_attr' => $post_title_attr,
318 'summary' => $post_excerpt,
319 'stats' => $post_meta,
320 'img' => ( ! empty($post_thumbnail) ) ? '<a href="' . $permalink . '" ' . ($post_title_attr !== $post_title ? 'title="' . $post_title_attr . '" ' : '' ) . 'target="' . $this->admin_options['tools']['link']['target'] . '">' . $post_thumbnail . '</a>' : '',
321 'img_no_link' => $post_thumbnail,
322 'url' => $permalink,
323 'text_title' => $post_title,
324 'taxonomy' => $post_taxonomies,
325 'author' => ( ! empty($post_author) ) ? '<a href="' . get_author_posts_url($post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid ) . '">' . $post_author . '</a>' : '',
326 'views' => ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" ) ? number_format_i18n($post_views) : number_format_i18n($post_views, 2),
327 'comments' => number_format_i18n($post_comments),
328 'date' => $post_date,
329 'total_items' => count($this->data),
330 'item_position' => $position
331 ];
332 $post = $this->format_content(htmlspecialchars_decode($this->public_options['markup']['post-html'], ENT_QUOTES), $data, $this->public_options['rating']). "\n";
333 } // Use the "stock" HTML output
334 else {
335 $wpp_post_class = [];
336
337 if ( $is_current_post ) {
338 $wpp_post_class[] = "current";
339 }
340
341 // Allow themers / plugin developer
342 // to add custom classes to each post
343 $wpp_post_class = apply_filters("wpp_post_class", $wpp_post_class, $post_id);
344
345 $post_thumbnail = ( ! empty($post_thumbnail) )
346 ? "<a href=\"{$permalink}\" " . ($post_title_attr !== $post_title ? "title=\"{$post_title_attr}\" " : "") . "target=\"{$this->admin_options['tools']['link']['target']}\">{$post_thumbnail}</a>\n"
347 : "";
348
349 $post_excerpt = ( ! empty($post_excerpt) )
350 ? " <span class=\"wpp-excerpt\">{$post_excerpt}</span>\n"
351 : "";
352
353 $post_meta = ( ! empty($post_meta) )
354 ? " <span class=\"wpp-meta post-stats\">{$post_meta}</span>\n"
355 : '';
356
357 $post_rating = ( ! empty($post_rating) )
358 ? " <span class=\"wpp-rating\">{$post_rating}</span>\n"
359 : "";
360
361 $post =
362 "<li" . ( ( is_array($wpp_post_class) && ! empty($wpp_post_class) ) ? ' class="' . esc_attr(implode(" ", $wpp_post_class)) . '"' : '') . ">\n"
363 . $post_thumbnail
364 . "<a href=\"{$permalink}\" " . ($post_title_attr !== $post_title ? "title=\"{$post_title_attr}\" " : "") . "class=\"wpp-post-title\" target=\"{$this->admin_options['tools']['link']['target']}\">{$post_title}</a>\n"
365 . $post_excerpt
366 . $post_meta
367 . $post_rating
368 . "</li>\n";
369 }
370
371 return apply_filters('wpp_post', $post, $post_object, $this->public_options);
372 }
373
374 /**
375 * Return the processed post/page title.
376 *
377 * @since 3.0.0
378 * @access private
379 * @param object $post_object
380 * @param integer $post_id
381 * @return string
382 */
383 private function get_title(\stdClass $post_object, $post_id)
384 {
385 if ( $post_object->id != $post_id ) {
386 $title = get_the_title($post_id);
387 } else {
388 $title = $post_object->title;
389 }
390
391 // Run the_title filter so core/plugin title hooks can
392 // be applied to the post title
393 $title = apply_filters('the_title', $title, $post_object->id);
394
395 return apply_filters('wpp_the_title', $title, $post_object->id, $post_id);
396 }
397
398 /**
399 * Return the permalink.
400 *
401 * @since 4.0.12
402 * @access private
403 * @param object $post_object
404 * @param integer $post_id
405 * @return string
406 */
407 private function get_permalink(\stdClass $post_object, $post_id) {
408 $permalink = get_permalink($post_object->id);
409
410 return $post_object->id != $post_id
411 ? $this->translate->url($permalink, $this->translate->get_current_language())
412 : $permalink;
413 }
414
415 /**
416 * Return the processed thumbnail.
417 *
418 * @since 3.0.0
419 * @access private
420 * @param object $post_object
421 * @return string
422 */
423 private function get_thumbnail(\stdClass $post_object)
424 {
425 $thumbnail = '';
426
427 if ( $this->public_options['thumbnail']['active'] ) {
428 $thumbnail = $this->thumbnail->get(
429 $post_object,
430 [
431 $this->public_options['thumbnail']['width'],
432 $this->public_options['thumbnail']['height']
433 ],
434 $this->admin_options['tools']['thumbnail']['source'],
435 $this->public_options['thumbnail']['crop'],
436 $this->public_options['thumbnail']['build']
437 );
438 }
439
440 return $thumbnail;
441 }
442
443 /**
444 * Return post excerpt.
445 *
446 * @since 3.0.0
447 * @access private
448 * @param object $post_object
449 * @param integer $post_id
450 * @return string
451 */
452 private function get_excerpt(\stdClass $post_object, $post_id)
453 {
454 $excerpt = '';
455
456 if ( $this->public_options['post-excerpt']['active'] ) {
457
458 if ( $post_object->id != $post_id ) {
459 $the_post = get_post($post_id);
460
461 $excerpt = ( empty($the_post->post_excerpt) )
462 ? $the_post->post_content
463 : $the_post->post_excerpt;
464 }
465 else {
466 $excerpt = ( empty($post_object->post_excerpt) )
467 ? $post_object->post_content
468 : $post_object->post_excerpt;
469 }
470
471 // remove caption tags
472 $excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $excerpt);
473
474 // remove Flash objects
475 $excerpt = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,\\n\\r\\t]+/smi", "", $excerpt);
476
477 // remove iframes
478 $excerpt = preg_replace("/<iframe.*?\/iframe>/i", "", $excerpt);
479
480 // remove WP shortcodes
481 $excerpt = strip_shortcodes($excerpt);
482
483 // remove style/script tags
484 $excerpt = preg_replace('@<(script|style)[^>]*?>.*?</\\1>@si', '', $excerpt);
485
486 // remove HTML tags if requested
487 if ( $this->public_options['post-excerpt']['keep_format'] ) {
488 $excerpt = strip_tags($excerpt, '<a><b><i><em><strong>');
489 } else {
490 $excerpt = strip_tags($excerpt);
491
492 // remove URLs, too
493 $excerpt = preg_replace('_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS', '', $excerpt);
494 }
495
496 }
497
498 // Balance tags, if needed
499 if ( '' !== $excerpt ) {
500
501 $excerpt = Helper::truncate($excerpt, $this->public_options['post-excerpt']['length'], $this->public_options['post-excerpt']['words'], $this->more);
502
503 if ( $this->public_options['post-excerpt']['keep_format'] )
504 $excerpt = force_balance_tags($excerpt);
505 }
506
507 return $excerpt;
508 }
509
510 /**
511 * Return post rating.
512 *
513 * @since 3.0.0
514 * @access private
515 * @param object $post_object
516 * @return string
517 */
518 private function get_rating(\stdClass $post_object)
519 {
520 $rating = '';
521
522 if ( function_exists('the_ratings_results') && $this->public_options['rating'] ) {
523 $rating = the_ratings_results($post_object->id);
524 }
525
526 return $rating;
527 }
528
529 /**
530 * Get post date.
531 *
532 * @since 3.0.0
533 * @access private
534 * @param object $post_object
535 * @return string
536 */
537 private function get_date(\stdClass $post_object)
538 {
539 $date = '';
540
541 if ( $this->public_options['stats_tag']['date']['active'] ) {
542 // Check locale
543 if ( ! $current_language_locale = $this->translate->get_locale($this->translate->get_current_language()) ) {
544 $current_language_locale = get_locale();
545 }
546
547 try {
548 Moment\Moment::setLocale($current_language_locale);
549 } // Locale not found, fallback to English (US)
550 catch( \Exception $e ) {
551 Moment\Moment::setLocale('en_US');
552 }
553
554 $m = new Moment\Moment('@' . strtotime($post_object->date));
555
556 $date = ( 'relative' == $this->public_options['stats_tag']['date']['format'] )
557 ? $m->fromNow()->getRelative()
558 : $m->format($this->public_options['stats_tag']['date']['format']);
559 }
560
561 return $date;
562 }
563
564 /**
565 * Get post taxonomies.
566 *
567 * @since 3.0.0
568 * @access private
569 * @param integer $post_id
570 * @return string
571 */
572 private function get_taxonomies($post_id)
573 {
574 $post_tax = '';
575
576 if ( (isset($this->public_options['stats_tag']['category']) && $this->public_options['stats_tag']['category']) || $this->public_options['stats_tag']['taxonomy'] ) {
577
578 $taxonomy = 'category';
579
580 if (
581 $this->public_options['stats_tag']['taxonomy']['active']
582 && ! empty($this->public_options['stats_tag']['taxonomy']['name'])
583 ) {
584 $taxonomy = $this->public_options['stats_tag']['taxonomy']['name'];
585 }
586
587 $terms = wp_get_post_terms($post_id, $taxonomy);
588
589 if ( ! is_wp_error($terms) ) {
590 // Usage: https://wordpress.stackexchange.com/a/46824
591 if ( has_filter('wpp_post_exclude_terms') ) {
592 $args = apply_filters('wpp_post_exclude_terms', []);
593 $terms = wp_list_filter($terms, $args, 'NOT');
594 }
595
596 $terms = apply_filters('wpp_post_terms', $terms);
597
598 if (
599 is_array($terms)
600 && ! empty($terms)
601 ) {
602 $taxonomy_separator = apply_filters('wpp_taxonomy_separator', ', ');
603
604 foreach ($terms as $term) {
605 $term_link = get_term_link($term);
606
607 if ( is_wp_error($term_link) )
608 continue;
609
610 $term_link = $this->translate->url($term_link, $this->translate->get_current_language());
611 $post_tax .= "<a href=\"{$term_link}\" class=\"{$taxonomy} {$taxonomy}-{$term->term_id}\">{$term->name}</a>" . $taxonomy_separator;
612 }
613 }
614 }
615
616 if ( '' != $post_tax )
617 $post_tax = rtrim($post_tax, $taxonomy_separator);
618
619 }
620
621 return $post_tax;
622 }
623
624 /**
625 * Get post author.
626 *
627 * @since 3.0.0
628 * @access private
629 * @param object $post_object
630 * @param integer $post_id
631 * @return string
632 */
633 private function get_author(\stdClass $post_object, $post_id)
634 {
635 $author = ( $this->public_options['stats_tag']['author'] )
636 ? get_the_author_meta('display_name', $post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid)
637 : "";
638
639 return $author;
640 }
641
642 /**
643 * Return post views count.
644 *
645 * @since 3.0.0
646 * @access private
647 * @param object $post_object
648 * @return int|float
649 */
650 private function get_pageviews(\stdClass $post_object)
651 {
652 $pageviews = 0;
653
654 if (
655 (
656 $this->public_options['order_by'] == "views"
657 || $this->public_options['order_by'] == "avg"
658 || $this->public_options['stats_tag']['views']
659 )
660 && ( isset($post_object->pageviews) || isset($post_object->avg_views) )
661 ) {
662 $pageviews = ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" )
663 ? $post_object->pageviews
664 : $post_object->avg_views;
665 }
666
667 return $pageviews;
668 }
669
670 /**
671 * Return post comment count.
672 *
673 * @since 3.0.0
674 * @access private
675 * @param object $post_object
676 * @return int
677 */
678 private function get_comments(\stdClass $post_object)
679 {
680 $comments = ( ( $this->public_options['order_by'] == "comments" || $this->public_options['stats_tag']['comment_count'] ) && isset($post_object->comment_count) )
681 ? $post_object->comment_count
682 : 0;
683
684 return $comments;
685 }
686
687 /**
688 * Return post metadata.
689 *
690 * @since 3.0.0
691 * @access private
692 * @param object $post_object
693 * @param integer $post_id
694 * @return array
695 */
696 private function get_metadata(\stdClass $post_object, $post_id)
697 {
698 $stats = [];
699
700 // comments
701 if ( $this->public_options['stats_tag']['comment_count'] ) {
702 $comments = $this->get_comments($post_object);
703
704 $comments_text = sprintf(
705 _n('%s comment', '%s comments', $comments, 'wordpress-popular-posts'),
706 number_format_i18n($comments)
707 );
708 }
709
710 // views
711 if ( $this->public_options['stats_tag']['views'] ) {
712 $pageviews = $this->get_pageviews($post_object);
713
714 if ( $this->public_options['order_by'] == 'avg' ) {
715 $views_text = sprintf(
716 _n('%s view per day', '%s views per day', $pageviews, 'wordpress-popular-posts'),
717 number_format_i18n($pageviews, (fmod($pageviews, 1) !== 0.0 ? 2 : 0))
718 );
719 }
720 else {
721 $views_text = sprintf(
722 _n('%s view', '%s views', $pageviews, 'wordpress-popular-posts'),
723 number_format_i18n($pageviews)
724 );
725 }
726 }
727
728 if ( "comments" == $this->public_options['order_by'] ) {
729 if ( $this->public_options['stats_tag']['comment_count'] )
730 $stats[] = '<span class="wpp-comments">' . $comments_text . '</span>'; // First comments count
731 if ( $this->public_options['stats_tag']['views'] )
732 $stats[] = '<span class="wpp-views">' . $views_text . "</span>"; // ... then views
733 } else {
734 if ( $this->public_options['stats_tag']['views'] )
735 $stats[] = '<span class="wpp-views">' . $views_text . "</span>"; // First views count
736 if ( $this->public_options['stats_tag']['comment_count'] )
737 $stats[] = '<span class="wpp-comments">' . $comments_text . '</span>'; // ... then comments
738 }
739
740 // author
741 if ( $this->public_options['stats_tag']['author'] ) {
742 $author = $this->get_author($post_object, $post_id);
743 $author_url = get_author_posts_url($post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid);
744 $display_name = '<a href="' . $this->translate->url($author_url, $this->translate->get_current_language()) . '">' . $author . '</a>';
745 $stats[] = '<span class="wpp-author">' . sprintf(__('by %s', 'wordpress-popular-posts'), $display_name) . '</span>';
746 }
747
748 // date
749 if ( $this->public_options['stats_tag']['date']['active'] ) {
750 $date = $this->get_date($post_object);
751 $stats[] = '<span class="wpp-date">' . ( 'relative' == $this->public_options['stats_tag']['date']['format'] ? sprintf(__('posted %s', 'wordpress-popular-posts'), $date) : sprintf(__('posted on %s', 'wordpress-popular-posts'), $date) ) . '</span>';
752 }
753
754 // taxonomy
755 if ( $this->public_options['stats_tag']['category'] ) {
756 $post_tax = $this->get_taxonomies($post_id);
757
758 if ( $post_tax != '' ) {
759 $stats[] = '<span class="wpp-category">' . sprintf(__('under %s', 'wordpress-popular-posts'), $post_tax) . '</span>';
760 }
761 }
762
763 return $stats;
764 }
765
766 /**
767 * Parse content tags.
768 *
769 * @since 1.4.6
770 * @access private
771 * @param string HTML string with content tags
772 * @param array Post data
773 * @param bool Used to display post rating (if functionality is available)
774 * @return string
775 */
776 private function format_content($string, $data = [], $rating) {
777
778 if ( empty($string) || ( empty($data) || ! is_array($data) ) )
779 return false;
780
781 $params = [];
782 $pattern = '/\{(pid|excerpt|summary|meta|stats|title|title_attr|image|thumb|thumb_img|thumb_url|rating|score|url|text_title|author|taxonomy|category|views|comments|date|total_items|item_position)\}/i';
783 preg_match_all($pattern, $string, $matches);
784
785 array_map('strtolower', $matches[0]);
786
787 if ( in_array("{pid}", $matches[0]) ) {
788 $string = str_replace("{pid}", $data['id'], $string);
789 }
790
791 if ( in_array("{title}", $matches[0]) ) {
792 $string = str_replace("{title}", $data['title'], $string);
793 }
794
795 if ( in_array("{title_attr}", $matches[0]) ) {
796 $string = str_replace("{title_attr}", $data['title_attr'], $string);
797 }
798
799 if ( in_array("{meta}", $matches[0]) || in_array("{stats}", $matches[0]) ) {
800 $string = str_replace(["{meta}", "{stats}"], $data['stats'], $string);
801 }
802
803 if ( in_array("{excerpt}", $matches[0]) || in_array("{summary}", $matches[0]) ) {
804 $string = str_replace(["{excerpt}", "{summary}"], $data['summary'], $string);
805 }
806
807 if ( in_array("{image}", $matches[0]) || in_array("{thumb}", $matches[0]) ) {
808 $string = str_replace(["{image}", "{thumb}"], $data['img'], $string);
809 }
810
811 if ( in_array("{thumb_img}", $matches[0]) ) {
812 $string = str_replace("{thumb_img}", $data['img_no_link'], $string);
813 }
814
815 if ( in_array("{thumb_url}", $matches[0]) && ! empty($data['img_no_link']) ) {
816 $dom = new \DOMDocument;
817
818 if ( $dom->loadHTML($data['img_no_link']) ) {
819 $img_tag = $dom->getElementsByTagName('img');
820
821 if ( $img_tag->length ) {
822 foreach( $img_tag as $node ) {
823 if ( $node->hasAttribute('src') || $node->hasAttribute('data-img-src') ) {
824 $src = $node->hasAttribute('src') ? $node->getAttribute('src') : $node->getAttribute('data-img-src');
825 $string = str_replace("{thumb_url}", $src, $string);
826 }
827 }
828 }
829 }
830 }
831
832 // WP-PostRatings check
833 if ( $rating ) {
834 if ( function_exists('the_ratings_results') && in_array("{rating}", $matches[0]) ) {
835 $string = str_replace("{rating}", the_ratings_results($data['id']), $string);
836 }
837
838 if ( function_exists('expand_ratings_template') && in_array("{score}", $matches[0]) ) {
839 $string = str_replace("{score}", expand_ratings_template('%RATINGS_SCORE%', $data['id']), $string);
840 // removing the redundant plus sign
841 $string = str_replace('+', '', $string);
842 }
843 }
844
845 if ( in_array("{url}", $matches[0]) ) {
846 $string = str_replace("{url}", $data['url'], $string);
847 }
848
849 if ( in_array("{text_title}", $matches[0]) ) {
850 $string = str_replace("{text_title}", $data['text_title'], $string);
851 }
852
853 if ( in_array("{author}", $matches[0]) ) {
854 $string = str_replace("{author}", $data['author'], $string);
855 }
856
857 if ( in_array("{taxonomy}", $matches[0]) || in_array("{category}", $matches[0]) ) {
858 $string = str_replace(["{taxonomy}", "{category}"], $data['taxonomy'], $string);
859 }
860
861 if ( in_array("{views}", $matches[0]) ) {
862 $string = str_replace("{views}", $data['views'], $string);
863 }
864
865 if ( in_array("{comments}", $matches[0]) ) {
866 $string = str_replace("{comments}", $data['comments'], $string);
867 }
868
869 if ( in_array("{date}", $matches[0]) ) {
870 $string = str_replace("{date}", $data['date'], $string);
871 }
872
873 if ( in_array("{total_items}", $matches[0]) ) {
874 $string = str_replace("{total_items}", $data['total_items'], $string);
875 }
876
877 if ( in_array("{item_position}", $matches[0]) ) {
878 $string = str_replace("{item_position}", $data['item_position'], $string);
879 }
880
881 return apply_filters("wpp_parse_custom_content_tags", $string, $data['id']);
882 }
883
884 /**
885 * Checks whether we're currently seeing a single post/page/CPT.
886 *
887 * @since 5.0.0
888 * @return int
889 */
890 public function is_single()
891 {
892 return apply_filters('wpp_is_single', Helper::is_single());
893 }
894 }
895