rn null; } /** * Validates input data against the input schema. * * @since 6.9.0 * * @param mixed $input Optional. The input data to validate. Default `null`. * @return true|WP_Error Returns true if valid or the WP_Error object if validation fails. */ public function validate_input( $input = null ) { $input_schema = $this->get_input_schema(); if ( empty( $input_schema ) ) { if ( null === $input ) { return true; } return new WP_Error( 'ability_missing_input_schema', sprintf( /* translators: %s ability name. */ __( 'Ability "%s" does not define an input schema required to validate the provided input.' ), esc_html( $this->name ) ) ); } $valid_input = rest_validate_value_from_schema( $input, $input_schema, 'input' ); if ( is_wp_error( $valid_input ) ) { return new WP_Error( 'ability_invalid_input', sprintf( /* translators: %1$s ability name, %2$s error message. */ __( 'Ability "%1$s" has invalid input. Reason: %2$s' ), esc_html( $this->name ), $valid_input->get_error_message() ) ); } return true; } /** * Invokes a callable, ensuring the input is passed through only if the input schema is defined. * * @since 6.9.0 * * @param callable $callback The callable to invoke. * @param mixed $input Optional. The input data for the ability. Default `null`. * @return mixed The result of the callable execution, or a `WP_Error` if the callback threw. */ protected function invoke_callback( callable $callback, $input = null ) { $args = array(); if ( ! empty( $this->get_input_schema() ) ) { $args[] = $input; } try { return $callback( ...$args ); } catch ( Throwable $e ) { return new WP_Error( 'ability_callback_exception', sprintf( /* translators: 1: Ability name, 2: Exception message. */ __( 'Ability "%1$s" callback threw an exception: %2$s' ), esc_html( $this->name ), esc_html( $e->getMessage() ) ) ); } } /** * Checks whether the ability has the necessary permissions. * * Please note that input is not automatically validated against the input schema. * Use `validate_input()` method to validate input before calling this method if needed. * * @since 6.9.0 * * @see validate_input() * * @param mixed $input Optional. The valid input data for permission checking. Default `null`. * @return bool|WP_Error Whether the ability has the necessary permission. */ public function check_permissions( $input = null ) { if ( ! is_callable( $this->permission_callback ) ) { return new WP_Error( 'ability_invalid_permission_callback', /* translators: %s ability name. */ sprintf( __( 'Ability "%s" does not have a valid permission callback.' ), esc_html( $this->name ) ) ); } return $this->invoke_callback( $this->permission_callback, $input ); } /** * Executes the ability callback. * * @since 6.9.0 * * @param mixed $input Optional. The input data for the ability. Default `null`. * @return mixed|WP_Error The result of the ability execution, or WP_Error on failure. */ protected function do_execute( $input = null ) { if ( ! is_callable( $this->execute_callback ) ) { return new WP_Error( 'ability_invalid_execute_callback', /* translators: %s ability name. */ sprintf( __( 'Ability "%s" does not have a valid execute callback.' ), esc_html( $this->name ) ) ); } return $this->invoke_callback( $this->execute_callback, $input ); } /** * Validates output data against the output schema. * * @since 6.9.0 * * @param mixed $output The output data to validate. * @return true|WP_Error Returns true if valid, or a WP_Error object if validation fails. */ protected function validate_output( $output ) { $output_schema = $this->get_output_schema(); if ( empty( $output_schema ) ) { return true; } $valid_output = rest_validate_value_from_schema( $output, $output_schema, 'output' ); if ( is_wp_error( $valid_output ) ) { return new WP_Error( 'ability_invalid_output', sprintf( /* translators: %1$s ability name, %2$s error message. */ __( 'Ability "%1$s" has invalid output. Reason: %2$s' ), esc_html( $this->name ), $valid_output->get_error_message() ) ); } return true; } /** * Executes the ability after input validation and running a permission check. * Before returning the return value, it also validates the output. * * @since 6.9.0 * * @param mixed $input Optional. The input data for the ability. Default `null`. * @return mixed|WP_Error The result of the ability execution, or WP_Error on failure. */ public function execute( $input = null ) { $input = $this->normalize_input( $input ); $is_valid = $this->validate_input( $input ); if ( is_wp_error( $is_valid ) ) { return $is_valid; } $has_permissions = $this->check_permissions( $input ); if ( true !== $has_permissions ) { if ( is_wp_error( $has_permissions ) ) { // Don't leak the permission check error to someone without the correct perms. _doing_it_wrong( __METHOD__, esc_html( $has_permissions->get_error_message() ), '6.9.0' ); } return new WP_Error( 'ability_invalid_permissions', /* translators: %s ability name. */ sprintf( __( 'Ability "%s" does not have necessary permission.' ), esc_html( $this->name ) ) ); } /** * Fires before an ability gets executed, after input validation and permissions check. * * @since 6.9.0 * * @param string $ability_name The name of the ability. * @param mixed $input The input data for the ability. */ do_action( 'wp_before_execute_ability', $this->name, $input ); $result = $this->do_execute( $input ); if ( is_wp_error( $result ) ) { return $result; } $is_valid = $this->validate_output( $result ); if ( is_wp_error( $is_valid ) ) { return $is_valid; } /** * Fires immediately after an ability finished executing. * * @since 6.9.0 * * @param string $ability_name The name of the ability. * @param mixed $input The input data for the ability. * @param mixed $result The result of the ability execution. */ do_action( 'wp_after_execute_ability', $this->name, $input, $result ); return $result; } /** * Wakeup magic method. * * @since 6.9.0 * @throws LogicException If the ability object is unserialized. * This is a security hardening measure to prevent unserialization of the ability. */ public function __wakeup(): void { throw new LogicException( __CLASS__ . ' should never be unserialized.' ); } /** * Sleep magic method. * * @since 6.9.0 * @throws LogicException If the ability object is serialized. * This is a security hardening measure to prevent serialization of the ability. */ public function __sleep(): array { throw new LogicException( __CLASS__ . ' should never be serialized.' ); } } 66000 CEKET KAHVERENGİ 36000 PANTOLON KAHVERENGİ