<?php
/**
 * @package         Cache Cleaner
 * @version         8.2.2
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://regularlabs.com
 * @copyright       Copyright © 2022 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Language\Text as JText;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Extension as RL_Extension;
use RegularLabs\Library\SystemPlugin as RL_SystemPlugin;
use RegularLabs\Plugin\System\CacheCleaner\Cache;

if ( ! is_file(JPATH_LIBRARIES . '/regularlabs/regularlabs.xml')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/SystemPlugin.php')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/DownloadKey.php')
)
{
    JFactory::getLanguage()->load('plg_system_cachecleaner', __DIR__);
    JFactory::getApplication()->enqueueMessage(
        JText::sprintf('AA_EXTENSION_CAN_NOT_FUNCTION', JText::_('CACHECLEANER'))
        . ' ' . JText::_('AA_REGULAR_LABS_LIBRARY_NOT_INSTALLED'),
        'error'
    );

    return;
}

if ( ! RL_Document::isJoomlaVersion(4, 'CACHECLEANER'))
{
    RL_Extension::disable('cachecleaner', 'plugin');

    RL_Document::adminError(
        JText::sprintf('RL_PLUGIN_HAS_BEEN_DISABLED', JText::_('CACHECLEANER'))
    );

    return;
}

if (true)
{
    class PlgSystemCacheCleaner extends RL_SystemPlugin
    {
        public $_lang_prefix     = 'CC';
        public $_page_types      = ['html', 'ajax', 'json', 'raw'];
        public $_enable_in_admin = true;
        public $_jversion        = 4;

        public function handleOnAfterRoute()
        {
            Cache::clean();
        }

        protected function changeFinalHtmlOutput(&$html)
        {
            return true;
        }

        protected function cleanFinalHtmlOutput(&$html)
        {
            $html = str_replace(['{nocdn}', '{/nocdn}'], '', $html);
        }
    }
}
