Automattic\WooCommerce\Abilities

ExtensibleAbilities::add_fields_to_object │ public static │ WC 11.3.0

Add the field values of an object to its formatted output, under extensions.<namespace>.<field>. A value is cast to its schema type, so '12' becomes 12 for an integer field. A field that throws or returns a value that its schema does not allow is left out. A namespace with no values is left out, and extensions is left out when no namespace has values or the feature is off.

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

Хуков нет.

Возвращает

array<string, mixed>.

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

$result = ExtensibleAbilities::add_fields_to_object( $output, $resource_name, $subject ): array;
$output(array<string, mixed>) (обязательный)
Formatted object.
$resource_name(строка) (обязательный)
Resource.
$subject(объект) (обязательный)
Object to read.

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

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

Код ExtensibleAbilities::add_fields_to_object() WC 11.3.0

public static function add_fields_to_object( array $output, string $resource_name, $subject ): array {
	if ( empty( self::$fields[ $resource_name ] ) || ! self::is_enabled() ) {
		return $output;
	}

	$extensions = array();
	foreach ( self::$fields[ $resource_name ] as $namespace => $fields ) {
		foreach ( $fields as $field => $args ) {
			try {
				$value = call_user_func( $args['get_callback'], $subject );
			} catch ( \Throwable $e ) {
				wc_get_logger()->error(
					sprintf( 'Ability field "%s.%s" of "%s" failed: %s', $namespace, $field, $resource_name, $e->getMessage() ),
					array( 'source' => 'extensible-abilities' )
				);
				continue;
			}
			if ( ! self::matches_schema( $args, $value ) ) {
				continue;
			}
			$sanitized = rest_sanitize_value_from_schema( $value, $args['schema'], $field );
			if ( ! is_wp_error( $sanitized ) ) {
				$extensions[ $namespace ][ $field ] = $sanitized;
			}
		}
	}
	if ( ! empty( $extensions ) ) {
		$output['extensions'] = $extensions;
	}
	return $output;
}