<?php
/**
 * GitHub auto update.
 * 
 * @package rundiz-downloads
 */


namespace RundizDownloads\App\Controllers\Front\Hooks\Query;


if (!class_exists('\\RundizDownloads\\App\\Controllers\\Front\\Hooks\\Query\\GithubAutoUpdatePage')) {
    /**
     * GithubAutoUpdatePage class.
     */
    class GithubAutoUpdatePage implements \RundizDownloads\App\Controllers\ControllerInterface
    {


        use \RundizDownloads\App\AppTrait;


        /**
         * Accept GitHub webhook for auto update.
         * 
         * This method contain `exit()` function, after call to this method where pagename is matched the process will be stopped.
         */
        public function githubAutoUpdatePage()
        {
            if (get_query_var('pagename') !== 'rundiz_downloads_github_autoupdate') {
                return false;
            }

            $Github = new \RundizDownloads\App\Libraries\Github();
            if ($Github->isSettingToAutoUpdate() !== true) {
                return false;
            }
            unset($Github);

            $GitHubAutoUpdatePage = new GithubAutoUpdatePage\GithubAutoUpdatePage();
            $GitHubAutoUpdatePage->pageIndex();
            unset($GitHubAutoUpdatePage);

            exit();// required to display just thispage, otherwise the normal WordPress page will be render.
        }// githubAutoUpdatePage


        /**
         * {@inheritDoc}
         */
        public function registerHooks()
        {
            if (!is_admin()) {
                add_action('template_redirect', [$this, 'githubAutoUpdatePage']);
            }
        }// registerHooks


    }// GithubAutoUpdatePage
}
