1<?php
2/**
3 * Sitemaps: Public functions
4 *
5 * This file contains a variety of public functions developers can use to interact with
6 * the XML Sitemaps API.
7 *
8 * @package WordPress
9 * @subpackage Sitemaps
10 * @since 5.5.0
11 */
12
13/**
14 * Retrieves the current Sitemaps server instance.
15 *
16 * @since 5.5.0
17 *
18 * @global WP_Sitemaps $wp_sitemaps Global Core Sitemaps instance.
19 *
20 * @return WP_Sitemaps Sitemaps instance.
21 */
22function wp_sitemaps_get_server() {
23 global $wp_sitemaps;
24
25 // If there isn't a global instance, set and bootstrap the sitemaps system.
26 if ( empty( $wp_sitemaps ) ) {
27 $wp_sitemaps = new WP_Sitemaps();
28 $wp_sitemaps->init();
29
30 /**
31 * Fires when initializing the Sitemaps object.
32 *
33 * Additional sitemaps should be registered on this hook.
34 *
35 * @since 5.5.0
36 *
37 * @param WP_Sitemaps $wp_sitemaps Sitemaps object.
38 */
39 do_action( 'wp_sitemaps_init', $wp_sitemaps );
40 }
41
42 return $wp_sitemaps;
43}
44
45/**
46 * Gets an array of sitemap providers.
47 *
48 * @since 5.5.0
49 *
50 * @return WP_Sitemaps_Provider[] Array of sitemap providers.
51 */
52function wp_get_sitemap_providers() {
53 $sitemaps = wp_sitemaps_get_server();
54
55 return $sitemaps->registry->get_providers();
56}
57
58/**
59 * Registers a new sitemap provider.
60 *
61 * @since 5.5.0
62 *
63 * @param string $name Unique name for the sitemap provider.
64 * @param WP_Sitemaps_Provider $provider The `Sitemaps_Provider` instance implementing the sitemap.
65 * @return bool Whether the sitemap was added.
66 */
67function wp_register_sitemap_provider( $name, WP_Sitemaps_Provider $provider ) {
68 $sitemaps = wp_sitemaps_get_server();
69
70 return $sitemaps->registry->add_provider( $name, $provider );
71}
72
73/**
74 * Gets the maximum number of URLs for a sitemap.
75 *
76 * @since 5.5.0
77 *
78 * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user').
79 * @return int The maximum number of URLs.
80 */
81function wp_sitemaps_get_max_urls( $object_type ) {
82 /**
83 * Filters the maximum number of URLs displayed on a sitemap.
84 *
85 * @since 5.5.0
86 *
87 * @param int $max_urls The maximum number of URLs included in a sitemap. Default 2000.
88 * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user').
89 */
90 return apply_filters( 'wp_sitemaps_max_urls', 2000, $object_type );
91}
92
93/**
94 * Retrieves the full URL for a sitemap.
95 *
96 * @since 5.5.1
97 *
98 * @param string $name The sitemap name.
99 * @param string $subtype_name The sitemap subtype name. Default empty string.
100 * @param int $page The page of the sitemap. Default 1.
101 * @return string|false The sitemap URL or false if the sitemap doesn't exist.
102 */
103function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
104 $sitemaps = wp_sitemaps_get_server();
105
106 if ( ! $sitemaps ) {
107 return false;
108 }
109
110 if ( 'index' === $name ) {
111 return $sitemaps->index->get_index_url();
112 }
113
114 $provider = $sitemaps->registry->get_provider( $name );
115 if ( ! $provider ) {
116 return false;
117 }
118
119 if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) {
120 return false;
121 }
122
123 $page = absint( $page );
124 if ( 0 >= $page ) {
125 $page = 1;
126 }
127
128 return $provider->get_sitemap_url( $subtype_name, $page );
129}
130