| 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\Model; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Joomla\Filesystem\File; |
| 17 |
use _JchOptimizeVendor\V91\Psr\Http\Message\UploadedFileInterface; |
| 18 |
use Exception; |
| 19 |
use JchOptimize\Core\Mvc\Model; |
| 20 |
use JchOptimize\Core\Registry; |
| 21 |
use JchOptimize\Core\SystemUri\SystemUri; |
| 22 |
|
| 23 |
use function array_unique; |
| 24 |
use function in_array; |
| 25 |
use function pathinfo; |
| 26 |
use function strtolower; |
| 27 |
use function update_option; |
| 28 |
|
| 29 |
use const JCH_PLUGIN_DIR; |
| 30 |
use const PATHINFO_EXTENSION; |
| 31 |
|
| 32 |
class BulkSettings extends Model |
| 33 |
{ |
| 34 |
public function setDefaultSettings(): bool |
| 35 |
{ |
| 36 |
return update_option('jch-optimize_settings', []); |
| 37 |
} |
| 38 |
|
| 39 |
public function exportSettings(): string |
| 40 |
{ |
| 41 |
$file = JCH_PLUGIN_DIR . 'tmp/' . SystemUri::currentUri()->getHost() . '_jchoptimize_settings.json'; |
| 42 |
|
| 43 |
$params = $this->state->toString(); |
| 44 |
|
| 45 |
File::write($file, $params); |
| 46 |
|
| 47 |
return $file; |
| 48 |
} |
| 49 |
|
| 50 |
public function importSettings(UploadedFileInterface $uploadedFile): void |
| 51 |
{ |
| 52 |
$clientFilename = $uploadedFile->getClientFilename(); |
| 53 |
|
| 54 |
if ( |
| 55 |
$clientFilename === null |
| 56 |
|| strtolower(pathinfo($clientFilename, PATHINFO_EXTENSION)) !== 'json' |
| 57 |
) { |
| 58 |
throw new Exception('Only JSON settings files can be imported.'); |
| 59 |
} |
| 60 |
|
| 61 |
$clientMediaType = strtolower((string)$uploadedFile->getClientMediaType()); |
| 62 |
|
| 63 |
$allowedMediaTypes = ['application/json', 'application/octet-stream', 'text/json', 'text/plain']; |
| 64 |
|
| 65 |
if ($clientMediaType !== '' && !in_array($clientMediaType, $allowedMediaTypes, true)) { |
| 66 |
throw new Exception('Invalid settings file type.'); |
| 67 |
} |
| 68 |
|
| 69 |
$stream = $uploadedFile->getStream(); |
| 70 |
$stream->rewind(); |
| 71 |
$uploadedSettings = (new Registry())->loadString($stream->getContents()); |
| 72 |
|
| 73 |
if ($uploadedSettings->get('merge')) { |
| 74 |
$this->mergeSettings($uploadedSettings); |
| 75 |
} else { |
| 76 |
$this->setState($uploadedSettings); |
| 77 |
update_option('jch-optimize_settings', $uploadedSettings->toArray()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
private function mergeSettings(Registry $uploadedSettings): void |
| 82 |
{ |
| 83 |
$uploadedSettings->remove('merge'); |
| 84 |
|
| 85 |
foreach ($uploadedSettings as $setting => $value) { |
| 86 |
if (is_array($value)) { |
| 87 |
$mergedSetting = array_unique(array_merge($this->state->get($setting, []), $value)); |
| 88 |
} else { |
| 89 |
$mergedSetting = $value; |
| 90 |
} |
| 91 |
|
| 92 |
$this->state->set($setting, $mergedSetting); |
| 93 |
} |
| 94 |
|
| 95 |
update_option('jch-optimize_settings', $this->state->toArray()); |
| 96 |
} |
| 97 |
} |
| 98 |
|