Untitled

mail@pastecode.io avatar
unknown
plain_text
3 days ago
3.9 kB
2
Indexable
Never
if(!function_exists('log_it')){
    function log_it( $message ) {
        if( WP_DEBUG === true ){
            if( is_array( $message ) || is_object( $message ) ){
                error_log( print_r( $message, true ) );
            } else {
                error_log( $message );
            }
        }
    }
}

//Fires after datas are saved
//add_action('acf/save_post', 'cpt_store_to_amelia_emplacement');
add_action('save_post', 'cpt_store_to_amelia_emplacement',10,3);

function cpt_store_to_amelia_emplacement( $post_id ) {
    
    $post_type = get_post_type($post_id);

    if( 'magasins' == $post_type ){

        $ameliaID = get_field('id_amelia_location', $post_id);
        
        if(empty($ameliaID)){
        //If it's a new location*/
            
            $ameliaEmplacementID = addUpdateAmeliaEmplacement($post_id, false);

            log_it("Amelia emplacement created :" . $ameliaEmplacementID);

            if(!empty($ameliaEmplacementID) && $ameliaEmplacementID != "false"){
                update_field('id_amelia_location', $ameliaEmplacementID, $post_id);
            }

        }else{
        //If it's an updated location

            addUpdateAmeliaEmplacement($post_id, true);

        }
    }
}

function addUpdateAmeliaEmplacement($post_id, $update){

    $curl = curl_init();

    //PREPARING DATAS
    $datas = array(
        'name' => get_the_title($post_id)
    );

    if($update){
        $location_id = get_field('id_amelia_location', $post_id);

        $prise_rdv = get_field('actif_rdv', $post_id);
        $datas['status'] = $prise_rdv ? 'visible' : 'hidden';
    }

    $datas['description'] = get_field('presentation', $post_id) ? get_field('presentation', $post_id) : '';

    if(get_field('adresse', $post_id)['rue']){
        $address = get_field('adresse', $post_id);
        $address_string = $address['rue'].' '.$address['code_postal'].' '.$address['ville'];

        if($address['complement_adresse']){
            $address_string = $address['complement_adresse'].', '.$address_string;
        }

        $datas['address'] = $address_string;
    }

    $datas['phone'] = get_field('no_tel', $post_id) ? get_field('no_tel', $post_id) : '';

    $coords = get_field('coordonnees_geographiques', $post_id);
    $datas['latitude'] = $coords['latitude'] ? $coords['latitude'] : '';
    $datas['longitude'] = $coords['longitude'] ? $coords['longitude'] : '';

    // BUILD cURL OPTIONS
    $opt_URL = "";

    if($update){
        $opt_URL = get_site_url() . '/wp-admin/admin-ajax.php?action=wpamelia_api&call=/api/v1/locations/' . $location_id;
    }else{
        $opt_URL = get_site_url() . '/wp-admin/admin-ajax.php?action=wpamelia_api&call=/api/v1/locations';
    }

    $options = array(
        CURLOPT_URL => $opt_URL,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_FAILONERROR => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => json_encode($datas),
        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/json',
          'Amelia: ' . AMELIA_API_SECRET_KEY
        ),
    );

    log_it($datas);

    curl_setopt_array($curl, $options);

    $response = curl_exec($curl);

    $curl_errno = curl_errno($curl);
    $curl_error = curl_error($curl);

    curl_close($curl);

    log_it($response);


    // Return
    if ($curl_errno > 0) {
        log_it("cURL Error ($curl_errno): $curl_error <br/>");
        return "";
    } else {
        $data = json_decode($response);
        
        if(!empty($data->data)){
            return $data->data->location->id;
        }else{
            return "false";
        }
    }
}
Leave a Comment