<?php

/*
Plugin Name: Digger By Digtective
Plugin URI: http://digtective.com
Description: The wordpress plugin for Digtective Digger service.
Version: 1.2.0
Author: Digtective As
Author URI: https://digtective.com
License: GPL3
*/

defined('ABSPATH') or die ('');

if(file_exists(dirname(__FILE__) . "/vendor/autoload.php")){
    require_once dirname(__FILE__) . "/vendor/autoload.php";
}

define("DIGTECTIVE_PATH", plugin_dir_path(__FILE__));
define("DIGTECTIVE_URL", plugin_dir_url(__FILE__));
define("DIGTECTIVE", plugin_basename(__FILE__));

function digtective_activate(){
    \Digtective\Base\Activate::activate();
}

function digtective_deactivate(){
    \Digtective\Base\Deactivate::deactivate();
}

register_activation_hook(__FILE__, "digtective_activate");
register_deactivation_hook(__FILE__, "digtective_deactivate");

if(class_exists("Digtective\\Init")){
    Digtective\Init::register_services();

    add_action('elementor_pro/forms/process', 'digtectiveProcessSession', 10, 2);
}

function digtectiveAddSessionRawFields($rawFields, $fieldToAdd){
    $rawFields[$fieldToAdd]['id'] = $fieldToAdd;
    $rawFields[$fieldToAdd]['value'] = sanitize_text_field($_SESSION[$fieldToAdd] ?? "");
    $rawFields[$fieldToAdd]['raw_value'] = sanitize_text_field($_SESSION[$fieldToAdd] ?? "");
    $rawFields[$fieldToAdd]['title'] = $fieldToAdd;
    $rawFields[$fieldToAdd]['required'] = false;

    return $rawFields;
}

function digtectiveAddSessionRawGoogleClick($rawFields, $type, $id){
    $rawFields['clickId']['id'] = 'clickId';
    $rawFields['clickId']['value'] = $id;
    $rawFields['clickId']['raw_value'] = $id;
    $rawFields['clickId']['title'] = 'clickId';
    $rawFields['clickId']['required'] = false;

    $rawFields['clickType']['id'] = 'clickType';
    $rawFields['clickType']['value'] = $type;
    $rawFields['clickType']['raw_value'] = $type;
    $rawFields['clickType']['title'] = 'clickType';
    $rawFields['clickType']['required'] = false;

    return $rawFields;
}

function digtectiveProcessSession($record, $handler) {
    session_start();
    $rawFields = $record->get( 'fields' );

    $rawFields = digtectiveAddSessionRawFields($rawFields, 'sessionId');
    $rawFields = digtectiveAddSessionRawFields($rawFields, 'path');
    $rawFields = digtectiveAddSessionRawFields($rawFields, 'referrer');
    $rawFields = digtectiveAddSessionRawFields($rawFields, 'originalReferrer');
    $rawFields = digtectiveAddSessionRawFields($rawFields, 'adCode');
    $rawFields = digtectiveAddSessionRawFields($rawFields, 'browser');

    if(isset($_SESSION['wbraid'])){
        $rawFields = digtectiveAddSessionRawGoogleClick($rawFields, 'wbraid', sanitize_text_field($_SESSION['wbraid'] ?? ""));
    }

    if(isset($_SESSION['gbraid'])){
        $rawFields = digtectiveAddSessionRawGoogleClick($rawFields, 'gbraid', sanitize_text_field($_SESSION['gbraid'] ?? ""));
    }

    if(isset($_SESSION['gclid'])){
        $rawFields = digtectiveAddSessionRawGoogleClick($rawFields, 'gclid', sanitize_text_field($_SESSION['gclid'] ?? ""));
    }

    $record->set( 'fields', $rawFields );
}

add_action( 'wpforms_process_complete', function( $fields, $entry, $form_data, $entry_id ) {
    try {
        session_start();
        $entry = wpforms()->entry->get( $entry_id );

        // Fields are in JSON, so we decode to an array
        $entry_fields = json_decode( $entry->fields, true );

        $entry_fields[] = [
            'id' => 9999,
            'name' => "Session Id",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['sessionId'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9998,
            'name' => "Path",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['path'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9997,
            'name' => "Referrer",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['referrer'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9996,
            'name' => "Original Referrer",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['originalReferrer'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9995,
            'name' => "Ad Code",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['adCode'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9994,
            'name' => "Browser",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['browser'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9993,
            'name' => "Gbraid",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['gbraid'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9992,
            'name' => "Wbraid",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['wbraid'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9991,
            'name' => "Gclid",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['gclid'] ?? ""),
        ];

        $entry_fields[] = [
            'id' => 9990,
            'name' => "Keyword",
            'type' => "text",
            'value' => sanitize_text_field($_SESSION['kwd'] ?? ""),
        ];

        $entry_fields = json_encode( $entry_fields );

        // Save changes
        wpforms()->entry->update( $entry_id, array( 'fields' => $entry_fields ), '', '', array( 'cap' => false ) );
    }catch(\Exception $e){
        error_log($e);
    }
}, 10, 4);

if(\Digtective\Integrations\WooCommerceIntegration::isActive()){
    (new \Digtective\Integrations\WooCommerceIntegration())->applyFilters();
}

if(\Digtective\Integrations\ElementorIntegration::isActive()){
    (new \Digtective\Integrations\ElementorIntegration())->applyFilters();
}



