/** * @param int $max_entries Entries per sitemap. * * @return array */ public function get_index_links($max_entries) { $taxonomies = get_taxonomies(array('public' => true), 'objects'); if (empty($taxonomies)) { return array(); } $taxonomy_names = array_filter(array_keys($taxonomies), array($this, 'is_valid_taxonomy')); $taxonomies = array_intersect_key($taxonomies, array_flip($taxonomy_names)); // Retrieve all the taxonomies and their terms so we can do a proper count on them. /** * Filter the setting of excluding empty terms from the XML sitemap. * * @param boolean $exclude Defaults to true. * @param array $taxonomy_names Array of names for the taxonomies being processed. */ $hide_empty = apply_filters('wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names); $all_taxonomies = array(); foreach ($taxonomy_names as $taxonomy_name) { $taxonomy_terms = get_terms($taxonomy_name, array('hide_empty' => $hide_empty, 'fields' => 'ids')); if (count($taxonomy_terms) > 0) { $all_taxonomies[$taxonomy_name] = $taxonomy_terms; } } $index = array(); foreach ($taxonomies as $tax_name => $tax) { if (!isset($all_taxonomies[$tax_name])) { // No eligible terms found. continue; } $total_count = isset($all_taxonomies[$tax_name]) ? count($all_taxonomies[$tax_name]) : 1; $max_pages = 1; if ($total_count > $max_entries) { $max_pages = (int) ceil($total_count / $max_entries); } $last_modified_gmt = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type); for ($page_counter = 0; $page_counter < $max_pages; $page_counter++) { $current_page = $max_pages > 1 ? $page_counter + 1 : ''; if (!is_array($tax->object_type) || count($tax->object_type) == 0) { continue; } $terms = array_splice($all_taxonomies[$tax_name], 0, $max_entries); if (!$terms) { continue; } $args = array('post_type' => $tax->object_type, 'tax_query' => array(array('taxonomy' => $tax_name, 'terms' => $terms)), 'orderby' => 'modified', 'order' => 'DESC', 'posts_per_page' => 1); $query = new WP_Query($args); if ($query->have_posts()) { $date = $query->posts[0]->post_modified_gmt; } else { $date = $last_modified_gmt; } $index[] = array('loc' => WPSEO_Sitemaps_Router::get_base_url($tax_name . '-sitemap' . $current_page . '.xml'), 'lastmod' => $date); } } return $index; }
/** * @param int $max_entries Entries per sitemap. * * @return array */ public function get_index_links($max_entries) { global $wpdb; $taxonomies = get_taxonomies(array('public' => true), 'objects'); if (empty($taxonomies)) { return array(); } $taxonomy_names = array_filter(array_keys($taxonomies), array($this, 'is_valid_taxonomy')); $taxonomies = array_intersect_key($taxonomies, array_flip($taxonomy_names)); // Retrieve all the taxonomies and their terms so we can do a proper count on them. /** * Filter the setting of excluding empty terms from the XML sitemap. * * @param boolean $exclude Defaults to true. * @param array $taxonomy_names Array of names for the taxonomies being processed. */ $hide_empty = apply_filters('wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names) ? 'count != 0 AND' : ''; $sql = "\n\t\t\tSELECT taxonomy, term_id\n\t\t\tFROM {$wpdb->term_taxonomy}\n\t\t\tWHERE {$hide_empty} taxonomy IN ('" . implode("','", $taxonomy_names) . "');\n\t\t"; $all_taxonomy_terms = $wpdb->get_results($sql); $all_taxonomies = array(); foreach ($all_taxonomy_terms as $obj) { $all_taxonomies[$obj->taxonomy][] = $obj->term_id; } unset($hide_empty, $sql, $all_taxonomy_terms, $obj); $index = array(); foreach ($taxonomies as $tax_name => $tax) { if (!isset($all_taxonomies[$tax_name])) { // No eligible terms found. continue; } $steps = $max_entries; $count = isset($all_taxonomies[$tax_name]) ? count($all_taxonomies[$tax_name]) : 1; $n = $count > $max_entries ? (int) ceil($count / $max_entries) : 1; for ($i = 0; $i < $n; $i++) { $count = $n > 1 ? $i + 1 : ''; if (!is_array($tax->object_type) || count($tax->object_type) == 0) { continue; } if (empty($count) || $count == $n) { $date = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type); } else { $terms = array_splice($all_taxonomies[$tax_name], 0, $steps); if (!$terms) { continue; } $args = array('post_type' => $tax->object_type, 'tax_query' => array(array('taxonomy' => $tax_name, 'terms' => $terms)), 'orderby' => 'modified', 'order' => 'DESC'); $query = new WP_Query($args); if ($query->have_posts()) { $date = $query->posts[0]->post_modified_gmt; } else { $date = WPSEO_Sitemaps::get_last_modified_gmt($tax->object_type); } unset($terms, $args, $query); } $index[] = array('loc' => WPSEO_Sitemaps_Router::get_base_url($tax_name . '-sitemap' . $count . '.xml'), 'lastmod' => $date); } } return $index; }
/** * Produces set of links to prepend at start of first sitemap page. * * @param string $post_type Post type to produce links for. * * @return array */ protected function get_first_links($post_type) { $links = array(); $needs_archive = true; if (!$this->get_page_on_front_id() && ($post_type == 'post' || $post_type == 'page')) { $links[] = array('loc' => $this->get_home_url(), 'chf' => 'daily', 'pri' => 1); $needs_archive = false; } elseif ($this->get_page_on_front_id() && $post_type === 'post' && $this->get_page_for_posts_id()) { $page_for_posts_url = get_permalink($this->get_page_for_posts_id()); $links[] = array('loc' => $page_for_posts_url, 'chf' => 'daily', 'pri' => 1); $needs_archive = false; } if (!$needs_archive) { return $links; } $archive_url = get_post_type_archive_link($post_type); /** * Filter the URL Yoast SEO uses in the XML sitemap for this post type archive. * * @param string $archive_url The URL of this archive * @param string $post_type The post type this archive is for. */ $archive_url = apply_filters('wpseo_sitemap_post_type_archive_link', $archive_url, $post_type); if ($archive_url) { /** * Filter the priority of the URL Yoast SEO uses in the XML sitemap. * * @param float $priority The priority for this URL, ranging from 0 to 1 * @param string $post_type The post type this archive is for. */ $links[] = array('loc' => $archive_url, 'mod' => WPSEO_Sitemaps::get_last_modified_gmt($post_type), 'chf' => 'daily', 'pri' => 1); } return $links; }
/** * Produces set of links to prepend at start of first sitemap page. * * @param string $post_type Post type to produce links for. * * @return array */ protected function get_first_links($post_type) { $links = array(); $front_id = get_option('page_on_front'); $page_for_posts = get_option('page_for_posts'); if (!$front_id && ($post_type == 'post' || $post_type == 'page')) { $links[] = array('loc' => $this->get_home_url(), 'pri' => 1, 'chf' => WPSEO_Sitemaps::filter_frequency('homepage', 'daily', $this->get_home_url())); } elseif ($front_id && $post_type === 'post' && $page_for_posts) { $page_for_posts_url = get_permalink($page_for_posts); $links[] = array('loc' => $page_for_posts_url, 'pri' => 1, 'chf' => WPSEO_Sitemaps::filter_frequency('blogpage', 'daily', $page_for_posts_url)); unset($page_for_posts_url); } $archive_url = get_post_type_archive_link($post_type); /** * Filter the URL Yoast SEO uses in the XML sitemap for this post type archive. * * @param string $archive_url The URL of this archive * @param string $post_type The post type this archive is for. */ $archive_url = apply_filters('wpseo_sitemap_post_type_archive_link', $archive_url, $post_type); if ($archive_url) { /** * Filter the priority of the URL Yoast SEO uses in the XML sitemap. * * @param float $priority The priority for this URL, ranging from 0 to 1 * @param string $post_type The post type this archive is for. */ $links[] = array('loc' => $archive_url, 'pri' => apply_filters('wpseo_xml_post_type_archive_priority', 0.8, $post_type), 'chf' => WPSEO_Sitemaps::filter_frequency($post_type . '_archive', 'weekly', $archive_url), 'mod' => WPSEO_Sitemaps::get_last_modified_gmt($post_type)); } return $links; }