PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.0.1
WP Popular Posts v5.0.1
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 WPP_Query.php 6 years ago WordPressPopularPosts.php 6 years ago template-tags.php 6 years ago
Output.php
883 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 'summary' => $post_excerpt,
318 'stats' => $post_meta,
319 '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>' : '',
320 'img_no_link' => $post_thumbnail,
321 'url' => $permalink,
322 'text_title' => $post_title_attr,
323 'taxonomy' => $post_taxonomies,
324 '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>' : '',
325 'views' => ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" ) ? number_format_i18n($post_views) : number_format_i18n($post_views, 2),
326 'comments' => number_format_i18n($post_comments),
327 'date' => $post_date,
328 'total_items' => count($this->data),
329 'item_position' => $position
330 ];
331 $post = $this->format_content(htmlspecialchars_decode($this->public_options['markup']['post-html'], ENT_QUOTES), $data, $this->public_options['rating']). "\n";
332 } // Use the "stock" HTML output
333 else {
334 $wpp_post_class = [];
335
336 if ( $is_current_post ) {
337 $wpp_post_class[] = "current";
338 }
339
340 // Allow themers / plugin developer
341 // to add custom classes to each post
342 $wpp_post_class = apply_filters("wpp_post_class", $wpp_post_class, $post_id);
343
344 $post_thumbnail = ( ! empty($post_thumbnail) )
345 ? "<a href=\"{$permalink}\" " . ($post_title_attr !== $post_title ? "title=\"{$post_title_attr}\" " : "") . "target=\"{$this->admin_options['tools']['link']['target']}\">{$post_thumbnail}</a>\n"
346 : "";
347
348 $post_excerpt = ( ! empty($post_excerpt) )
349 ? " <span class=\"wpp-excerpt\">{$post_excerpt}</span>\n"
350 : "";
351
352 $post_meta = ( ! empty($post_meta) )
353 ? " <span class=\"wpp-meta post-stats\">{$post_meta}</span>\n"
354 : '';
355
356 $post_rating = ( ! empty($post_rating) )
357 ? " <span class=\"wpp-rating\">{$post_rating}</span>\n"
358 : "";
359
360 $post =
361 "<li" . ( ( is_array($wpp_post_class) && ! empty($wpp_post_class) ) ? ' class="' . esc_attr(implode(" ", $wpp_post_class)) . '"' : '') . ">\n"
362 . $post_thumbnail
363 . "<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"
364 . $post_excerpt
365 . $post_meta
366 . $post_rating
367 . "</li>\n";
368 }
369
370 return apply_filters('wpp_post', $post, $post_object, $this->public_options);
371 }
372
373 /**
374 * Return the processed post/page title.
375 *
376 * @since 3.0.0
377 * @access private
378 * @param object $post_object
379 * @param integer $post_id
380 * @return string
381 */
382 private function get_title(\stdClass $post_object, $post_id)
383 {
384 if ( $post_object->id != $post_id ) {
385 $title = get_the_title($post_id);
386 } else {
387 $title = $post_object->title;
388 }
389 return apply_filters('wpp_the_title', $title, $post_object->id, $post_id);
390 }
391
392 /**
393 * Return the permalink.
394 *
395 * @since 4.0.12
396 * @access private
397 * @param object $post_object
398 * @param integer $post_id
399 * @return string
400 */
401 private function get_permalink(\stdClass $post_object, $post_id) {
402 $permalink = get_permalink($post_object->id);
403
404 return $post_object->id != $post_id
405 ? $this->translate->url($permalink, $this->translate->get_current_language())
406 : $permalink;
407 }
408
409 /**
410 * Return the processed thumbnail.
411 *
412 * @since 3.0.0
413 * @access private
414 * @param object $post_object
415 * @return string
416 */
417 private function get_thumbnail(\stdClass $post_object)
418 {
419 $thumbnail = '';
420
421 if ( $this->public_options['thumbnail']['active'] ) {
422 $thumbnail = $this->thumbnail->get(
423 $post_object,
424 [
425 $this->public_options['thumbnail']['width'],
426 $this->public_options['thumbnail']['height']
427 ],
428 $this->admin_options['tools']['thumbnail']['source'],
429 $this->public_options['thumbnail']['crop'],
430 $this->public_options['thumbnail']['build']
431 );
432 }
433
434 return $thumbnail;
435 }
436
437 /**
438 * Return post excerpt.
439 *
440 * @since 3.0.0
441 * @access private
442 * @param object $post_object
443 * @param integer $post_id
444 * @return string
445 */
446 private function get_excerpt(\stdClass $post_object, $post_id)
447 {
448 $excerpt = '';
449
450 if ( $this->public_options['post-excerpt']['active'] ) {
451
452 if ( $post_object->id != $post_id ) {
453 $the_post = get_post($post_id);
454
455 $excerpt = ( empty($the_post->post_excerpt) )
456 ? $the_post->post_content
457 : $the_post->post_excerpt;
458 }
459 else {
460 $excerpt = ( empty($post_object->post_excerpt) )
461 ? $post_object->post_content
462 : $post_object->post_excerpt;
463 }
464
465 // remove caption tags
466 $excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $excerpt);
467
468 // remove Flash objects
469 $excerpt = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,\\n\\r\\t]+/smi", "", $excerpt);
470
471 // remove iframes
472 $excerpt = preg_replace("/<iframe.*?\/iframe>/i", "", $excerpt);
473
474 // remove WP shortcodes
475 $excerpt = strip_shortcodes($excerpt);
476
477 // remove style/script tags
478 $excerpt = preg_replace('@<(script|style)[^>]*?>.*?</\\1>@si', '', $excerpt);
479
480 // remove HTML tags if requested
481 if ( $this->public_options['post-excerpt']['keep_format'] ) {
482 $excerpt = strip_tags($excerpt, '<a><b><i><em><strong>');
483 } else {
484 $excerpt = strip_tags($excerpt);
485
486 // remove URLs, too
487 $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);
488 }
489
490 }
491
492 // Balance tags, if needed
493 if ( '' !== $excerpt ) {
494
495 $excerpt = Helper::truncate($excerpt, $this->public_options['post-excerpt']['length'], $this->public_options['post-excerpt']['words'], $this->more);
496
497 if ( $this->public_options['post-excerpt']['keep_format'] )
498 $excerpt = force_balance_tags($excerpt);
499 }
500
501 return $excerpt;
502 }
503
504 /**
505 * Return post rating.
506 *
507 * @since 3.0.0
508 * @access private
509 * @param object $post_object
510 * @return string
511 */
512 private function get_rating(\stdClass $post_object)
513 {
514 $rating = '';
515
516 if ( function_exists('the_ratings_results') && $this->public_options['rating'] ) {
517 $rating = the_ratings_results($post_object->id);
518 }
519
520 return $rating;
521 }
522
523 /**
524 * Get post date.
525 *
526 * @since 3.0.0
527 * @access private
528 * @param object $post_object
529 * @return string
530 */
531 private function get_date(\stdClass $post_object)
532 {
533 $date = '';
534
535 if ( $this->public_options['stats_tag']['date']['active'] ) {
536 // Check locale
537 if ( ! $current_language_locale = $this->translate->get_locale($this->translate->get_current_language()) ) {
538 $current_language_locale = get_locale();
539 }
540
541 try {
542 Moment\Moment::setLocale($current_language_locale);
543 } // Locale not found, fallback to English (US)
544 catch( \Exception $e ) {
545 Moment\Moment::setLocale('en_US');
546 }
547
548 $m = new Moment\Moment('@' . strtotime($post_object->date));
549
550 $date = ( 'relative' == $this->public_options['stats_tag']['date']['format'] )
551 ? $m->fromNow()->getRelative()
552 : $m->format($this->public_options['stats_tag']['date']['format']);
553 }
554
555 return $date;
556 }
557
558 /**
559 * Get post taxonomies.
560 *
561 * @since 3.0.0
562 * @access private
563 * @param integer $post_id
564 * @return string
565 */
566 private function get_taxonomies($post_id)
567 {
568 $post_tax = '';
569
570 if ( (isset($this->public_options['stats_tag']['category']) && $this->public_options['stats_tag']['category']) || $this->public_options['stats_tag']['taxonomy'] ) {
571
572 $taxonomy = 'category';
573
574 if (
575 $this->public_options['stats_tag']['taxonomy']['active']
576 && ! empty($this->public_options['stats_tag']['taxonomy']['name'])
577 ) {
578 $taxonomy = $this->public_options['stats_tag']['taxonomy']['name'];
579 }
580
581 $terms = wp_get_post_terms($post_id, $taxonomy);
582
583 if ( ! is_wp_error($terms) ) {
584 // Usage: https://wordpress.stackexchange.com/a/46824
585 if ( has_filter('wpp_post_exclude_terms') ) {
586 $args = apply_filters('wpp_post_exclude_terms', []);
587 $terms = wp_list_filter($terms, $args, 'NOT');
588 }
589
590 if (
591 is_array($terms)
592 && ! empty($terms)
593 ) {
594 $taxonomy_separator = apply_filters('wpp_taxonomy_separator', ', ');
595
596 foreach ($terms as $term) {
597 $term_link = get_term_link($term);
598
599 if ( is_wp_error($term_link) )
600 continue;
601
602 $term_link = $this->translate->url($term_link, $this->translate->get_current_language());
603 $post_tax .= "<a href=\"{$term_link}\" class=\"{$taxonomy} {$taxonomy}-{$term->term_id}\">{$term->name}</a>" . $taxonomy_separator;
604 }
605 }
606 }
607
608 if ( '' != $post_tax )
609 $post_tax = rtrim($post_tax, $taxonomy_separator);
610
611 }
612
613 return $post_tax;
614 }
615
616 /**
617 * Get post author.
618 *
619 * @since 3.0.0
620 * @access private
621 * @param object $post_object
622 * @param integer $post_id
623 * @return string
624 */
625 private function get_author(\stdClass $post_object, $post_id)
626 {
627 $author = ( $this->public_options['stats_tag']['author'] )
628 ? get_the_author_meta('display_name', $post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid)
629 : "";
630
631 return $author;
632 }
633
634 /**
635 * Return post views count.
636 *
637 * @since 3.0.0
638 * @access private
639 * @param object $post_object
640 * @return int|float
641 */
642 private function get_pageviews(\stdClass $post_object)
643 {
644 $pageviews = 0;
645
646 if (
647 (
648 $this->public_options['order_by'] == "views"
649 || $this->public_options['order_by'] == "avg"
650 || $this->public_options['stats_tag']['views']
651 )
652 && ( isset($post_object->pageviews) || isset($post_object->avg_views) )
653 ) {
654 $pageviews = ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" )
655 ? $post_object->pageviews
656 : $post_object->avg_views;
657 }
658
659 return $pageviews;
660 }
661
662 /**
663 * Return post comment count.
664 *
665 * @since 3.0.0
666 * @access private
667 * @param object $post_object
668 * @return int
669 */
670 private function get_comments(\stdClass $post_object)
671 {
672 $comments = ( ( $this->public_options['order_by'] == "comments" || $this->public_options['stats_tag']['comment_count'] ) && isset($post_object->comment_count) )
673 ? $post_object->comment_count
674 : 0;
675
676 return $comments;
677 }
678
679 /**
680 * Return post metadata.
681 *
682 * @since 3.0.0
683 * @access private
684 * @param object $post_object
685 * @param integer $post_id
686 * @return array
687 */
688 private function get_metadata(\stdClass $post_object, $post_id)
689 {
690 $stats = [];
691
692 // comments
693 if ( $this->public_options['stats_tag']['comment_count'] ) {
694 $comments = $this->get_comments($post_object);
695
696 $comments_text = sprintf(
697 _n('%s comment', '%s comments', $comments, 'wordpress-popular-posts'),
698 number_format_i18n($comments)
699 );
700 }
701
702 // views
703 if ( $this->public_options['stats_tag']['views'] ) {
704 $pageviews = $this->get_pageviews($post_object);
705
706 if ( $this->public_options['order_by'] == 'avg' ) {
707 $views_text = sprintf(
708 _n('%s view per day', '%s views per day', $pageviews, 'wordpress-popular-posts'),
709 number_format_i18n($pageviews, (fmod($pageviews, 1) !== 0.0 ? 2 : 0))
710 );
711 }
712 else {
713 $views_text = sprintf(
714 _n('%s view', '%s views', $pageviews, 'wordpress-popular-posts'),
715 number_format_i18n($pageviews)
716 );
717 }
718 }
719
720 if ( "comments" == $this->public_options['order_by'] ) {
721 if ( $this->public_options['stats_tag']['comment_count'] )
722 $stats[] = '<span class="wpp-comments">' . $comments_text . '</span>'; // First comments count
723 if ( $this->public_options['stats_tag']['views'] )
724 $stats[] = '<span class="wpp-views">' . $views_text . "</span>"; // ... then views
725 } else {
726 if ( $this->public_options['stats_tag']['views'] )
727 $stats[] = '<span class="wpp-views">' . $views_text . "</span>"; // First views count
728 if ( $this->public_options['stats_tag']['comment_count'] )
729 $stats[] = '<span class="wpp-comments">' . $comments_text . '</span>'; // ... then comments
730 }
731
732 // author
733 if ( $this->public_options['stats_tag']['author'] ) {
734 $author = $this->get_author($post_object, $post_id);
735 $author_url = get_author_posts_url($post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid);
736 $display_name = '<a href="' . $this->translate->url($author_url, $this->translate->get_current_language()) . '">' . $author . '</a>';
737 $stats[] = '<span class="wpp-author">' . sprintf(__('by %s', 'wordpress-popular-posts'), $display_name) . '</span>';
738 }
739
740 // date
741 if ( $this->public_options['stats_tag']['date']['active'] ) {
742 $date = $this->get_date($post_object);
743 $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>';
744 }
745
746 // taxonomy
747 if ( $this->public_options['stats_tag']['category'] ) {
748 $post_tax = $this->get_taxonomies($post_id);
749
750 if ( $post_tax != '' ) {
751 $stats[] = '<span class="wpp-category">' . sprintf(__('under %s', 'wordpress-popular-posts'), $post_tax) . '</span>';
752 }
753 }
754
755 return $stats;
756 }
757
758 /**
759 * Parse content tags.
760 *
761 * @since 1.4.6
762 * @access private
763 * @param string HTML string with content tags
764 * @param array Post data
765 * @param bool Used to display post rating (if functionality is available)
766 * @return string
767 */
768 private function format_content($string, $data = [], $rating) {
769
770 if ( empty($string) || ( empty($data) || ! is_array($data) ) )
771 return false;
772
773 $params = [];
774 $pattern = '/\{(pid|excerpt|summary|meta|stats|title|image|thumb|thumb_img|thumb_url|rating|score|url|text_title|author|taxonomy|category|views|comments|date|total_items|item_position)\}/i';
775 preg_match_all($pattern, $string, $matches);
776
777 array_map('strtolower', $matches[0]);
778
779 if ( in_array("{pid}", $matches[0]) ) {
780 $string = str_replace("{pid}", $data['id'], $string);
781 }
782
783 if ( in_array("{title}", $matches[0]) ) {
784 $string = str_replace("{title}", $data['title'], $string);
785 }
786
787 if ( in_array("{meta}", $matches[0]) || in_array("{stats}", $matches[0]) ) {
788 $string = str_replace(["{meta}", "{stats}"], $data['stats'], $string);
789 }
790
791 if ( in_array("{excerpt}", $matches[0]) || in_array("{summary}", $matches[0]) ) {
792 $string = str_replace(["{excerpt}", "{summary}"], $data['summary'], $string);
793 }
794
795 if ( in_array("{image}", $matches[0]) || in_array("{thumb}", $matches[0]) ) {
796 $string = str_replace(["{image}", "{thumb}"], $data['img'], $string);
797 }
798
799 if ( in_array("{thumb_img}", $matches[0]) ) {
800 $string = str_replace("{thumb_img}", $data['img_no_link'], $string);
801 }
802
803 if ( in_array("{thumb_url}", $matches[0]) && ! empty($data['img_no_link']) ) {
804 $dom = new \DOMDocument;
805
806 if ( $dom->loadHTML($data['img_no_link']) ) {
807 $img_tag = $dom->getElementsByTagName('img');
808
809 if ( $img_tag->length ) {
810 foreach( $img_tag as $node ) {
811 if ( $node->hasAttribute('src') || $node->hasAttribute('data-img-src') ) {
812 $src = $node->hasAttribute('src') ? $node->getAttribute('src') : $node->getAttribute('data-img-src');
813 $string = str_replace("{thumb_url}", $src, $string);
814 }
815 }
816 }
817 }
818 }
819
820 // WP-PostRatings check
821 if ( $rating ) {
822 if ( function_exists('the_ratings_results') && in_array("{rating}", $matches[0]) ) {
823 $string = str_replace("{rating}", the_ratings_results($data['id']), $string);
824 }
825
826 if ( function_exists('expand_ratings_template') && in_array("{score}", $matches[0]) ) {
827 $string = str_replace("{score}", expand_ratings_template('%RATINGS_SCORE%', $data['id']), $string);
828 // removing the redundant plus sign
829 $string = str_replace('+', '', $string);
830 }
831 }
832
833 if ( in_array("{url}", $matches[0]) ) {
834 $string = str_replace("{url}", $data['url'], $string);
835 }
836
837 if ( in_array("{text_title}", $matches[0]) ) {
838 $string = str_replace("{text_title}", $data['text_title'], $string);
839 }
840
841 if ( in_array("{author}", $matches[0]) ) {
842 $string = str_replace("{author}", $data['author'], $string);
843 }
844
845 if ( in_array("{taxonomy}", $matches[0]) || in_array("{category}", $matches[0]) ) {
846 $string = str_replace(["{taxonomy}", "{category}"], $data['taxonomy'], $string);
847 }
848
849 if ( in_array("{views}", $matches[0]) ) {
850 $string = str_replace("{views}", $data['views'], $string);
851 }
852
853 if ( in_array("{comments}", $matches[0]) ) {
854 $string = str_replace("{comments}", $data['comments'], $string);
855 }
856
857 if ( in_array("{date}", $matches[0]) ) {
858 $string = str_replace("{date}", $data['date'], $string);
859 }
860
861 if ( in_array("{total_items}", $matches[0]) ) {
862 $string = str_replace("{total_items}", $data['total_items'], $string);
863 }
864
865 if ( in_array("{item_position}", $matches[0]) ) {
866 $string = str_replace("{item_position}", $data['item_position'], $string);
867 }
868
869 return apply_filters("wpp_parse_custom_content_tags", $string, $data['id']);
870 }
871
872 /**
873 * Checks whether we're currently seeing a single post/page/CPT.
874 *
875 * @since 5.0.0
876 * @return int
877 */
878 public function is_single()
879 {
880 return apply_filters('wpp_is_single', Helper::is_single());
881 }
882 }
883