PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.3.6
WP Popular Posts v5.3.6
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 4 years ago Admin 4 years ago Block 4 years ago Container 4 years ago Front 4 years ago Rest 4 years ago Widget 4 years ago Bootstrap.php 4 years ago Cache.php 4 years ago Helper.php 4 years ago I18N.php 4 years ago Image.php 4 years ago Output.php 4 years ago Query.php 4 years ago Settings.php 4 years ago Themer.php 4 years ago Translate.php 4 years ago WordPressPopularPosts.php 4 years ago deprecated.php 4 years ago template-tags.php 4 years ago
Output.php
936 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 Settings::get('widget_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->get_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 $this->output = "\n" . ( WP_DEBUG ? '<!-- WordPress Popular Posts v' . WPP_VERSION . ( $this->admin_options['tools']['cache']['active'] ? ' - cached' : '' ) . ' -->' : '' ) . "\n" . $this->output;
147 return $this->output;
148 }
149
150 /**
151 * Build the HTML output.
152 *
153 * @since 4.0.0
154 */
155 public function build_output()
156 {
157 // Got some posts, format 'em!
158 if ( ! empty($this->data) ) {
159
160 $this->output = '';
161
162 // Allow WP themers / coders access to raw data
163 // so they can build their own output
164 if ( has_filter('wpp_custom_html') ) {
165 $this->output .= apply_filters('wpp_custom_html', $this->data, $this->public_options);
166 return;
167 }
168
169 if (
170 isset($this->public_options['theme']['name'])
171 && $this->public_options['theme']['name']
172 ) {
173 $this->output .= '<div class="popular-posts-sr">';
174
175 if ( @file_exists(get_template_directory() . '/wordpress-popular-posts/themes/' . $this->public_options['theme']['name'] . '/style.css') ) {
176 $theme_stylesheet = get_template_directory() . '/wordpress-popular-posts/themes/' . $this->public_options['theme']['name'] . '/style.css';
177 } else {
178 $theme_stylesheet = $this->themer->get_theme($this->public_options['theme']['name'])['path'] . '/style.css';
179 }
180
181 $theme_css_rules = wp_strip_all_tags(file_get_contents($theme_stylesheet), true);
182 $additional_styles = '';
183
184 if ( has_filter('wpp_additional_theme_styles') ) {
185 $additional_styles = wp_strip_all_tags(apply_filters('wpp_additional_theme_styles', '', $this->public_options['theme']['name']), true);
186
187 if ( $additional_styles )
188 $additional_styles = ' /* additional rules */ ' . $additional_styles;
189 }
190
191 $this->output .= '<style>' . $theme_css_rules . $additional_styles . '</style>';
192 }
193
194 /* Open HTML wrapper */
195 // Output a custom wrapper
196 if (
197 isset($this->public_options['markup']['custom_html'])
198 && $this->public_options['markup']['custom_html']
199 && isset($this->public_options['markup']['wpp-start'])
200 && isset($this->public_options['markup']['wpp-end'])
201 ){
202 $this->output .= "\n" . htmlspecialchars_decode($this->public_options['markup']['wpp-start'], ENT_QUOTES) ."\n";
203 }
204 // Output the default wrapper
205 else {
206
207 $classes = "wpp-list";
208
209 if ( $this->public_options['thumbnail']['active'] )
210 $classes .= " wpp-list-with-thumbnails";
211
212 $this->output .= "\n" . "<ul class=\"{$classes}\">" . "\n";
213
214 }
215
216 $position = 0;
217
218 // Format each post
219 foreach( $this->data as $post_object ) {
220 $position++;
221 $this->output .= $this->render_post($post_object, $position);
222 }
223
224 /* Close HTML wrapper */
225 // Output a custom wrapper
226 if (
227 isset($this->public_options['markup']['custom_html'])
228 && $this->public_options['markup']['custom_html']
229 && isset($this->public_options['markup']['wpp-start'])
230 && isset($this->public_options['markup']['wpp-end'])
231 ){
232 $this->output .= "\n" . htmlspecialchars_decode($this->public_options['markup']['wpp-end'], ENT_QUOTES) ."\n";
233 }
234 // Output default wrapper
235 else {
236 $this->output .= "</ul>" . "\n";
237 }
238
239 if (
240 isset($this->public_options['theme']['name'])
241 && $this->public_options['theme']['name']
242 ) {
243 $this->output .= "</div>";
244 }
245
246 }
247 // Got nothing to show, give 'em the old "Sorry. No data so far." message!
248 else {
249 $this->output = apply_filters('wpp_no_data', "<p class=\"wpp-no-data\">" . __('Sorry. No data so far.', 'wordpress-popular-posts') . "</p>");
250 }
251 }
252
253 /**
254 * Build the HTML markup for a single post.
255 *
256 * @since 4.0.0
257 * @access private
258 * @param object $post_object
259 * @param integer $position
260 * @return string
261 */
262 private function render_post(\stdClass $post_object, $position = 1)
263 {
264 $is_single = $this->is_single();
265 $post = '';
266 $post_id = $post_object->id;
267 $trid = $this->translate->get_object_id(
268 $post_object->id,
269 get_post_type($post_object->id)
270 );
271
272 if ( $post_id != $trid ) {
273 $post_id = $trid;
274 }
275
276 $is_current_post = ( $is_single && ($is_single == $post_id || $is_single == $post_object->id) ) ? true : false;
277
278 // Permalink
279 $permalink = $this->get_permalink($post_object, $post_id);
280
281 // Post title (and title attribute)
282 $post_title_attr = esc_attr(wp_strip_all_tags($this->get_title($post_object, $post_id)));
283 $post_title = $this->get_title($post_object, $post_id);
284
285 if ( $this->public_options['shorten_title']['active'] ) {
286 $length = ( filter_var($this->public_options['shorten_title']['length'], FILTER_VALIDATE_INT) && $this->public_options['shorten_title']['length'] > 0 )
287 ? $this->public_options['shorten_title']['length']
288 : 25;
289
290 $post_title = Helper::truncate($post_title, $length, $this->public_options['shorten_title']['words'], $this->more);
291 }
292
293 // Thumbnail
294 $post_thumbnail = $this->get_thumbnail($post_id);
295
296 // Post excerpt
297 $post_excerpt = $this->get_excerpt($post_object, $post_id);
298
299 // Post rating
300 $post_rating = $this->get_rating($post_object);
301
302 /**
303 * Post meta
304 */
305
306 // Post date
307 $post_date = $this->get_date($post_object);
308
309 // Post taxonomies
310 $post_taxonomies = $this->get_taxonomies($post_id);
311
312 // Post author
313 $post_author = $this->get_author($post_object, $post_id);
314
315 // Post views count
316 $post_views = $this->get_pageviews($post_object);
317
318 // Post comments count
319 $post_comments = $this->get_comments($post_object);
320
321 // Post meta
322 $meta_arr = $this->get_metadata(
323 $post_object,
324 $post_id,
325 $post_date,
326 $post_taxonomies,
327 $post_author,
328 $post_views,
329 $post_comments
330 );
331
332 if (
333 is_array($meta_arr)
334 && ! empty($meta_arr)
335 && "views" == $this->public_options['order_by']
336 ) {
337 $keys = ['views', 'comments', 'author', 'date', 'taxonomy'];
338 $new_meta_arr = [];
339
340 foreach($keys as $key) {
341 if ( isset($meta_arr[$key]))
342 $new_meta_arr[$key] = $meta_arr[$key];
343 }
344
345 if ( ! empty($new_meta_arr) )
346 $meta_arr = $new_meta_arr;
347 }
348
349 $post_meta_separator = apply_filters('wpp_post_meta_separator', ' | ');
350 $post_meta = join($post_meta_separator, $meta_arr);
351
352 $prettify_numbers = apply_filters('wpp_pretiffy_numbers', true);
353
354 // Build custom HTML output
355 if ( $this->public_options['markup']['custom_html'] ) {
356 $data = [
357 'id' => $post_id,
358 '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>',
359 'title_attr' => $post_title_attr,
360 'summary' => $post_excerpt,
361 'stats' => $post_meta,
362 '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>' : '',
363 'img_no_link' => $post_thumbnail,
364 'url' => $permalink,
365 'text_title' => $post_title,
366 'taxonomy' => $post_taxonomies,
367 'taxonomy_copy' => isset($meta_arr['taxonomy']) ? $meta_arr['taxonomy'] : null,
368 '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>' : '',
369 'author_copy' => isset($meta_arr['author']) ? $meta_arr['author'] : null,
370 'views' => ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" ) ? ($prettify_numbers ? Helper::prettify_number($post_views) : number_format_i18n($post_views)) : ($prettify_numbers ? Helper::prettify_number($post_views, 2) : number_format_i18n($post_views, 2)),
371 'views_copy' => isset($meta_arr['views']) ? $meta_arr['views'] : null,
372 'comments' => $prettify_numbers ? Helper::prettify_number($post_comments) : number_format_i18n($post_comments),
373 'comments_copy' => isset($meta_arr['comments']) ? $meta_arr['comments'] : null,
374 'date' => $post_date,
375 'date_copy' => isset($meta_arr['date']) ? $meta_arr['date'] : null,
376 'total_items' => count($this->data),
377 'item_position' => $position
378 ];
379 $post = $this->format_content(htmlspecialchars_decode($this->public_options['markup']['post-html'], ENT_QUOTES), $data, $this->public_options['rating']). "\n";
380 } // Use the "stock" HTML output
381 else {
382 $wpp_post_class = [];
383
384 if ( $is_current_post ) {
385 $wpp_post_class[] = "current";
386 }
387
388 // Allow themers / plugin developer
389 // to add custom classes to each post
390 $wpp_post_class = apply_filters("wpp_post_class", $wpp_post_class, $post_id);
391
392 $post_thumbnail = ( ! empty($post_thumbnail) )
393 ? "<a href=\"{$permalink}\" " . ($post_title_attr !== $post_title ? "title=\"{$post_title_attr}\" " : "") . "target=\"{$this->admin_options['tools']['link']['target']}\">{$post_thumbnail}</a>\n"
394 : "";
395
396 $post_excerpt = ( ! empty($post_excerpt) )
397 ? " <span class=\"wpp-excerpt\">{$post_excerpt}</span>\n"
398 : "";
399
400 $post_meta = ( ! empty($post_meta) )
401 ? " <span class=\"wpp-meta post-stats\">{$post_meta}</span>\n"
402 : '';
403
404 $post_rating = ( ! empty($post_rating) )
405 ? " <span class=\"wpp-rating\">{$post_rating}</span>\n"
406 : "";
407
408 $post =
409 "<li" . ( ( is_array($wpp_post_class) && ! empty($wpp_post_class) ) ? ' class="' . esc_attr(implode(" ", $wpp_post_class)) . '"' : '') . ">\n"
410 . $post_thumbnail
411 . "<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"
412 . $post_excerpt
413 . $post_meta
414 . $post_rating
415 . "</li>\n";
416 }
417
418 return apply_filters('wpp_post', $post, $post_object, $this->public_options);
419 }
420
421 /**
422 * Return the processed post/page title.
423 *
424 * @since 3.0.0
425 * @access private
426 * @param object $post_object
427 * @param integer $post_id
428 * @return string
429 */
430 private function get_title(\stdClass $post_object, $post_id)
431 {
432 if ( $post_object->id != $post_id ) {
433 $title = get_the_title($post_id);
434 } else {
435 $title = $post_object->title;
436 }
437
438 // Run the_title filter so core/plugin title hooks can
439 // be applied to the post title
440 $title = apply_filters('the_title', $title, $post_object->id);
441
442 return apply_filters('wpp_the_title', $title, $post_object->id, $post_id);
443 }
444
445 /**
446 * Return the permalink.
447 *
448 * @since 4.0.12
449 * @access private
450 * @param object $post_object
451 * @param integer $post_id
452 * @return string
453 */
454 private function get_permalink(\stdClass $post_object, $post_id) {
455 if ( $post_object->id != $post_id ) {
456 return get_permalink($post_id);
457 }
458
459 return get_permalink($post_object->id);
460 }
461
462 /**
463 * Return the processed thumbnail.
464 *
465 * @since 3.0.0
466 * @access private
467 * @param int $post_id
468 * @return string
469 */
470 private function get_thumbnail($post_id)
471 {
472 $thumbnail = '';
473
474 if ( $this->public_options['thumbnail']['active'] ) {
475 $thumbnail = $this->thumbnail->get(
476 $post_id,
477 [
478 $this->public_options['thumbnail']['width'],
479 $this->public_options['thumbnail']['height']
480 ],
481 $this->admin_options['tools']['thumbnail']['source'],
482 $this->public_options['thumbnail']['crop'],
483 $this->public_options['thumbnail']['build']
484 );
485 }
486
487 return $thumbnail;
488 }
489
490 /**
491 * Return post excerpt.
492 *
493 * @since 3.0.0
494 * @access private
495 * @param object $post_object
496 * @param integer $post_id
497 * @return string
498 */
499 private function get_excerpt(\stdClass $post_object, $post_id)
500 {
501 $excerpt = '';
502
503 if ( $this->public_options['post-excerpt']['active'] ) {
504
505 if ( $post_object->id != $post_id ) {
506 $the_post = get_post($post_id);
507
508 $excerpt = ( empty($the_post->post_excerpt) )
509 ? $the_post->post_content
510 : $the_post->post_excerpt;
511 }
512 else {
513 $excerpt = ( empty($post_object->post_excerpt) )
514 ? $post_object->post_content
515 : $post_object->post_excerpt;
516 }
517
518 // remove caption tags
519 $excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $excerpt);
520
521 // remove Flash objects
522 $excerpt = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,\\n\\r\\t]+/smi", "", $excerpt);
523
524 // remove iframes
525 $excerpt = preg_replace("/<iframe.*?\/iframe>/i", "", $excerpt);
526
527 // remove WP shortcodes
528 $excerpt = strip_shortcodes($excerpt);
529
530 // remove style/script tags
531 $excerpt = preg_replace('@<(script|style)[^>]*?>.*?</\\1>@si', '', $excerpt);
532
533 // remove HTML tags if requested
534 if ( $this->public_options['post-excerpt']['keep_format'] ) {
535 $excerpt = strip_tags($excerpt, '<a><b><i><em><strong>');
536 } else {
537 $excerpt = strip_tags($excerpt);
538
539 // remove URLs, too
540 $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);
541 }
542
543 }
544
545 // Balance tags, if needed
546 if ( '' !== $excerpt ) {
547
548 $excerpt = Helper::truncate($excerpt, $this->public_options['post-excerpt']['length'], $this->public_options['post-excerpt']['words'], $this->more);
549
550 if ( $this->public_options['post-excerpt']['keep_format'] )
551 $excerpt = force_balance_tags($excerpt);
552 }
553
554 return $excerpt;
555 }
556
557 /**
558 * Return post rating.
559 *
560 * @since 3.0.0
561 * @access private
562 * @param object $post_object
563 * @return string
564 */
565 private function get_rating(\stdClass $post_object)
566 {
567 $rating = '';
568
569 if ( function_exists('the_ratings_results') && $this->public_options['rating'] ) {
570 $rating = the_ratings_results($post_object->id);
571 }
572
573 return $rating;
574 }
575
576 /**
577 * Get post date.
578 *
579 * @since 3.0.0
580 * @access private
581 * @param object $post_object
582 * @return string
583 */
584 private function get_date(\stdClass $post_object)
585 {
586 $date = '';
587
588 if ( $this->public_options['stats_tag']['date']['active'] ) {
589 $date = ( 'relative' == $this->public_options['stats_tag']['date']['format'] )
590 ? sprintf(__('%s ago', 'wordpress-popular-posts'), human_time_diff(strtotime($post_object->date), current_time('timestamp')))
591 : date_i18n($this->public_options['stats_tag']['date']['format'], strtotime($post_object->date));
592 }
593
594 return $date;
595 }
596
597 /**
598 * Get post taxonomies.
599 *
600 * @since 3.0.0
601 * @access private
602 * @param integer $post_id
603 * @return string
604 */
605 private function get_taxonomies($post_id)
606 {
607 $post_tax = '';
608
609 if (
610 (isset($this->public_options['stats_tag']['category']) && $this->public_options['stats_tag']['category'])
611 || $this->public_options['stats_tag']['taxonomy']['active']
612 ) {
613
614 $taxonomy = 'category';
615
616 if (
617 $this->public_options['stats_tag']['taxonomy']['active']
618 && ! empty($this->public_options['stats_tag']['taxonomy']['name'])
619 ) {
620 $taxonomy = $this->public_options['stats_tag']['taxonomy']['name'];
621 }
622
623 $terms = wp_get_post_terms($post_id, $taxonomy);
624
625 if ( ! is_wp_error($terms) ) {
626 // Usage: https://wordpress.stackexchange.com/a/46824
627 if ( has_filter('wpp_post_exclude_terms') ) {
628 $args = apply_filters('wpp_post_exclude_terms', []);
629 $terms = wp_list_filter($terms, $args, 'NOT');
630 }
631
632 $terms = apply_filters('wpp_post_terms', $terms);
633
634 if (
635 is_array($terms)
636 && ! empty($terms)
637 ) {
638 $taxonomy_separator = apply_filters('wpp_taxonomy_separator', ', ');
639
640 foreach ($terms as $term) {
641 $term_link = get_term_link($term);
642
643 if ( is_wp_error($term_link) )
644 continue;
645
646 $term_link = $this->translate->url($term_link, $this->translate->get_current_language());
647 $post_tax .= "<a href=\"{$term_link}\" class=\"wpp-taxonomy {$taxonomy} {$taxonomy}-{$term->term_id}\">{$term->name}</a>" . $taxonomy_separator;
648 }
649 }
650 }
651
652 if ( '' != $post_tax )
653 $post_tax = rtrim($post_tax, $taxonomy_separator);
654
655 }
656
657 return $post_tax;
658 }
659
660 /**
661 * Get post author.
662 *
663 * @since 3.0.0
664 * @access private
665 * @param object $post_object
666 * @param integer $post_id
667 * @return string
668 */
669 private function get_author(\stdClass $post_object, $post_id)
670 {
671 $author = ( $this->public_options['stats_tag']['author'] )
672 ? get_the_author_meta('display_name', $post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid)
673 : "";
674
675 return $author;
676 }
677
678 /**
679 * Return post views count.
680 *
681 * @since 3.0.0
682 * @access private
683 * @param object $post_object
684 * @return int|float
685 */
686 private function get_pageviews(\stdClass $post_object)
687 {
688 $pageviews = 0;
689
690 if (
691 (
692 $this->public_options['order_by'] == "views"
693 || $this->public_options['order_by'] == "avg"
694 || $this->public_options['stats_tag']['views']
695 )
696 && ( isset($post_object->pageviews) || isset($post_object->avg_views) )
697 ) {
698 $pageviews = ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" )
699 ? $post_object->pageviews
700 : $post_object->avg_views;
701 }
702
703 return $pageviews;
704 }
705
706 /**
707 * Return post comment count.
708 *
709 * @since 3.0.0
710 * @access private
711 * @param object $post_object
712 * @return int
713 */
714 private function get_comments(\stdClass $post_object)
715 {
716 $comments = ( ( $this->public_options['order_by'] == "comments" || $this->public_options['stats_tag']['comment_count'] ) && isset($post_object->comment_count) )
717 ? $post_object->comment_count
718 : 0;
719
720 return $comments;
721 }
722
723 /**
724 * Return post metadata.
725 *
726 * @since 3.0.0
727 * @access private
728 * @param object $post_object
729 * @param integer $post_id
730 * @return array
731 */
732 //private function get_metadata(\stdClass $post_object, $post_id)
733 private function get_metadata(\stdClass $post_object, $post_id, $date, $post_tax, $author, $pageviews, $comments)
734 {
735 $stats = [];
736
737 $prettify_numbers = apply_filters('wpp_pretiffy_numbers', true);
738
739 // comments
740 if ( $this->public_options['stats_tag']['comment_count'] ) {
741 $comments_text = sprintf(
742 _n('%s comment', '%s comments', $comments, 'wordpress-popular-posts'),
743 $prettify_numbers ? Helper::prettify_number($comments) : number_format_i18n($comments)
744 );
745
746 $stats['comments'] = '<span class="wpp-comments">' . $comments_text . '</span>';
747 }
748
749 // views
750 if ( $this->public_options['stats_tag']['views'] ) {
751 if ( $this->public_options['order_by'] == 'avg' ) {
752 $views_text = sprintf(
753 _n('%s view per day', '%s views per day', $pageviews, 'wordpress-popular-posts'),
754 $prettify_numbers ? Helper::prettify_number($pageviews, 2) : number_format_i18n($pageviews, (fmod($pageviews, 1) !== 0.0 ? 2 : 0))
755 );
756 }
757 else {
758 $views_text = sprintf(
759 _n('%s view', '%s views', $pageviews, 'wordpress-popular-posts'),
760 $prettify_numbers ? Helper::prettify_number($pageviews) : number_format_i18n($pageviews)
761 );
762 }
763
764 $stats['views'] = '<span class="wpp-views">' . $views_text . "</span>";
765 }
766
767 // author
768 if ( $this->public_options['stats_tag']['author'] ) {
769 $author_url = get_author_posts_url($post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid);
770 $display_name = '<a href="' . $this->translate->url($author_url, $this->translate->get_current_language()) . '">' . $author . '</a>';
771 $stats['author'] = '<span class="wpp-author">' . sprintf(__('by %s', 'wordpress-popular-posts'), $display_name) . '</span>';
772 }
773
774 // date
775 if ( $this->public_options['stats_tag']['date']['active'] ) {
776 $stats['date'] = '<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>';
777 }
778
779 // taxonomy
780 if ( ($this->public_options['stats_tag']['category'] || $this->public_options['stats_tag']['taxonomy']['active']) && $post_tax != '' ) {
781 $stats['taxonomy'] = '<span class="wpp-category">' . sprintf(__('under %s', 'wordpress-popular-posts'), $post_tax) . '</span>';
782 }
783
784 return $stats;
785 }
786
787 /**
788 * Parse content tags.
789 *
790 * @since 1.4.6
791 * @access private
792 * @param string HTML string with content tags
793 * @param array Post data
794 * @param bool Used to display post rating (if functionality is available)
795 * @return string
796 */
797 private function format_content($string, $data, $rating) {
798
799 if ( empty($string) || ( empty($data) || ! is_array($data) ) )
800 return false;
801
802 $params = [];
803 $pattern = '/\{(pid|excerpt|summary|meta|stats|title|title_attr|image|thumb|thumb_img|thumb_url|rating|score|url|text_title|author|author_copy|taxonomy|taxonomy_copy|category|category_copy|views|views_copy|comments|comments_copy|date|date_copy|total_items|item_position)\}/i';
804 preg_match_all($pattern, $string, $matches);
805
806 array_map('strtolower', $matches[0]);
807
808 if ( in_array("{pid}", $matches[0]) ) {
809 $string = str_replace("{pid}", $data['id'], $string);
810 }
811
812 if ( in_array("{title}", $matches[0]) ) {
813 $string = str_replace("{title}", $data['title'], $string);
814 }
815
816 if ( in_array("{title_attr}", $matches[0]) ) {
817 $string = str_replace("{title_attr}", $data['title_attr'], $string);
818 }
819
820 if ( in_array("{meta}", $matches[0]) || in_array("{stats}", $matches[0]) ) {
821 $string = str_replace(["{meta}", "{stats}"], $data['stats'], $string);
822 }
823
824 if ( in_array("{excerpt}", $matches[0]) || in_array("{summary}", $matches[0]) ) {
825 $string = str_replace(["{excerpt}", "{summary}"], $data['summary'], $string);
826 }
827
828 if ( in_array("{image}", $matches[0]) || in_array("{thumb}", $matches[0]) ) {
829 $string = str_replace(["{image}", "{thumb}"], $data['img'], $string);
830 }
831
832 if ( in_array("{thumb_img}", $matches[0]) ) {
833 $string = str_replace("{thumb_img}", $data['img_no_link'], $string);
834 }
835
836 if ( in_array("{thumb_url}", $matches[0]) && ! empty($data['img_no_link']) ) {
837 $dom = new \DOMDocument;
838
839 if ( $dom->loadHTML($data['img_no_link']) ) {
840 $img_tag = $dom->getElementsByTagName('img');
841
842 if ( $img_tag->length ) {
843 foreach( $img_tag as $node ) {
844 if ( $node->hasAttribute('src') || $node->hasAttribute('data-img-src') ) {
845 $src = $node->hasAttribute('src') ? $node->getAttribute('src') : $node->getAttribute('data-img-src');
846 $string = str_replace("{thumb_url}", $src, $string);
847 }
848 }
849 }
850 }
851 }
852
853 // WP-PostRatings check
854 if ( $rating ) {
855 if ( function_exists('the_ratings_results') && in_array("{rating}", $matches[0]) ) {
856 $string = str_replace("{rating}", the_ratings_results($data['id']), $string);
857 }
858
859 if ( function_exists('expand_ratings_template') && in_array("{score}", $matches[0]) ) {
860 $string = str_replace("{score}", expand_ratings_template('%RATINGS_SCORE%', $data['id']), $string);
861 // removing the redundant plus sign
862 $string = str_replace('+', '', $string);
863 }
864 }
865
866 if ( in_array("{url}", $matches[0]) ) {
867 $string = str_replace("{url}", $data['url'], $string);
868 }
869
870 if ( in_array("{text_title}", $matches[0]) ) {
871 $string = str_replace("{text_title}", $data['text_title'], $string);
872 }
873
874 if ( in_array("{author}", $matches[0]) ) {
875 $string = str_replace("{author}", $data['author'], $string);
876 }
877
878 if ( in_array("{author_copy}", $matches[0]) ) {
879 $string = str_replace("{author_copy}", $data['author_copy'], $string);
880 }
881
882 if ( in_array("{taxonomy}", $matches[0]) || in_array("{category}", $matches[0]) ) {
883 $string = str_replace(["{taxonomy}", "{category}"], $data['taxonomy'], $string);
884 }
885
886 if ( in_array("{taxonomy_copy}", $matches[0]) || in_array("{category_copy}", $matches[0]) ) {
887 $string = str_replace(["{taxonomy_copy}", "{category_copy}"], $data['taxonomy_copy'], $string);
888 }
889
890 if ( in_array("{views}", $matches[0]) ) {
891 $string = str_replace("{views}", $data['views'], $string);
892 }
893
894 if ( in_array("{views_copy}", $matches[0]) ) {
895 $string = str_replace("{views_copy}", $data['views_copy'], $string);
896 }
897
898 if ( in_array("{comments}", $matches[0]) ) {
899 $string = str_replace("{comments}", $data['comments'], $string);
900 }
901
902 if ( in_array("{comments_copy}", $matches[0]) ) {
903 $string = str_replace("{comments_copy}", $data['comments_copy'], $string);
904 }
905
906 if ( in_array("{date}", $matches[0]) ) {
907 $string = str_replace("{date}", $data['date'], $string);
908 }
909
910 if ( in_array("{date_copy}", $matches[0]) ) {
911 $string = str_replace("{date_copy}", $data['date_copy'], $string);
912 }
913
914 if ( in_array("{total_items}", $matches[0]) ) {
915 $string = str_replace("{total_items}", $data['total_items'], $string);
916 }
917
918 if ( in_array("{item_position}", $matches[0]) ) {
919 $string = str_replace("{item_position}", $data['item_position'], $string);
920 }
921
922 return apply_filters("wpp_parse_custom_content_tags", $string, $data['id']);
923 }
924
925 /**
926 * Checks whether we're currently seeing a single post/page/CPT.
927 *
928 * @since 5.0.0
929 * @return int
930 */
931 public function is_single()
932 {
933 return apply_filters('wpp_is_single', Helper::is_single());
934 }
935 }
936