Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushTokenRestController::prepare_tokens_for_response │ private │ WC 1.0

Formats tokens for the index response.

The account fields are added here rather than in PushToken::to_rest_format(), so the users for a whole page are loaded in one query rather than one per token.

Requesting three named columns rather than user objects skips loading usermeta, and keeps WP_User_Query's result cache on.

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

Хуков нет.

Возвращает

array[].

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

// private - только в коде основоного (родительского) класса
$result = $this->prepare_tokens_for_response( $tokens ): array;
$tokens(PushToken[]) (обязательный)
The tokens to format.

Код PushTokenRestController::prepare_tokens_for_response() WC 11.3.0

private function prepare_tokens_for_response( array $tokens ): array {
	$user_ids = array_values(
		array_unique(
			array_filter(
				array_map( fn ( PushToken $token ) => $token->get_user_id(), $tokens )
			)
		)
	);

	if ( ! $user_ids ) {
		return array();
	}

	$users = array_column(
		get_users(
			array(
				'include' => $user_ids,
				'fields'  => array( 'ID', 'user_login', 'user_email' ),
			)
		),
		null,
		'ID'
	);

	return array_map(
		function ( PushToken $token ) use ( $users ) {
			$user = $users[ $token->get_user_id() ] ?? null;

			return array_merge(
				$token->to_rest_format(),
				array(
					'user_login' => $user ? $user->user_login : null,
					'user_email' => $user ? $user->user_email : null,
				)
			);
		},
		$tokens
	);
}