<commit_before><?php
/*
 * This file is part of WPCore project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace WPCore;

use WPCore\hooks\AdminScript;
use WPCore\hooks\ThemeScript;

/**
 * WP feature
 *
 * @author <NAME> <<EMAIL>>
 */

abstract class WPfeature implements WPhook
{
    use HasParentFeature;
    
    protected $name;
    protected $slug = 'pweb';

    private $base_path;
    private $base_url;
    private $css_url;
    private $js_url;
    private $img_url;

    private $baseRelurl;

    private $asset_path = 'assets/';
    private $img_path   = 'img/';
    private $css_path   = 'css/';
    private $js_path    = 'js/';

    private $views_path = 'views/';

    private $isHooked = false;

    protected $scripts = array();
    protected $styles  = array();

    protected $hooks   = array();

    protected $HThemeScript;
    protected $HAdminScript;

    protected $features = array();

    protected $enabled;

    public function __construct($name, $slug)
    {
        $this->name = $name;
        $this->slug = $slug;

        $this->enable();

        $this->setBaseUrl(get_template_directory_uri());
        $this->setBasePath(get_template_directory());

        $this->HThemeScript = new ThemeScript();
        $this->HAdminScript = new AdminScript();
        $this->hook($this->HThemeScript);
        $this->hook($this->HAdminScript);
    }
    
    abstract public function init();

    public function getSlug()
    {
        return $this->slug;
    }
    public function getName()
    {
        return $this->name;
    }
    public function enable()
    {
        $this->enabled = true;
        add_theme_support($this->slug);
    }

    public function disable()
    {
        $this->enabled = true;
        remove_theme_support($this->slug);
    }

    public function register()
    {
        if ($this->enabled === true) {
            if (!empty($this->hooks)) {
                foreach ($this->hooks as $hook) {
                    $hook->register();
                }
            }
        }
    }

    public function remove()
    {
        if ($this->enabled === true) {
            if (!empty($this->hooks)) {
                foreach ($this->hooks as $hook) {
                    $hook->remove();
                }
            }
        }
    }

    public function registerScriptsStyles()
    {
        $this->HThemeScript->registerScriptsStyles();
        $this->HAdminScript->registerScriptsStyles();
    }

    public function addScript(WPscript $script)
    {
        if ($script instanceof WPscriptTheme) {
            $this->HThemeScript->addScript($script);
        } elseif ($script instanceof WPscriptAdmin) {
            $this->HAdminScript->addScript($script);
        }
    }

    public function addStyle(WPstyle $style)
    {
        if ($style instanceof WPstyleTheme) {
            $this->HThemeScript->addStyle($style);
        } elseif ($style instanceof WPstyleAdmin) {
            $this->HAdminScript->addStyle($style);
        }
    }

    public function hook(WPhook $hook)
    {
        if ($hook instanceof WPfeature) {
            //If Feature is already hooked by another, skip updating base url,
            //so that same feature use same base url everywhere it is hooked
            if (!$hook->isHooked) {
                $relPath = substr($hook->getBasePath(), strlen($this->getBasePath()));
                $hook->setBaseUrl($this->getBaseUrl().$relPath);
            }<commit_msg>add methods to access scripts hooks and initAjaxCalls<commit_after><?php
/*
 * This file is part of WPCore project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace WPCore;

use WPCore\hooks\AdminScript;
use WPCore\hooks\ThemeScript;

/**
 * WP feature
 *
 * @author <NAME> <<EMAIL>>
 */

abstract class WPfeature implements WPhook
{
    use HasParentFeature;
    
    protected $name;
    protected $slug = 'pweb';

    private $base_path;
    private $base_url;
    private $css_url;
    private $js_url;
    private $img_url;

    private $baseRelurl;

    private $asset_path = 'assets/';
    private $img_path   = 'img/';
    private $css_path   = 'css/';
    private $js_path    = 'js/';

    private $views_path = 'views/';

    private $isHooked = false;

    protected $scripts = array();
    protected $styles  = array();

    protected $hooks   = array();

    protected $HThemeScript;
    protected $HAdminScript;

    protected $features = array();

    protected $enabled;

    public function __construct($name, $slug)
    {
        $this->name = $name;
        $this->slug = $slug;

        $this->enable();

        $this->setBaseUrl(get_template_directory_uri());
        $this->setBasePath(get_template_directory());

        $this->HThemeScript = new ThemeScript();
        $this->HAdminScript = new AdminScript();
        $this->hook($this->HThemeScript);
        $this->hook($this->HAdminScript);
    }
    
    abstract public function init();

    public function getSlug()
    {
        return $this->slug;
    }
    public function getName()
    {
        return $this->name;
    }
    public function enable()
    {
        $this->enabled = true;
        add_theme_support($this->slug);
    }

    public function disable()
    {
        $this->enabled = true;
        remove_theme_support($this->slug);
    }

    public function register()
    {
        if ($this->enabled === true) {
            if (!empty($this->hooks)) {
                foreach ($this->hooks as $hook) {
                    $hook->register();
                }
            }
        }
    }

    public function remove()
    {
        if ($this->enabled === true) {
            if (!empty($this->hooks)) {
                foreach ($this->hooks as $hook) {
                    $hook->remove();
                }
            }
        }
    }

    public function registerScriptsStyles()
    {
        $this->HThemeScript->registerScriptsStyles();
        $this->HAdminScript->registerScriptsStyles();
    }

    public function getThemeScriptHook()
    {
        return $this->HThemeScript;
    }

    public function getAdminScriptHook()
    {
        return $this->HAdminScript;
    }

    public function addScript(WPscript $script)
    {
        if ($script instanceof WPscriptTheme) {
            $this->HThemeScript->addScript($script);
        } elseif ($script instanceof WPscriptAdmin) {
            $this->HAdminScript->addScript($script);
        }
    }

    public function addStyle(WPstyle $style)
    {
        if ($style instanceof WPstyleTheme) {
            $this->HThemeScript->addStyle($style);
        } elseif ($style instanceof WPstyleAdmin) {
            $this->HAdminScript->addStyle($style);
        }
    }

    public function initAjaxCalls()
    {
        if ($this->enabled !== true) {
            return;
        }

        if (empty($this->hooks)) {
            return;
        }

        foreach ($this->hooks as $hook) {
            if (!$hook instanceof WPajaxCall) {
                continue;
            }
            $hook->init();
        }
    }

    public function hook(WPhook $hook)
    {
        if ($hook instanceof WPfeature) {
            //If Feature is already hooked by another, skip updating base url,
            //so that same feature use same base url everywhere it is hooked
            if (!$hook->isHooked) {
                $relPath = substr($hook->getBasePath(), strlen($this->getBasePath()));
                $hook->setBaseUrl($this->getBaseUrl().$relPath);
            }
{'fraction_non_alphanumeric': 0.1282648047582105, 'fraction_numerical': 0.0, 'mean_word_length': 2.016770670826833, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 105, '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': '64677', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2199, 'n_words': 521}