<?php
/*
Plugin Name: Campaign Tracking
Plugin URI: https://www.connectamerica.com
Description: Retrieve campaigns from Salesforce to track conversions
Author: Anthony Volpe
Version: 2.0
Author URI: https://www.connectamerica.com
Email: anthony.volpe@connectamerica.com
*/

if (!defined('ABSPATH')) die("Access denied!");

$sfdc = get_option('select-environment');
if ($sfdc == 'full') {
    include_once "encryption/config-full.php";
} else {
    include_once "encryption/config.php";
}

include_once('CampaignController.php');
include_once('functions.php');

function campaign_activate(){

}
register_activation_hook(__FILE__,'campaign_activate');


function campaign_deactivate(){

}

register_deactivation_hook(__FILE__,'campaign_deactivate');



function campaign_cron_loaded(){
    if ( empty( $GLOBALS['wp']->query_vars['campaign_backend_route'] ) )
        return;

    include_once(plugin_dir_path(__FILE__).'sync.php');

    die();
}

function campaign_cron_init(){

    campaign_backend_register_rewrites();
    global $wp;
    $wp->add_query_var( 'campaign_backend_route' );
}

function  campaign_backend_register_rewrites(){

    $pre='campaign_cron';
    $pre=str_replace("/","",$pre);

    add_rewrite_rule( '^' . $pre . '/?$','index.php?campaign_backend_route=/','top' );
    add_rewrite_rule( '^' . $pre . '(.*)?','index.php?campaign_backend_route=$matches[1]','top' );
}

    add_action( 'template_redirect',  'campaign_cron_loaded', -100 );
    add_action( 'init', 'campaign_cron_init' );

