@@ 630-689 (lines=60) @@ | ||
627 | * @type string $last_modified The most recent timestamp to appear on the sitemap. |
|
628 | * } |
|
629 | */ |
|
630 | public function build_one_image_sitemap( $number, $from_id ) { |
|
631 | $last_post_id = $from_id; |
|
632 | $any_posts_left = true; |
|
633 | ||
634 | if ( $this->logger ) { |
|
635 | $debug_name = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, $number ); |
|
636 | $this->logger->report( "-- Building $debug_name" ); |
|
637 | } |
|
638 | ||
639 | $buffer = new Jetpack_Sitemap_Buffer_Image( |
|
640 | JP_SITEMAP_MAX_ITEMS, |
|
641 | JP_SITEMAP_MAX_BYTES |
|
642 | ); |
|
643 | ||
644 | // Add as many items to the buffer as possible. |
|
645 | while ( false === $buffer->is_full() ) { |
|
646 | $posts = $this->librarian->query_images_after_id( |
|
647 | $last_post_id, JP_SITEMAP_BATCH_SIZE |
|
648 | ); |
|
649 | ||
650 | if ( null == $posts ) { // WPCS: loose comparison ok. |
|
651 | $any_posts_left = false; |
|
652 | break; |
|
653 | } |
|
654 | ||
655 | foreach ( $posts as $post ) { |
|
656 | $current_item = $this->image_post_to_sitemap_item( $post ); |
|
657 | ||
658 | if ( true === $buffer->append( $current_item['xml'] ) ) { |
|
659 | $last_post_id = $post->ID; |
|
660 | $buffer->view_time( $current_item['last_modified'] ); |
|
661 | } else { |
|
662 | break; |
|
663 | } |
|
664 | } |
|
665 | } |
|
666 | ||
667 | // If no items were added, return false. |
|
668 | if ( true === $buffer->is_empty() ) { |
|
669 | return false; |
|
670 | } |
|
671 | ||
672 | // Store the buffer as the content of a jp_sitemap post. |
|
673 | $this->librarian->store_sitemap_data( |
|
674 | $number, |
|
675 | JP_IMAGE_SITEMAP_TYPE, |
|
676 | $buffer->contents(), |
|
677 | $buffer->last_modified() |
|
678 | ); |
|
679 | ||
680 | /* |
|
681 | * Now report back with the ID of the last post to be |
|
682 | * successfully added and whether there are any posts left. |
|
683 | */ |
|
684 | return array( |
|
685 | 'last_id' => $last_post_id, |
|
686 | 'any_left' => $any_posts_left, |
|
687 | 'last_modified' => $buffer->last_modified(), |
|
688 | ); |
|
689 | } |
|
690 | ||
691 | /** |
|
692 | * Build and store a single video sitemap. Returns false if no sitemap is built. |
|
@@ 708-768 (lines=61) @@ | ||
705 | * @type string $last_modified The most recent timestamp to appear on the sitemap. |
|
706 | * } |
|
707 | */ |
|
708 | public function build_one_video_sitemap( $number, $from_id ) { |
|
709 | $last_post_id = $from_id; |
|
710 | $any_posts_left = true; |
|
711 | ||
712 | if ( $this->logger ) { |
|
713 | $debug_name = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, $number ); |
|
714 | $this->logger->report( "-- Building $debug_name" ); |
|
715 | } |
|
716 | ||
717 | $buffer = new Jetpack_Sitemap_Buffer_Video( |
|
718 | JP_SITEMAP_MAX_ITEMS, |
|
719 | JP_SITEMAP_MAX_BYTES |
|
720 | ); |
|
721 | ||
722 | // Add as many items to the buffer as possible. |
|
723 | while ( false === $buffer->is_full() ) { |
|
724 | $posts = $this->librarian->query_videos_after_id( |
|
725 | $last_post_id, JP_SITEMAP_BATCH_SIZE |
|
726 | ); |
|
727 | ||
728 | if ( null == $posts ) { // WPCS: loose comparison ok. |
|
729 | $any_posts_left = false; |
|
730 | break; |
|
731 | } |
|
732 | ||
733 | foreach ( $posts as $post ) { |
|
734 | $current_item = $this->video_post_to_sitemap_item( $post ); |
|
735 | ||
736 | if ( true === $buffer->append( $current_item['xml'] ) ) { |
|
737 | $last_post_id = $post->ID; |
|
738 | $buffer->view_time( $current_item['last_modified'] ); |
|
739 | } else { |
|
740 | break; |
|
741 | } |
|
742 | } |
|
743 | } |
|
744 | ||
745 | // If no items were added, return false. |
|
746 | if ( true === $buffer->is_empty() ) { |
|
747 | return false; |
|
748 | } |
|
749 | ||
750 | if ( false === $buffer->is_empty() ) { |
|
751 | $this->librarian->store_sitemap_data( |
|
752 | $number, |
|
753 | JP_VIDEO_SITEMAP_TYPE, |
|
754 | $buffer->contents(), |
|
755 | $buffer->last_modified() |
|
756 | ); |
|
757 | } |
|
758 | ||
759 | /* |
|
760 | * Now report back with the ID of the last post to be |
|
761 | * successfully added and whether there are any posts left. |
|
762 | */ |
|
763 | return array( |
|
764 | 'last_id' => $last_post_id, |
|
765 | 'any_left' => $any_posts_left, |
|
766 | 'last_modified' => $buffer->last_modified(), |
|
767 | ); |
|
768 | } |
|
769 | ||
770 | /** |
|
771 | * Build and store a single page sitemap index. Return false if no index is built. |