[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/sitemaps/ -> class-wp-sitemaps-provider.php (source)

   1  <?php
   2  /**
   3   * Sitemaps: WP_Sitemaps_Provider class
   4   *
   5   * This class is a base class for other sitemap providers to extend and contains shared functionality.
   6   *
   7   * @package WordPress
   8   * @subpackage Sitemaps
   9   * @since 5.5.0
  10   */
  11  
  12  /**
  13   * Class WP_Sitemaps_Provider.
  14   *
  15   * @since 5.5.0
  16   */
  17  abstract class WP_Sitemaps_Provider {
  18      /**
  19       * Provider name.
  20       *
  21       * This will also be used as the public-facing name in URLs.
  22       *
  23       * @since 5.5.0
  24       *
  25       * @var string
  26       */
  27      protected $name = '';
  28  
  29      /**
  30       * Object type name (e.g. 'post', 'term', 'user').
  31       *
  32       * @since 5.5.0
  33       *
  34       * @var string
  35       */
  36      protected $object_type = '';
  37  
  38      /**
  39       * Gets a URL list for a sitemap.
  40       *
  41       * @since 5.5.0
  42       *
  43       * @param int    $page_num       Page of results.
  44       * @param string $object_subtype Optional. Object subtype name. Default empty.
  45       * @return array[] Array of URL information for a sitemap.
  46       */
  47      abstract public function get_url_list( $page_num, $object_subtype = '' );
  48  
  49      /**
  50       * Gets the max number of pages available for the object type.
  51       *
  52       * @since 5.5.0
  53       *
  54       * @param string $object_subtype Optional. Object subtype. Default empty.
  55       * @return int Total number of pages.
  56       */
  57      abstract public function get_max_num_pages( $object_subtype = '' );
  58  
  59      /**
  60       * Gets data about each sitemap type.
  61       *
  62       * @since 5.5.0
  63       *
  64       * @return array[] Array of sitemap types including object subtype name and number of pages.
  65       */
  66  	public function get_sitemap_type_data() {
  67          $sitemap_data = array();
  68  
  69          $object_subtypes = $this->get_object_subtypes();
  70  
  71          // If there are no object subtypes, include a single sitemap for the
  72          // entire object type.
  73          if ( empty( $object_subtypes ) ) {
  74              $sitemap_data[] = array(
  75                  'name'  => '',
  76                  'pages' => $this->get_max_num_pages(),
  77              );
  78              return $sitemap_data;
  79          }
  80  
  81          // Otherwise, include individual sitemaps for every object subtype.
  82          foreach ( $object_subtypes as $object_subtype_name => $data ) {
  83              $object_subtype_name = (string) $object_subtype_name;
  84  
  85              $sitemap_data[] = array(
  86                  'name'  => $object_subtype_name,
  87                  'pages' => $this->get_max_num_pages( $object_subtype_name ),
  88              );
  89          }
  90  
  91          return $sitemap_data;
  92      }
  93  
  94      /**
  95       * Lists sitemap pages exposed by this provider.
  96       *
  97       * The returned data is used to populate the sitemap entries of the index.
  98       *
  99       * @since 5.5.0
 100       *
 101       * @return array[] Array of sitemap entries.
 102       */
 103  	public function get_sitemap_entries() {
 104          $sitemaps = array();
 105  
 106          $sitemap_types = $this->get_sitemap_type_data();
 107  
 108          foreach ( $sitemap_types as $type ) {
 109              for ( $page = 1; $page <= $type['pages']; $page ++ ) {
 110                  $sitemap_entry = array(
 111                      'loc' => $this->get_sitemap_url( $type['name'], $page ),
 112                  );
 113  
 114                  /**
 115                   * Filters the sitemap entry for the sitemap index.
 116                   *
 117                   * @since 5.5.0
 118                   *
 119                   * @param array  $sitemap_entry  Sitemap entry for the post.
 120                   * @param string $object_type    Object empty name.
 121                   * @param string $object_subtype Object subtype name.
 122                   *                               Empty string if the object type does not support subtypes.
 123                   * @param int    $page           Page number of results.
 124                   */
 125                  $sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );
 126  
 127                  $sitemaps[] = $sitemap_entry;
 128              }
 129          }
 130  
 131          return $sitemaps;
 132      }
 133  
 134      /**
 135       * Gets the URL of a sitemap entry.
 136       *
 137       * @since 5.5.0
 138       *
 139       * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 140       *
 141       * @param string $name The name of the sitemap.
 142       * @param int    $page The page of the sitemap.
 143       * @return string The composed URL for a sitemap entry.
 144       */
 145  	public function get_sitemap_url( $name, $page ) {
 146          global $wp_rewrite;
 147  
 148          // Accounts for cases where name is not included, ex: sitemaps-users-1.xml.
 149          $params = array_filter(
 150              array(
 151                  'sitemap'         => $this->name,
 152                  'sitemap-subtype' => $name,
 153                  'paged'           => $page,
 154              )
 155          );
 156  
 157          $basename = sprintf(
 158              '/wp-sitemap-%1$s.xml',
 159              implode( '-', $params )
 160          );
 161  
 162          if ( ! $wp_rewrite->using_permalinks() ) {
 163              $basename = '/?' . http_build_query( $params, '', '&' );
 164          }
 165  
 166          return home_url( $basename );
 167      }
 168  
 169      /**
 170       * Returns the list of supported object subtypes exposed by the provider.
 171       *
 172       * @since 5.5.0
 173       *
 174       * @return array List of object subtypes objects keyed by their name.
 175       */
 176  	public function get_object_subtypes() {
 177          return array();
 178      }
 179  }


Generated: Sat Apr 20 01:00:03 2024 Cross-referenced by PHPXref 0.7.1