| @@ 607-666 (lines=60) @@ | ||
| 604 | * @type string $last_modified The most recent timestamp to appear on the sitemap. |
|
| 605 | * } |
|
| 606 | */ |
|
| 607 | public function build_one_image_sitemap( $number, $from_id ) { |
|
| 608 | $last_post_id = $from_id; |
|
| 609 | $any_posts_left = true; |
|
| 610 | ||
| 611 | if ( $this->logger ) { |
|
| 612 | $debug_name = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, $number ); |
|
| 613 | $this->logger->report( "-- Building $debug_name" ); |
|
| 614 | } |
|
| 615 | ||
| 616 | $buffer = new Jetpack_Sitemap_Buffer_Image( |
|
| 617 | JP_SITEMAP_MAX_ITEMS, |
|
| 618 | JP_SITEMAP_MAX_BYTES |
|
| 619 | ); |
|
| 620 | ||
| 621 | // Add as many items to the buffer as possible. |
|
| 622 | while ( false === $buffer->is_full() ) { |
|
| 623 | $posts = $this->librarian->query_images_after_id( |
|
| 624 | $last_post_id, JP_SITEMAP_BATCH_SIZE |
|
| 625 | ); |
|
| 626 | ||
| 627 | if ( null == $posts ) { // WPCS: loose comparison ok. |
|
| 628 | $any_posts_left = false; |
|
| 629 | break; |
|
| 630 | } |
|
| 631 | ||
| 632 | foreach ( $posts as $post ) { |
|
| 633 | $current_item = $this->image_post_to_sitemap_item( $post ); |
|
| 634 | ||
| 635 | if ( true === $buffer->append( $current_item['xml'] ) ) { |
|
| 636 | $last_post_id = $post->ID; |
|
| 637 | $buffer->view_time( $current_item['last_modified'] ); |
|
| 638 | } else { |
|
| 639 | break; |
|
| 640 | } |
|
| 641 | } |
|
| 642 | } |
|
| 643 | ||
| 644 | // If no items were added, return false. |
|
| 645 | if ( true === $buffer->is_empty() ) { |
|
| 646 | return false; |
|
| 647 | } |
|
| 648 | ||
| 649 | // Store the buffer as the content of a jp_sitemap post. |
|
| 650 | $this->librarian->store_sitemap_data( |
|
| 651 | $number, |
|
| 652 | JP_IMAGE_SITEMAP_TYPE, |
|
| 653 | $buffer->contents(), |
|
| 654 | $buffer->last_modified() |
|
| 655 | ); |
|
| 656 | ||
| 657 | /* |
|
| 658 | * Now report back with the ID of the last post to be |
|
| 659 | * successfully added and whether there are any posts left. |
|
| 660 | */ |
|
| 661 | return array( |
|
| 662 | 'last_id' => $last_post_id, |
|
| 663 | 'any_left' => $any_posts_left, |
|
| 664 | 'last_modified' => $buffer->last_modified(), |
|
| 665 | ); |
|
| 666 | } |
|
| 667 | ||
| 668 | /** |
|
| 669 | * Build and store a single video sitemap. Returns false if no sitemap is built. |
|
| @@ 685-745 (lines=61) @@ | ||
| 682 | * @type string $last_modified The most recent timestamp to appear on the sitemap. |
|
| 683 | * } |
|
| 684 | */ |
|
| 685 | public function build_one_video_sitemap( $number, $from_id ) { |
|
| 686 | $last_post_id = $from_id; |
|
| 687 | $any_posts_left = true; |
|
| 688 | ||
| 689 | if ( $this->logger ) { |
|
| 690 | $debug_name = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, $number ); |
|
| 691 | $this->logger->report( "-- Building $debug_name" ); |
|
| 692 | } |
|
| 693 | ||
| 694 | $buffer = new Jetpack_Sitemap_Buffer_Video( |
|
| 695 | JP_SITEMAP_MAX_ITEMS, |
|
| 696 | JP_SITEMAP_MAX_BYTES |
|
| 697 | ); |
|
| 698 | ||
| 699 | // Add as many items to the buffer as possible. |
|
| 700 | while ( false === $buffer->is_full() ) { |
|
| 701 | $posts = $this->librarian->query_videos_after_id( |
|
| 702 | $last_post_id, JP_SITEMAP_BATCH_SIZE |
|
| 703 | ); |
|
| 704 | ||
| 705 | if ( null == $posts ) { // WPCS: loose comparison ok. |
|
| 706 | $any_posts_left = false; |
|
| 707 | break; |
|
| 708 | } |
|
| 709 | ||
| 710 | foreach ( $posts as $post ) { |
|
| 711 | $current_item = $this->video_post_to_sitemap_item( $post ); |
|
| 712 | ||
| 713 | if ( true === $buffer->append( $current_item['xml'] ) ) { |
|
| 714 | $last_post_id = $post->ID; |
|
| 715 | $buffer->view_time( $current_item['last_modified'] ); |
|
| 716 | } else { |
|
| 717 | break; |
|
| 718 | } |
|
| 719 | } |
|
| 720 | } |
|
| 721 | ||
| 722 | // If no items were added, return false. |
|
| 723 | if ( true === $buffer->is_empty() ) { |
|
| 724 | return false; |
|
| 725 | } |
|
| 726 | ||
| 727 | if ( false === $buffer->is_empty() ) { |
|
| 728 | $this->librarian->store_sitemap_data( |
|
| 729 | $number, |
|
| 730 | JP_VIDEO_SITEMAP_TYPE, |
|
| 731 | $buffer->contents(), |
|
| 732 | $buffer->last_modified() |
|
| 733 | ); |
|
| 734 | } |
|
| 735 | ||
| 736 | /* |
|
| 737 | * Now report back with the ID of the last post to be |
|
| 738 | * successfully added and whether there are any posts left. |
|
| 739 | */ |
|
| 740 | return array( |
|
| 741 | 'last_id' => $last_post_id, |
|
| 742 | 'any_left' => $any_posts_left, |
|
| 743 | 'last_modified' => $buffer->last_modified(), |
|
| 744 | ); |
|
| 745 | } |
|
| 746 | ||
| 747 | /** |
|
| 748 | * Build and store a single page sitemap index. Return false if no index is built. |
|