| | 340 | |
| | 341 | /** |
| | 342 | * Retrieve post meta field for a post. |
| | 343 | * |
| | 344 | * @since 4.6.0 |
| | 345 | * |
| | 346 | * @param string $key Optional. The meta key to retrieve. By default, returns |
| | 347 | * data for all keys. Default empty. |
| | 348 | * @param bool $single Optional. Whether to return a single value. Default false. |
| | 349 | * |
| | 350 | * @return mixed Will be an array if $single is false. Will be value of meta data |
| | 351 | * field if $single is true. |
| | 352 | */ |
| | 353 | public function get_meta( $key = '', $single = false ) { |
| | 354 | return get_post_meta( $this->ID, $key, $single ); |
| | 355 | } |
| | 356 | |
| | 357 | /** |
| | 358 | * Retrieve post thumbnail ID. |
| | 359 | * |
| | 360 | * @since 4.6.0 |
| | 361 | * |
| | 362 | * @return string|int Post thumbnail ID or empty string. |
| | 363 | */ |
| | 364 | public function get_post_thumbnail_id() { |
| | 365 | return $this->get_meta( '_thumbnail_id', true ); |
| | 366 | } |
| | 367 | |
| | 368 | /** |
| | 369 | * Check if post has an image attached. |
| | 370 | * |
| | 371 | * @since 4.6.0 |
| | 372 | * |
| | 373 | * @return bool Whether the post has an image attached. |
| | 374 | */ |
| | 375 | public function has_post_thumbnail(){ |
| | 376 | return (bool)$this->get_post_thumbnail_id(); |
| | 377 | } |
| | 378 | |
| | 379 | /** |
| | 380 | * Retrieve the post thumbnail. |
| | 381 | * |
| | 382 | * When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' image size |
| | 383 | * is registered, which differs from the 'thumbnail' image size managed via the |
| | 384 | * Settings > Media screen. |
| | 385 | * |
| | 386 | * When using the_post_thumbnail() or related functions, the 'post-thumbnail' image |
| | 387 | * size is used by default, though a different size can be specified instead as needed. |
| | 388 | * |
| | 389 | * @since 4.6.0 |
| | 390 | * |
| | 391 | * @param string|array $size Optional. Image size to use. Accepts any valid image size, or |
| | 392 | * an array of width and height values in pixels (in that order). |
| | 393 | * Default 'post-thumbnail'. |
| | 394 | * @param string|array $attr Optional. Query string or array of attributes. Default empty. |
| | 395 | * @return string The post thumbnail image tag. |
| | 396 | */ |
| | 397 | function get_the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { |
| | 398 | |
| | 399 | $post_thumbnail_id = $this->get_post_thumbnail_id(); |
| | 400 | |
| | 401 | /** |
| | 402 | * Filter the post thumbnail size. |
| | 403 | * |
| | 404 | * @since 2.9.0 |
| | 405 | * |
| | 406 | * @param string|array $size The post thumbnail size. Image size or array of width and height |
| | 407 | * values (in that order). Default 'post-thumbnail'. |
| | 408 | */ |
| | 409 | $size = apply_filters( 'post_thumbnail_size', $size ); |
| | 410 | |
| | 411 | if ( $post_thumbnail_id ) { |
| | 412 | |
| | 413 | /** |
| | 414 | * Fires before fetching the post thumbnail HTML. |
| | 415 | * |
| | 416 | * Provides "just in time" filtering of all filters in wp_get_attachment_image(). |
| | 417 | * |
| | 418 | * @since 2.9.0 |
| | 419 | * |
| | 420 | * @param int $post_id The post ID. |
| | 421 | * @param string $post_thumbnail_id The post thumbnail ID. |
| | 422 | * @param string|array $size The post thumbnail size. Image size or array of width |
| | 423 | * and height values (in that order). Default 'post-thumbnail'. |
| | 424 | */ |
| | 425 | do_action( 'begin_fetch_post_thumbnail_html', $this->ID, $post_thumbnail_id, $size ); |
| | 426 | if ( in_the_loop() ) |
| | 427 | update_post_thumbnail_cache(); |
| | 428 | $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr ); |
| | 429 | |
| | 430 | /** |
| | 431 | * Fires after fetching the post thumbnail HTML. |
| | 432 | * |
| | 433 | * @since 2.9.0 |
| | 434 | * |
| | 435 | * @param int $post_id The post ID. |
| | 436 | * @param string $post_thumbnail_id The post thumbnail ID. |
| | 437 | * @param string|array $size The post thumbnail size. Image size or array of width |
| | 438 | * and height values (in that order). Default 'post-thumbnail'. |
| | 439 | */ |
| | 440 | do_action( 'end_fetch_post_thumbnail_html', $this->ID, $post_thumbnail_id, $size ); |
| | 441 | |
| | 442 | } else { |
| | 443 | $html = ''; |
| | 444 | } |
| | 445 | /** |
| | 446 | * Filter the post thumbnail HTML. |
| | 447 | * |
| | 448 | * @since 2.9.0 |
| | 449 | * |
| | 450 | * @param string $html The post thumbnail HTML. |
| | 451 | * @param int $post_id The post ID. |
| | 452 | * @param string $post_thumbnail_id The post thumbnail ID. |
| | 453 | * @param string|array $size The post thumbnail size. Image size or array of width and height |
| | 454 | * values (in that order). Default 'post-thumbnail'. |
| | 455 | * @param string $attr Query string of attributes. |
| | 456 | */ |
| | 457 | return apply_filters( 'post_thumbnail_html', $html, $this->ID, $post_thumbnail_id, $size, $attr ); |
| | 458 | } |
| | 459 | |
| | 460 | /** |
| | 461 | * Return the post thumbnail URL. |
| | 462 | * |
| | 463 | * @since 4.6.0 |
| | 464 | * |
| | 465 | * @param string|array $size Optional. Registered image size to retrieve the source for or a flat |
| | 466 | * array of height and width dimensions. Default 'post-thumbnail'. |
| | 467 | * @return string|false Post thumbnail URL or false if no URL is available. |
| | 468 | */ |
| | 469 | function get_the_post_thumbnail_url($size = 'post-thumbnail' ) { |
| | 470 | $post_thumbnail_id = $this->get_post_thumbnail_id( ); |
| | 471 | if ( ! $post_thumbnail_id ) { |
| | 472 | return false; |
| | 473 | } |
| | 474 | return wp_get_attachment_image_url( $post_thumbnail_id, $size ); |
| | 475 | } |
| | 476 | |
| | 477 | /** |
| | 478 | * Retrieve the Post Global Unique Identifier (guid). |
| | 479 | * |
| | 480 | * The guid will appear to be a link, but should not be used as an link to the |
| | 481 | * post. The reason you should not use it as a link, is because of moving the |
| | 482 | * blog across domains. |
| | 483 | * |
| | 484 | * @since 4.6.0 |
| | 485 | * |
| | 486 | * @return string |
| | 487 | */ |
| | 488 | function get_the_guid( ) { |
| | 489 | /** |
| | 490 | * Filter the Global Unique Identifier (guid) of the post. |
| | 491 | * |
| | 492 | * @since 1.5.0 |
| | 493 | * |
| | 494 | * @param string $guid Global Unique Identifier (guid) of the post. |
| | 495 | * @param int $id The post ID. |
| | 496 | */ |
| | 497 | return apply_filters( 'get_the_guid', $this->guid, $this->ID ); |
| | 498 | } |
| | 499 | |
| | 500 | /** |
| | 501 | * Retrieve the post content. |
| | 502 | * |
| | 503 | * @since 4.6.0 |
| | 504 | * |
| | 505 | * @global int $page |
| | 506 | * @global int $more |
| | 507 | * @global bool $preview |
| | 508 | * @global array $pages |
| | 509 | * @global int $multipage |
| | 510 | * |
| | 511 | * @param string $more_link_text Optional. Content for when there is more text. |
| | 512 | * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. |
| | 513 | * @return string |
| | 514 | */ |
| | 515 | function get_the_content( $more_link_text = null, $strip_teaser = false ) { |
| | 516 | global $page, $more, $preview, $pages, $multipage; |
| | 517 | |
| | 518 | if ( null === $more_link_text ) |
| | 519 | $more_link_text = __( '(more…)' ); |
| | 520 | |
| | 521 | $output = ''; |
| | 522 | $has_teaser = false; |
| | 523 | |
| | 524 | // If post password required and it doesn't match the cookie. |
| | 525 | if ( post_password_required( $this ) ) |
| | 526 | return get_the_password_form( $this ); |
| | 527 | |
| | 528 | if ( $page > count( $pages ) ) // if the requested page doesn't exist |
| | 529 | $page = count( $pages ); // give them the highest numbered page that DOES exist |
| | 530 | |
| | 531 | $content = $pages[$page - 1]; |
| | 532 | if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { |
| | 533 | $content = explode( $matches[0], $content, 2 ); |
| | 534 | if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) |
| | 535 | $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); |
| | 536 | |
| | 537 | $has_teaser = true; |
| | 538 | } else { |
| | 539 | $content = array( $content ); |
| | 540 | } |
| | 541 | |
| | 542 | if ( false !== strpos( $this->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) ) |
| | 543 | $strip_teaser = true; |
| | 544 | |
| | 545 | $teaser = $content[0]; |
| | 546 | |
| | 547 | if ( $more && $strip_teaser && $has_teaser ) |
| | 548 | $teaser = ''; |
| | 549 | |
| | 550 | $output .= $teaser; |
| | 551 | |
| | 552 | if ( count( $content ) > 1 ) { |
| | 553 | if ( $more ) { |
| | 554 | $output .= '<span id="more-' . $this->ID . '"></span>' . $content[1]; |
| | 555 | } else { |
| | 556 | if ( ! empty( $more_link_text ) ) |
| | 557 | |
| | 558 | /** |
| | 559 | * Filter the Read More link text. |
| | 560 | * |
| | 561 | * @since 2.8.0 |
| | 562 | * |
| | 563 | * @param string $more_link_element Read More link element. |
| | 564 | * @param string $more_link_text Read More text. |
| | 565 | */ |
| | 566 | $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$this->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); |
| | 567 | $output = force_balance_tags( $output ); |
| | 568 | } |
| | 569 | } |
| | 570 | |
| | 571 | if ( $preview ) // Preview fix for JavaScript bug with foreign languages. |
| | 572 | $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output ); |
| | 573 | |
| | 574 | return $output; |
| | 575 | } |
| | 576 | |
| | 577 | /** |
| | 578 | * Retrieves the post excerpt. |
| | 579 | * |
| | 580 | * @since 4.6.0 |
| | 581 | * |
| | 582 | * @return string Post excerpt. |
| | 583 | */ |
| | 584 | function get_the_excerpt( ) { |
| | 585 | |
| | 586 | if ( post_password_required( $this ) ) { |
| | 587 | return __( 'There is no excerpt because this is a protected post.' ); |
| | 588 | } |
| | 589 | |
| | 590 | /** |
| | 591 | * Filter the retrieved post excerpt. |
| | 592 | * |
| | 593 | * @since 1.2.0 |
| | 594 | * @since 4.5.0 Introduced the `$post` parameter. |
| | 595 | * |
| | 596 | * @param string $post_excerpt The post excerpt. |
| | 597 | * @param WP_Post $post Post object. |
| | 598 | */ |
| | 599 | return apply_filters( 'get_the_excerpt', $this->post_excerpt, $this ); |
| | 600 | } |
| | 601 | |
| | 602 | /** |
| | 603 | * Whether post has excerpt. |
| | 604 | * |
| | 605 | * @since 4.6.0 |
| | 606 | * |
| | 607 | * @return bool |
| | 608 | */ |
| | 609 | function has_excerpt() { |
| | 610 | return ( !empty( $this->post_excerpt ) ); |
| | 611 | } |