�PNG  IHDR22?�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>���� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-��"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~<�5�j>{�-�]c�K'Xt����o��(�h���w��?�G�%�fI�q^D$.Tʳ?�D��*�A��,���� �`6�B$��BB d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p�a��(�� A�a!ڈb�X#����!�H�$ ɈQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=� �C��7�F� �dt1�����r�=�6��Ыhڏ>C�0��3�l0.��B�8, �c˱"� ���V����cϱw�E� 6wB aAHXLXN�H� $4� 7 �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +ȅ����3��!�[ �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9̓IK�����hh�i��t�ݕN��W���G���w ��Ljg(�gw��L�Ӌ�T071���oUX*�*|�� �J�&�*/T����ު U�U�T��^S}�FU3S� Ԗ�U��P�SSg�;���g�oT?�~Y��Y�L�OC�Q��_�� c�x,!k ��u�5�&���|v*�����=���9C3J3W�R�f?�q��tN �(���~���)�)�4L�1e\k����X�H�Q�G�6������E�Y��A�J'\'Gg����S�Sݧ �M=:��.�k���Dw�n��^��Lo��y���}/�T�m���G X� $� �<�5qo</���QC]�@C�a�a�ᄑ��<��F�F�i�\�$�m�mƣ&&!&KM�M�RM��)�;L;L���͢�֙5�=1�2��כ߷`ZxZ,����eI��Z�Yn�Z9Y�XUZ]�F���%ֻ�����N�N���gð�ɶ�����ۮ�m�}agbg�Ů��}�}��= ���Z~s�r:V:ޚΜ�?}����/gX���3��)�i�S��Ggg�s�󈋉K��.�>.���Ƚ�Jt�q]�z���������ۯ�6�i�ܟ�4�)�Y3s���C�Q��? ��0k߬~OCO�g��#/c/�W�װ��w��a�>�>r��>�<7�2�Y_�7��ȷ�O�o�_��C#�d�z����%g��A�[��z|!��?:�e����A���AA�������!h�쐭!��Α�i�P~���a�a��~ '���W�?�p�X�1�5w��Cs�D�D�Dޛg1O9�-J5*>�.j<�7�4�?�.fY��X�XIlK9.*�6nl������� �{�/�]py�����.,:�@L�N8��A*��%�w%� y��g"/�6ш�C\*N�H*Mz�쑼5y$�3�,幄'���L Lݛ:��v m2=:�1����qB�!M��g�g�fvˬe����n��/��k���Y- �B��TZ(�*�geWf�͉�9���+��̳�ې7�����ᒶ��KW-X潬j9�������(�x��oʿ�ܔ���Ĺd�f�f���-�[����n �ڴ �V����E�/��(ۻ��C���<��e����;?T�T�T�T6��ݵa��n��{��4���[���>ɾ�UUM�f�e�I���?�������m]�Nmq����#�׹���=TR��+�G�����w- 6 U����#pDy��� �� :�v�{���vg/jB��F�S��[b[�O�>����z�G��499�?r����C�d�&����ˮ/~�����јѡ�򗓿m|������������x31^�V���w�w��O�| (�h���SЧ��������c3-� cHRMz%������u0�`:�o�_�F5IDATx��ytUս�?�;���fN$$��@B�!�k��� -*�gE���j�O�kQ�ʠ`U�B_ �AÐ0%�Ȕ�@r3ޛ;߳��$�@��v����^�s����������$��ߢ !�W��5�̱'-٘������4�>�Rƫ$�G��0U�H}�_ _� #��k�MJ/=�9Du *ڲ�k�PQ;0��j|�*�$�^컂脛y���>�z×� B��� ��(�:$�ʔ_�6��C!B����MVx��a��ζyY����j��ƋY2�9�F�{r��r�딟F��Q��hY6��� x��[l��RC��������nt@��;P��3��� F�w��7��:�P���ɋ'���mj���Bƪ�$�Q�L��y��߷g��\���z����v5�p�v$�v櫻�`6~K �Y�X����,�:��"�o�e0II"(��,������l_�C���/4�+f�E�!q��:����u����c��樼.UE�@� �]K�W���$-�h]�JG���Dz)�`��.�A/ ���mᏣc�3jK� �!�>���$�Q�!�_�? \�C2(E �M$Z I{�U���s�ǣ���=��@Բe ޻��W�����@ �w����}�9v,)��.}*|"�s��di���,���G�]W�AC�������|<����[��ߕ������K�*�G nߴ!��‹��J�!��6(L�a)��Am��������;adf��:�xm��nh�;֦I��(���o�C~ډa?�=��s����h��E������xp����)���_���t��W�����´;8���`dz=(��@@�+,�;�`�fhl��XȌ�3,uI�~z(J����� �ZX�A����H'�À�㜦�82|��a�n�z2��I���� ���EY�h�ε<�0�q�{��C��v�˅��?"@��χ�x�WS+��N�~�KQ�J�8ل�� _.W��� !TU{�����,��"�� �;�������HI S� ��))�o�1�H�D� ���� (� ;����"Ҕ��,���� �F=���O��&�Ph>�`���y��߲ &S��P�<��'��1s�1cĂ�N����2)����@�u��tv��<�+�-&�����h��[!FSG���' � z z0��<����8d&�����E0�,��<��"!P�b4��� �{S�����X����.�囤���T��hEF��X�q0"-<����h���]�Py*����F⩍��@v�˂�Y08�� .q��a��s����(!<̫1�3�L�H��7sYW�`��qtu�\7��?�@�Y�� %Ka�x�GZ�*W�P��y�����N0r9��Z�c�%���q,��k�����!!,��>���IB��� �x1�Xu�Y���v-���ֺ��N������JP��a�2`�e5��w��u;�6�i���̃��(K���ťa��(l51��JL��O'%���]���ٖ뿉!��{*�p+f ���i8��D�ȑ���ܩ�M��V��ӟz��x�7Z�1i}�^͜���6e����x��)u�d�ˏ�V�eG�\�X1�����=n�U'�~Y����L�\,�(d!x ,�:�TW8� M`r���@��r+v�Q0��>^;˴�VbF��('����� �*O��i��j�Ӆ����^�S3&� ���{������.ی�� N;��NU%d0�Ce\r"�C�b��&t7N�7��� �ȪD�[%l�۪�$��'m?�A9tI�9>��NVL ��� 6v~M�����ъ�B���0����9$&=ώm��W�0���L���&��E���f�f��'+�9��hv��7,���M��6�D�&�^� �����8 D֮�����o��k^z��O��hP�8q�5d*��YVq3�{��������#LX4�?��jP=3�~�؅c�ݰ�̱�ұ��8̲��2�G�����j��έ�9��w7���U�X�w�85C��?!I&���o&����L���I#�^�eV�,�^t5�k� �xs˛\��U�8��."���{���ǵ�l4��ݼ�;��r"���G�/~���gJV�N糦��ZZ^k+NN�D��I$�=OE�z����|*�*{��%k���G��>�`9��b;�s���8,�qs��c��.b��q�%�>r��n�R�Ƨ�%�,�A��le#�8���}; 21�0�b�&�QX�f���q�00����$>�h�N�'�a3��A���/�r ��L5�8�GԲ'�����w;��c#�q1���h$*��V^�k��QĽ�b:i������� ����H F#�_���,��]g����Ը����`RUU���� ��"�qD���>-��ϤI�~��3�̴�����NZ[[555�eeek7�m����YF#[��Đ��R��i��L�H�r ��@��J-�Pŝ��_|�-��6y��ks�V�U~��6���w.����(��0A� 4��f��� /���C�xŊ�ٳ����z=6�����rg���mKo��o�/����a�g���?'9%9��P��斓��~���L{�� �H��{AW��B��$����v�1b�&Mb�ر��ر#�0`�`xMBp! )Z�Nmo�mZ�g�}�eذaWTVV ���'99��C5��&�&+5�bT>|8��PUUETT����&���t��~TE%��'������b�tEGG;RSS�S�N�v��ѯ��t����u1@�ޚ"f��͈���r'L�Ph��m��ƌ�74o��`�x�q�P���NRSS�X,�����l&&&��MFFA!-��`��ҷ�n��Yuu�����x<I�LB%����KԒ50�!�3�bz�����ʸKL���;---�l6[��`��������Ȏ;�����egg4��!D�T��jWujyZ��������K����%����p�^���X,6��sV=�ܖ����6�3�X ���4ҿ��A2�&����_k�;d�� �VfOIEND�B`�
Warning: session_start(): Session cannot be started after headers have already been sent in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 124

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 130

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 131

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 132

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 133

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 134

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 135
0x0x
PHP 8.2.31
Preview: ResolverHelpers.php Size: 19.25 KB
/home/mciraet/www/wp-content/plugins/woocommerce/src/Api/Infrastructure/ResolverHelpers.php
<?php

declare(strict_types=1);

namespace Automattic\WooCommerce\Api\Infrastructure;

use Automattic\WooCommerce\Api\Infrastructure\Schema\Error;

/**
 * Shared utilities for the auto-generated GraphQL resolvers.
 *
 * The public surface uses only {@see Schema\Error} (a stable subclass of the
 * engine's Error) on throws/returns so generated code never imports an
 * engine-specific symbol — a future engine switch can rewrite the bodies
 * here without invalidating already-committed plugin trees.
 */
class ResolverHelpers {
	/**
	 * Compute the complexity cost of a paginated connection field.
	 *
	 * Used as the `complexity` callable on every generated resolver field
	 * that returns a `Connection`. Runs during query validation (before
	 * resolver execution, so before `PaginationParams::validate_args()` has
	 * a chance to reject bad input) — so out-of-range / wrong-type values
	 * are clamped to MAX_PAGE_SIZE here. Using MAX_PAGE_SIZE as the
	 * fallback means a malicious attempt to shrink cost via e.g. a
	 * negative `first` value only inflates the computed complexity,
	 * closing the cost-bypass angle.
	 *
	 * @param int   $child_complexity The complexity of a single child node.
	 * @param array $args             The field arguments (expects `first` / `last`).
	 *
	 * @return int The total complexity for this connection field.
	 */
	public static function complexity_from_pagination( int $child_complexity, array $args ): int {
		$requested = $args['first'] ?? $args['last'] ?? \Automattic\WooCommerce\Api\Pagination\PaginationParams::get_default_page_size();
		$page_size = ( is_int( $requested ) && $requested >= 0 && $requested <= \Automattic\WooCommerce\Api\Pagination\PaginationParams::MAX_PAGE_SIZE )
			? $requested
			: \Automattic\WooCommerce\Api\Pagination\PaginationParams::MAX_PAGE_SIZE;
		return $page_size * ( $child_complexity + 1 );
	}

	/**
	 * Build a PaginationParams instance from the standard GraphQL pagination
	 * arguments (first, last, after, before).
	 *
	 * @param array $args The GraphQL field arguments.
	 *
	 * @return \Automattic\WooCommerce\Api\Pagination\PaginationParams
	 * @throws Error When a pagination value is out of range.
	 */
	public static function create_pagination_params( array $args ): \Automattic\WooCommerce\Api\Pagination\PaginationParams {
		return self::create_input(
			fn() => new \Automattic\WooCommerce\Api\Pagination\PaginationParams(
				first: $args['first'] ?? null,
				last: $args['last'] ?? null,
				after: $args['after'] ?? null,
				before: $args['before'] ?? null,
			)
		);
	}

	/**
	 * Invoke a factory callable, catching InvalidArgumentException and
	 * converting it to a client-visible GraphQL error.
	 *
	 * Used to wrap construction of unrolled input types (PaginationParams,
	 * ProductFilterInput, etc.) whose constructors may validate their
	 * arguments and throw.
	 *
	 * @param callable $factory A callable that returns the constructed object.
	 *
	 * @return mixed The return value of the factory.
	 * @throws Error When the factory throws InvalidArgumentException.
	 */
	public static function create_input( callable $factory ): mixed {
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Not HTML; serialized as JSON.
		try {
			return $factory();
		} catch ( \InvalidArgumentException $e ) {
			throw new Error(
				$e->getMessage(),
				extensions: array( 'code' => 'INVALID_ARGUMENT' )
			);
		}
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	/**
	 * Execute a command's execute() method, translating any thrown exceptions
	 * into spec-compliant GraphQL errors.
	 *
	 * @param object $command      The command instance (must have an execute() method).
	 * @param array  $execute_args Named arguments to pass to execute().
	 *
	 * @return mixed The return value of execute().
	 * @throws Error On any exception from the command.
	 */
	public static function execute_command( object $command, array $execute_args ): mixed {
		return self::translate_exceptions(
			static fn() => $command->execute( ...$execute_args )
		);
	}

	/**
	 * Invoke a command's authorize() method, translating any thrown exceptions
	 * into spec-compliant GraphQL errors.
	 *
	 * Mirror of execute_command() for the authorize step. Needed because an
	 * authorize() call can throw an ApiException (e.g. UnauthorizedException
	 * when a target record does not exist); without this wrapper the
	 * exception would propagate up to the engine and lose its error code and
	 * user-visible message on its way through the generic error formatter.
	 *
	 * @param object $command        The command instance (must have an authorize() method).
	 * @param array  $authorize_args Named arguments to pass to authorize().
	 *
	 * @return bool The return value of authorize().
	 * @throws Error On any exception from the authorize method.
	 */
	public static function authorize_command( object $command, array $authorize_args ): bool {
		return self::translate_exceptions(
			static fn() => $command->authorize( ...$authorize_args )
		);
	}

	/**
	 * Build the GraphQL error to throw when an authorization check fails.
	 *
	 * Distinguishes the two HTTP-correct shapes:
	 *  - **UNAUTHORIZED (401)** when the principal is anonymous — the caller
	 *    could plausibly fix it by authenticating, so the response invites
	 *    re-auth.
	 *  - **FORBIDDEN (403)** otherwise — the principal is recognised but
	 *    isn't allowed; re-authenticating wouldn't help.
	 *
	 * The "anonymous" check is opt-in by convention: the principal's
	 * `is_authenticated(): bool` method, when present, decides. Principals
	 * that don't define it fall through to FORBIDDEN — generated resolvers
	 * still emit a coded error, just without the 401/403 distinction.
	 *
	 * Used for class-level denials (operation-level "you cannot call this
	 * query/mutation"). For field-level denials that should carry a
	 * structured `subject` payload (type / field / attribute), see
	 * {@see self::build_field_authorization_error()}.
	 *
	 * @param object $principal The resolved request principal.
	 */
	public static function build_authorization_error( object $principal ): Error {
		$is_anonymous = method_exists( $principal, 'is_authenticated' ) && ! $principal->is_authenticated();
		return new Error(
			$is_anonymous ? 'Authentication required.' : 'You do not have permission to perform this action.',
			extensions: array( 'code' => $is_anonymous ? 'UNAUTHORIZED' : 'FORBIDDEN' )
		);
	}

	/**
	 * Like {@see self::build_authorization_error()} but carries a structured
	 * `subject` payload identifying *what* was denied — the enclosing type,
	 * the field (when applicable), and the attribute class name driving the
	 * decision. Clients can branch on `extensions.subject.field` to tell a
	 * field-level deny apart from an operation-level one.
	 *
	 * The error code (UNAUTHORIZED / FORBIDDEN) is preserved verbatim so
	 * existing client handlers continue to work; the subject payload is
	 * additive.
	 *
	 * @param object  $principal       The resolved request principal.
	 * @param string  $type            GraphQL type name carrying the gate.
	 * @param ?string $field           Field name when the deny is field-level; null for type/operation-level denies.
	 * @param string  $attribute_short Short class name of the deciding authorization attribute (no namespace).
	 */
	public static function build_field_authorization_error( object $principal, string $type, ?string $field, string $attribute_short ): Error {
		$is_anonymous = method_exists( $principal, 'is_authenticated' ) && ! $principal->is_authenticated();
		$subject      = array(
			'type'      => $type,
			'attribute' => $attribute_short,
		);
		if ( null !== $field ) {
			$subject['field'] = $field;
		}
		return new Error(
			$is_anonymous ? 'Authentication required.' : 'You do not have permission to perform this action.',
			extensions: array(
				'code'    => $is_anonymous ? 'UNAUTHORIZED' : 'FORBIDDEN',
				'subject' => $subject,
			)
		);
	}

	/**
	 * Compute the value `_preauthorized` would carry for the given command and
	 * principal (the AND of the autodiscovered authorization attributes'
	 * authorize() outcomes).
	 *
	 * Lets code-API callers (and tests) ask "would this command's attribute-based
	 * authorization grant access to this principal?" without going through the
	 * GraphQL pipeline.
	 *
	 * Note that it returns true when the command has no authorization attributes
	 * (in that case the command's own `authorize()` method, if any, is the sole
	 * guard; and consulting it requires running the command, which this helper
	 * deliberately doesn't do).
	 *
	 * Note: this provides the attribute-level authorization only. A command with
	 * both attributes and an `authorize()` method composes the two via the
	 * `_preauthorized` infrastructure parameter; this helper returns the value
	 * that `_preauthorized` would carry, not the final `authorize()` outcome.
	 *
	 * Scope is class-level (queries / mutations). Field-level authorization
	 * lives on output-type / input-type properties and is enforced inside
	 * the generated resolvers. To inspect a field's declared authorization
	 * from code, walk {@see \Automattic\WooCommerce\Api\Utils\SchemaHandle::find_metadata()}
	 * and read the `authorization` slice on each row.
	 *
	 * @param string $command_fqcn Fully-qualified command class name.
	 * @param object $principal    The resolved principal. Anonymous requests are represented by a sentinel principal (e.g. {@see \Automattic\WooCommerce\Api\Infrastructure\Principal} whose underlying WP_User has ID=0), not by null.
	 *
	 * @throws \InvalidArgumentException When `$command_fqcn` does not name an existing class.
	 */
	public static function compute_preauthorized( string $command_fqcn, object $principal ): bool {
		if ( ! class_exists( $command_fqcn ) ) {
			throw new \InvalidArgumentException(
				sprintf( 'Class %s does not exist.', esc_html( $command_fqcn ) )
			);
		}
		$ref    = new \ReflectionClass( $command_fqcn );
		$direct = self::collect_authorization_instances( $ref );
		$usages = $direct;
		if ( empty( $usages ) ) {
			// No direct attribute — collect from the entire ancestor tree:
			// the parent chain plus each ancestor's traits and interfaces
			// (recursively). All inherited sources contribute as peers; the
			// only thing direct attributes shadow is the inherited tree as a
			// whole. Mirrors
			// {@see \Automattic\WooCommerce\Api\Infrastructure\DesignTime\ApiBuilder::resolve_authorization()}.
			$visited = array();
			$stack   = array_merge(
				$ref->getParentClass() ? array( $ref->getParentClass() ) : array(),
				$ref->getTraits(),
				$ref->getInterfaces(),
			);
			while ( ! empty( $stack ) ) {
				$source = array_shift( $stack );
				$name   = $source->getName();
				if ( in_array( $name, $visited, true ) ) {
					continue;
				}
				$visited[] = $name;
				$usages    = array_merge( $usages, self::collect_authorization_instances( $source ) );
				if ( false !== $source->getParentClass() ) {
					$stack[] = $source->getParentClass();
				}
				$stack = array_merge( $stack, $source->getTraits(), $source->getInterfaces() );
			}
		}

		$query_metadata = self::harvest_class_metadata( $ref );

		foreach ( $usages as $instance ) {
			$auth_method = new \ReflectionMethod( $instance, 'authorize' );
			$call_args   = self::build_authorize_call_args(
				$auth_method,
				$principal,
				array( 'query' => $query_metadata ),
				array(),
				null
			);
			$result      = $instance->authorize( ...$call_args );
			if ( ! $result ) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Mirror of `ApiBuilder::harvest_metadata()` for the runtime path. Walks
	 * {@see \Automattic\WooCommerce\Api\Attributes\Metadata}-subclass attributes
	 * on a class reflector and returns `name => value`. Duplicate names are
	 * resolved last-wins — the build-time validator already errors on
	 * duplicates, so this is only relevant for in-process classes that
	 * never went through a build.
	 *
	 * The per-target `_apiMetadata` opt-out (`shows_in_metadata_query()`)
	 * is not applied here: the `$_metadata` slot threaded into a class-
	 * level attribute's `authorize()` is for policy input, not discovery,
	 * so attribute authors see every entry regardless of how it surfaces
	 * through `_apiMetadata`.
	 *
	 * @param \ReflectionClass $ref The class to read metadata from.
	 * @return array<string, bool|int|float|string|null>
	 */
	private static function harvest_class_metadata( \ReflectionClass $ref ): array {
		$entries = array();
		foreach ( $ref->getAttributes( \Automattic\WooCommerce\Api\Attributes\Metadata::class, \ReflectionAttribute::IS_INSTANCEOF ) as $attribute ) {
			$instance                         = $attribute->newInstance();
			$entries[ $instance->get_name() ] = $instance->get_value();
		}
		return $entries;
	}

	/**
	 * Build the positional/named argument list for an attribute's `authorize()`
	 * method based on which opt-in slots its signature declares.
	 *
	 * The principal is always passed first (positionally) when the method
	 * declares a non-`_`-prefixed parameter; infrastructure parameters
	 * (`$_metadata`, `$_args`, `$_parent`) are passed as named arguments so
	 * the attribute can omit any subset without affecting the call shape.
	 *
	 * @param \ReflectionMethod $method    The attribute's `authorize()` method.
	 * @param object            $principal The resolved principal to pass when the method takes one.
	 * @param array             $metadata  Value for `$_metadata` (passed if the method declares it).
	 * @param array             $args      Value for `$_args` (passed if the method declares it).
	 * @param mixed             $parent    Value for `$_parent` (passed if the method declares it).
	 *
	 * @return array<int|string, mixed> Positional principal first (if any), then named infra slots. Use with `...` spread.
	 */
	private static function build_authorize_call_args( \ReflectionMethod $method, object $principal, array $metadata, array $args, mixed $parent ): array {
		$call_args = array();
		foreach ( $method->getParameters() as $param ) {
			$name = $param->getName();
			if ( '_metadata' === $name ) {
				$call_args['_metadata'] = $metadata;
			} elseif ( '_args' === $name ) {
				$call_args['_args'] = $args;
			} elseif ( '_parent' === $name ) {
				$call_args['_parent'] = $parent;
			} elseif ( '' === $name || '_' !== $name[0] ) {
				// Principal — positional, must be the first entry in the spread.
				array_unshift( $call_args, $principal );
			}
		}
		return $call_args;
	}

	/**
	 * Collect attribute instances declared on $source whose class declares an
	 * authorization-shaped `authorize()` method.
	 *
	 * Mirrors {@see \Automattic\WooCommerce\Api\Infrastructure\DesignTime\ApiBuilder::collect_authorization_usages()}
	 * for the runtime path: same direct-then-inherited precedence, same
	 * "any class with a bool-returning authorize() method qualifies" rule.
	 *
	 * @param \ReflectionClass $source Class/trait/interface to read attributes from.
	 *
	 * @return array<int, object>
	 */
	private static function collect_authorization_instances( \ReflectionClass $source ): array {
		$instances = array();
		foreach ( $source->getAttributes() as $attr ) {
			$name = $attr->getName();
			if ( ! class_exists( $name ) || ! method_exists( $name, 'authorize' ) ) {
				continue;
			}
			$method = new \ReflectionMethod( $name, 'authorize' );
			if ( ! self::authorize_method_shape_is_valid( $method ) ) {
				continue;
			}
			$instances[] = $attr->newInstance();
		}
		return $instances;
	}

	/**
	 * Whether a method's shape matches the authorization-attribute contract:
	 * public, non-static, returns bool, and parameters drawn from the accepted
	 * set — at most one principal (any non-`_`-prefixed name, non-nullable
	 * typed) plus any subset of `$_metadata` (array), `$_args` (array), and
	 * `$_parent` (any type).
	 *
	 * Mirrors the build-time `ApiBuilder::validate_attribute_authorize_shape()`
	 * check so the runtime helper recognises the same set of attributes ApiBuilder
	 * would have emitted into a resolver.
	 *
	 * @param \ReflectionMethod $method The method to inspect.
	 */
	private static function authorize_method_shape_is_valid( \ReflectionMethod $method ): bool {
		if ( $method->isStatic() || ! $method->isPublic() ) {
			return false;
		}
		$return_type = $method->getReturnType();
		if ( ! $return_type instanceof \ReflectionNamedType || 'bool' !== $return_type->getName() ) {
			return false;
		}

		$principal_seen = false;
		foreach ( $method->getParameters() as $param ) {
			$name = $param->getName();
			if ( '_metadata' === $name || '_args' === $name ) {
				$type = $param->getType();
				if ( ! $type instanceof \ReflectionNamedType || 'array' !== $type->getName() ) {
					return false;
				}
				continue;
			}
			if ( '_parent' === $name ) {
				continue;
			}
			if ( '' !== $name && '_' === $name[0] ) {
				// Unknown infra parameter — reject.
				return false;
			}
			if ( $principal_seen ) {
				return false;
			}
			$type = $param->getType();
			if ( ! $type instanceof \ReflectionNamedType || $type->allowsNull() ) {
				return false;
			}
			$principal_seen = true;
		}
		return true;
	}

	/**
	 * Invoke a callable, translating any thrown exception into a
	 * spec-compliant GraphQL error with a machine-readable code.
	 *
	 * - ApiException       → its own code + extensions, with the original message.
	 * - InvalidArgumentException → INVALID_ARGUMENT, with the original message.
	 * - Any other Throwable     → INTERNAL_ERROR, with a generic message; the
	 *   original throwable is attached as `previous` for debug-mode surfacing.
	 *
	 * Public so that generated resolvers can wrap Code-API calls that happen
	 * outside the execute()/authorize() pair (e.g. the Connection::slice()
	 * call emitted for nested paginated connection fields, which can throw
	 * InvalidArgumentException when pagination bounds are exceeded).
	 *
	 * @param callable $operation Callable to invoke.
	 *
	 * @return mixed The return value of the callable.
	 * @throws Error On any exception from the callable.
	 */
	public static function translate_exceptions( callable $operation ): mixed {
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Not HTML; serialized as JSON.
		try {
			return $operation();
		} catch ( \Automattic\WooCommerce\Api\ApiException $e ) {
			// Caller-supplied extensions come first so the canonical
			// getErrorCode() can't be silently overridden by an extensions
			// entry keyed 'code'. The invariant "the code on the wire
			// equals ApiException::getErrorCode()" is worth enforcing.
			throw new Error(
				$e->getMessage(),
				extensions: array_merge(
					$e->getExtensions(),
					array( 'code' => $e->getErrorCode() )
				)
			);
		} catch ( \InvalidArgumentException $e ) {
			throw new Error(
				$e->getMessage(),
				extensions: array( 'code' => 'INVALID_ARGUMENT' )
			);
		} catch ( \Throwable $e ) {
			throw new Error(
				'An unexpected error occurred.',
				previous: $e,
				extensions: array( 'code' => 'INTERNAL_ERROR' )
			);
		}//end try
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	/**
	 * Lazy-initialize and return the WP_Filesystem global, or null when the
	 * direct method isn't available (e.g. credentials prompt would be needed).
	 */
	public static function wp_filesystem(): ?\WP_Filesystem_Base {
		global $wp_filesystem;
		if ( ! $wp_filesystem ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			if ( ! WP_Filesystem() ) {
				return null;
			}
		}
		return $wp_filesystem;
	}
}

Directory Contents

Dirs: 1 × Files: 8
Name Size Perms Modified Actions
Schema DIR
- drwxr-xr-x 2026-08-11 10:05:47
Edit Download
945 B lrw-r--r-- 2026-08-11 10:05:47
Edit Download
41.99 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
15.65 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
15.63 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
2.43 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
1.12 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
7.35 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
19.25 KB lrw-r--r-- 2026-08-11 10:05:47
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 0xNothings — Secure File Manager. All rights reserved. Built with ❤️ & Tailwind x Dark UI