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