librarian = new Jetpack_Sitemap_Librarian(); $this->finder = new Jetpack_Sitemap_Finder(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $this->logger = new Jetpack_Sitemap_Logger(); } update_option( 'jetpack_sitemap_post_types', /** * The array of post types to be included in the sitemap. * * Add your custom post type name to the array to have posts of * that type included in the sitemap. The default array includes * 'page' and 'post'. * * The result of this filter is cached in an option, 'jetpack_sitemap_post_types', * so this filter only has to be applied once per generation. * * @since 4.8.0 */ apply_filters( 'jetpack_sitemap_post_types', array( 'post', 'page' ) ) ); return; } /** * Update the sitemap. * * All we do here is call build_next_sitemap_file a bunch of times. * * @since 4.8.0 */ public function update_sitemap() { if ( $this->logger ) { $this->logger->report( '-- Updating...' ); } for ( $i = 1; $i <= JP_SITEMAP_UPDATE_SIZE; $i++ ) { $this->build_next_sitemap_file(); } if ( $this->logger ) { $this->logger->report( '-- ...done for now.' ); $this->logger->time(); } } /** * Generate the next sitemap file. * * Reads the most recent state of the sitemap generation phase, * constructs the next file, and updates the state. * * @since 4.8.0 */ private function build_next_sitemap_file() { // Get the most recent state, and lock the state. $state = Jetpack_Sitemap_State::check_out(); // Do nothing if the state was locked. if ( false === $state ) { return; } // Otherwise, branch on the sitemap-type key of $state. switch ( $state['sitemap-type'] ) { case JP_PAGE_SITEMAP_TYPE: $this->build_next_sitemap_of_type( JP_PAGE_SITEMAP_TYPE, array( $this, 'build_one_page_sitemap' ), $state ); break; case JP_PAGE_SITEMAP_INDEX_TYPE: $this->build_next_sitemap_index_of_type( JP_PAGE_SITEMAP_INDEX_TYPE, JP_IMAGE_SITEMAP_TYPE, $state ); break; case JP_IMAGE_SITEMAP_TYPE: $this->build_next_sitemap_of_type( JP_IMAGE_SITEMAP_TYPE, array( $this, 'build_one_image_sitemap' ), $state ); break; case JP_IMAGE_SITEMAP_INDEX_TYPE: $this->build_next_sitemap_index_of_type( JP_IMAGE_SITEMAP_INDEX_TYPE, JP_VIDEO_SITEMAP_TYPE, $state ); break; case JP_VIDEO_SITEMAP_TYPE: $this->build_next_sitemap_of_type( JP_VIDEO_SITEMAP_TYPE, array( $this, 'build_one_video_sitemap' ), $state ); break; case JP_VIDEO_SITEMAP_INDEX_TYPE: $this->build_next_sitemap_index_of_type( JP_VIDEO_SITEMAP_INDEX_TYPE, JP_MASTER_SITEMAP_TYPE, $state ); break; case JP_MASTER_SITEMAP_TYPE: $this->build_master_sitemap( $state['max'] ); // Reset the state and quit. Jetpack_Sitemap_State::reset( JP_PAGE_SITEMAP_TYPE ); if ( $this->logger ) { $this->logger->report( '-- Finished.' ); $this->logger->time(); } die(); default: // Otherwise, reset the state. Jetpack_Sitemap_State::reset( JP_PAGE_SITEMAP_TYPE ); die(); } // Unlock the state. Jetpack_Sitemap_State::unlock(); return; } /** * Build the next sitemap of a given type and update the sitemap state. * * @since 4.8.0 * * @param string $sitemap_type The type of the sitemap being generated. * @param callback $build_one A callback which builds a single sitemap file. * @param array $state A sitemap state. */ private function build_next_sitemap_of_type( $sitemap_type, $build_one, $state ) { $index_type = jp_sitemap_index_type_of( $sitemap_type ); // Try to build a sitemap. $result = call_user_func_array( $build_one, array( $state['number'] + 1, $state['last-added'], ) ); if ( false === $result ) { // If no sitemap was generated, advance to the next type. Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $index_type, 'last-added' => 0, 'number' => 0, 'last-modified' => '1970-01-01 00:00:00', ) ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $sitemap_type" ); } // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( $state['number'], $sitemap_type ); return; } // Otherwise, update the state. Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $state['sitemap-type'], 'last-added' => $result['last_id'], 'number' => $state['number'] + 1, 'last-modified' => $result['last_modified'], ) ); if ( true === $result['any_left'] ) { // If there's more work to be done with this type, return. return; } // Otherwise, advance state to the next sitemap type. Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $index_type, 'last-added' => 0, 'number' => 0, 'last-modified' => '1970-01-01 00:00:00', ) ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $sitemap_type" ); } // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( $state['number'] + 1, $sitemap_type ); return; } /** * Build the next sitemap index of a given type and update the state. * * @since 4.8.0 * * @param string $index_type The type of index being generated. * @param string $next_type The next type to generate after this one. * @param array $state A sitemap state. */ private function build_next_sitemap_index_of_type( $index_type, $next_type, $state ) { $sitemap_type = jp_sitemap_child_type_of( $index_type ); // If only 0 or 1 sitemaps were built, advance to the next type and return. if ( 1 >= $state['max'][ $sitemap_type ]['number'] ) { Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $next_type, 'last-added' => 0, 'number' => 0, 'last-modified' => '1970-01-01 00:00:00', ) ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); } // There are no indices of this type. $this->librarian->delete_numbered_sitemap_rows_after( 0, $index_type ); return; } // Otherwise, try to build a sitemap index. $result = $this->build_one_sitemap_index( $state['number'] + 1, $state['last-added'], $state['last-modified'], $index_type ); // If no index was built, advance to the next type and return. if ( false === $result ) { Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $next_type, 'last-added' => 0, 'number' => 0, 'last-modified' => '1970-01-01 00:00:00', ) ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); } // Clean up old files. $this->librarian->delete_numbered_sitemap_rows_after( $state['number'], $index_type ); return; } // Otherwise, check in the state. Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $index_type, 'last-added' => $result['last_id'], 'number' => $state['number'] + 1, 'last-modified' => $result['last_modified'], ) ); // If there are still sitemaps left to index, return. if ( true === $result['any_left'] ) { return; } // Otherwise, advance to the next type. Jetpack_Sitemap_State::check_in( array( 'sitemap-type' => $next_type, 'last-added' => 0, 'number' => 0, 'last-modified' => '1970-01-01 00:00:00', ) ); if ( $this->logger ) { $this->logger->report( "-- Cleaning Up $index_type" ); } // We're done generating indices of this type. $this->librarian->delete_numbered_sitemap_rows_after( $state['number'] + 1, $index_type ); return; } /** * Builds the master sitemap index. * * @param array $max Array of sitemap types with max index and datetime. * * @since 4.8.0 */ private function build_master_sitemap( $max ) { $sitemap_index_xsl_url = $this->finder->construct_sitemap_url( 'sitemap-index.xsl' ); $jetpack_version = JETPACK__VERSION; if ( $this->logger ) { $this->logger->report( '-- Building Master Sitemap.' ); } $buffer = new Jetpack_Sitemap_Buffer( JP_SITEMAP_MAX_ITEMS, JP_SITEMAP_MAX_BYTES, <<
HEADER , <<