PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.4.2
WP Popular Posts v5.4.2
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
943 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 = $this->public_options['shorten_title']['words'] ? ' ' . $this->more : $this->more;
287 $more = apply_filters('wpp_title_more', $more);
288 $post_title = Helper::truncate($post_title, $length, $this->public_options['shorten_title']['words'], $more);
289 }
290
291 // Thumbnail
292 $post_thumbnail = $this->get_thumbnail($post_id);
293
294 // Post excerpt
295 $post_excerpt = $this->get_excerpt($post_object, $post_id);
296
297 // Post rating
298 $post_rating = $this->get_rating($post_object);
299
300 /**
301 * Post meta
302 */
303
304 // Post date
305 $post_date = $this->get_date($post_object);
306
307 // Post taxonomies
308 $post_taxonomies = $this->get_taxonomies($post_id);
309
310 // Post author
311 $post_author = $this->get_author($post_object, $post_id);
312
313 // Post views count
314 $post_views = $this->get_pageviews($post_object);
315
316 // Post comments count
317 $post_comments = $this->get_comments($post_object);
318
319 // Post meta
320 $meta_arr = $this->get_metadata(
321 $post_object,
322 $post_id,
323 $post_date,
324 $post_taxonomies,
325 $post_author,
326 $post_views,
327 $post_comments
328 );
329
330 if (
331 is_array($meta_arr)
332 && ! empty($meta_arr)
333 && "views" == $this->public_options['order_by']
334 ) {
335 $keys = ['views', 'comments', 'author', 'date', 'taxonomy'];
336 $new_meta_arr = [];
337
338 foreach($keys as $key) {
339 if ( isset($meta_arr[$key]))
340 $new_meta_arr[$key] = $meta_arr[$key];
341 }
342
343 if ( ! empty($new_meta_arr) )
344 $meta_arr = $new_meta_arr;
345 }
346
347 $post_meta_separator = apply_filters('wpp_post_meta_separator', ' | ');
348 $post_meta = join($post_meta_separator, $meta_arr);
349
350 $prettify_numbers = apply_filters('wpp_pretiffy_numbers', true);
351
352 // Build custom HTML output
353 if ( $this->public_options['markup']['custom_html'] ) {
354 $data = [
355 'id' => $post_id,
356 'is_current_post' => $is_current_post,
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 $excerpt = trim($excerpt);
543
544 }
545
546 // Balance tags, if needed
547 if ( '' !== $excerpt ) {
548
549 $more = $this->public_options['post-excerpt']['words'] ? ' ' . $this->more : $this->more;
550 $more = apply_filters('wpp_excerpt_more', $more);
551 $excerpt = Helper::truncate($excerpt, $this->public_options['post-excerpt']['length'], $this->public_options['post-excerpt']['words'], $more);
552
553 if ( $this->public_options['post-excerpt']['keep_format'] )
554 $excerpt = force_balance_tags($excerpt);
555 }
556
557 return $excerpt;
558 }
559
560 /**
561 * Return post rating.
562 *
563 * @since 3.0.0
564 * @access private
565 * @param object $post_object
566 * @return string
567 */
568 private function get_rating(\stdClass $post_object)
569 {
570 $rating = '';
571
572 if ( function_exists('the_ratings_results') && $this->public_options['rating'] ) {
573 $rating = the_ratings_results($post_object->id);
574 }
575
576 return $rating;
577 }
578
579 /**
580 * Get post date.
581 *
582 * @since 3.0.0
583 * @access private
584 * @param object $post_object
585 * @return string
586 */
587 private function get_date(\stdClass $post_object)
588 {
589 $date = '';
590
591 if ( $this->public_options['stats_tag']['date']['active'] ) {
592 $date = ( 'relative' == $this->public_options['stats_tag']['date']['format'] )
593 ? sprintf(__('%s ago', 'wordpress-popular-posts'), human_time_diff(strtotime($post_object->date), current_time('timestamp')))
594 : date_i18n($this->public_options['stats_tag']['date']['format'], strtotime($post_object->date));
595 }
596
597 return $date;
598 }
599
600 /**
601 * Get post taxonomies.
602 *
603 * @since 3.0.0
604 * @access private
605 * @param integer $post_id
606 * @return string
607 */
608 private function get_taxonomies($post_id)
609 {
610 $post_tax = '';
611
612 if (
613 (isset($this->public_options['stats_tag']['category']) && $this->public_options['stats_tag']['category'])
614 || $this->public_options['stats_tag']['taxonomy']['active']
615 ) {
616
617 $taxonomy = 'category';
618
619 if (
620 $this->public_options['stats_tag']['taxonomy']['active']
621 && ! empty($this->public_options['stats_tag']['taxonomy']['name'])
622 ) {
623 $taxonomy = $this->public_options['stats_tag']['taxonomy']['name'];
624 }
625
626 $terms = wp_get_post_terms($post_id, $taxonomy);
627
628 if ( ! is_wp_error($terms) ) {
629 // Usage: https://wordpress.stackexchange.com/a/46824
630 if ( has_filter('wpp_post_exclude_terms') ) {
631 $args = apply_filters('wpp_post_exclude_terms', []);
632 $terms = wp_list_filter($terms, $args, 'NOT');
633 }
634
635 $terms = apply_filters('wpp_post_terms', $terms);
636
637 if (
638 is_array($terms)
639 && ! empty($terms)
640 ) {
641 $taxonomy_separator = apply_filters('wpp_taxonomy_separator', ', ');
642
643 foreach ($terms as $term) {
644 $term_link = get_term_link($term);
645
646 if ( is_wp_error($term_link) )
647 continue;
648
649 $term_link = $this->translate->url($term_link, $this->translate->get_current_language());
650 $post_tax .= "<a href=\"{$term_link}\" class=\"wpp-taxonomy {$taxonomy} {$taxonomy}-{$term->term_id}\">{$term->name}</a>" . $taxonomy_separator;
651 }
652 }
653 }
654
655 if ( '' != $post_tax )
656 $post_tax = rtrim($post_tax, $taxonomy_separator);
657
658 }
659
660 return $post_tax;
661 }
662
663 /**
664 * Get post author.
665 *
666 * @since 3.0.0
667 * @access private
668 * @param object $post_object
669 * @param integer $post_id
670 * @return string
671 */
672 private function get_author(\stdClass $post_object, $post_id)
673 {
674 $author = ( $this->public_options['stats_tag']['author'] )
675 ? get_the_author_meta('display_name', $post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid)
676 : "";
677
678 return $author;
679 }
680
681 /**
682 * Return post views count.
683 *
684 * @since 3.0.0
685 * @access private
686 * @param object $post_object
687 * @return int|float
688 */
689 private function get_pageviews(\stdClass $post_object)
690 {
691 $pageviews = 0;
692
693 if (
694 (
695 $this->public_options['order_by'] == "views"
696 || $this->public_options['order_by'] == "avg"
697 || $this->public_options['stats_tag']['views']
698 )
699 && ( isset($post_object->pageviews) || isset($post_object->avg_views) )
700 ) {
701 $pageviews = ( $this->public_options['order_by'] == "views" || $this->public_options['order_by'] == "comments" )
702 ? $post_object->pageviews
703 : $post_object->avg_views;
704 }
705
706 return $pageviews;
707 }
708
709 /**
710 * Return post comment count.
711 *
712 * @since 3.0.0
713 * @access private
714 * @param object $post_object
715 * @return int
716 */
717 private function get_comments(\stdClass $post_object)
718 {
719 $comments = ( ( $this->public_options['order_by'] == "comments" || $this->public_options['stats_tag']['comment_count'] ) && isset($post_object->comment_count) )
720 ? $post_object->comment_count
721 : 0;
722
723 return $comments;
724 }
725
726 /**
727 * Return post metadata.
728 *
729 * @since 3.0.0
730 * @access private
731 * @param object $post_object
732 * @param integer $post_id
733 * @return array
734 */
735 //private function get_metadata(\stdClass $post_object, $post_id)
736 private function get_metadata(\stdClass $post_object, $post_id, $date, $post_tax, $author, $pageviews, $comments)
737 {
738 $stats = [];
739
740 $prettify_numbers = apply_filters('wpp_pretiffy_numbers', true);
741
742 // comments
743 if ( $this->public_options['stats_tag']['comment_count'] ) {
744 $comments_text = sprintf(
745 _n('%s comment', '%s comments', $comments, 'wordpress-popular-posts'),
746 $prettify_numbers ? Helper::prettify_number($comments) : number_format_i18n($comments)
747 );
748
749 $stats['comments'] = '<span class="wpp-comments">' . $comments_text . '</span>';
750 }
751
752 // views
753 if ( $this->public_options['stats_tag']['views'] ) {
754 if ( $this->public_options['order_by'] == 'avg' ) {
755 $views_text = sprintf(
756 _n('%s view per day', '%s views per day', $pageviews, 'wordpress-popular-posts'),
757 $prettify_numbers ? Helper::prettify_number($pageviews, 2) : number_format_i18n($pageviews, (fmod($pageviews, 1) !== 0.0 ? 2 : 0))
758 );
759 }
760 else {
761 $views_text = sprintf(
762 _n('%s view', '%s views', $pageviews, 'wordpress-popular-posts'),
763 $prettify_numbers ? Helper::prettify_number($pageviews) : number_format_i18n($pageviews)
764 );
765 }
766
767 $stats['views'] = '<span class="wpp-views">' . $views_text . "</span>";
768 }
769
770 // author
771 if ( $this->public_options['stats_tag']['author'] ) {
772 $author_url = get_author_posts_url($post_object->uid != $post_id ? get_post_field('post_author', $post_id) : $post_object->uid);
773 $display_name = '<a href="' . $this->translate->url($author_url, $this->translate->get_current_language()) . '">' . $author . '</a>';
774 $stats['author'] = '<span class="wpp-author">' . sprintf(__('by %s', 'wordpress-popular-posts'), $display_name) . '</span>';
775 }
776
777 // date
778 if ( $this->public_options['stats_tag']['date']['active'] ) {
779 $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>';
780 }
781
782 // taxonomy
783 if ( ($this->public_options['stats_tag']['category'] || $this->public_options['stats_tag']['taxonomy']['active']) && $post_tax != '' ) {
784 $stats['taxonomy'] = '<span class="wpp-category">' . sprintf(__('under %s', 'wordpress-popular-posts'), $post_tax) . '</span>';
785 }
786
787 return $stats;
788 }
789
790 /**
791 * Parse content tags.
792 *
793 * @since 1.4.6
794 * @access private
795 * @param string HTML string with content tags
796 * @param array Post data
797 * @param bool Used to display post rating (if functionality is available)
798 * @return string
799 */
800 private function format_content($string, $data, $rating) {
801
802 if ( empty($string) || ( empty($data) || ! is_array($data) ) )
803 return false;
804
805 $params = [];
806 $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';
807 preg_match_all($pattern, $string, $matches);
808
809 array_map('strtolower', $matches[0]);
810
811 if ( in_array("{pid}", $matches[0]) ) {
812 $string = str_replace("{pid}", $data['id'], $string);
813 }
814
815 if ( in_array("{current_class}", $matches[0]) ) {
816 $string = str_replace("{current_class}", ( $data['is_current_post'] ? 'current' : '' ), $string);
817 }
818
819 if ( in_array("{title}", $matches[0]) ) {
820 $string = str_replace("{title}", $data['title'], $string);
821 }
822
823 if ( in_array("{title_attr}", $matches[0]) ) {
824 $string = str_replace("{title_attr}", $data['title_attr'], $string);
825 }
826
827 if ( in_array("{meta}", $matches[0]) || in_array("{stats}", $matches[0]) ) {
828 $string = str_replace(["{meta}", "{stats}"], $data['stats'], $string);
829 }
830
831 if ( in_array("{excerpt}", $matches[0]) || in_array("{summary}", $matches[0]) ) {
832 $string = str_replace(["{excerpt}", "{summary}"], $data['summary'], $string);
833 }
834
835 if ( in_array("{image}", $matches[0]) || in_array("{thumb}", $matches[0]) ) {
836 $string = str_replace(["{image}", "{thumb}"], $data['img'], $string);
837 }
838
839 if ( in_array("{thumb_img}", $matches[0]) ) {
840 $string = str_replace("{thumb_img}", $data['img_no_link'], $string);
841 }
842
843 if ( in_array("{thumb_url}", $matches[0]) && ! empty($data['img_no_link']) ) {
844 $dom = new \DOMDocument;
845
846 if ( $dom->loadHTML($data['img_no_link']) ) {
847 $img_tag = $dom->getElementsByTagName('img');
848
849 if ( $img_tag->length ) {
850 foreach( $img_tag as $node ) {
851 if ( $node->hasAttribute('src') || $node->hasAttribute('data-img-src') ) {
852 $src = $node->hasAttribute('src') ? $node->getAttribute('src') : $node->getAttribute('data-img-src');
853 $string = str_replace("{thumb_url}", $src, $string);
854 }
855 }
856 }
857 }
858 }
859
860 // WP-PostRatings check
861 if ( $rating ) {
862 if ( function_exists('the_ratings_results') && in_array("{rating}", $matches[0]) ) {
863 $string = str_replace("{rating}", the_ratings_results($data['id']), $string);
864 }
865
866 if ( function_exists('expand_ratings_template') && in_array("{score}", $matches[0]) ) {
867 $string = str_replace("{score}", expand_ratings_template('%RATINGS_SCORE%', $data['id']), $string);
868 // removing the redundant plus sign
869 $string = str_replace('+', '', $string);
870 }
871 }
872
873 if ( in_array("{url}", $matches[0]) ) {
874 $string = str_replace("{url}", $data['url'], $string);
875 }
876
877 if ( in_array("{text_title}", $matches[0]) ) {
878 $string = str_replace("{text_title}", $data['text_title'], $string);
879 }
880
881 if ( in_array("{author}", $matches[0]) ) {
882 $string = str_replace("{author}", $data['author'], $string);
883 }
884
885 if ( in_array("{author_copy}", $matches[0]) ) {
886 $string = str_replace("{author_copy}", $data['author_copy'], $string);
887 }
888
889 if ( in_array("{taxonomy}", $matches[0]) || in_array("{category}", $matches[0]) ) {
890 $string = str_replace(["{taxonomy}", "{category}"], $data['taxonomy'], $string);
891 }
892
893 if ( in_array("{taxonomy_copy}", $matches[0]) || in_array("{category_copy}", $matches[0]) ) {
894 $string = str_replace(["{taxonomy_copy}", "{category_copy}"], $data['taxonomy_copy'], $string);
895 }
896
897 if ( in_array("{views}", $matches[0]) ) {
898 $string = str_replace("{views}", $data['views'], $string);
899 }
900
901 if ( in_array("{views_copy}", $matches[0]) ) {
902 $string = str_replace("{views_copy}", $data['views_copy'], $string);
903 }
904
905 if ( in_array("{comments}", $matches[0]) ) {
906 $string = str_replace("{comments}", $data['comments'], $string);
907 }
908
909 if ( in_array("{comments_copy}", $matches[0]) ) {
910 $string = str_replace("{comments_copy}", $data['comments_copy'], $string);
911 }
912
913 if ( in_array("{date}", $matches[0]) ) {
914 $string = str_replace("{date}", $data['date'], $string);
915 }
916
917 if ( in_array("{date_copy}", $matches[0]) ) {
918 $string = str_replace("{date_copy}", $data['date_copy'], $string);
919 }
920
921 if ( in_array("{total_items}", $matches[0]) ) {
922 $string = str_replace("{total_items}", $data['total_items'], $string);
923 }
924
925 if ( in_array("{item_position}", $matches[0]) ) {
926 $string = str_replace("{item_position}", $data['item_position'], $string);
927 }
928
929 return apply_filters("wpp_parse_custom_content_tags", $string, $data['id']);
930 }
931
932 /**
933 * Checks whether we're currently seeing a single post/page/CPT.
934 *
935 * @since 5.0.0
936 * @return int
937 */
938 public function is_single()
939 {
940 return apply_filters('wpp_is_single', Helper::is_single());
941 }
942 }
943