@charset "UTF-8";
/*
    Template: swell
    Theme Name: SWELL CHILD
    Theme URI: https://swell-theme.com/
    Description: SWELLの子テーマ
    Version: 1.0.0
    Author: LOOS WEB STUDIO
    Author URI: https://loos-web-studio.com/

    License: GNU General Public License
    License URI: http://www.gnu.org/licenses/gpl.html
*/






add_action( 'forminator_custom_form_submit_before_set_fields', function( $entry, $form_id, $field_data_array ) {
    if ( $form_id != 179 ) {
        return;
    }

    $gas_url = 'https://script.google.com/macros/s/AKfycbwz7I3lpZCujPPYv8njmQZVq8I_EiP_2dFkCLONMbu4cX9f98A4FeLwpAh0IcYYOKRsUw/exec';

    $raw_dump = array();
    $data = array(
        'ref'     => '',
        'name'    => '',
        'email'   => '',
        'phone'   => '',
        'address' => '',
        'file'    => '',
        'debug'   => '' // ここに全受信データを入れます
    );

    foreach ( $field_data_array as $field ) {
        $name = isset( $field['name'] ) ? $field['name'] : '';
        $val  = isset( $field['value'] ) ? $field['value'] : '';

        // デバッグ用に全フィールド名と値を記録
        $raw_dump[$name] = $val;

        // 添付ファイル
        if ( strpos( $name, 'upload' ) !== false ) {
            if ( is_array( $val ) ) {
                $data['file'] = isset( $val['file']['file_url'] ) ? $val['file']['file_url'] : ( isset( $val[0]['file']['file_url'] ) ? $val[0]['file']['file_url'] : json_encode( $val, JSON_UNESCAPED_UNICODE ) );
            } else {
                $data['file'] = (string)$val;
            }
            continue;
        }

        // 住所などの配列結合
        if ( is_array( $val ) ) {
            $val = implode( ' ', array_filter( array_map( 'strval', $val ) ) );
        }

        // 各フィールドの判定
        if ( strpos( $name, 'hidden' ) !== false || strpos( $name, 'ref' ) !== false ) {
            $data['ref'] = (string)$val;
        } elseif ( strpos( $name, 'name' ) !== false ) {
            $data['name'] = (string)$val;
        } elseif ( strpos( $name, 'email' ) !== false ) {
            $data['email'] = (string)$val;
        } elseif ( strpos( $name, 'phone' ) !== false ) {
            $data['phone'] = (string)$val;
        } elseif ( strpos( $name, 'address' ) !== false ) {
            $data['address'] = (string)$val;
        }
    }

    // 全生データをdebug列に入れる
    $data['debug'] = json_encode( $raw_dump, JSON_UNESCAPED_UNICODE );

    wp_remote_post( $gas_url, array(
        'method'      => 'POST',
        'timeout'     => 15,
        'headers'     => array( 'Content-Type' => 'application/json; charset=utf-8' ),
        'body'        => json_encode( $data ),
        'data_format' => 'body',
    ));
}, 10, 3 );







// Bogoの国旗アイコンを非表示にする
add_filter( 'bogo_use_flags', '__return_false' );

// 言語の表示名を「Jp」「En」に変更する
add_filter( 'bogo_language_switcher_links', function( $links ) {
    foreach ( $links as $key => $link ) {
        if ( $link['locale'] === 'ja' ) {
            $links[$key]['native_name'] = 'Jp';
        } elseif ( strpos( $link['locale'], 'en' ) === 0 ) {
            $links[$key]['native_name'] = 'En';
        }
    }
    return $links;
} );