WP_Ability_Category
云策文档标注
概述
WP_Ability_Category 是一个 WordPress 类,用于封装特定能力类别的属性和方法。它提供了一种结构化的方式来管理能力类别的数据,包括 slug、标签、描述和元数据。
关键要点
- WP_Ability_Category 是一个 final 类,用于表示能力类别,包含 slug、label、description 和 meta 等受保护属性。
- 构造函数 __construct 接受 slug 和 args 数组,通过 prepare_properties 方法验证参数,并初始化属性。
- 提供 get_slug、get_label、get_description 和 get_meta 等公共方法,用于检索能力类别的相关信息。
- 包含 __sleep 和 __wakeup 魔术方法,用于防止序列化和反序列化,作为安全加固措施。
- 建议使用 wp_register_ability_category() 函数注册能力类别,而不是直接实例化此类。
注意事项
- 构造函数为私有访问(@access private),开发者应通过 wp_register_ability_category() 函数来注册能力类别。
- 在实例化时,必须提供非空的 slug 字符串,以及包含 label 和 description 字符串的 args 数组,否则会抛出 InvalidArgumentException。
- meta 参数是可选的,但如果提供,必须是数组类型。
- 序列化和反序列化操作会抛出 LogicException,以确保对象安全。
原文内容
Encapsulates the properties and methods related to a specific ability category.
Description
See also
Methods
| Name | Description |
|---|---|
| WP_Ability_Category::__construct | Constructor. |
| WP_Ability_Category::__sleep | Sleep magic method. |
| WP_Ability_Category::__wakeup | Wakeup magic method. |
| WP_Ability_Category::get_description | Retrieves the detailed description for the ability category. |
| WP_Ability_Category::get_label | Retrieves the human-readable label for the ability category. |
| WP_Ability_Category::get_meta | Retrieves the metadata for the ability category. |
| WP_Ability_Category::get_slug | Retrieves the slug of the ability category. |
| WP_Ability_Category::prepare_properties | Prepares and validates the properties used to instantiate the ability category. |
Source
final class WP_Ability_Category {
/**
* The unique slug for the ability category.
*
* @since 6.9.0
* @var string
*/
protected $slug;
/**
* The human-readable ability category label.
*
* @since 6.9.0
* @var string
*/
protected $label;
/**
* The detailed ability category description.
*
* @since 6.9.0
* @var string
*/
protected $description;
/**
* The optional ability category metadata.
*
* @since 6.9.0
* @var array<string, mixed>
*/
protected $meta = array();
/**
* Constructor.
*
* Do not use this constructor directly. Instead, use the `wp_register_ability_category()` function.
*
* @access private
*
* @since 6.9.0
*
* @see wp_register_ability_category()
*
* @param string $slug The unique slug for the ability category.
* @param array<string, mixed> $args {
* An associative array of arguments for the ability category.
*
* @type string $label The human-readable label for the ability category.
* @type string $description A description of the ability category.
* @type array<string, mixed> $meta Optional. Additional metadata for the ability category.
* }
*/
public function __construct( string $slug, array $args ) {
if ( empty( $slug ) ) {
throw new InvalidArgumentException(
__( 'The ability category slug cannot be empty.' )
);
}
$this->slug = $slug;
$properties = $this->prepare_properties( $args );
foreach ( $properties as $property_name => $property_value ) {
if ( ! property_exists( $this, $property_name ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: Property name. */
__( 'Property "%1$s" is not a valid property for ability category "%2$s". Please check the %3$s class for allowed properties.' ),
'<code>' . esc_html( $property_name ) . '</code>',
'<code>' . esc_html( $this->slug ) . '</code>',
'<code>' . __CLASS__ . '</code>'
),
'6.9.0'
);
continue;
}
$this->$property_name = $property_value;
}
}
/**
* Prepares and validates the properties used to instantiate the ability category.
*
* @since 6.9.0
*
* @param array<string, mixed> $args $args {
* An associative array of arguments used to instantiate the ability category class.
*
* @type string $label The human-readable label for the ability category.
* @type string $description A description of the ability category.
* @type array<string, mixed> $meta Optional. Additional metadata for the ability category.
* }
* @return array<string, mixed> $args {
* An associative array with validated and prepared ability category properties.
*
* @type string $label The human-readable label for the ability category.
* @type string $description A description of the ability category.
* @type array<string, mixed> $meta Optional. Additional metadata for the ability category.
* }
* @throws InvalidArgumentException if an argument is invalid.
*/
protected function prepare_properties( array $args ): array {
// Required args must be present and of the correct type.
if ( empty( $args['label'] ) || ! is_string( $args['label'] ) ) {
throw new InvalidArgumentException(
__( 'The ability category properties must contain a `label` string.' )
);
}
if ( empty( $args['description'] ) || ! is_string( $args['description'] ) ) {
throw new InvalidArgumentException(
__( 'The ability category properties must contain a `description` string.' )
);
}
// Optional args only need to be of the correct type if they are present.
if ( isset( $args['meta'] ) && ! is_array( $args['meta'] ) ) {
throw new InvalidArgumentException(
__( 'The ability category properties should provide a valid `meta` array.' )
);
}
return $args;
}
/**
* Retrieves the slug of the ability category.
*
* @since 6.9.0
*
* @return string The ability category slug.
*/
public function get_slug(): string {
return $this->slug;
}
/**
* Retrieves the human-readable label for the ability category.
*
* @since 6.9.0
*
* @return string The human-readable ability category label.
*/
public function get_label(): string {
return $this->label;
}
/**
* Retrieves the detailed description for the ability category.
*
* @since 6.9.0
*
* @return string The detailed description for the ability category.
*/
public function get_description(): string {
return $this->description;
}
/**
* Retrieves the metadata for the ability category.
*
* @since 6.9.0
*
* @return array<string,mixed> The metadata for the ability category.
*/
public function get_meta(): array {
return $this->meta;
}
/**
* Wakeup magic method.
*
* @since 6.9.0
* @throws LogicException If the ability category object is unserialized.
* This is a security hardening measure to prevent unserialization of the ability category.
*/
public function __wakeup(): void {
throw new LogicException( __CLASS__ . ' should never be unserialized.' );
}
/**
* Sleep magic method.
*
* @since 6.9.0
* @throws LogicException If the ability category object is serialized.
* This is a security hardening measure to prevent serialization of the ability category.
*/
public function __sleep(): array {
throw new LogicException( __CLASS__ . ' should never be serialized.' );
}
}
Changelog
| Version | Description |
|---|---|
| 6.9.0 | Introduced. |