<?php
//###### ajax function for calls to app.datadriventool.com

//get facebook accounts
 if (!function_exists('ddt_auto_setup_facebook_get_accounts')) {
    function ddt_auto_setup_facebook_get_accounts(){

        if(!empty($_POST['key']) &&  !empty($_POST['id']) && !empty($_POST['apikey']) ):



    	   $key = sanitize_text_field( $_POST['key'] );
           $id= sanitize_text_field( $_POST['id'] );
           $api_key = sanitize_text_field( $_POST['apikey'] );

	     if( $key == 'ddtpixel' ){

                    $result = wp_remote_retrieve_body( wp_remote_get( "https://app.datadriventool.com/system/rest-api/wp-plugin-facebookintegration/fb_get_ad_accounts.php?ddt_wp_id=".$id."&apikey=".$api_key ) );
                    $result2 = json_decode(json_decode($result, true),true);
                   // echo json_encode($result2["data"]);
                    if(count($result2) == 2 && isset($result2['data']) && isset($result2['paging'])):
                       unset($result2["paging"]);
                        function ddt_sanitize_response($array){

                                for($i=0;$i<count($array["data"]);$i++){
                                    foreach ( $array["data"][$i] as $key => &$value ) {
                                            if ( is_array( $value ) ) {
                                                return false;
                                            }
                                            else {
                                                $value = sanitize_text_field($value);

                                            }
                                        }


                                }
                                return $array;
                        }
                             $output = ddt_sanitize_response($result2);
	                   if($output !== false): echo wp_json_encode($output); endif;
	               endif;
	     }

	    endif;


        die();

    }
    add_action( 'wp_ajax_' . 'ddt_facebook_pixel_api_get_account', 'ddt_auto_setup_facebook_get_accounts' );
    add_action( 'wp_ajax_nopriv_' . 'ddt_facebook_pixel_api_get_account', 'ddt_auto_setup_facebook_get_accounts' );
}

//get facebook pixel
 if (!function_exists('ddt_auto_setup_facebook_get_pixel')) {
    function ddt_auto_setup_facebook_get_pixel(){

	if(!empty($_POST['key']) && !empty($_POST['action']) && !empty($_POST['id']) && !empty($_POST['account']) && !empty($_POST['apikey'])):


	   $key = sanitize_text_field( $_POST['key'] );
       $id= sanitize_text_field( $_POST['id'] );
       $account = sanitize_text_field(  $_POST['account'] );
       $api_key = sanitize_text_field( $_POST['apikey']);

	     if( $key == 'ddtpixel'){

                    $result = wp_remote_retrieve_body( wp_remote_get( "https://app.datadriventool.com/system/rest-api/wp-plugin-facebookintegration/fb_get_pixel.php?ddt_wp_id=".$id."&account=".$account."&apikey=".$api_key ) );

	                 $result2 = json_decode(json_decode($result, true),true);
                   // echo json_encode($result2["data"]);
                    if(count($result2) == 2 && isset($result2['data']) && isset($result2['paging'])):
                       unset($result2["paging"]);
                        function ddt_sanitize_response($array){

                                for($i=0;$i<count($array["data"]);$i++){
                                    foreach ( $array["data"][$i] as $key => &$value ) {
                                            if ( is_array( $value ) ) {
                                                return false;
                                            }
                                            else {
                                                $value = sanitize_text_field($value);

                                            }
                                        }


                                }
                                return $array;
                        }
                             $output = ddt_sanitize_response($result2);
	                   if($output !== false): echo wp_json_encode($output); endif;
	               endif;


	     }

	endif;


        die();

    }
    add_action( 'wp_ajax_' . 'ddt_facebook_pixel_api_get_pixel', 'ddt_auto_setup_facebook_get_pixel' );
    add_action( 'wp_ajax_nopriv_' . 'ddt_facebook_pixel_api_get_pixel', 'ddt_auto_setup_facebook_get_pixel' );
}

//unlink facebook with ddt
 if (!function_exists('ddt_auto_setup_facebook_unlink')) {
    function ddt_auto_setup_facebook_unlink(){

	if( !empty($_POST['apikey']) && !empty($_POST['websitekey']) ):

	   $key = sanitize_text_field( $_POST['websitekey'] );
       $api_key = sanitize_text_field(  $_POST['apikey'] );


                    $result = wp_remote_retrieve_body( wp_remote_get( "https://app.datadriventool.com/system/rest-api/wp-plugin-facebookintegration/fb_unlink.php?ddt_wp_id=".$key."&apikey=".$api_key ) );

	                echo esc_js($result);


	endif;


        die();

    }
    add_action( 'wp_ajax_' . 'ddt_facebook_pixel_api', 'ddt_auto_setup_facebook_unlink' );
    add_action( 'wp_ajax_nopriv_' . 'ddt_facebook_pixel_api', 'ddt_auto_setup_facebook_unlink' );
}
