| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/wordpress-platform |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2022 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\WordPress\Model; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Joomla\DI\Container; |
| 17 |
use _JchOptimizeVendor\V91\Joomla\Input\Input; |
| 18 |
use Exception; |
| 19 |
use JchOptimize\Core\Mvc\Model; |
| 20 |
use JchOptimize\Core\PageCache\CaptureCache; |
| 21 |
use JchOptimize\Core\PageCache\PageCache as CorePageCache; |
| 22 |
use JchOptimize\Core\Registry; |
| 23 |
|
| 24 |
use function get_transient; |
| 25 |
use function is_array; |
| 26 |
use function is_null; |
| 27 |
use function set_transient; |
| 28 |
|
| 29 |
class PageCache extends Model |
| 30 |
{ |
| 31 |
/** |
| 32 |
* name of currently used cache adapter |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private string $adapter; |
| 37 |
|
| 38 |
public function __construct(Input $input, private CorePageCache $pageCache, Container $container) |
| 39 |
{ |
| 40 |
$this->setContainer($container); |
| 41 |
|
| 42 |
try { |
| 43 |
$registry = $this->populateRegistryFromRequest($input, ['filter', 'list']); |
| 44 |
} catch (Exception $e) { |
| 45 |
$registry = new Registry(); |
| 46 |
} |
| 47 |
|
| 48 |
$this->setState($registry); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @param Input $input |
| 53 |
* @param string[] $keys |
| 54 |
* @return Registry |
| 55 |
*/ |
| 56 |
private function populateRegistryFromRequest(Input $input, array $keys): Registry |
| 57 |
{ |
| 58 |
$data = new Registry(); |
| 59 |
|
| 60 |
foreach ($keys as $key) { |
| 61 |
//Check for value from input first |
| 62 |
/** @var string[]|null $requestKey */ |
| 63 |
$requestKey = $input->get($key); |
| 64 |
|
| 65 |
if (is_null($requestKey)) { |
| 66 |
//Not found, let's check the transients |
| 67 |
/** @var string[]|null $requestKey */ |
| 68 |
$requestKey = get_transient('jch_optimize_state_' . $key); |
| 69 |
} |
| 70 |
|
| 71 |
//If we've got one by now let's set it in registry |
| 72 |
if (! empty($requestKey) && is_array($requestKey)) { |
| 73 |
foreach ($requestKey as $requestName => $requestValue) { |
| 74 |
if (! empty($requestValue) || $requestValue == '0') { |
| 75 |
$data->set($key . '_' . $requestName, $requestValue); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
//Let's save this one in the transient |
| 80 |
set_transient('jch_optimize_state_' . $key, $requestKey, 300); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $data; |
| 85 |
} |
| 86 |
|
| 87 |
public function updateHtaccess(): void |
| 88 |
{ |
| 89 |
if (JCH_PRO && $this->pageCache instanceof CaptureCache) { |
| 90 |
/** @see CaptureCache::updateHtaccess() */ |
| 91 |
$this->getContainer()->get(CaptureCache::class)->updateHtaccess(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public function getItems(): array |
| 96 |
{ |
| 97 |
$filters = [ |
| 98 |
'time-1', |
| 99 |
'time-2', |
| 100 |
'search', |
| 101 |
'device', |
| 102 |
'adapter', |
| 103 |
'http-request' |
| 104 |
]; |
| 105 |
|
| 106 |
foreach ($filters as $filter) { |
| 107 |
/** @var string|null $filterState */ |
| 108 |
$filterState = $this->getState()->get("filter_{$filter}"); |
| 109 |
|
| 110 |
if (! empty($filterState)) { |
| 111 |
$this->pageCache->setFilter("filter_{$filter}", $filterState); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** @var string|null $fullOrderingList */ |
| 116 |
$fullOrderingList = $this->getState()->get('list_fullordering'); |
| 117 |
|
| 118 |
if (! empty($fullOrderingList)) { |
| 119 |
$this->pageCache->setList('list_fullordering', str_replace('_', ' ', $fullOrderingList)); |
| 120 |
} |
| 121 |
|
| 122 |
return $this->pageCache->getItems(); |
| 123 |
} |
| 124 |
|
| 125 |
public function delete(array $ids): bool |
| 126 |
{ |
| 127 |
return $this->pageCache->deleteItemsByIds($ids); |
| 128 |
} |
| 129 |
|
| 130 |
public function deleteAll(): bool |
| 131 |
{ |
| 132 |
return $this->pageCache->deleteAllItems(); |
| 133 |
} |
| 134 |
|
| 135 |
public function getAdapterName(): string |
| 136 |
{ |
| 137 |
return $this->pageCache->getAdapterName(); |
| 138 |
} |
| 139 |
|
| 140 |
public function isCaptureCacheEnabled(): bool |
| 141 |
{ |
| 142 |
return $this->pageCache->isCaptureCacheEnabled(); |
| 143 |
} |
| 144 |
} |
| 145 |
|