<?php
defined('ABSPATH') || exit;
if (!class_exists('yascode_custompages')) :
    class yascode_custompages
    {
        //init before run
        private static $instance;
        public static function get_instance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        private $pages = array(
            'blog' => 'blog',
            'panel' => array(
                'slug' => PANEL_URI,
                'sub_query_vars' => array('panel_page', 'sub_page', 'sub_id')
            ),
            'login' => LOGIN_URI,
            'register' => REGISTER_URI,
            'forget' => FORGET_PASS_URI,
            'forget-confirm' => array(
                'slug' => FORGET_PASS_CONFIRM_URI,
                'sub_query_vars' => array('forget-hash')
            ),
            'payment' => array(
                'slug' => 'payment',
                'sub_query_vars' => array('transaction')
            ),
            'playonline' => array(
                'slug' => 'play',
                'sub_query_vars' => array('post_id', 'season', 'episode')
            ),
        );

        public function __construct()
        {
            add_action('init', array($this, 'yascode_add_rewrite_rules'));
            add_filter('query_vars', array($this, 'yascode_query_vars'));
            add_filter('template_redirect', array($this, 'yascode_template_redirects'));
        }

        public function yascode_add_rewrite_rules()
        {
            add_rewrite_rule('^' . $this->pages['blog'] . '/page/([0-9]+)/?', 'index.php?' . $this->pages['blog'] . '=1&paged=$matches[1]', 'top');
            add_rewrite_rule('^' . $this->pages['blog'] . '/?', 'index.php?' . $this->pages['blog'] . '=1', 'top');
            add_rewrite_rule('^' . $this->pages['panel']['slug'] . '/([^/]*)/([^/]*)/([0-9]+)/?', 'index.php?' . $this->pages['panel']['slug'] . '=1&panel_page=$matches[1]&sub_page=$matches[2]&sub_id=$matches[3]', 'top');
            add_rewrite_rule('^' . $this->pages['panel']['slug'] . '/([^/]*)/([^/]*)/?', 'index.php?' . $this->pages['panel']['slug'] . '=1&panel_page=$matches[1]&sub_page=$matches[2]', 'top');
            add_rewrite_rule('^' . $this->pages['panel']['slug'] . '/([^/]*)/?', 'index.php?' . $this->pages['panel']['slug'] . '=1&panel_page=$matches[1]', 'top');
            add_rewrite_rule('^' . $this->pages['panel']['slug'] . '/?', 'index.php?' . $this->pages['panel']['slug'] . '=1', 'top');
            add_rewrite_rule('^' . $this->pages['login'] . '/?', 'index.php?' . $this->pages['login'] . '=1', 'top');
            add_rewrite_rule('^' . $this->pages['register'] . '/?', 'index.php?' . $this->pages['register'] . '=1', 'top');
            add_rewrite_rule('^' . $this->pages['forget'] . '/?', 'index.php?' . $this->pages['forget'] . '=1', 'top');
            add_rewrite_rule('^' . $this->pages['forget-confirm']['slug'] . '/([^/]*)/?', 'index.php?' . $this->pages['forget-confirm']['slug'] . '=1&forget-hash=$matches[1]', 'top');
            add_rewrite_rule('^' . $this->pages['payment']['slug'] . '/([^/]*)/?', 'index.php?' . $this->pages['payment']['slug'] . '=1&transaction=$matches[1]', 'top');
            add_rewrite_rule('^' . $this->pages['playonline']['slug'] . '/([0-9]+)/([0-9]+)/([0-9]+)/?$', 'index.php?' . $this->pages['playonline']['slug'] . '=1&post_id=$matches[1]&season=$matches[2]&episode=$matches[3]', 'top');
            add_rewrite_rule('^' . $this->pages['playonline']['slug'] . '/([0-9]+)/?$', 'index.php?' . $this->pages['playonline']['slug'] . '=1&post_id=$matches[1]', 'top');
        }

        public function yascode_template_redirects()
        {
            global $wp_query;
            if (!empty($this->pages) && is_array($this->pages)) {
                foreach ($this->pages as $page_path => $page) {
                    if (is_array($page) && !empty($page['slug'])) $page = $page['slug'];
                    if ($wp_query->get($page) && file_exists(THEME_TEMPLATE_URL_PATH . '/templates/pages/' . $page_path . '.php')) {
                        get_template_part('templates/pages/' . $page_path);
                        exit();
                    }
                }
            }
        }
        public function yascode_query_vars($query_vars)
        {
            if (!empty($this->pages) && is_array($this->pages)) {
                foreach ($this->pages as $page) {
                    if (!is_array($page)) $query_vars[] = $page;
                    else {
                        $query_vars[] = $page['slug'];
                        if (!empty($page['sub_query_vars']))
                            foreach ($page['sub_query_vars'] as $query_var) $query_vars[] = $query_var;
                    }
                }
            }
            return $query_vars;
        }
    }
    yascode_custompages::get_instance();
endif;
