sitemaps.php 3.2 KB

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