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