<?php
/**
 * Plugin Name: Click Voodoo v4
 * Plugin URI : https://blogprofitnetwork.com
 * Description: Click Voodoo v4 is an awesome tool created by Blog Profit Network.
 * Version: 4.0.1
 * Author: Blog Profit Network
 * Author URI: https://blogprofitnetwork.com
 *
 *
 *
 * Filename: click-voodoo-v4.php
 * Created by: BlogProfitNetwork.Com <marcus@blogprofitnetwork.com> (https://BlogProfitNetwork.Com)
 * Copyright:2018 All Rights Reserved - BlogProfitNetwork.Com <marcus@blogprofitnetwork.com> (https://BlogProfitNetwork.Com)
 *
 * Skype: blogprofitnetwork
 * Email: marcus@blogprofitnetwork.com
 */
define ('_IN_MAIN_MODULE', 1);

include_once ('config.local.php');
include_once ('lib/_database.php');
include_once ('lib/_pages.php');
include_once ('lib/_metaboxes.php');
include_once ('lib/_shortcodes.php');
include_once ('lib/_widgets.php');
include_once ('lib/_helpers.php');
include_once ('lib/_init.admin.php');
include_once ('lib/_init.frontend.php');

//* init variables
$clickVoodooV4Database = new clickVoodooV4Database();
$clickVoodooV4Helpers  = new clickVoodooV4Helpers();

//* hooks for table installation / uninstallation
register_activation_hook(__FILE__, array($clickVoodooV4Database, 'install'));
register_deactivation_hook(__FILE__, array($clickVoodooV4Database, 'uninstall'));

//* ajax requests
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-update-settings', array($clickVoodooV4Database, 'updateSettings'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-generate-url-value', array($clickVoodooV4Helpers, 'generateRandomChars'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-add-url', array($clickVoodooV4Database, 'addUrl'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-reset-url-stat', array($clickVoodooV4Database, 'resetUrlStat'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-update-status', array($clickVoodooV4Database, 'updateUrlStatus'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE. '-delete-url', array($clickVoodooV4Database, 'deleteUrl'));
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-update-url', array($clickVoodooV4Database, 'updateUrl'));


//* ajax menus
add_action('wp_ajax_'. CLICKVOODOOV4_CODE .'-ajax-menus', 'clickVoodooV4AjaxMenus');
function clickVoodooV4AjaxMenus() {
    $tab = trim(strip_tags($_GET['tab']));
    switch ($tab) {
        case 'introduction' :
            include_once (CLICKVOODOOV4_PATH .'tmpl/introduction.php');
            break;

        case 'manage' :
            include_once (CLICKVOODOOV4_PATH .'tmpl/manage.php');
            break;

        case 'settings' :
            include_once (CLICKVOODOOV4_PATH .'tmpl/settings.php');
            break;
    }

    wp_die();
}


add_action('wp', 'clickVoodooV4UrlParser');
function clickVoodooV4UrlParser() {
    global $clickVoodooV4Database;

    $options    = get_option($clickVoodooV4Database->tableOptions);
    $urlParser  = $options['url_parser'];

    //* check permalink structure
    $permalink  = (get_option('permalink_structure')) ? 'true' : 'false';

    //* get current url
    $currentUrl = home_url($_SERVER['REQUEST_URI']);

    if ($permalink == 'true') {
        $u = get_bloginfo('home') .'/'. $urlParser .'/';
        $trimUrl = str_replace($u, '', $currentUrl);
    }
    elseif ($permalink == 'false') {
        $u = get_bloginfo('home') .'/?'. $urlParser .'=';
        $trimUrl = str_replace($u, '', $currentUrl);
    }


    if (strpos($currentUrl, $u) !== false) {
        if ($permalink == 'true') {
            $explode = array_filter(explode('/', $trimUrl));
        }
        else {
            $explode = array_filter(explode('&', $trimUrl));
        }
    }


    $value  = $explode[0];
    $url    = $clickVoodooV4Database->getUrlByValue($value);
    if ($url) {
        if ($url['status'] == CLICKVOODOOV4_ENABLED) {
            $urlid = $url['urlid'];

            //* add click
            $clickVoodooV4Database->addUrlClicksById($urlid);

            //* check if there's an extra parameters in the url
            if (count($explode) > 1) {
                for ($a = 1; $a <= count($explode); $a++) {
                    $clickVoodooV4Database->addTracking($urlid, $explode[$a]);
                }
            }

            echo '<script>window.location.href="'. $url['url'] .'";</script>';
            exit();
        }
    }
}