wp/wp-includes/sitemaps.php
changeset 16 a86126ab1dd4
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
       
     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  */
       
    22 function 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  */
       
    52 function wp_get_sitemap_providers() {
       
    53 	$sitemaps = wp_sitemaps_get_server();
       
    54 	return $sitemaps->registry->get_providers();
       
    55 }
       
    56 
       
    57 /**
       
    58  * Registers a new sitemap provider.
       
    59  *
       
    60  * @since 5.5.0
       
    61  *
       
    62  * @param string               $name     Unique name for the sitemap provider.
       
    63  * @param WP_Sitemaps_Provider $provider The `Sitemaps_Provider` instance implementing the sitemap.
       
    64  * @return bool Whether the sitemap was added.
       
    65  */
       
    66 function wp_register_sitemap_provider( $name, WP_Sitemaps_Provider $provider ) {
       
    67 	$sitemaps = wp_sitemaps_get_server();
       
    68 	return $sitemaps->registry->add_provider( $name, $provider );
       
    69 }
       
    70 
       
    71 /**
       
    72  * Gets the maximum number of URLs for a sitemap.
       
    73  *
       
    74  * @since 5.5.0
       
    75  *
       
    76  * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user').
       
    77  * @return int The maximum number of URLs.
       
    78  */
       
    79 function wp_sitemaps_get_max_urls( $object_type ) {
       
    80 	/**
       
    81 	 * Filters the maximum number of URLs displayed on a sitemap.
       
    82 	 *
       
    83 	 * @since 5.5.0
       
    84 	 *
       
    85 	 * @param int    $max_urls    The maximum number of URLs included in a sitemap. Default 2000.
       
    86 	 * @param string $object_type Object type for sitemap to be filtered (e.g. 'post', 'term', 'user').
       
    87 	 */
       
    88 	return apply_filters( 'wp_sitemaps_max_urls', 2000, $object_type );
       
    89 }
       
    90 
       
    91 /**
       
    92  * Retrieves the full URL for a sitemap.
       
    93  *
       
    94  * @since 5.5.1
       
    95  *
       
    96  * @param string $name         The sitemap name.
       
    97  * @param string $subtype_name The sitemap subtype name.  Default empty string.
       
    98  * @param int    $page         The page of the sitemap.  Default 1.
       
    99  * @return string|false The sitemap URL or false if the sitemap doesn't exist.
       
   100  */
       
   101 function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) {
       
   102 	$sitemaps = wp_sitemaps_get_server();
       
   103 	if ( ! $sitemaps ) {
       
   104 		return false;
       
   105 	}
       
   106 
       
   107 	if ( 'index' === $name ) {
       
   108 		return $sitemaps->index->get_index_url();
       
   109 	}
       
   110 
       
   111 	$provider = $sitemaps->registry->get_provider( $name );
       
   112 	if ( ! $provider ) {
       
   113 		return false;
       
   114 	}
       
   115 
       
   116 	if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) {
       
   117 		return false;
       
   118 	}
       
   119 
       
   120 	$page = absint( $page );
       
   121 	if ( 0 >= $page ) {
       
   122 		$page = 1;
       
   123 	}
       
   124 	return $provider->get_sitemap_url( $subtype_name, $page );
       
   125 }