Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'elementor_pro/forms/process', function( $record, $ajax_handler ) {
- $form_settings = $record->get( 'form_settings' );
- $fields = $record->get( 'fields' );
- if ( empty( $form_settings['form_fields'] ) || empty( $fields ) ) {
- return;
- }
- // Pro snadnější dohledání nastavení pole si připravíme mapu: field_id => settings
- $settings_map = [];
- foreach ( $form_settings['form_fields'] as $f ) {
- $fid = isset( $f['custom_id'] ) && $f['custom_id'] !== '' ? $f['custom_id'] : $f['_id'];
- $settings_map[ $fid ] = $f;
- }
- foreach ( $fields as $field_id => $field ) {
- // Zajímá nás jen checkbox, select a radio
- if ( ! isset( $field['type'] ) ) continue;
- if ( ! in_array( $field['type'], [ 'checkbox', 'select', 'radio' ], true ) ) continue;
- // Najdi odpovídající nastavení pole
- if ( ! isset( $settings_map[ $field['id'] ] ) ) continue;
- $current_settings = $settings_map[ $field['id'] ];
- // Z pole "Options" slož mapu value => label
- if ( empty( $current_settings['field_options'] ) ) continue;
- $options_lines = preg_split( "/\r\n|\r|\n/", $current_settings['field_options'] );
- if ( ! $options_lines ) continue;
- $value_to_label = [];
- foreach ( $options_lines as $line ) {
- $line = trim( $line );
- if ( $line === '' ) continue;
- if ( strpos( $line, '|' ) === false ) {
- // Pokud není pipe, ber label == value
- $label = $line;
- $value = $line;
- } else {
- list( $label, $value ) = array_map( 'trim', explode( '|', $line, 2 ) );
- }
- // Prosákne i numerické stringy jako "1500"
- $value_to_label[ (string) $value ] = $label;
- }
- // Převod raw_value na label nebo seznam labelů
- $raw = isset( $field['raw_value'] ) ? $field['raw_value'] : $field['value'];
- if ( is_array( $raw ) ) {
- // Checkboxy vrací pole
- $labels = [];
- foreach ( $raw as $v ) {
- $v = (string) $v;
- $labels[] = isset( $value_to_label[ $v ] ) ? $value_to_label[ $v ] : $v;
- }
- $new_value = implode( ', ', $labels ); // klidně změň na "\n" pokud chceš každý na nový řádek
- } else {
- $v = (string) $raw;
- $new_value = isset( $value_to_label[ $v ] ) ? $value_to_label[ $v ] : $v;
- }
- // Přepiš hodnotu pole, kterou Elementor používá v e-mailech a záznamech
- $record->update_field( $field['id'], 'value', $new_value );
- }
- }, 20, 2 );
Advertisement
Add Comment
Please, Sign In to add comment