map->linksPerIndex = PHP_INT_MAX; if ( 1 === count( $chunks ) ) { $filename = "$postType-$sitemapName.xml"; $files[ $filename ] = [ 'total' => $total, 'entries' => $chunks[0] ]; } else { for ( $i = 1; $i <= count( $chunks ); $i++ ) { $filename = "$postType-$sitemapName$i.xml"; $files[ $filename ] = [ 'total' => $total, 'entries' => $chunks[ $i - 1 ] ]; } } } } $taxonomies = aioseo()->sitemap->helpers->includedTaxonomies(); if ( $taxonomies ) { foreach ( $taxonomies as $taxonomy ) { aioseo()->sitemap->indexName = $taxonomy; $terms = aioseo()->sitemap->content->terms( $taxonomy ); if ( ! $terms ) { continue; } $total = aioseo()->sitemap->query->terms( $taxonomy, [ 'count' => true ] ); // We need to temporarily reset the linksPerIndex count here so that we can properly chunk. aioseo()->sitemap->linksPerIndex = aioseo()->options->sitemap->general->linksPerIndex; $chunks = aioseo()->sitemap->helpers->chunkEntries( $terms ); aioseo()->sitemap->linksPerIndex = PHP_INT_MAX; if ( 1 === count( $chunks ) ) { $filename = "$taxonomy-$sitemapName.xml"; $files[ $filename ] = [ 'total' => $total, 'entries' => $chunks[0] ]; } else { for ( $i = 1; $i <= count( $chunks ); $i++ ) { $filename = "$taxonomy-$sitemapName$i.xml"; $files[ $filename ] = [ 'total' => $total, 'entries' => $chunks[ $i - 1 ] ]; } } } } $this->writeSitemaps( $files ); } /** * Writes all sitemap files. * * @since 4.0.0 * * @param array $files The sitemap files. * @return void */ public function writeSitemaps( $files ) { $sitemapName = aioseo()->sitemap->helpers->filename(); if ( aioseo()->sitemap->indexes ) { $indexes = []; foreach ( $files as $filename => $data ) { if ( empty( $data['entries'] ) ) { continue; } $indexes[] = [ 'loc' => trailingslashit( home_url() ) . $filename, 'lastmod' => array_values( $data['entries'] )[0]['lastmod'], 'count' => count( $data['entries'] ) ]; } $files[ "$sitemapName.xml" ] = [ 'total' => 0, 'entries' => $indexes, ]; foreach ( $files as $filename => $data ) { $this->writeSitemap( $filename, $data['entries'], $data['total'] ); } return; } $content = []; foreach ( $files as $filename => $data ) { foreach ( $data['entries'] as $entry ) { $content[] = $entry; } } $this->writeSitemap( "$sitemapName.xml", $content, count( $content ) ); } /** * Writes a given sitemap file to the root dir. * * Helper function for writeSitemaps(). * * @since 4.0.0 * * @param string $filename The name of the file. * @param array $entries The sitemap entries for the file. * @return void */ protected function writeSitemap( $filename, $entries, $total = 0 ) { $sitemapName = aioseo()->sitemap->helpers->filename(); aioseo()->sitemap->indexName = $filename; if ( "$sitemapName.xml" === $filename && aioseo()->sitemap->indexes ) { // Set index name to root so that we use the right output template. aioseo()->sitemap->indexName = 'root'; } aioseo()->sitemap->xsl->saveXslData( $filename, $entries, $total ); ob_start(); aioseo()->sitemap->output->output( $entries ); aioseo()->addons->doAddonFunction( 'output', 'output', [ $entries, $total ] ); $content = ob_get_clean(); $fs = aioseo()->core->fs; $file = ABSPATH . sanitize_file_name( $filename ); $fileExists = $fs->exists( $file ); if ( ! $fileExists || $fs->isWritable( $file ) ) { $fs->putContents( $file, $content ); } } /** * Return an array of sitemap files. * * @since 4.0.0 * * @return array An array of files. */ public function files() { require_once ABSPATH . 'wp-admin/includes/file.php'; $files = list_files( get_home_path(), 1 ); if ( ! count( $files ) ) { return []; } $sitemapFiles = []; foreach ( $files as $filename ) { if ( preg_match( '#.*sitemap.*#', $filename ) ) { $sitemapFiles[] = $filename; } } return $sitemapFiles; } }