<commit_before><?php

namespace Gwa\Wordpress\Template\Zero\Library;

/**
 * Zero - a PHP 5.4 Wordpress Theme.
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   2015 Great White Ark
 *
 * @link        http://www.greatwhiteark.com
 *
 * @license     MIT
 */

use LogicException;
use RuntimeException;
use Timber;
use TimberLoader;
use WP_Query;

/**
 * AbstractController.
 *
 * @author  GWA
 *
 */
abstract class AbstractController
{
    protected $cacheType = [
        'none'           => TimberLoader::CACHE_NONE,
        'object'         => TimberLoader::CACHE_OBJECT,
        'transient'      => TimberLoader::CACHE_TRANSIENT,
        'site.transient' => TimberLoader::CACHE_SITE_TRANSIENT,
        'default'        => TimberLoader::CACHE_USE_DEFAULT,
    ];

    /**
     * Cache expires time
     *
     * @var int
     */
    protected $cacheExpiresSecond;

    /**
     * Cache mode.
     *
     * @var string
     */
    protected $cacheMode = TimberLoader::CACHE_USE_DEFAULT;

    /**
     * WP_Query instance.
     *
     * @var \WP_Query
     */
    protected $wpQuery;

    /**
     * AbstractController instance.
     */
    public function __construct()
    {
        if (!class_exists('TimberLoader')) {
            throw new RuntimeException(
                'Timber not activated. Make sure you activate the plugin in <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>'
            );
        }
    }

    /**
     * Set Wp_Query
     */
    public function setWpQuery(array $args)
    {
        $this->wpQuery = new WP_Query($args);
    }

    /**
     * Get Wp_Query
     *
     * @return \WP_Query
     */
    public function getWpQuery()
    {
        global $wp_query;
        return isset($this->wpQuery) ? $this->wpQuery : $wp_query;
    }

    /**
     * Set cache mode
     *
     * @param string  $mode
     *
     * @return self
     */
    public function setCacheMode($mode = 'default')
    {
        $this->cacheMode = $this->cacheType[$mode];

        return $this;
    }

    /**
     * Get cache mode
     *
     * @return \TimberLoader
     */
    public function getCacheMode()
    {
        return $this->cacheType[$this->cacheMode];
    }

    /**
     * Set cache expires seconds
     *
     * Timber will cache the template for 10 minutes (600 / 60 = 10).
     *
     * @param int $second
     *
     * @return self
     */
    public function setCacheExpiresSecond($second)
    {
        $this->cacheExpiresSecond = $second;

        return $this;
    }

    /**
     * Get cache expires seconds
     *
     * @return int
     */
    public function getCacheExpiresSecond()
    {
        return $this->cacheExpiresSecond;
    }

    /**
     * Get context
     *
     * @return array<string,\Timber|string>|null|array
     */
    abstract public function getContext();

    /**
     * Get template
     *
     * @return string[]
     */
    abstract public function getTemplates();

    /**
     * Get Post
     *
     * Works the same as AbstractController::addPostsToContext but limited to one post as the return object.
     *
     * @param string[]|boolean $args
     * @param string           $postClass
     *
     * @return \Timber
     */
    public function getPost($args = false, $postClass = '\TimberPost')
    {
        return Timber::get_post($args, $postClass);
    }

    /**
     * Get Posts
     *
     * @param string[]|boolean $args
     * @param string           $postClass
     * @param boolean          $collection
     *
     * @return \Timber
     */
    public function getPosts($args = false, $postClass = '\TimberPost', $collection = false)
    {
        return Timber::get_posts($args, $postClass, $collection);
    }

    /**
     *  Render template
     *
     * @return boolean|string|null
     */
    public function render()
    {
        $this->validateTemplates($this->getTemplates());
        $this->validateContext($this->getContext());

        Timber::render(
            $this->getTemplates(),
            array_merge(Timber::get_context(), $this->getContext()),
            // False disables cache altogether.
            ($this->getCacheExpiresSecond() ?: false),
            $this->getCacheMode()
        );
    }

