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 / BrowserCaching.php

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

67 lines 2.1 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) 2021 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\Input\Input;
17 use JchOptimize\Core\Admin\AdminTasks;
18 use JchOptimize\Core\Mvc\Controller;
19 use JchOptimize\WordPress\Log\WordpressNoticeLogger;
20
21 use function __;
22 use function wp_redirect;
23
24 class BrowserCaching extends Controller
25 {
26 use AuthorizesAdminRequestsTrait;
27
28 public function __construct(private AdminTasks $tasks, ?Input $input = null)
29 {
30 parent::__construct($input);
31 }
32
33 public function execute(): bool
34 {
35 $this->authorizeManageOptions();
36 /** @var WordpressNoticeLogger $logger */
37 $logger = $this->logger;
38 $success = null;
39 $expires = $this->tasks->leverageBrowserCaching($success);
40
41 if ($success === false) {
42 $logger->error(
43 __('Failed trying to add browser caching codes to the .htaccess file', 'jch-optimize')
44 );
45 $result = false;
46 } elseif ($expires === 'FILEDOESNTEXIST') {
47 $logger->warning(__('No .htaccess file were found in the root of this site', 'jch-optimize'));
48 $result = false;
49 } elseif ($expires === 'CODEUPDATEDSUCCESS') {
50 $logger->success(__('The .htaccess file was updated successfully', 'jch-optimize'));
51 $result = true;
52 } elseif ($expires === 'CODEUPDATEDFAIL') {
53 $logger->error(__('Failed to update the .htaccess file', 'jch-optimize'));
54 $result = false;
55 } else {
56 $logger->success(
57 __('Successfully added codes to the .htaccess file to promote browser caching', 'jch-optimize')
58 );
59 $result = true;
60 }
61
62 wp_redirect('options-general.php?page=jch_optimize');
63
64 return $result;
65 }
66 }
67