PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / Controller / PageCache.php

PageCache.php in JCH Optimize trunk, at src/Controller/PageCache.php

157 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Controller;
15
16 use _JchOptimizeVendor\V91\Joomla\DI\Container;
17 use _JchOptimizeVendor\V91\Joomla\Input\Input;
18 use JchOptimize\Core\Laminas\ArrayPaginator;
19 use JchOptimize\Core\Mvc\Controller;
20 use JchOptimize\Core\Registry;
21 use JchOptimize\WordPress\Contracts\WillEnqueueAssets;
22 use JchOptimize\WordPress\Log\WordpressNoticeLogger;
23 use JchOptimize\WordPress\Model\PageCache as PageCacheModel;
24 use JchOptimize\WordPress\Model\ReCache;
25 use JchOptimize\WordPress\Plugin\Admin;
26 use JchOptimize\WordPress\View\PageCacheHtml;
27
28 use function __;
29 use function check_admin_referer;
30 use function wp_redirect;
31
32 class PageCache extends Controller implements WillEnqueueAssets
33 {
34 use AuthorizesAdminRequestsTrait;
35
36 private ?ReCache $reCache = null;
37
38 public function __construct(
39 private Registry $params,
40 private PageCacheHtml $view,
41 private PageCacheModel $model,
42 Container $container,
43 ?Input $input = null
44 ) {
45 parent::__construct($input);
46
47 $this->setContainer($container);
48
49 if (JCH_PRO) {
50 $this->reCache = $this->getContainer()->get(ReCache::class);
51 }
52 }
53
54 private string $redirect = 'options-general.php?page=jch_optimize&tab=pagecache';
55
56 /**
57 * @inheritDoc
58 */
59 public function execute(): bool
60 {
61 $this->authorizeAnyCapability(['manage_options', 'jch_clear_page_cache']);
62 /** @var WordpressNoticeLogger $logger */
63 $logger = $this->logger;
64 /** @var Input $input */
65 $input = $this->getInput();
66
67 if ($input->get('action') !== null) {
68 check_admin_referer('jch_pagecache');
69 }
70
71 if ($input->get('action') == 'delete') {
72 if ($input->get('cid')) {
73 $success = $this->model->delete((array)$input->get('cid'));
74 } else {
75 $success = false;
76 }
77 }
78
79 if ($input->get('action') == 'deleteall') {
80 $success = $this->model->deleteAll();
81 }
82
83 if (JCH_PRO && $input->get('action') == 'recache') {
84 $this->reCache->runOnce();
85 $logger->success(__('ReCache successfully started.'));
86
87 wp_redirect($this->redirect);
88 exit();
89 }
90
91 if (isset($success)) {
92 if ($success) {
93 $logger->success(__('Page cache deleted successfully.'));
94 } else {
95 $logger->error(__('Error deleting page cache.'));
96 }
97
98 wp_redirect($this->redirect);
99 exit();
100 }
101
102 if (!$this->params->get('cache_enable', '0')) {
103 $logger->warning(
104 __(
105 'Page Cache is not enabled. Please enable it on the Dashboard or Configurations tab.'
106 . ' You may also want to disable other page cache plugins.'
107 )
108 );
109 Admin::publishAdminNotices();
110 }
111
112 $this->model->updateHtaccess();
113
114 $limit = (int)$this->model->getState()->get('list_limit', '20');
115 $page = (int)$input->get('list_page', '1');
116
117 $paginator = new ArrayPaginator($this->model->getItems());
118 $paginator->setCurrentPageNumber($page)
119 ->setItemCountPerPage($limit);
120
121 $this->view->setData([
122 'items' => $paginator,
123 'tab' => 'pagecache',
124 'paginator' => $paginator->getPages(),
125 'pageLink' => $this->redirect,
126 'action' => $this->redirect,
127 'adapter' => $this->model->getAdapterName(),
128 'httpRequest' => $this->model->isCaptureCacheEnabled(),
129 ]);
130
131 $this->view->renderStatefulElements($this->model->getState());
132
133 echo $this->view->render();
134
135 return true;
136 }
137
138 public function setRedirect(string $redirect): PageCache
139 {
140 $this->redirect = $redirect;
141
142 return $this;
143 }
144
145 public function setLayout(string $layout): PageCache
146 {
147 $this->view->getRenderer()->getRenderer()->setLayout($layout);
148
149 return $this;
150 }
151
152 public function enqueueAssets(): void
153 {
154 $this->view->loadResources();
155 }
156 }
157