    /**
     * Check if context is a array
     *
     * @param array|null $context<commit_msg>fixes #22<commit_after><?php

namespace Gwa\Wordpress\Template\Zero\Library;

/**
 * Zero - a PHP 5.4 Wordpress Theme.
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   2015 Great White Ark
 *
 * @link        http://www.greatwhiteark.com
 *
 * @license     MIT
 */

use LogicException;
use RuntimeException;
use Timber;
use TimberLoader;
use WP_Query;

/**
 * AbstractController.
 *
 * @author  GWA
 *
 */
abstract class AbstractController
{
    protected $cacheType = [
        'none'           => TimberLoader::CACHE_NONE,
        'object'         => TimberLoader::CACHE_OBJECT,
        'transient'      => TimberLoader::CACHE_TRANSIENT,
        'site.transient' => TimberLoader::CACHE_SITE_TRANSIENT,
        'default'        => TimberLoader::CACHE_USE_DEFAULT,
    ];

    /**
     * Cache expires time
     *
     * @var int
     */
    protected $cacheExpiresSecond;

    /**
     * Cache mode.
     *
     * @var string
     */
    protected $cacheMode = TimberLoader::CACHE_USE_DEFAULT;

    /**
     * WP_Query instance.
     *
     * @var \WP_Query
     */
    protected $wpQuery;

    /**
     * AbstractController instance.
     */
    public function __construct()
    {
        if (!class_exists('TimberLoader')) {
            throw new RuntimeException(
                'Timber not activated. Make sure you activate the plugin in <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>'
            );
        }
    }

    /**
     * Set Wp_Query
     */
    public function setWpQuery(array $args)
    {
        $this->wpQuery = new WP_Query($args);
    }

    /**
     * Get Wp_Query
     *
     * @return \WP_Query
     */
    public function getWpQuery()
    {
        global $wp_query;
        return isset($this->wpQuery) ? $this->wpQuery : $wp_query;
    }

    /**
     * Set cache mode
     *
     * @param string  $mode
     *
     * @return self
     */
    public function setCacheMode($mode = 'default')
    {
        $this->cacheMode = $this->cacheType[$mode];

        return $this;
    }

    /**
     * Get cache mode
     *
     * @return \TimberLoader
     */
    public function getCacheMode()
    {
        return $this->cacheType[$this->cacheMode];
    }

    /**
     * Set cache expires seconds
     *
     * Timber will cache the template for 10 minutes (600 / 60 = 10).
     *
     * @param int $second
     *
     * @return self
     */
    public function setCacheExpiresSecond($second)
    {
        $this->cacheExpiresSecond = $second;

        return $this;
    }

    /**
     * Get cache expires seconds
     *
     * @return int
     */
    public function getCacheExpiresSecond()
    {
        return $this->cacheExpiresSecond;
    }

    /**
     * Get context
     *
     * @return array<string,\Timber|string>|null|array
     */
    abstract public function getContext();

    /**
     * Get template
     *
     * @return string[]
     */
    abstract public function getTemplates();

    /**
     * Get Post
     *
     * Works the same as AbstractController::addPostsToContext but limited to one post as the return object.
     *
     * @param string[]|boolean $args
     * @param string           $postClass
     *
     * @return \Timber
     */
    public function getPost($args = false, $postClass = '\TimberPost')
    {
        return Timber::get_post($args, $postClass);
    }

    /**
     * Get Posts
     *
     * @param string[]|boolean $args
     * @param string           $postClass
     * @param boolean          $collection
     *
     * @return \Timber
     */
    public function getPosts($args = false, $postClass = '\TimberPost', $collection = false)
    {
        return Timber::get_posts($args, $postClass, $collection);
    }

    /**
     *  Render template
     *
     * @return boolean|string|null
     */
    public function render()
    {
        $context = $this->getContext();
        
        $this->validateTemplates($this->getTemplates());
        $this->validateContext($context);

        Timber::render(
            $this->getTemplates(),
            array_merge(Timber::get_context(), $context),
            // False disables cache altogether.
            ($this->getCacheExpiresSecond() ?: false),
            $this->getCacheMode()
        );
    }

    /**
     * Check if context is a array
     *
     * @param array|null $context
{'fraction_non_alphanumeric': 0.12709610269457616, 'fraction_numerical': 0.00370070544697583, 'mean_word_length': 1.8588429752066116, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1176847', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2568, 'n_words': 562}