read_pixel_list_from_db();
if(count($remote_pixels_json) != count($local_pixel_array)){
foreach ($remote_pixels_json as $remote_pixel){
$pixel = $remote_pixel['id'];
if(!array_search( $pixel, $local_pixel_array)){
$this->save_pixel_to_db( $pixel,1);
}
}
}
}
function read_pixel_list_from_db($status = 2){
global $wpdb;
$get_active_pixel_query = "SELECT pixel FROM {$this->pixel_table}";
if($status == 0 or $status == 1){
$get_active_pixel_query = $wpdb->prepare("SELECT pixel FROM {$this->pixel_table} WHERE status = %d", $status);
}
$pixels = $wpdb->get_col($get_active_pixel_query);
return $pixels;
}
function fetch_pixel_json($relative_url){
$response = $this->custom_wp_remote(
$this->add_base_url($relative_url),
array()
);
$pixel_data = wp_remote_retrieve_body($response);
$pixel_json = json_decode($pixel_data, true);
return $pixel_json;
}
function show_table($pixels_json){
$count = 1;
?>
| # |
Pixel ID |
Pixel Name |
Account Name |
Account ID |
Status |
Delete |
show_pixel_row(
$count,
$pixel_data_dict
);
$count = $count + 1;
}
?>
get_pixel_status($pixel_data_dict['id']);
if ($status == 1){
$pixel_status = "checked";
}
else{
$pixel_status = "";
}
$toggle_nonce = wp_create_nonce( 'toggle_pixel_'. $pixel_data_dict['id']);
$delete_nonce = wp_create_nonce( 'delete_pixel_'. $pixel_data_dict['id']);
$row =
"
| $count |
{$pixel_data_dict['id']} |
{$pixel_data_dict['name']} |
{$pixel_data_dict['adwords_account_name']} |
{$pixel_data_dict['adwords_account_id']} |
|
|
";
echo $row;
}
function save_pixel_to_db($pixel_id, $status = 1){
global $wpdb;
require(ABSPATH . 'wp-admin/includes/upgrade.php');
$wpdb->insert(
$this->pixel_table,
array(
'pixel' => $pixel_id,
'status' => $status),
array('%s', '%d'));
}
function get_action_url($action = "fetch-all"){
$action_value = "";
if ($action == "pause"){
$action_value = "disable";
}
elseif ($action == "enable" or $action == "unpause"){
$action_value = "add";
}
elseif ($action == "delete"){
$action_value = "remove";
}
elseif ($action == "fetch-all"){
return "woocommerce/fetch/pixels";
}
$url = "woocommerce/" . $action_value ."/pixel";
return $url;
}
function add_pixel_url(){
$query = http_build_query(array(
'app_id' => $this::$app_id,
'store' => get_option('adnabu_store_id')
));
$url = "https://www.adnabu.com/woocommerce/add/pixel?" ;
return $url . $query;
}
function enable_new_pixel($pixel_id){
$data = array('pixel_id' => $pixel_id);
$url = $this->get_action_url("fetch-all");
$response = $this->custom_wp_remote(
$this->add_base_url($url),
$data);
if( is_wp_error( $response ) ) {
return;
}
if($pixel_id == json_decode(wp_remote_retrieve_body($response))['0']->id){
$this->save_pixel_to_db($pixel_id);
}
}
function delete_pixel($pixel_id){
$url = $this->get_action_url("delete");
$data = array('pixel_id' => $pixel_id);
$response = $this->custom_wp_remote(
$this->add_base_url($url),
$data);
if( is_wp_error( $response ) ) {
return;
}
$message = json_decode(wp_remote_retrieve_body($response))->message;
$type = json_decode(wp_remote_retrieve_body($response))->type;
$this->show_message($message, $type);
if ($type == "SUCCESS"){
global $wpdb;
require(ABSPATH . 'wp-admin/includes/upgrade.php');
$wpdb->delete(
$this->pixel_table,
array('pixel' => $pixel_id));
}
}
function get_pixel_status($pixel_id){
global $wpdb;
$get_pixel_status_query = $wpdb->prepare(
"select status from {$this->pixel_table} where pixel = %s",
$pixel_id);
$status = $wpdb->get_row(
$get_pixel_status_query,
ARRAY_A ,
0)['status'];
return (int)$status;
}
function set_pixel_status($pixel_id, $status){
global $wpdb;
$row_affected = $wpdb->update(
$this->pixel_table,
array('status' => $status),
array('pixel' => $pixel_id,),
array('%d'),
array('%s')
);
return $row_affected;
}
function flip_pixel_status($pixel_id){
$current_status = $this->get_pixel_status($pixel_id);
if ($current_status == 1 ){
$status = 0;
$message = "Tracker disabled successfully.";
}
else{
$status = 1;
$message = "Tracker enabled successfully.";
}
$row_affected = $this->set_pixel_status($pixel_id, $status);
if($row_affected == 1){
$this->show_message($message, "SUCCESS");
}
else{
$this->show_message('Failed!', 'FAILURE');
}
}
}