Automattic\WooCommerce\Internal\Admin\Suggestions
PaymentExtensionSuggestions::with_country_details() │ private │ WC 1.0
Merges country-specific details into the base details of a payment extension.
This function processes special _append, _remove, and _merge_on_type instructions to modify list-type entries within the base details.
Method of the class: PaymentExtensionSuggestions{}
No Hooks.
Return
Array
. The merged details, with country-specific modifications applied.
Usage
// private - for code of main (parent) class only
$result = $this->with_country_details( $base_details, $country_details ): array;
- $base_details(array) (required)
- The base details of the payment extension.
- $country_details(array) (required)
- The country-specific details, which may include special _append and _remove instructions.
PaymentExtensionSuggestions::with_country_details() PaymentExtensionSuggestions::with country details code
WC 9.6.1
private function with_country_details( array $base_details, array $country_details ): array {
// Process any append instructions.
if ( isset( $country_details['_append'] ) ) {
if ( ! is_array( $country_details['_append'] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _append entry.' );
}
foreach ( $country_details['_append'] as $append_key => $append_list ) {
// Sanity checks.
if ( ! is_string( $append_key ) ||
! is_array( $append_list ) ||
! ArrayUtil::array_is_list( $append_list )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _append details.' );
}
// If the target entry doesn't exist, create it as an empty list.
if ( ! isset( $base_details[ $append_key ] ) ) {
$base_details[ $append_key ] = array();
}
if ( ! is_array( $base_details[ $append_key ] ) ||
! ArrayUtil::array_is_list( $base_details[ $append_key ] )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Invalid country extension details _append target.' );
}
$base_details[ $append_key ] = array_merge( $base_details[ $append_key ], $append_list );
}
// Remove the special entry because we don't need it anymore.
unset( $country_details['_append'] );
}
// Process any remove instructions.
if ( isset( $country_details['_remove'] ) ) {
if ( ! is_array( $country_details['_remove'] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _remove entry.' );
}
foreach ( $country_details['_remove'] as $removal_key => $removal_list ) {
// Sanity checks.
if ( ! is_string( $removal_key ) ||
! is_array( $removal_list ) ||
! ArrayUtil::array_is_list( $removal_list )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _remove details.' );
}
if ( ! isset( $base_details[ $removal_key ] ) ) {
// If the target entry doesn't exist, we don't need to do anything.
continue;
}
if ( ! is_array( $base_details[ $removal_key ] ) ||
! ArrayUtil::array_is_list( $base_details[ $removal_key ] )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Invalid country extension details _remove target.' );
}
$base_details[ $removal_key ] = array_diff( $base_details[ $removal_key ], $removal_list );
}
// Remove the special entry because we don't need it anymore.
unset( $country_details['_remove'] );
}
// Process any merge on type instructions.
if ( isset( $country_details['_merge_on_type'] ) ) {
if ( ! is_array( $country_details['_merge_on_type'] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _merge_on_type entry.' );
}
foreach ( $country_details['_merge_on_type'] as $merge_key => $merge_list ) {
// Sanity checks.
if ( ! is_string( $merge_key ) ||
! is_array( $merge_list ) ||
! ArrayUtil::array_is_list( $merge_list ) ||
count( array_column( $merge_list, '_type' ) ) !== count( $merge_list )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Malformed country extension details _merge_on_type details.' );
}
if ( ! isset( $base_details[ $merge_key ] ) ) {
// If the target entry doesn't exist, create it.
$base_details[ $merge_key ] = array();
}
if ( ! is_array( $base_details[ $merge_key ] ) ||
! ArrayUtil::array_is_list( $base_details[ $merge_key ] ) ||
count( array_column( $base_details[ $merge_key ], '_type' ) ) !== count( $base_details[ $merge_key ] )
) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( 'Invalid country extension details _merge_on_type target.' );
}
// Merge the lists based on the '_type' values.
$base_details[ $merge_key ] = ArrayUtil::merge_by_key( $base_details[ $merge_key ], $merge_list, '_type' );
}
// Remove the special entry because we don't need it anymore.
unset( $country_details['_merge_on_type'] );
}
// Merge any remaining country details so they overwrite the base details.
return array_merge( $base_details, $country_details );
}