array( 'admin_language' => 2, 'default_document_language' => 2, 'enabled_customer_languages' => 'list', 'initialize_language_defaults' => 'bool', ), 2 => array( 'legal_name' => 512, 'registered_name' => 512, 'registration_number' => 64, 'registration_authority' => 512, 'tax_id' => 64, 'vat_registered' => 'bool', 'vat_number' => 64, 'address' => 2048, 'city' => 256, 'postcode' => 32, 'phone' => 64, 'email' => 254, 'site' => 2048, 'establishment' => 4, ), 3 => array( 'distance_sale_registration_confirmed' => 'bool', 'physical_goods_scope_confirmed' => 'bool', 'complaints_email' => 254, 'complaints_phone' => 64, 'complaints_contact' => 8192, 'returns_same_as_complaints' => 'bool', 'returns_email' => 254, 'returns_phone' => 64, 'returns_address' => 2048, 'returns_contact' => 8192, 'returns_use_merchant_address' => 'bool', 'returns_recipient' => 512, 'returns_street' => 2048, 'returns_postcode' => 32, 'returns_city' => 256, 'returns_country' => 256, 'offer_validity_configured' => 'bool', 'warranty_information_configured' => 'bool', 'sells_across_bih' => 'bool', 'supplier_relationship' => 32, 'separate_supplier_name' => 512, 'separate_supplier_email' => 254, ), 4 => array( 'terms_page_id' => 'id', 'contract_formation_mode' => 32, 'contract_formation_status' => 64, 'contract_formation_provider' => 128, 'order_process_information_confirmed' => 'bool', 'terms_accessible_saveable_confirmed' => 'bool', 'rs_payment_obligation_confirmed' => 'bool', ), 5 => array( 'completed_means_dispatched' => 'bool', 'customer_email_id' => 128, 'payment_delivery_information_confirmed' => 'bool', ), 6 => array( 'basic_pdf_enabled' => 'bool', 'basic_pdf_email_id' => 128, ), ); public static function boolean( $value ): bool { return in_array( $value, array( true, 1, '1', 'on', 'true', 'yes' ), true ); } /** Select known fields before unslashing; unknown subtrees are never traversed. */ public static function normalize( $input, int $step, array $settings = array() ): array { if ( ! is_array( $input ) || empty( $input ) || count( $input ) > 64 || ! isset( self::FIELDS[ $step ] ) ) { throw new \InvalidArgumentException( 'Invalid setup payload.' ); } $old = array(); $result = array(); foreach ( array( 'languages', 'merchant', 'sales', 'terms', 'delivery', 'documents' ) as $bucket ) { $old = array_replace( $old, (array) ( $settings[ $bucket ] ?? array() ) ); } foreach ( self::FIELDS[ $step ] as $key => $bound ) { if ( ! array_key_exists( $key, $input ) ) { continue; } $value = $input[ $key ]; if ( 'list' === $bound ) { if ( ! is_array( $value ) || count( $value ) > 4 || ! array_is_list( $value ) ) { throw new \InvalidArgumentException( 'Invalid language list.' ); } foreach ( $value as $code ) { if ( ! is_string( $code ) || ! in_array( $code, LanguageCatalog::supported_codes(), true ) ) { throw new \InvalidArgumentException( 'Invalid language.' ); } } $result[ $key ] = array_values( array_unique( $value ) ); continue; } if ( 'bool' === $bound ) { if ( ! in_array( $value, array( true, false, 1, 0, '1', '0', 'on', 'off', 'true', 'false', 'yes', 'no', '' ), true ) ) { throw new \InvalidArgumentException( 'Invalid checkbox value.' ); } $result[ $key ] = self::boolean( $value ); continue; } if ( ! is_string( $value ) || false !== strpos( $value, "\0" ) || 1 !== preg_match( '//u', $value ) ) { throw new \InvalidArgumentException( 'Invalid scalar value.' ); } $value = wp_unslash( $value ); $enums = array( 'admin_language' => LanguageCatalog::supported_codes(), 'default_document_language' => LanguageCatalog::supported_codes(), 'establishment' => array( '', 'FBIH', 'RS', 'BD' ), 'supplier_relationship' => array( '', 'SAME_AS_MERCHANT', 'SEPARATE_CONTRACT_SUPPLIER' ), 'contract_formation_mode' => array( '', 'ORDER_CREATED', 'PAYMENT_COMPLETED', 'ORDER_STATUS', 'CUSTOM_PROVIDER' ), ); if ( isset( $enums[ $key ] ) && ! in_array( $value, $enums[ $key ], true ) ) { throw new \InvalidArgumentException( 'Invalid setup enum.' ); } if ( 'id' === $bound ) { if ( '' !== $value && null === \Leanmerce\CommerceCompliance\Infrastructure\WordPress\RequestGuard::positive_id( $value ) ) { throw new \InvalidArgumentException( 'Invalid page ID.' ); } } elseif ( strlen( $value ) > $bound && ( $old[ $key ] ?? null ) !== $value ) { throw new \InvalidArgumentException( 'Setup input exceeds its byte limit.' ); } $result[ $key ] = $value; } if ( empty( $result ) ) { throw new \InvalidArgumentException( 'No known setup fields.' ); } return $result; } private function __construct() {} }