*/ public static function types(): array { return array( 'text' => __( 'Text', 'aiolos-checkout-for-woocommerce' ), 'textarea' => __( 'Longer text', 'aiolos-checkout-for-woocommerce' ), 'checkbox' => __( 'Checkbox', 'aiolos-checkout-for-woocommerce' ), 'select' => __( 'Choice (dropdown)', 'aiolos-checkout-for-woocommerce' ), ); } /** * Where a field is asked. * * @return array */ public static function positions(): array { return array( 'details' => __( 'Details step, with the contact details', 'aiolos-checkout-for-woocommerce' ), 'notes' => __( 'With the order notes', 'aiolos-checkout-for-woocommerce' ), ); } /** * The saved fields. * * @return array */ public static function definitions(): array { return self::sanitize( Settings::get( self::OPTION_KEY, array() ) ); } /** * The checkout field key of a definition. * * @param array $def Definition. */ private static function field_key( array $def ): string { return 'aiolos_' . $def['key']; } /** * Adds the fields to WooCommerce's checkout fields. * * @param array $fields Checkout fields. * @return array */ public static function checkout_fields( $fields ) { if ( ! is_array( $fields ) ) { return $fields; } foreach ( self::definitions() as $index => $def ) { $args = array( 'type' => $def['type'], 'label' => $def['label'], 'required' => $def['required'], 'class' => array( 'form-row-wide', 'acw-custom-field' ), 'priority' => 200 + $index, ); if ( 'select' === $def['type'] ) { $args['options'] = array( '' => __( 'Choose…', 'aiolos-checkout-for-woocommerce' ) ) + array_combine( $def['options'], $def['options'] ); } $group = 'notes' === $def['position'] ? 'order' : 'billing'; $fields[ $group ][ self::field_key( $def ) ] = $args; } return $fields; } /** * Saves the answers on the order. * * @param \WC_Order $order Order. * @param array $data Posted data (sanitized by WooCommerce). */ public static function save( $order, $data ): void { if ( ! $order instanceof \WC_Order || ! is_array( $data ) ) { return; } foreach ( self::definitions() as $def ) { $value = $data[ self::field_key( $def ) ] ?? ''; if ( ! is_scalar( $value ) ) { continue; } $value = 'textarea' === $def['type'] ? sanitize_textarea_field( (string) $value ) : sanitize_text_field( (string) $value ); if ( 'checkbox' === $def['type'] ) { $value = $value ? '1' : ''; } elseif ( 'select' === $def['type'] && ! in_array( $value, $def['options'], true ) ) { $value = ''; } if ( '' !== $value ) { $order->update_meta_data( self::META . $def['key'], $value ); } } } /** * The answers of an order: label => text. * * @param \WC_Order $order Order. * @return array */ public static function answers( $order ): array { $out = array(); if ( ! $order instanceof \WC_Order ) { return $out; } foreach ( self::definitions() as $def ) { $value = (string) $order->get_meta( self::META . $def['key'] ); if ( '' !== $value ) { $out[ $def['label'] ] = 'checkbox' === $def['type'] ? __( 'Yes', 'aiolos-checkout-for-woocommerce' ) : $value; } } return $out; } /** * Shown on the admin order screen (classic and HPOS). * * @param \WC_Order $order Order. */ public static function admin( $order ): void { foreach ( self::answers( $order ) as $label => $value ) { echo '

' . esc_html( $label ) . ':
' . nl2br( esc_html( $value ) ) . '

'; } } /** * Added to the order emails. * * @param array $fields Fields. * @param bool $sent_to_admin Whether the email goes to the shop. * @param \WC_Order $order Order. * @return array */ public static function email( $fields, $sent_to_admin = false, $order = null ) { unset( $sent_to_admin ); $fields = is_array( $fields ) ? $fields : array(); if ( ! $order instanceof \WC_Order ) { return $fields; } $answers = self::answers( $order ); // Keyed by the field's own key: labels in Greek or Cyrillic all give the same sanitized key. foreach ( self::definitions() as $def ) { if ( isset( $answers[ $def['label'] ] ) && '' !== (string) $order->get_meta( self::META . $def['key'] ) ) { $fields[ 'aiolos_field_' . $def['key'] ] = array( 'label' => $def['label'], 'value' => $answers[ $def['label'] ], ); } } return $fields; } /** * Shown under the customer's addresses (thank-you page, My Account). * * @param \WC_Order $order Order. */ public static function customer_view( $order ): void { $answers = self::answers( $order ); if ( ! $answers ) { return; } echo '
'; foreach ( $answers as $label => $value ) { echo '
' . esc_html( $label ) . '
' . nl2br( esc_html( $value ) ) . '
'; } echo '
'; } /** * The editor: the saved fields plus two empty rows (no script needed). * * @param string $name Input name. * @param mixed $value Saved fields. */ public static function render( string $name, $value ): void { $rows = self::sanitize( $value ); $rows = array_pad( $rows, min( self::MAX, count( $rows ) + 2 ), array() ); ?> $row ) : ?>
aria-label="">
$try, 'label' => $label, 'type' => $type, 'options' => $options, 'required' => isset( $row['required'] ) && ( true === $row['required'] || 'yes' === $row['required'] ), 'position' => isset( $row['position'] ) && is_string( $row['position'] ) && isset( self::positions()[ $row['position'] ] ) ? $row['position'] : 'details', ); if ( count( $out ) >= self::MAX ) { break; } } return $out; } }