Nessuna descrizione

class-wp-sitemaps-index.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Index class.
  4. *
  5. * Generates the sitemap index.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Class WP_Sitemaps_Index.
  13. * Builds the sitemap index page that lists the links to all of the sitemaps.
  14. *
  15. * @since 5.5.0
  16. */
  17. class WP_Sitemaps_Index {
  18. /**
  19. * The main registry of supported sitemaps.
  20. *
  21. * @since 5.5.0
  22. * @var WP_Sitemaps_Registry
  23. */
  24. protected $registry;
  25. /**
  26. * Maximum number of sitemaps to include in an index.
  27. *
  28. * @sincee 5.5.0
  29. *
  30. * @var int Maximum number of sitemaps.
  31. */
  32. private $max_sitemaps = 50000;
  33. /**
  34. * WP_Sitemaps_Index constructor.
  35. *
  36. * @since 5.5.0
  37. *
  38. * @param WP_Sitemaps_Registry $registry Sitemap provider registry.
  39. */
  40. public function __construct( WP_Sitemaps_Registry $registry ) {
  41. $this->registry = $registry;
  42. }
  43. /**
  44. * Gets a sitemap list for the index.
  45. *
  46. * @since 5.5.0
  47. *
  48. * @return array[] Array of all sitemaps.
  49. */
  50. public function get_sitemap_list() {
  51. $sitemaps = array();
  52. $providers = $this->registry->get_providers();
  53. /* @var WP_Sitemaps_Provider $provider */
  54. foreach ( $providers as $name => $provider ) {
  55. $sitemap_entries = $provider->get_sitemap_entries();
  56. // Prevent issues with array_push and empty arrays on PHP < 7.3.
  57. if ( ! $sitemap_entries ) {
  58. continue;
  59. }
  60. // Using array_push is more efficient than array_merge in a loop.
  61. array_push( $sitemaps, ...$sitemap_entries );
  62. if ( count( $sitemaps ) >= $this->max_sitemaps ) {
  63. break;
  64. }
  65. }
  66. return array_slice( $sitemaps, 0, $this->max_sitemaps, true );
  67. }
  68. /**
  69. * Builds the URL for the sitemap index.
  70. *
  71. * @since 5.5.0
  72. *
  73. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  74. *
  75. * @return string The sitemap index URL.
  76. */
  77. public function get_index_url() {
  78. global $wp_rewrite;
  79. if ( ! $wp_rewrite->using_permalinks() ) {
  80. return home_url( '/?sitemap=index' );
  81. }
  82. return home_url( '/wp-sitemap.xml' );
  83. }
  84. }