<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              http://example.com
 * @since             1.0.0
 * @package           JamKazam
 *
 * @wordpress-plugin
 * Plugin Name:       WordPress JamKazam Plugin
 * Plugin URI:        http://example.com/jamkazam-uri/
 * Description:       This is a short description of what the plugin does. It's displayed in the WordPress admin area.
 * Version:           1.0.0
 * Author:            Your Name or Your Company
 * Author URI:        http://example.com/
 * License:           JamKazam Source
 * License URI:       http://www.nowhere.com
 * Text Domain:       jamkazam
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define( 'JAMKAZAM_VERSION', '1.0.0' );

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-jamkazam-activator.php
 */
function activate_jamkazam() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-jamkazam-activator.php';
	JamKazam_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-jamkazam-deactivator.php
 */
function deactivate_jamkazam() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-jamkazam-deactivator.php';
	JamKazam_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'activate_jamkazam' );
register_deactivation_hook( __FILE__, 'deactivate_jamkazam' );

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-jamkazam.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_jamkazam() {

	$plugin = new JamKazam();
	$plugin->run();

}
run_jamkazam();

function track_affiliate() {
     $affiliate_id = $_GET['affiliate'];

     $expiration = current_time( 'timestamp' ) + ( DAY_IN_SECONDS * 2 );
     #cookies[:affiliate_visitor] = { :value => params[:affiliate], :expires => Time.now + 3600 * 24} # 1 day from now

     if(isset($affiliate_id)) {
        setcookie('affiliate_visitor', $affiliate_id, $expiration, '/', '.jamkazam.com');
     }
}

function track_origin() {
    $source = $_GET["utm_source"];

    if(isset($source)) {
        setcookie('origin', json_encode(array('utm_source' => $_GET['utm_source'], 'utm_medium' => $_GET['utm_medium'], 'utm_campaign' => $_GET['utm_campaign'])), strtotime( '+1 year' ), '/', '.jamkazam.com');
    }
}

/**
* Set affiliate tracking cookie if there is an `?affiliate=xxxx` parameter in the URL
* The rails set honors this cookie, and a new user will be associated with the affiliate.
*/
add_action( 'init', 'process_post' );
function process_post() {
    track_affiliate();
    track_origin();
}
