WP_Sitemaps_Provider
云策文档标注
概述
WP_Sitemaps_Provider 是 WordPress 5.5.0 引入的抽象类,用于为站点地图系统提供基础框架。它定义了核心方法和属性,支持对象类型(如文章、分类、用户)的站点地图生成,并处理分页和子类型逻辑。
关键要点
- WP_Sitemaps_Provider 是一个抽象类,必须通过子类实现来提供具体的站点地图数据。
- 核心抽象方法包括 get_url_list() 和 get_max_num_pages(),用于获取 URL 列表和最大页数。
- 提供 get_sitemap_type_data() 和 get_sitemap_entries() 方法,自动处理站点地图类型和条目生成。
- get_sitemap_url() 方法根据参数动态构建站点地图 URL,支持固定链接和非固定链接设置。
- get_object_subtypes() 方法默认返回空数组,子类可覆盖以支持对象子类型。
- 属性 $name 和 $object_type 用于标识提供者名称和对象类型,其中 $name 必须仅包含字母。
注意事项
- 扩展此类时,属性 $name 必须仅包含字母,否则可能导致 URL 生成问题。
- 站点地图条目可通过 wp_sitemaps_index_entry 过滤器进行自定义修改。
原文内容
Class WP_Sitemaps_Provider.
Methods
| Name | Description |
|---|---|
| WP_Sitemaps_Provider::get_max_num_pages | Gets the max number of pages available for the object type. |
| WP_Sitemaps_Provider::get_object_subtypes | Returns the list of supported object subtypes exposed by the provider. |
| WP_Sitemaps_Provider::get_sitemap_entries | Lists sitemap pages exposed by this provider. |
| WP_Sitemaps_Provider::get_sitemap_type_data | Gets data about each sitemap type. |
| WP_Sitemaps_Provider::get_sitemap_url | Gets the URL of a sitemap entry. |
| WP_Sitemaps_Provider::get_url_list | Gets a URL list for a sitemap. |
Source
abstract class WP_Sitemaps_Provider {
/**
* Provider name.
*
* This will also be used as the public-facing name in URLs.
*
* @since 5.5.0
*
* @var string
*/
protected $name = '';
/**
* Object type name (e.g. 'post', 'term', 'user').
*
* @since 5.5.0
*
* @var string
*/
protected $object_type = '';
/**
* Gets a URL list for a sitemap.
*
* @since 5.5.0
*
* @param int $page_num Page of results.
* @param string $object_subtype Optional. Object subtype name. Default empty.
* @return array[] Array of URL information for a sitemap.
*/
abstract public function get_url_list( $page_num, $object_subtype = '' );
/**
* Gets the max number of pages available for the object type.
*
* @since 5.5.0
*
* @param string $object_subtype Optional. Object subtype. Default empty.
* @return int Total number of pages.
*/
abstract public function get_max_num_pages( $object_subtype = '' );
/**
* Gets data about each sitemap type.
*
* @since 5.5.0
*
* @return array[] Array of sitemap types including object subtype name and number of pages.
*/
public function get_sitemap_type_data() {
$sitemap_data = array();
$object_subtypes = $this->get_object_subtypes();
/*
* If there are no object subtypes, include a single sitemap for the
* entire object type.
*/
if ( empty( $object_subtypes ) ) {
$sitemap_data[] = array(
'name' => '',
'pages' => $this->get_max_num_pages(),
);
return $sitemap_data;
}
// Otherwise, include individual sitemaps for every object subtype.
foreach ( $object_subtypes as $object_subtype_name => $data ) {
$object_subtype_name = (string) $object_subtype_name;
$sitemap_data[] = array(
'name' => $object_subtype_name,
'pages' => $this->get_max_num_pages( $object_subtype_name ),
);
}
return $sitemap_data;
}
/**
* Lists sitemap pages exposed by this provider.
*
* The returned data is used to populate the sitemap entries of the index.
*
* @since 5.5.0
*
* @return array[] Array of sitemap entries.
*/
public function get_sitemap_entries() {
$sitemaps = array();
$sitemap_types = $this->get_sitemap_type_data();
foreach ( $sitemap_types as $type ) {
for ( $page = 1; $page <= $type['pages']; $page++ ) {
$sitemap_entry = array(
'loc' => $this->get_sitemap_url( $type['name'], $page ),
);
/**
* Filters the sitemap entry for the sitemap index.
*
* @since 5.5.0
*
* @param array $sitemap_entry Sitemap entry for the post.
* @param string $object_type Object empty name.
* @param string $object_subtype Object subtype name.
* Empty string if the object type does not support subtypes.
* @param int $page Page number of results.
*/
$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );
$sitemaps[] = $sitemap_entry;
}
}
return $sitemaps;
}
/**
* Gets the URL of a sitemap entry.
*
* @since 5.5.0
*
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*
* @param string $name The name of the sitemap.
* @param int $page The page of the sitemap.
* @return string The composed URL for a sitemap entry.
*/
public function get_sitemap_url( $name, $page ) {
global $wp_rewrite;
// Accounts for cases where name is not included, ex: sitemaps-users-1.xml.
$params = array_filter(
array(
'sitemap' => $this->name,
'sitemap-subtype' => $name,
'paged' => $page,
)
);
$basename = sprintf(
'/wp-sitemap-%1$s.xml',
implode( '-', $params )
);
if ( ! $wp_rewrite->using_permalinks() ) {
$basename = '/?' . http_build_query( $params, '', '&' );
}
return home_url( $basename );
}
/**
* Returns the list of supported object subtypes exposed by the provider.
*
* @since 5.5.0
*
* @return array List of object subtypes objects keyed by their name.
*/
public function get_object_subtypes() {
return array();
}
}
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
Skip to note 2 content
pcarvalho
beware when extending this class, the property $name must be letters only