<commit_before><?php
namespace MyProject\LolitaFramework\Configuration\Modules;

use \MyProject\LolitaFramework\Configuration\Configuration;
use \MyProject\LolitaFramework\Configuration\IModule;
use \MyProject\LolitaFramework\Core\Data;
use \MyProject\LolitaFramework\Core\Wp;
use \MyProject\LolitaFramework\Core\Loc;

class Routes implements IModule
{
    /**
     * Routes class constructor
     *
     * @author <NAME> <<EMAIL>>
     * @param array $data engine data.
     */
    public function __construct($data = null)
    {
        $this->data = $data;
        if (is_array($this->data)) {
            $this->install();
        }
    }

    /**
     * Install our configuration
     *
     * @author <NAME> <<EMAIL>>
     * @return Routes instance.
     */
    private function install()
    {
        add_filter('template_include', array($this, 'blockDefaultTemplates'));
        add_filter('theme_page_templates', array($this, 'templates'), 10, 3);
        add_action('template_redirect', array(&$this, 'customRoutes'), 10, 0);
        return $this;
    }

    /**
     * Filter theme_page_templates
     *
     * @author <NAME> <<EMAIL>>
     * @param  array $page_templates page templates.
     * @param  object $me WP_Theme class instance.
     * @param  WP_Post $post post object.
     * @return array page templates.
     */
    public function templates($page_templates, $me, $post)
    {
        return array_merge($page_templates, $this->getTemplateNames());
    }

    /**
     * Get all templates from data
     *
     * @author <NAME> <<EMAIL>>
     * @return array templates.
     */
    private function getTemplateNames()
    {
        $templates = array();
        foreach ($this->data as $key => $el) {
            if (is_array($el) && array_key_exists('template_name', $el)) {
                $templates[$key] = $el['template_name'];
            }
        }
        return $templates;
    }

    /**
     * Get HTML from route element
     *
     * @author <NAME> <<EMAIL>>
     * @param  mixed $element route element.
     * @return string HTML code.
     */
    private function render($element)
    {
        if (is_callable($element)) {
            return $element();
        }
        if (is_array($element)) {
            if (array_key_exists('class', $element)) {
                $class = $element['class'];
                add_filter(
                    'body_class',
                    function ($classes) use ($class) {
                        return $this->addBodyClass($classes, $class);
                    }
                );
            }
            if (array_key_exists('html', $element)) {
                $element = $element['html'];
            }
        }
        return Data::interpret($element);
    }

    /**
     * Add css class to body
     *
     * @param array $classes
     * @param string $class
     * @return array
     */
    public function addBodyClass($classes, $class)
    {
        if (is_array($class)) {
            $class = implode(' ', $class);
        }
        $classes[] = $class;
        return $classes;
    }

    /**
     * Render custom routes.
     *
     * @author <NAME> <<EMAIL>>
     * @return void
     */
    public function customRoutes()
    {
        $wp_query = Loc::wpQuery();
        $page = $wp_query->query_vars['name'];
        if (array_key_exists($page, $this->data)) {
            echo $this->getHTML($this->data[ $page ]);
            exit;
        }
    }

    /**
     * Block default template
     *
     * @author <NAME> <<EMAIL>>
     * @param  string $template_path default template path.
     * @return string empty.
     */
    public function blockDefaultTemplates($template_path)
    {
        $post          = Loc::post();
        $route_type    = Wp::routeType();
        $templates     = $this->getTempaltes();
        $page_template = '';

        if (null !== $post) {
            $page_template = (string) get_post_meta($post->ID, '_wp_page_template', true);
        }

        if (array_key_exists($page_template, $this->data)) {
            // ==============================================================
            // Page from template option
            // ==============================================================<commit_msg>Working on: Routes<commit_after><?php
namespace MyProject\LolitaFramework\Configuration\Modules;

use \MyProject\LolitaFramework\Configuration\Configuration;
use \MyProject\LolitaFramework\Configuration\IModule;
use \MyProject\LolitaFramework\Core\Data;
use \MyProject\LolitaFramework\Core\Wp;
use \MyProject\LolitaFramework\Core\Loc;

class Routes implements IModule
{
    /**
     * Routes class constructor
     *
     * @author <NAME> <<EMAIL>>
     * @param array $data engine data.
     */
    public function __construct($data = null)
    {
        $this->data = $data;
        if (is_array($this->data)) {
            $this->install();
        }
    }

    /**
     * Install our configuration
     *
     * @author <NAME> <<EMAIL>>
     * @return Routes instance.
     */
    private function install()
    {
        add_filter('template_include', array($this, 'blockDefaultTemplates'));
        add_filter('theme_page_templates', array($this, 'templates'), 10, 3);
        add_action('template_redirect', array(&$this, 'customRoutes'), 10, 0);
        return $this;
    }

    /**
     * Filter theme_page_templates
     *
     * @author <NAME> <<EMAIL>>
     * @param  array $page_templates page templates.
     * @param  object $me WP_Theme class instance.
     * @param  WP_Post $post post object.
     * @return array page templates.
     */
    public function templates($page_templates, $me, $post)
    {
        return array_merge($page_templates, $this->getTemplateNames());
    }

    /**
     * Get all templates from data
     *
     * @author <NAME> <<EMAIL>>
     * @return array templates.
     */
    private function getTemplateNames()
    {
        $templates = array();
        foreach ($this->data as $key => $el) {
            if (is_array($el) && array_key_exists('template_name', $el)) {
                $templates[$key] = $el['template_name'];
            }
        }
        return $templates;
    }

    /**
     * Get HTML from route element
     *
     * @author <NAME> <<EMAIL>>
     * @param  mixed $element route element.
     * @return string HTML code.
     */
    private function render($element)
    {
        if (is_callable($element)) {
            return $element();
        }
        if (is_array($element)) {
            if (array_key_exists('class', $element)) {
                $class = $element['class'];
                add_filter(
                    'body_class',
                    function ($classes) use ($class) {
                        return $this->addBodyClass($classes, $class);
                    }
                );
            }
            if (array_key_exists('html', $element)) {
                $element = $element['html'];
            }
        }
        return Data::interpret($element);
    }

    /**
     * Add css class to body
     *
     * @param array $classes
     * @param string $class
     * @return array
     */
    public function addBodyClass($classes, $class)
    {
        if (is_array($class)) {
            $class = implode(' ', $class);
        }
        $classes[] = $class;
        return $classes;
    }

    /**
     * Render custom routes.
     *
     * @author <NAME> <<EMAIL>>
     * @return void
     */
    public function customRoutes()
    {
        $wp_query = Loc::wpQuery();
        $page = $wp_query->query_vars['name'];
        if (array_key_exists($page, $this->data)) {
            echo $this->render($this->data[ $page ]);
            exit;
        }
    }

    /**
     * Block default template
     *
     * @author <NAME> <<EMAIL>>
     * @param  string $template_path default template path.
     * @return string empty.
     */
    public function blockDefaultTemplates($template_path)
    {
        $post          = Loc::post();
        $route_type    = Wp::routeType();
        $templates     = $this->getTempaltes();
        $page_template = '';

        if (null !== $post) {
            $page_template = (string) get_post_meta($post->ID, '_wp_page_template', true);
        }

        if (array_key_exists($page_template, $this->data)) {
            // ==============================================================
            // Page from template option
            // ==============================================================
{'fraction_non_alphanumeric': 0.1633779657035471, 'fraction_numerical': 0.0014094432699083862, 'mean_word_length': 1.9617391304347827, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1341589', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2389, 'n_words': 524}