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