Automattic\WooCommerce\Abilities

ExtensibleAbilities::register_validator │ public static │ WC 11.3.0

Register a validator that can refuse a change to an object of a resource.

Call it inside the woocommerce_extensible_abilities_init Validators run only when the extensible_abilities feature is on.

Use a field update_callback to refuse a bad value of one field. It runs only when the input sends that field. Use a validator to refuse a change as a whole. It runs on every change of the resource, also when the input sends no extension fields. For example, a validator refuses a product change that would break a subscription plan.

Core's product abilities run validators. Core's order abilities keep their trunk write for now, so validators for order run only in the ActionableAbility classes of extensions.

Метод класса: ExtensibleAbilities{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$result = ExtensibleAbilities::register_validator( $args ): void;
$args(массив) (обязательный)

Validator arguments.

  • resource(строка)
    Resource that an ActionableAbility changes, such as product.

  • namespace(строка)
    Slug of the extension that owns the validator, such as subscriptions.

  • callback(callable)
    Receives the changed object and the ability, after Core's changes and the field callbacks, before the one save. It returns a WP_Error to refuse the change, and then nothing is saved. The error gets the namespace in its data, and the 400 status when it has no status. Its message can name the ability or screen to use instead. Rules:
    1. It only reads. It does not change the object, save, send email or make HTTP requests.
    2. It checks the ability name when the extension's own abilities change the same resource.

Список изменений

С версии 11.3.0 Введена.

Код ExtensibleAbilities::register_validator() WC 11.3.0

public static function register_validator( array $args ): void {
	if ( did_action( 'woocommerce_extensible_abilities_init' ) && ! doing_action( 'woocommerce_extensible_abilities_init' ) ) {
		wc_doing_it_wrong( __METHOD__, __( 'Register a validator inside the woocommerce_extensible_abilities_init action.', 'woocommerce' ), '11.3.0' );
		return;
	}
	foreach ( array( 'resource', 'namespace' ) as $key ) {
		if ( ! is_string( $args[ $key ] ?? null ) || '' === $args[ $key ] || sanitize_key( $args[ $key ] ) !== $args[ $key ] ) {
			wc_doing_it_wrong(
				__METHOD__,
				sprintf(
					/* translators: %s: argument name. */
					__( 'The "%s" argument must be a slug of lowercase letters, numbers, dashes and underscores.', 'woocommerce' ),
					$key
				),
				'11.3.0'
			);
			return;
		}
	}
	if ( ! is_callable( $args['callback'] ?? null ) ) {
		wc_doing_it_wrong( __METHOD__, __( 'The "callback" argument must be callable.', 'woocommerce' ), '11.3.0' );
		return;
	}
	if ( ! isset( self::$resources[ $args['resource'] ] ) ) {
		wc_doing_it_wrong(
			__METHOD__,
			sprintf(
				/* translators: %s: resource name. */
				__( 'The "%s" resource is not registered. Call ExtensibleAbilities::register_resource() first.', 'woocommerce' ),
				$args['resource']
			),
			'11.3.0'
		);
		return;
	}

	self::$validators[ $args['resource'] ][] = $args;
}