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

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

118 lines 3.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) 2023 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\GuzzleHttp\Psr7\UploadedFile;
17 use _JchOptimizeVendor\V91\Joomla\Input\Input;
18 use Exception;
19 use JchOptimize\Core\Mvc\Controller;
20 use JchOptimize\WordPress\Log\WordpressNoticeLogger;
21 use JchOptimize\WordPress\Model\BulkSettings;
22
23 use function __;
24 use function check_admin_referer;
25 use function sprintf;
26 use function wp_redirect;
27
28 use const UPLOAD_ERR_CANT_WRITE;
29 use const UPLOAD_ERR_EXTENSION;
30 use const UPLOAD_ERR_FORM_SIZE;
31 use const UPLOAD_ERR_INI_SIZE;
32 use const UPLOAD_ERR_NO_FILE;
33 use const UPLOAD_ERR_NO_TMP_DIR;
34 use const UPLOAD_ERR_OK;
35 use const UPLOAD_ERR_PARTIAL;
36
37 class ImportSettings extends Controller
38 {
39 use AuthorizesAdminRequestsTrait;
40
41 public function __construct(private BulkSettings $bulkSettings, ?Input $input)
42 {
43 parent::__construct($input);
44 }
45
46
47 public function execute(): bool
48 {
49 $this->authorizeManageOptions();
50 check_admin_referer('jch_bulksettings');
51
52 /** @var Input $input */
53 $input = $this->getInput();
54 /** @var WordpressNoticeLogger $logger */
55 $logger = $this->logger;
56 /** @var array{tmp_name:string, size:int, error:int, name?:string, type?:string}|null $file */
57 $file = $input->files->get('file', []);
58
59 if (empty($file)) {
60 $logger->error(__('No file was uploaded'));
61
62 $this->redirect();
63
64 return false;
65 }
66
67 $uploadErrorMap = [
68 UPLOAD_ERR_OK => __('File uploaded successfully'),
69 UPLOAD_ERR_INI_SIZE => __('File exceeded the limit set in php.ini'),
70 UPLOAD_ERR_FORM_SIZE => __('File exceeded the value set in form'),
71 UPLOAD_ERR_PARTIAL => __(' File was only partially uploaded'),
72 UPLOAD_ERR_NO_FILE => __('No file was uploaded'),
73 UPLOAD_ERR_NO_TMP_DIR => __('No tmp directory configured for file upload'),
74 UPLOAD_ERR_CANT_WRITE => __('Upload directory not writable'),
75 UPLOAD_ERR_EXTENSION => __('An unknown extension prevented the file from being loaded')
76 ];
77
78 try {
79 $uploadedFile = new UploadedFile(
80 $file['tmp_name'],
81 $file['size'],
82 $file['error'],
83 $file['name'] ?? null,
84 $file['type'] ?? null
85 );
86
87 if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
88 throw new Exception($uploadErrorMap[$uploadedFile->getError()]);
89 }
90 } catch (Exception $exception) {
91 $logger->error(sprintf(__('Error uploaded file: %s'), $exception->getMessage()));
92
93 $this->redirect();
94
95 return false;
96 }
97
98 try {
99 $this->bulkSettings->importSettings($uploadedFile);
100 } catch (Exception $exception) {
101 $logger->error(sprintf(__('Error importing settings: %s'), $exception->getMessage()));
102
103 return false;
104 }
105
106 $logger->success(__('Settings successfully imported'));
107
108 $this->redirect();
109
110 return true;
111 }
112
113 private function redirect(): void
114 {
115 wp_redirect('options-general.php?page=jch_optimize');
116 }
117 }
118