Code Duplication    Length = 98-99 lines in 2 locations

modules/sitemaps/sitemap-builder.php 2 locations

@@ 662-759 (lines=98) @@
659
	 *   @type string $last_modified The most recent timestamp to appear on the sitemap.
660
	 * }
661
	 */
662
	public function build_one_image_sitemap( $number, $from_id ) {
663
		$last_post_id = $from_id;
664
		$any_posts_left = true;
665
666
		if ( $this->logger ) {
667
			$debug_name = jp_sitemap_filename( JP_IMAGE_SITEMAP_TYPE, $number );
668
			$this->logger->report( "-- Building $debug_name" );
669
		}
670
671
		$image_sitemap_xsl_url = $this->finder->construct_sitemap_url( 'image-sitemap.xsl' );
672
673
		$jetpack_version = JETPACK__VERSION;
674
675
		$namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
676
			/**
677
			 * Filter the XML namespaces included in image sitemaps.
678
			 *
679
			 * @module sitemaps
680
			 *
681
			 * @since 4.7.0
682
			 *
683
			 * @param array $namespaces Associative array with namespaces and namespace URIs.
684
			 */
685
			apply_filters(
686
				'jetpack_sitemap_image_ns',
687
				array(
688
					'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
689
					'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
690
					'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
691
					'xmlns:image'        => 'http://www.google.com/schemas/sitemap-image/1.1',
692
				)
693
			)
694
		);
695
696
		$buffer = new Jetpack_Sitemap_Buffer(
697
			JP_SITEMAP_MAX_ITEMS,
698
			JP_SITEMAP_MAX_BYTES,
699
			<<<HEADER
700
<?xml version='1.0' encoding='UTF-8'?>
701
<!-- generator='jetpack-{$jetpack_version}' -->
702
<?xml-stylesheet type='text/xsl' href='{$image_sitemap_xsl_url}'?>
703
<urlset{$namespaces}>\n
704
HEADER
705
			,
706
			<<<FOOTER
707
</urlset>\n
708
FOOTER
709
			,
710
			/* epoch */
711
			'1970-01-01 00:00:00'
712
		);
713
714
		// Add as many items to the buffer as possible.
715
		while ( false === $buffer->is_full() ) {
716
			$posts = $this->librarian->query_images_after_id(
717
				$last_post_id, JP_SITEMAP_BATCH_SIZE
718
			);
719
720
			if ( null == $posts ) { // WPCS: loose comparison ok.
721
				$any_posts_left = false;
722
				break;
723
			}
724
725
			foreach ( $posts as $post ) {
726
				$current_item = $this->image_post_to_sitemap_item( $post );
727
728
				if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
729
					$last_post_id = $post->ID;
730
					$buffer->view_time( $current_item['last_modified'] );
731
				} else {
732
					break;
733
				}
734
			}
735
		}
736
737
		// If no items were added, return false.
738
		if ( true === $buffer->is_empty() ) {
739
			return false;
740
		}
741
742
		// Store the buffer as the content of a jp_sitemap post.
743
		$this->librarian->store_sitemap_data(
744
			$number,
745
			JP_IMAGE_SITEMAP_TYPE,
746
			$buffer->contents(),
747
			$buffer->last_modified()
748
		);
749
750
		/*
751
		 * Now report back with the ID of the last post to be
752
		 * successfully added and whether there are any posts left.
753
		 */
754
		return array(
755
			'last_id'       => $last_post_id,
756
			'any_left'      => $any_posts_left,
757
			'last_modified' => $buffer->last_modified(),
758
		);
759
	}
760
761
	/**
762
	 * Build and store a single video sitemap. Returns false if no sitemap is built.
@@ 778-876 (lines=99) @@
775
	 *   @type string $last_modified The most recent timestamp to appear on the sitemap.
776
	 * }
777
	 */
778
	public function build_one_video_sitemap( $number, $from_id ) {
779
		$last_post_id = $from_id;
780
		$any_posts_left = true;
781
782
		if ( $this->logger ) {
783
			$debug_name = jp_sitemap_filename( JP_VIDEO_SITEMAP_TYPE, $number );
784
			$this->logger->report( "-- Building $debug_name" );
785
		}
786
787
		$video_sitemap_xsl_url = $this->finder->construct_sitemap_url( 'video-sitemap.xsl' );
788
789
		$jetpack_version = JETPACK__VERSION;
790
791
		$namespaces = Jetpack_Sitemap_Buffer::array_to_xml_attr_string(
792
			/**
793
			 * Filter the XML namespaces included in video sitemaps.
794
			 *
795
			 * @module sitemaps
796
			 *
797
			 * @since 4.7.0
798
			 *
799
			 * @param array $namespaces Associative array with namespaces and namespace URIs.
800
			 */
801
			apply_filters(
802
				'jetpack_sitemap_video_ns',
803
				array(
804
					'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
805
					'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
806
					'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
807
					'xmlns:video'        => 'http://www.google.com/schemas/sitemap-video/1.1',
808
				)
809
			)
810
		);
811
812
		$buffer = new Jetpack_Sitemap_Buffer(
813
			JP_SITEMAP_MAX_ITEMS,
814
			JP_SITEMAP_MAX_BYTES,
815
			<<<HEADER
816
<?xml version='1.0' encoding='UTF-8'?>
817
<!-- generator='jetpack-{$jetpack_version}' -->
818
<?xml-stylesheet type='text/xsl' href='{$video_sitemap_xsl_url}'?>
819
<urlset{$namespaces}>\n
820
HEADER
821
			,
822
			<<<FOOTER
823
</urlset>\n
824
FOOTER
825
			,
826
			/* epoch */
827
			'1970-01-01 00:00:00'
828
		);
829
830
		// Add as many items to the buffer as possible.
831
		while ( false === $buffer->is_full() ) {
832
			$posts = $this->librarian->query_videos_after_id(
833
				$last_post_id, JP_SITEMAP_BATCH_SIZE
834
			);
835
836
			if ( null == $posts ) { // WPCS: loose comparison ok.
837
				$any_posts_left = false;
838
				break;
839
			}
840
841
			foreach ( $posts as $post ) {
842
				$current_item = $this->video_post_to_sitemap_item( $post );
843
844
				if ( true === $buffer->try_to_add_item( $current_item['xml'] ) ) {
845
					$last_post_id = $post->ID;
846
					$buffer->view_time( $current_item['last_modified'] );
847
				} else {
848
					break;
849
				}
850
			}
851
		}
852
853
		// If no items were added, return false.
854
		if ( true === $buffer->is_empty() ) {
855
			return false;
856
		}
857
858
		if ( false === $buffer->is_empty() ) {
859
			$this->librarian->store_sitemap_data(
860
				$number,
861
				JP_VIDEO_SITEMAP_TYPE,
862
				$buffer->contents(),
863
				$buffer->last_modified()
864
			);
865
		}
866
867
		/*
868
		 * Now report back with the ID of the last post to be
869
		 * successfully added and whether there are any posts left.
870
		 */
871
		return array(
872
			'last_id'       => $last_post_id,
873
			'any_left'      => $any_posts_left,
874
			'last_modified' => $buffer->last_modified(),
875
		);
876
	}
877
878
	/**
879
	 * Build and store a single page sitemap index. Return false if no index is built.