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