), true )->result(); $totalPosts = aioseo()->core->db->execute( aioseo()->core->db->db->prepare( "SELECT COUNT(*) as count FROM {$postsTable} as p LEFT JOIN {$aioseoPostsTable} as ap ON p.ID = ap.post_id WHERE p.post_status IN ( 'publish', 'inherit' ) AND p.post_type = %s AND p.post_password = '' AND (ap.robots_noindex IS NULL OR ap.robots_default = 1 OR ap.robots_noindex = 0) {$whereClause} ", [ $postType ] ), true )->result(); if ( $posts ) { $indexes = []; $filename = aioseo()->sitemap->filename; $postCount = count( $posts ); for ( $i = 0; $i < $postCount; $i++ ) { $indexNumber = 0 !== $i && 1 < $postCount ? $i + 1 : ''; $indexes[] = [ 'loc' => aioseo()->helpers->localizedUrl( "/$postType-$filename$indexNumber.xml" ), 'lastmod' => aioseo()->helpers->dateTimeToIso8601( $posts[ $i ]->post_modified_gmt ), 'count' => $linksPerIndex ]; } // We need to update the count of the last index since it won't necessarily be the same as the links per index. $indexes[ count( $indexes ) - 1 ]['count'] = $totalPosts[0]->count - ( $linksPerIndex * ( $postCount - 1 ) ); return $indexes; } if ( ! $posts ) { $addonsPosts = aioseo()->addons->doAddonFunction( 'root', 'buildIndexesPostType', [ $postType ] ); foreach ( $addonsPosts as $addonPosts ) { if ( $addonPosts ) { $posts = $addonPosts; break; } } } if ( ! $posts ) { return []; } return $this->buildIndexes( $postType, $posts ); } /** * Builds indexes for all eligible terms of a given taxonomy. * * @since 4.0.0 * * @param string $taxonomy The taxonomy. * @return array The indexes. */ private function buildIndexesTaxonomy( $taxonomy ) { $terms = aioseo()->sitemap->content->terms( $taxonomy, [ 'root' => true ] ); if ( ! $terms ) { $addonsTerms = aioseo()->addons->doAddonFunction( 'root', 'buildIndexesTaxonomy', [ $taxonomy ] ); foreach ( $addonsTerms as $addonTerms ) { if ( $addonTerms ) { $terms = $addonTerms; break; } } } if ( ! $terms ) { return []; } return $this->buildIndexes( $taxonomy, $terms ); } /** * Builds indexes for a given type. * * Acts as a helper function for buildIndexesPostTypes() and buildIndexesTaxonomies(). * * @since 4.0.0 * * @param string $name The name of the object parent. * @param array $entries The sitemap entries. * @return array The indexes. */ public function buildIndexes( $name, $entries ) { $filename = aioseo()->sitemap->filename; $chunks = aioseo()->sitemap->helpers->chunkEntries( $entries ); $indexes = []; for ( $i = 0; $i < count( $chunks ); $i++ ) { $chunk = array_values( $chunks[ $i ] ); $indexNumber = 0 !== $i && 1 < count( $chunks ) ? $i + 1 : ''; $index = [ 'loc' => aioseo()->helpers->localizedUrl( "/$name-$filename$indexNumber.xml" ), 'count' => count( $chunks[ $i ] ) ]; if ( isset( $entries[0]->ID ) ) { $ids = array_map( function( $post ) { return $post->ID; }, $chunk ); $ids = implode( "', '", $ids ); $lastModified = null; if ( ! apply_filters( 'aioseo_sitemap_lastmod_disable', false ) ) { $lastModified = aioseo()->core->db ->start( aioseo()->core->db->db->posts . ' as p', true ) ->select( 'MAX(`p`.`post_modified_gmt`) as last_modified' ) ->whereRaw( "( `p`.`ID` IN ( '$ids' ) )" ) ->run() ->result(); } if ( ! empty( $lastModified[0]->last_modified ) ) { $index['lastmod'] = aioseo()->helpers->dateTimeToIso8601( $lastModified[0]->last_modified ); } $indexes[] = $index; continue; } $termIds = []; foreach ( $chunk as $term ) { $termIds[] = $term->term_id; } $termIds = implode( "', '", $termIds ); $termRelationshipsTable = aioseo()->core->db->db->prefix . 'term_relationships'; $lastModified = null; if ( ! apply_filters( 'aioseo_sitemap_lastmod_disable', false ) ) { $lastModified = aioseo()->core->db ->start( aioseo()->core->db->db->posts . ' as p', true ) ->select( 'MAX(`p`.`post_modified_gmt`) as last_modified' ) ->whereRaw( " ( `p`.`ID` IN ( SELECT `tr`.`object_id` FROM `$termRelationshipsTable` as tr WHERE `tr`.`term_taxonomy_id` IN ( '$termIds' ) ) )" ) ->run() ->result(); } if ( ! empty( $lastModified[0]->last_modified ) ) { $index['lastmod'] = aioseo()->helpers->dateTimeToIso8601( $lastModified[0]->last_modified ); } $indexes[] = $index; } return $indexes; } }