Edit File by line
/home/zeestwma/ceyloniy.../wp-inclu.../abilitie...
File: class-wp-ability.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Abilities API
[2] Fix | Delete
*
[3] Fix | Delete
* Defines WP_Ability class.
[4] Fix | Delete
*
[5] Fix | Delete
* @package WordPress
[6] Fix | Delete
* @subpackage Abilities API
[7] Fix | Delete
* @since 6.9.0
[8] Fix | Delete
*/
[9] Fix | Delete
[10] Fix | Delete
declare( strict_types = 1 );
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Encapsulates the properties and methods related to a specific ability in the registry.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 6.9.0
[16] Fix | Delete
*
[17] Fix | Delete
* @see WP_Abilities_Registry
[18] Fix | Delete
*/
[19] Fix | Delete
class WP_Ability {
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* The default value for the `show_in_rest` meta.
[23] Fix | Delete
*
[24] Fix | Delete
* @since 6.9.0
[25] Fix | Delete
* @var bool
[26] Fix | Delete
*/
[27] Fix | Delete
protected const DEFAULT_SHOW_IN_REST = false;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* The default ability annotations.
[31] Fix | Delete
* They are not guaranteed to provide a faithful description of ability behavior.
[32] Fix | Delete
*
[33] Fix | Delete
* @since 6.9.0
[34] Fix | Delete
* @var array<string, bool|null>
[35] Fix | Delete
*/
[36] Fix | Delete
protected static $default_annotations = array(
[37] Fix | Delete
// If true, the ability does not modify its environment.
[38] Fix | Delete
'readonly' => null,
[39] Fix | Delete
/*
[40] Fix | Delete
* If true, the ability may perform destructive updates to its environment.
[41] Fix | Delete
* If false, the ability performs only additive updates.
[42] Fix | Delete
*/
[43] Fix | Delete
'destructive' => null,
[44] Fix | Delete
/*
[45] Fix | Delete
* If true, calling the ability repeatedly with the same arguments will have no additional effect
[46] Fix | Delete
* on its environment.
[47] Fix | Delete
*/
[48] Fix | Delete
'idempotent' => null,
[49] Fix | Delete
);
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* The name of the ability, with its namespace.
[53] Fix | Delete
* Example: `my-plugin/my-ability`.
[54] Fix | Delete
*
[55] Fix | Delete
* @since 6.9.0
[56] Fix | Delete
* @var string
[57] Fix | Delete
*/
[58] Fix | Delete
protected $name;
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* The human-readable ability label.
[62] Fix | Delete
*
[63] Fix | Delete
* @since 6.9.0
[64] Fix | Delete
* @var string
[65] Fix | Delete
*/
[66] Fix | Delete
protected $label;
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* The detailed ability description.
[70] Fix | Delete
*
[71] Fix | Delete
* @since 6.9.0
[72] Fix | Delete
* @var string
[73] Fix | Delete
*/
[74] Fix | Delete
protected $description;
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* The ability category.
[78] Fix | Delete
*
[79] Fix | Delete
* @since 6.9.0
[80] Fix | Delete
* @var string
[81] Fix | Delete
*/
[82] Fix | Delete
protected $category;
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* The optional ability input schema.
[86] Fix | Delete
*
[87] Fix | Delete
* @since 6.9.0
[88] Fix | Delete
* @var array<string, mixed>
[89] Fix | Delete
*/
[90] Fix | Delete
protected $input_schema = array();
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* The optional ability output schema.
[94] Fix | Delete
*
[95] Fix | Delete
* @since 6.9.0
[96] Fix | Delete
* @var array<string, mixed>
[97] Fix | Delete
*/
[98] Fix | Delete
protected $output_schema = array();
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* The ability execute callback.
[102] Fix | Delete
*
[103] Fix | Delete
* @since 6.9.0
[104] Fix | Delete
* @var callable( mixed $input= ): (mixed|WP_Error)
[105] Fix | Delete
*/
[106] Fix | Delete
protected $execute_callback;
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* The optional ability permission callback.
[110] Fix | Delete
*
[111] Fix | Delete
* @since 6.9.0
[112] Fix | Delete
* @var callable( mixed $input= ): (bool|WP_Error)
[113] Fix | Delete
*/
[114] Fix | Delete
protected $permission_callback;
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* The optional ability metadata.
[118] Fix | Delete
*
[119] Fix | Delete
* @since 6.9.0
[120] Fix | Delete
* @var array<string, mixed>
[121] Fix | Delete
*/
[122] Fix | Delete
protected $meta;
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Constructor.
[126] Fix | Delete
*
[127] Fix | Delete
* Do not use this constructor directly. Instead, use the `wp_register_ability()` function.
[128] Fix | Delete
*
[129] Fix | Delete
* @access private
[130] Fix | Delete
*
[131] Fix | Delete
* @since 6.9.0
[132] Fix | Delete
*
[133] Fix | Delete
* @see wp_register_ability()
[134] Fix | Delete
*
[135] Fix | Delete
* @param string $name The name of the ability, with its namespace.
[136] Fix | Delete
* @param array<string, mixed> $args {
[137] Fix | Delete
* An associative array of arguments for the ability.
[138] Fix | Delete
*
[139] Fix | Delete
* @type string $label The human-readable label for the ability.
[140] Fix | Delete
* @type string $description A detailed description of what the ability does.
[141] Fix | Delete
* @type string $category The ability category slug this ability belongs to.
[142] Fix | Delete
* @type callable $execute_callback A callback function to execute when the ability is invoked.
[143] Fix | Delete
* Receives optional mixed input and returns mixed result or WP_Error.
[144] Fix | Delete
* @type callable $permission_callback A callback function to check permissions before execution.
[145] Fix | Delete
* Receives optional mixed input and returns bool or WP_Error.
[146] Fix | Delete
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input.
[147] Fix | Delete
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
[148] Fix | Delete
* @type array<string, mixed> $meta {
[149] Fix | Delete
* Optional. Additional metadata for the ability.
[150] Fix | Delete
*
[151] Fix | Delete
* @type array<string, bool|null> $annotations {
[152] Fix | Delete
* Optional. Semantic annotations describing the ability's behavioral characteristics.
[153] Fix | Delete
* These annotations are hints for tooling and documentation.
[154] Fix | Delete
*
[155] Fix | Delete
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
[156] Fix | Delete
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
[157] Fix | Delete
* If false, the ability performs only additive updates.
[158] Fix | Delete
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
[159] Fix | Delete
* will have no additional effect on its environment.
[160] Fix | Delete
* }
[161] Fix | Delete
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. Default false.
[162] Fix | Delete
* }
[163] Fix | Delete
* }
[164] Fix | Delete
*/
[165] Fix | Delete
public function __construct( string $name, array $args ) {
[166] Fix | Delete
$this->name = $name;
[167] Fix | Delete
[168] Fix | Delete
$properties = $this->prepare_properties( $args );
[169] Fix | Delete
[170] Fix | Delete
foreach ( $properties as $property_name => $property_value ) {
[171] Fix | Delete
if ( ! property_exists( $this, $property_name ) ) {
[172] Fix | Delete
_doing_it_wrong(
[173] Fix | Delete
__METHOD__,
[174] Fix | Delete
sprintf(
[175] Fix | Delete
/* translators: %s: Property name. */
[176] Fix | Delete
__( 'Property "%1$s" is not a valid property for ability "%2$s". Please check the %3$s class for allowed properties.' ),
[177] Fix | Delete
'<code>' . esc_html( $property_name ) . '</code>',
[178] Fix | Delete
'<code>' . esc_html( $this->name ) . '</code>',
[179] Fix | Delete
'<code>' . __CLASS__ . '</code>'
[180] Fix | Delete
),
[181] Fix | Delete
'6.9.0'
[182] Fix | Delete
);
[183] Fix | Delete
continue;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$this->$property_name = $property_value;
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Prepares and validates the properties used to instantiate the ability.
[192] Fix | Delete
*
[193] Fix | Delete
* Errors are thrown as exceptions instead of WP_Errors to allow for simpler handling and overloading. They are then
[194] Fix | Delete
* caught and converted to a WP_Error when by WP_Abilities_Registry::register().
[195] Fix | Delete
*
[196] Fix | Delete
* @since 6.9.0
[197] Fix | Delete
*
[198] Fix | Delete
* @see WP_Abilities_Registry::register()
[199] Fix | Delete
*
[200] Fix | Delete
* @param array<string, mixed> $args {
[201] Fix | Delete
* An associative array of arguments used to instantiate the ability class.
[202] Fix | Delete
*
[203] Fix | Delete
* @type string $label The human-readable label for the ability.
[204] Fix | Delete
* @type string $description A detailed description of what the ability does.
[205] Fix | Delete
* @type string $category The ability category slug this ability belongs to.
[206] Fix | Delete
* @type callable $execute_callback A callback function to execute when the ability is invoked.
[207] Fix | Delete
* Receives optional mixed input and returns mixed result or WP_Error.
[208] Fix | Delete
* @type callable $permission_callback A callback function to check permissions before execution.
[209] Fix | Delete
* Receives optional mixed input and returns bool or WP_Error.
[210] Fix | Delete
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input. Required if ability accepts an input.
[211] Fix | Delete
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
[212] Fix | Delete
* @type array<string, mixed> $meta {
[213] Fix | Delete
* Optional. Additional metadata for the ability.
[214] Fix | Delete
*
[215] Fix | Delete
* @type array<string, bool|null> $annotations {
[216] Fix | Delete
* Optional. Semantic annotations describing the ability's behavioral characteristics.
[217] Fix | Delete
* These annotations are hints for tooling and documentation.
[218] Fix | Delete
*
[219] Fix | Delete
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
[220] Fix | Delete
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
[221] Fix | Delete
* If false, the ability performs only additive updates.
[222] Fix | Delete
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
[223] Fix | Delete
* will have no additional effect on its environment.
[224] Fix | Delete
* }
[225] Fix | Delete
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. Default false.
[226] Fix | Delete
* }
[227] Fix | Delete
* }
[228] Fix | Delete
* @return array<string, mixed> {
[229] Fix | Delete
* An associative array of arguments with validated and prepared properties for the ability class.
[230] Fix | Delete
*
[231] Fix | Delete
* @type string $label The human-readable label for the ability.
[232] Fix | Delete
* @type string $description A detailed description of what the ability does.
[233] Fix | Delete
* @type string $category The ability category slug this ability belongs to.
[234] Fix | Delete
* @type callable $execute_callback A callback function to execute when the ability is invoked.
[235] Fix | Delete
* Receives optional mixed input and returns mixed result or WP_Error.
[236] Fix | Delete
* @type callable $permission_callback A callback function to check permissions before execution.
[237] Fix | Delete
* Receives optional mixed input and returns bool or WP_Error.
[238] Fix | Delete
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input.
[239] Fix | Delete
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
[240] Fix | Delete
* @type array<string, mixed> $meta {
[241] Fix | Delete
* Additional metadata for the ability.
[242] Fix | Delete
*
[243] Fix | Delete
* @type array<string, bool|null> $annotations {
[244] Fix | Delete
* Semantic annotations describing the ability's behavioral characteristics.
[245] Fix | Delete
* These annotations are hints for tooling and documentation.
[246] Fix | Delete
*
[247] Fix | Delete
* @type bool|null $readonly If true, the ability does not modify its environment.
[248] Fix | Delete
* @type bool|null $destructive If true, the ability may perform destructive updates to its environment.
[249] Fix | Delete
* If false, the ability performs only additive updates.
[250] Fix | Delete
* @type bool|null $idempotent If true, calling the ability repeatedly with the same arguments
[251] Fix | Delete
* will have no additional effect on its environment.
[252] Fix | Delete
* }
[253] Fix | Delete
* @type bool $show_in_rest Whether to expose this ability in the REST API. Default false.
[254] Fix | Delete
* }
[255] Fix | Delete
* }
[256] Fix | Delete
* @throws InvalidArgumentException if an argument is invalid.
[257] Fix | Delete
*/
[258] Fix | Delete
protected function prepare_properties( array $args ): array {
[259] Fix | Delete
// Required args must be present and of the correct type.
[260] Fix | Delete
if ( empty( $args['label'] ) || ! is_string( $args['label'] ) ) {
[261] Fix | Delete
throw new InvalidArgumentException(
[262] Fix | Delete
__( 'The ability properties must contain a `label` string.' )
[263] Fix | Delete
);
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
if ( empty( $args['description'] ) || ! is_string( $args['description'] ) ) {
[267] Fix | Delete
throw new InvalidArgumentException(
[268] Fix | Delete
__( 'The ability properties must contain a `description` string.' )
[269] Fix | Delete
);
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
if ( empty( $args['category'] ) || ! is_string( $args['category'] ) ) {
[273] Fix | Delete
throw new InvalidArgumentException(
[274] Fix | Delete
__( 'The ability properties must contain a `category` string.' )
[275] Fix | Delete
);
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
if ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) {
[279] Fix | Delete
throw new InvalidArgumentException(
[280] Fix | Delete
__( 'The ability properties must contain a valid `execute_callback` function.' )
[281] Fix | Delete
);
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
if ( empty( $args['permission_callback'] ) || ! is_callable( $args['permission_callback'] ) ) {
[285] Fix | Delete
throw new InvalidArgumentException(
[286] Fix | Delete
__( 'The ability properties must provide a valid `permission_callback` function.' )
[287] Fix | Delete
);
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
// Optional args only need to be of the correct type if they are present.
[291] Fix | Delete
if ( isset( $args['input_schema'] ) && ! is_array( $args['input_schema'] ) ) {
[292] Fix | Delete
throw new InvalidArgumentException(
[293] Fix | Delete
__( 'The ability properties should provide a valid `input_schema` definition.' )
[294] Fix | Delete
);
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
if ( isset( $args['output_schema'] ) && ! is_array( $args['output_schema'] ) ) {
[298] Fix | Delete
throw new InvalidArgumentException(
[299] Fix | Delete
__( 'The ability properties should provide a valid `output_schema` definition.' )
[300] Fix | Delete
);
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
if ( isset( $args['meta'] ) && ! is_array( $args['meta'] ) ) {
[304] Fix | Delete
throw new InvalidArgumentException(
[305] Fix | Delete
__( 'The ability properties should provide a valid `meta` array.' )
[306] Fix | Delete
);
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
if ( isset( $args['meta']['annotations'] ) && ! is_array( $args['meta']['annotations'] ) ) {
[310] Fix | Delete
throw new InvalidArgumentException(
[311] Fix | Delete
__( 'The ability meta should provide a valid `annotations` array.' )
[312] Fix | Delete
);
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
if ( isset( $args['meta']['show_in_rest'] ) && ! is_bool( $args['meta']['show_in_rest'] ) ) {
[316] Fix | Delete
throw new InvalidArgumentException(
[317] Fix | Delete
__( 'The ability meta should provide a valid `show_in_rest` boolean.' )
[318] Fix | Delete
);
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
// Set defaults for optional meta.
[322] Fix | Delete
$args['meta'] = wp_parse_args(
[323] Fix | Delete
$args['meta'] ?? array(),
[324] Fix | Delete
array(
[325] Fix | Delete
'annotations' => static::$default_annotations,
[326] Fix | Delete
'show_in_rest' => self::DEFAULT_SHOW_IN_REST,
[327] Fix | Delete
)
[328] Fix | Delete
);
[329] Fix | Delete
$args['meta']['annotations'] = wp_parse_args(
[330] Fix | Delete
$args['meta']['annotations'],
[331] Fix | Delete
static::$default_annotations
[332] Fix | Delete
);
[333] Fix | Delete
[334] Fix | Delete
return $args;
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
/**
[338] Fix | Delete
* Retrieves the name of the ability, with its namespace.
[339] Fix | Delete
* Example: `my-plugin/my-ability`.
[340] Fix | Delete
*
[341] Fix | Delete
* @since 6.9.0
[342] Fix | Delete
*
[343] Fix | Delete
* @return string The ability name, with its namespace.
[344] Fix | Delete
*/
[345] Fix | Delete
public function get_name(): string {
[346] Fix | Delete
return $this->name;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/**
[350] Fix | Delete
* Retrieves the human-readable label for the ability.
[351] Fix | Delete
*
[352] Fix | Delete
* @since 6.9.0
[353] Fix | Delete
*
[354] Fix | Delete
* @return string The human-readable ability label.
[355] Fix | Delete
*/
[356] Fix | Delete
public function get_label(): string {
[357] Fix | Delete
return $this->label;
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
/**
[361] Fix | Delete
* Retrieves the detailed description for the ability.
[362] Fix | Delete
*
[363] Fix | Delete
* @since 6.9.0
[364] Fix | Delete
*
[365] Fix | Delete
* @return string The detailed description for the ability.
[366] Fix | Delete
*/
[367] Fix | Delete
public function get_description(): string {
[368] Fix | Delete
return $this->description;
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Retrieves the ability category for the ability.
[373] Fix | Delete
*
[374] Fix | Delete
* @since 6.9.0
[375] Fix | Delete
*
[376] Fix | Delete
* @return string The ability category for the ability.
[377] Fix | Delete
*/
[378] Fix | Delete
public function get_category(): string {
[379] Fix | Delete
return $this->category;
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
/**
[383] Fix | Delete
* Retrieves the input schema for the ability.
[384] Fix | Delete
*
[385] Fix | Delete
* @since 6.9.0
[386] Fix | Delete
*
[387] Fix | Delete
* @return array<string, mixed> The input schema for the ability.
[388] Fix | Delete
*/
[389] Fix | Delete
public function get_input_schema(): array {
[390] Fix | Delete
return $this->input_schema;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/**
[394] Fix | Delete
* Retrieves the output schema for the ability.
[395] Fix | Delete
*
[396] Fix | Delete
* @since 6.9.0
[397] Fix | Delete
*
[398] Fix | Delete
* @return array<string, mixed> The output schema for the ability.
[399] Fix | Delete
*/
[400] Fix | Delete
public function get_output_schema(): array {
[401] Fix | Delete
return $this->output_schema;
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
/**
[405] Fix | Delete
* Retrieves the metadata for the ability.
[406] Fix | Delete
*
[407] Fix | Delete
* @since 6.9.0
[408] Fix | Delete
*
[409] Fix | Delete
* @return array<string, mixed> The metadata for the ability.
[410] Fix | Delete
*/
[411] Fix | Delete
public function get_meta(): array {
[412] Fix | Delete
return $this->meta;
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/**
[416] Fix | Delete
* Retrieves a specific metadata item for the ability.
[417] Fix | Delete
*
[418] Fix | Delete
* @since 6.9.0
[419] Fix | Delete
*
[420] Fix | Delete
* @param string $key The metadata key to retrieve.
[421] Fix | Delete
* @param mixed $default_value Optional. The default value to return if the metadata item is not found. Default `null`.
[422] Fix | Delete
* @return mixed The value of the metadata item, or the default value if not found.
[423] Fix | Delete
*/
[424] Fix | Delete
public function get_meta_item( string $key, $default_value = null ) {
[425] Fix | Delete
return array_key_exists( $key, $this->meta ) ? $this->meta[ $key ] : $default_value;
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
/**
[429] Fix | Delete
* Normalizes the input for the ability, applying the default value from the input schema when needed.
[430] Fix | Delete
*
[431] Fix | Delete
* When no input is provided and the input schema is defined with a top-level `default` key, this method returns
[432] Fix | Delete
* the value of that key. If the input schema does not define a `default`, or if the input schema is empty,
[433] Fix | Delete
* this method returns null. If input is provided, it is returned as-is.
[434] Fix | Delete
*
[435] Fix | Delete
* @since 6.9.0
[436] Fix | Delete
*
[437] Fix | Delete
* @param mixed $input Optional. The raw input provided for the ability. Default `null`.
[438] Fix | Delete
* @return mixed The same input, or the default from schema, or `null` if default not set.
[439] Fix | Delete
*/
[440] Fix | Delete
public function normalize_input( $input = null ) {
[441] Fix | Delete
if ( null !== $input ) {
[442] Fix | Delete
return $input;
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
$input_schema = $this->get_input_schema();
[446] Fix | Delete
if ( ! empty( $input_schema ) && array_key_exists( 'default', $input_schema ) ) {
[447] Fix | Delete
return $input_schema['default'];
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
return null;
[451] Fix | Delete
}
[452] Fix | Delete
[453] Fix | Delete
/**
[454] Fix | Delete
* Validates input data against the input schema.
[455] Fix | Delete
*
[456] Fix | Delete
* @since 6.9.0
[457] Fix | Delete
*
[458] Fix | Delete
* @param mixed $input Optional. The input data to validate. Default `null`.
[459] Fix | Delete
* @return true|WP_Error Returns true if valid or the WP_Error object if validation fails.
[460] Fix | Delete
*/
[461] Fix | Delete
public function validate_input( $input = null ) {
[462] Fix | Delete
$input_schema = $this->get_input_schema();
[463] Fix | Delete
if ( empty( $input_schema ) ) {
[464] Fix | Delete
if ( null === $input ) {
[465] Fix | Delete
return true;
[466] Fix | Delete
}
[467] Fix | Delete
[468] Fix | Delete
return new WP_Error(
[469] Fix | Delete
'ability_missing_input_schema',
[470] Fix | Delete
sprintf(
[471] Fix | Delete
/* translators: %s ability name. */
[472] Fix | Delete
__( 'Ability "%s" does not define an input schema required to validate the provided input.' ),
[473] Fix | Delete
esc_html( $this->name )
[474] Fix | Delete
)
[475] Fix | Delete
);
[476] Fix | Delete
}
[477] Fix | Delete
[478] Fix | Delete
$valid_input = rest_validate_value_from_schema( $input, $input_schema, 'input' );
[479] Fix | Delete
if ( is_wp_error( $valid_input ) ) {
[480] Fix | Delete
return new WP_Error(
[481] Fix | Delete
'ability_invalid_input',
[482] Fix | Delete
sprintf(
[483] Fix | Delete
/* translators: %1$s ability name, %2$s error message. */
[484] Fix | Delete
__( 'Ability "%1$s" has invalid input. Reason: %2$s' ),
[485] Fix | Delete
esc_html( $this->name ),
[486] Fix | Delete
$valid_input->get_error_message()
[487] Fix | Delete
)
[488] Fix | Delete
);
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
return true;
[492] Fix | Delete
}
[493] Fix | Delete
[494] Fix | Delete
/**
[495] Fix | Delete
* Invokes a callable, ensuring the input is passed through only if the input schema is defined.
[496] Fix | Delete
*
[497] Fix | Delete
* @since 6.9.0
[498] Fix | Delete
*
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function