string, 'value' => string ), empty when absent or invalid. */ private static function read_fields() { if ( empty( $_SERVER[ self::HEADER_FIELDS ] ) ) { return array(); } // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- a transport encoding for non-ASCII names, not obfuscation. $json = base64_decode( sanitize_text_field( wp_unslash( $_SERVER[ self::HEADER_FIELDS ] ) ), true ); $list = false === $json ? null : json_decode( $json, true ); if ( ! is_array( $list ) || count( $list ) < 1 || count( $list ) > 10 ) { return array(); } $out = array(); $seen = array(); foreach ( $list as $f ) { if ( ! is_array( $f ) || ! isset( $f['name'], $f['value'] ) || ! is_string( $f['name'] ) || ! is_string( $f['value'] ) ) { return array(); } $name = sanitize_text_field( trim( $f['name'] ) ); $value = sanitize_text_field( trim( $f['value'] ) ); $key = mb_strtolower( $name ); $nlen = mb_strlen( $name ); $vlen = mb_strlen( $value ); if ( $nlen < 1 || $nlen > 50 || $vlen < 1 || $vlen > 250 || '_codexta' === $key || isset( $seen[ $key ] ) ) { return array(); } $seen[ $key ] = true; $out[] = array( 'name' => $name, 'value' => $value, ); } return $out; } /** * Show the visible fields (names not starting with _) on the cart line, so the Store API item_data and the cart page carry them. * * @param array $item_data Display rows. * @param array $cart_item The cart line. * @return array */ public static function show_fields( $item_data, $cart_item ) { foreach ( empty( $cart_item['codexta_fields'] ) ? array() : $cart_item['codexta_fields'] as $f ) { if ( 0 !== strpos( $f['name'], '_' ) ) { $item_data[] = array( 'key' => $f['name'], 'value' => $f['value'], ); } } return $item_data; } /** * Write the mark on the order's line item. * * @param WC_Order_Item_Product $item The order line. * @param string $cart_item_key Cart line key (unused). * @param array $values The cart line. */ public static function copy_to_order( $item, $cart_item_key, $values ) { unset( $cart_item_key ); if ( ! empty( $values['codexta_chat'] ) ) { $item->add_meta_data( self::META, $values['codexta_chat'], true ); } foreach ( empty( $values['codexta_fields'] ) ? array() : $values['codexta_fields'] as $f ) { $item->add_meta_data( $f['name'], $f['value'], true ); } } }