fgc-google-experiments-bkip.php 11.4 KB
<?php

add_filter('woocommerce_general_settings', 'add_experiments_ID_setting');

function add_experiments_ID_setting($settings) {
    $updated_settings = array();
    foreach ($settings as $section) {
// at the bottom of the General Options section
        if (isset($section['id']) && 'general_options' == $section['id'] &&
                isset($section['type']) && 'sectionend' == $section['type']) {
            $updated_settings[] = array(
                'name' => 'Experiment ID',
                'desc_tip' => 'Experiment ID',
                'id' => 'woocommerce_google_experiment_id',
                'type' => 'text',
                'css' => 'min-width:300px;',
                'std' => '05JR-AqkRqWBOVdeQicVGA', // WC < 2.0
                'default' => '05JR-AqkRqWBOVdeQicVGA', // WC >= 2.0
                'desc' => 'Sample ID: 05JR-AqkRqWBOVdeQicVGA',
            );
        }
        $updated_settings[] = $section;
    }
    return $updated_settings;
}

require( 'Experiment.php' );

class Fgc_Google_Experiments {

    public static function getTableName() {
        return "fgc_google_experiment_by_ip";
    }

    public static function getVarKeyType() {
        return array(0 => 'A', 1 => 'B', 2 => 'C');
    }

    public static function getTypeVarKey() {
        return array('A' => 0, 'B' => 1, 'C' => 2, 'D' => -1);
    }

    public static function getTypeSku() {
        return array('A' => 'REIZEA', 'B' => 'REIZEB', 'C' => 'REIZEC', 'D' => 'REIZE1');
    }

    public static function getSkuType() {
        return array('REIZEA' => 'A', 'REIZEB' => 'B', 'REIZEC' => 'C', 'REIZE1' => 'D');
    }

    public static function getTypeCategory() {
        return array('A' => 'Single Purchase A', 'B' => 'Single Purchase B', 'C' => 'Single Purchase C', 'D' => 'Single Purchase');
    }

    public static function getCategoryType() {
        return array('Single Purchase A' => 'A', 'Single Purchase B' => 'B', 'Single Purchase C' => 'C', 'Single Purchase' => 'D');
    }

    public static function getIpAddress() {
//        if ($_REQUEST['ipaddress']) {
//            return $_REQUEST['ipaddress'];
//        }
        return $_SERVER['REMOTE_ADDR'];

//        return $mac;
    }

    public static function init() {
        $ip_address = self::getIpAddress();
        $user_id = get_current_user_id();
        $current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
        if ($current_ip_address_info) { // Da co trong db
            if ($user_id > 0) {
                if ($current_ip_address_info->user_id == '0') { // Chua cap nhat vao db, can cap nhat vao db
                    if ($old_ip_address_info = self::get_google_experiment_data_by_user_id($user_id)[0]) { // user nay da tung login o ip khac, can cap nhat thong tin cho ip hien tai
                        self::update_info_for_ip_address($old_ip_address_info->id, $old_ip_address_info->var_key, $old_ip_address_info->user_id, $old_ip_address_info->type);
                    } else { // user nay chua login vao ip khac, chi can cap nhat userid
                        if ($type = self::get_type_of_user($user_id)) { // khach hang cu da co order
                            $var_key = self::getTypeVarKey()[$type];
                            self::update_info_for_ip_address($ip_address, $var_key, $user_id, $type);
                        } else { // khach hang moi, chi can cap nhat user id
                            self::update_user_id_for_ip_address($ip_address, $user_id);
                        }
                    }
                } else { // Da cap nhat vao db. Ko can sua doi
                }
            } else { // ko lam gi
            }
        } else { // Chua co ipaddress trong db, vao site lan dau hoac la khach hang cu
            if ($user_id > 0) { // Khach hang cu
                if ($type = self::get_type_of_user($user_id)) {
                    $var_key = self::getTypeVarKey()[$type];
                    self::add_new_ip_address($ip_address, $var_key, $type, $user_id);
                } else { // khach hang chua co order, tao type bat ky
                    $var_key = self::get_variation_by_google_experiment();
                    $type = self::getVarKeyType() [$var_key];
                    self::add_new_ip_address($ip_address, $var_key, $type, $user_id);
                }
            } else { // vao site lan dau
                $var_key = self::get_variation_by_google_experiment();
                $type = self::getVarKeyType() [$var_key];
                self::add_new_ip_address($ip_address, $var_key, $type);
            }
        }
    }

    public static function get_variation_by_google_experiment() {
        $google_experiment_id = get_option('woocommerce_google_experiment_id');
        $experiment = new \UnitedPrototype\GoogleAnalytics\Experiment($google_experiment_id);
//        return -1;
        return $variationKey = $experiment->chooseVariation();
    }

    public static function add_new_ip_address($ip_address = '', $var_key = 0, $type = '', $user_id = 0) {
        if ($ip_address != '' && $var_key != 0 && $type != '') {
            global $wpdb;
            $query = "INSERT INTO " . $wpdb->prefix . self::getTableName() . " (`IpAddress`, `var_key`, `user_id`,`type`) VALUES ('$ip_address', '$var_key', '$user_id' , '$type');";
            return $wpdb->query($query);
        }
        return false;
    }

