<?php
/**
 * Plugin Name: SlugVoodoo
 * Plugin URI : https://blogprofitnetwork.com
 * Description: SlugVoodoo is an awesome tool created by Blog Profit Network.
 * Version: 1.0.1
 * Author: Blog Profit Network
 * Author URI: https://blogprofitnetwork.com
 *
 *
 *
 * Filename: marcus-url-modifier.php
 * Created : Affiliate Marketing Dude <marcus@affiliatemarketingdude.com> (https://AffiliateMarketingDude.Com)
 * Copyright 2020 - Affiliate Marketing Dude <marcus@affiliatemarketingdude.com> (https://AffiliateMarketingDude.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
$marcusUrlModifierDatabase = new marcusUrlModifierDatabase();
$marcusUrlModifierHelpers  = new marcusUrlModifierHelpers();

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

//* ajax requests
add_action('wp_ajax_'. MARCUSURLMODIFIER_CODE .'-update-custom-url', array($marcusUrlModifierDatabase, 'updateCustomUrl'));




//* solution to cancel redirection in wordpress
add_filter( 'redirect_canonical', 'stopRedirectGuess' );
function stopRedirectGuess($url) {
    return;
}


//* add my custom page
add_filter('template_include', 'custom_single_template');
function custom_single_template($template) {
    global $wp_query, $wp, $wpdb, $marcusUrlModifierDatabase;

    $requestUri = trim(strip_tags($_SERVER['REQUEST_URI']));
    $customUrl  = $marcusUrlModifierDatabase->getCustomUrlByUrl($requestUri);


    if ($customUrl['type'] == 'post') {
        query_posts(array('p' => $customUrl['postid'], 'post_type' => $customUrl['type']));
        // $template = MARCUSURLMODIFIER_PATH .'tmpl/single.php';
        $template = get_template_directory() .'/single.php';
    }
    elseif ($customUrl['type'] == 'page') {
        query_posts(array('p' => $customUrl['postid'], 'post_type' => $customUrl['type']));
        // $template = MARCUSURLMODIFIER_PATH .'tmpl/page.php';
        $template = get_template_directory() .'/page.php';
    }
    // else {
    //     $template = 'no match';
    // }

    return $template;
}