    public static function update_user_id_for_ip_address($ip_address = '', $user_id = '') {
        if ($ip_address != '' && $user_id != '') {
            global $wpdb;
            $query = "UPDATE `" . $wpdb->prefix . self::getTableName() . "  SET user_id = '$user_id' WHERE  IpAddress  =  '" . $ip_address . "'";
            return $wpdb->query($query);
        }
        return false;
    }

    public static function update_info_for_ip_address($ip_address, $var_key = '', $user_id = '', $type = '') {
        if ($ip_address != '' && $user_id != '') {
            global $wpdb;
            $query = "UPDATE " . $wpdb->prefix . self::getTableName() . "  SET user_id = '$user_id' , var_key = '$var_key' , type = '$type'  WHERE  IpAddress  =  '$ip_address'";
            return $wpdb->query($query);
        }
        return false;
    }

    public static function get_google_experiment_data_by_ipAddress($ip_address = false) {
        if ($ip_address) {
            global $wpdb;
            $query = "SELECT * FROM " . $wpdb->prefix . self::getTableName() . " WHERE IpAddress = '" . $ip_address . "'";
            $results = $wpdb->get_results($query);
            if (empty($results)) { // The customer doesn't have any order
                return false;
            } else { // The customer has order.
                return $results[0];
            }
        }
        return false;
    }

    public static function get_google_experiment_data_by_user_id($user_id = false) {
        if (!$user_id && $user_id != '') {
            global $wpdb;
            $query = "SELECT * FROM " . $wpdb->prefix . self::getTableName() . " WHERE user_id = '" . $user_id . "'";
            $results = $wpdb->get_results($query);
            if (empty($results)) { // The customer doesn't have any order
                return false;
            } else { // The customer has order.
                return $results;
            }
        }
    }

    public static function get_type_of_user($user_id) {
        global $wpdb;
        $select_get_orders = "SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_customer_user' AND `meta_value` = " . $user_id;
        $results = $wpdb->get_results($select_get_orders);
//            var_dump($results);
        if (empty($results)) { // The customer doesn't have any order
            return false;
        } else { // The customer has order.
            return self::get_type_of_order($results[0]->post_id);
        }
    }

    public static function get_type_of_order($order_id) {
        $order = new WC_Order($order_id);
        $items = $order->get_items();

        foreach ($items as $key => $item) {
            $item_meta_array = $item['item_meta_array'];
            break;
        }
        foreach ($item_meta_array as $key => $value) {
            if ($value->key == '_variation_id') {
                $product_variation_id = $value->value;
            }
            if ($value->key == '_product_id') {
                $product_id = $value->value;
            }
        }
        if ($product_variation_id) {
            $product = new WC_Product($product_id);
            $sku = $product->get_sku();
            return self::getSkuType()[$sku];
        } else {
            $product = new WC_Product($product_id);
            $category = $product->get_categories();
            return self::getCategoryType()[$category];
        }
    }

    public static function get_products_for_slider($isSubscription) {
        $type = self::get_type_for_slider();
        if ($isSubscription) {
            $sku = self::getTypeSku()[$type];
            self::get_subscription_product_for_slider($sku);
        } else {
            $category = self::getTypeCategory()[$type];
            self::get_single_products_for_slider($category);
        }
    }

    public static function get_type_for_slider() {
        $ip_address = self::getIpAddress();
        $current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
        if ($current_ip_address_info) {
            return $current_ip_address_info->type;
        }
        return 'D';
    }

    public static function get_subscription_product_for_slider($sku) {
        echo WC_Shortcodes::product_page(array('sku' => $sku));
    }

    public static function get_single_products_for_slider($category) {
        echo WC_Shortcodes::product_category(array("category" => $category, "per_page" => "16", "orderby" => "price", "order" => "asc"));
    }

    public static function get_sku_for_get_started_page() {
        $type = self::get_type_for_slider();
        return self::getTypeSku()[$type];
    }

    public static function get_var_key_for_get_started_page() {
        $ip_address = self::getIpAddress();
        $current_ip_address_info = self::get_google_experiment_data_by_ipAddress($ip_address);
        return $current_ip_address_info->var_key;
    }

}

function test_google_experiment() {
    if (isset($_REQUEST['testgoogleexperiment'])) {
//        Fgc_Google_Experiments::get_type_for_slider();
        echo '<pre>';
        ob_start();
        system('getmac');
        $mycom = ob_get_contents();
        ob_clean();
        var_dump($mycom);
        ob_start();
        system('ipconfig-a');
        $mycom = ob_get_contents();
        ob_clean();
        var_dump($mycom);

        $findme = "Physical";
        $pmac = strpos($mycom, $findme);
        $mac = substr($mycom, ($pmac + 36), 17);
        var_dump($mycom);
        exit();
    }
}

add_action('init', 'test_google_experiment');
add_action('init', 'Fgc_Google_Experiments::init');

global $wpdb;
$google_experiment_db_version = '1.6';

$google_experiment_installed_ver = get_option("google_experiment_db_version");

if ($google_experiment_installed_ver != $google_experiment_db_version) {

    $table_name_new1 = $wpdb->prefix . 'fgc_google_experiment_by_ip';

    $sql1 = "CREATE TABLE IF NOT EXISTS `$table_name_new1` (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  `IpAddress` VARCHAR(255)  NULL,
  `var_key` BIGINT(20) NULL,
  `user_id` BIGINT(20) NULL,
  `type` VARCHAR(255) NULL,
  PRIMARY KEY (`id`));";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($sql1);

    update_option("google_experiment_db_version", $google_experiment_db_version);
}