| 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) 2020 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\Plugin; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Joomla\DI\Container; |
| 17 |
use _JchOptimizeVendor\V91\Joomla\DI\ContainerAwareInterface; |
| 18 |
use _JchOptimizeVendor\V91\Joomla\DI\ContainerAwareTrait; |
| 19 |
use _JchOptimizeVendor\V91\Joomla\Filesystem\File; |
| 20 |
use _JchOptimizeVendor\V91\Joomla\Filesystem\Folder; |
| 21 |
use Exception; |
| 22 |
use JchOptimize\Core\Admin\AdminHelper; |
| 23 |
use JchOptimize\Core\Admin\AdminTasks; |
| 24 |
use JchOptimize\Core\Model\CacheMaintainer; |
| 25 |
use JchOptimize\Core\Registry; |
| 26 |
use JchOptimize\WordPress\Model\ReCache; |
| 27 |
|
| 28 |
use function defined; |
| 29 |
use function delete_option; |
| 30 |
use function dirname; |
| 31 |
use function file_exists; |
| 32 |
use function file_get_contents; |
| 33 |
use function is_dir; |
| 34 |
use function json_decode; |
| 35 |
use function md5_file; |
| 36 |
use function update_option; |
| 37 |
|
| 38 |
use const ABSPATH; |
| 39 |
use const JCH_PLUGIN_DIR; |
| 40 |
use const JCH_PRO; |
| 41 |
use const JCH_VERSION; |
| 42 |
use const WPMU_PLUGIN_DIR; |
| 43 |
|
| 44 |
class Installer implements ContainerAwareInterface |
| 45 |
{ |
| 46 |
use ContainerAwareTrait; |
| 47 |
|
| 48 |
private ?ReCache $recache = null; |
| 49 |
|
| 50 |
public function __construct( |
| 51 |
private Registry $params, |
| 52 |
private AdminTasks $tasks, |
| 53 |
private AdminHelper $helper, |
| 54 |
private CacheMaintainer $cacheMaintainer, |
| 55 |
Container $container |
| 56 |
) { |
| 57 |
$this->setContainer($container); |
| 58 |
|
| 59 |
if (JCH_PRO) { |
| 60 |
$this->recache = $this->getContainer()->get(ReCache::class); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Fires when plugin is activated and create a dir.php file in plugin root containing |
| 66 |
* absolute path of plugin install |
| 67 |
*/ |
| 68 |
public function activate(): void |
| 69 |
{ |
| 70 |
$file = JCH_PLUGIN_DIR . 'dir.php'; |
| 71 |
$absPath = ABSPATH; |
| 72 |
$code = <<<PHPCODE |
| 73 |
<?php |
| 74 |
|
| 75 |
\$DIR = '$absPath'; |
| 76 |
|
| 77 |
PHPCODE; |
| 78 |
|
| 79 |
File::write($file, $code); |
| 80 |
$this->tasks->leverageBrowserCaching(); |
| 81 |
|
| 82 |
$this->installMUPlugin(); |
| 83 |
$this->rescheduleRecacheCron(); |
| 84 |
} |
| 85 |
|
| 86 |
public function installMUPlugin(): void |
| 87 |
{ |
| 88 |
if (JCH_PRO) { |
| 89 |
// Copy the mu-plugins in the correct folder |
| 90 |
|
| 91 |
$mu_folder = $this->getMUPluginDir(); |
| 92 |
|
| 93 |
if (!is_dir($mu_folder)) { |
| 94 |
Folder::create($mu_folder); |
| 95 |
} |
| 96 |
|
| 97 |
$src = JCH_PLUGIN_DIR . 'mu-plugins/jch-optimize-mode-switcher.php'; |
| 98 |
$target = $mu_folder . '/jch-optimize-mode-switcher.php'; |
| 99 |
|
| 100 |
$header = <<<PHP |
| 101 |
<?php |
| 102 |
|
| 103 |
/** |
| 104 |
* Plugin Name: JCH Optimize Mode Switcher |
| 105 |
* Plugin URI: https://www.jch-optimize.net/ |
| 106 |
* Description: Boost your WordPress site's performance with JCH Optimize as measured on PageSpeed |
| 107 |
* Version: {VERSION} |
| 108 |
* Author: Samuel Marshall |
| 109 |
* License: GNU/GPLv3 |
| 110 |
*/ |
| 111 |
PHP; |
| 112 |
|
| 113 |
$buffer = str_replace(['<?php', '{VERSION}'], [$header, JCH_VERSION], file_get_contents($src)); |
| 114 |
File::write($target, $buffer); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private function getMUPluginDir(): string |
| 119 |
{ |
| 120 |
$mu_folder = ABSPATH . 'wp-content/mu-plugins'; |
| 121 |
if (defined('WPMU_PLUGIN_DIR') && WPMU_PLUGIN_DIR) { |
| 122 |
$mu_folder = WPMU_PLUGIN_DIR; |
| 123 |
} |
| 124 |
|
| 125 |
return $mu_folder; |
| 126 |
} |
| 127 |
|
| 128 |
public function deactivate(): void |
| 129 |
{ |
| 130 |
delete_option('jch-optimize_settings'); |
| 131 |
|
| 132 |
$this->cacheMaintainer->cleanCache(); |
| 133 |
$this->tasks->cleanHtaccess(); |
| 134 |
$this->deleteMUPlugin(); |
| 135 |
$this->clearRecacheCron(); |
| 136 |
} |
| 137 |
|
| 138 |
public function deleteMUPlugin(): void |
| 139 |
{ |
| 140 |
if (JCH_PRO) { |
| 141 |
$mu_folder = $this->getMUPluginDir(); |
| 142 |
|
| 143 |
if (defined('WPMU_PLUGIN_DIR') && WPMU_PLUGIN_DIR) { |
| 144 |
$mu_folder = WPMU_PLUGIN_DIR; |
| 145 |
} |
| 146 |
|
| 147 |
try { |
| 148 |
File::delete($mu_folder . '/jch-optimize-mode-switcher.php'); |
| 149 |
} catch (Exception $e) { |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public function updateSettings(): void |
| 155 |
{ |
| 156 |
//Update new Load WEBP setting |
| 157 |
/** @var string|null $loadWebp */ |
| 158 |
$loadWebp = $this->params->get('pro_load_webp_images'); |
| 159 |
/** @var string|null $nextGenImages */ |
| 160 |
$nextGenImages = $this->params->get('pro_next_gen_images'); |
| 161 |
|
| 162 |
if (is_null($loadWebp) && $nextGenImages) { |
| 163 |
$this->params->set('pro_load_webp_images', '1'); |
| 164 |
} |
| 165 |
|
| 166 |
//Update Exclude JavaScript settings |
| 167 |
$oldJsSettings = [ |
| 168 |
'excludeJs_peo', |
| 169 |
'excludeJsComponents_peo', |
| 170 |
'excludeScripts_peo', |
| 171 |
'excludeJs', |
| 172 |
'excludeJsComponents', |
| 173 |
'excludeScripts', |
| 174 |
'dontmoveJs', |
| 175 |
'dontmoveScripts', |
| 176 |
]; |
| 177 |
|
| 178 |
$updateJsSettings = false; |
| 179 |
|
| 180 |
foreach ($oldJsSettings as $oldJsSetting) { |
| 181 |
/** @var array $oldJsSettingValue */ |
| 182 |
$oldJsSettingValue = json_decode(json_encode($this->params->get($oldJsSetting)), true); |
| 183 |
|
| 184 |
if ($oldJsSettingValue) { |
| 185 |
$firstValue = array_shift($oldJsSettingValue); |
| 186 |
if (!isset($firstValue['url']) && !isset($firstValue['script'])) { |
| 187 |
$updateJsSettings = true; |
| 188 |
} |
| 189 |
|
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if ($updateJsSettings) { |
| 195 |
$dontMoveJs = (array)$this->params->get('excludeJs'); |
| 196 |
$dontMoveScripts = (array)$this->params->get('dontmoveScripts'); |
| 197 |
$this->params->remove('dontmoveJs'); |
| 198 |
$this->params->remove('dontmoveScripts'); |
| 199 |
|
| 200 |
/** @var array<string, array{ieo:string, valueType: string, dontmove: array<array-key, string>}> $excludeJsPeoSettingsMap */ |
| 201 |
$excludeJsPeoSettingsMap = [ |
| 202 |
'excludeJs_peo' => [ |
| 203 |
'ieo' => 'excludeJs', |
| 204 |
'valueType' => 'url', |
| 205 |
'dontmove' => $dontMoveJs |
| 206 |
], |
| 207 |
'excludeJsComponents_peo' => [ |
| 208 |
'ieo' => 'excludeJsComponents', |
| 209 |
'valueType' => 'url', |
| 210 |
'dontmove' => $dontMoveJs |
| 211 |
], |
| 212 |
'excludeScripts_peo' => [ |
| 213 |
'ieo' => 'excludeScripts', |
| 214 |
'valueType' => 'script', |
| 215 |
'dontmove' => $dontMoveScripts |
| 216 |
] |
| 217 |
]; |
| 218 |
|
| 219 |
foreach ($excludeJsPeoSettingsMap as $excludeJsPeoSettingName => $settingsMap) { |
| 220 |
/** @var string[] $excludeJsPeoSetting */ |
| 221 |
$excludeJsPeoSetting = (array)$this->params->get($excludeJsPeoSettingName); |
| 222 |
$this->params->remove($excludeJsPeoSettingName); |
| 223 |
$newExcludeJs_peo = []; |
| 224 |
$i = 0; |
| 225 |
|
| 226 |
foreach ($excludeJsPeoSetting as $excludeJsPeoSettingValue) { |
| 227 |
$newExcludeJs_peo[$i][$settingsMap['valueType']] = $excludeJsPeoSettingValue; |
| 228 |
|
| 229 |
foreach ($settingsMap['dontmove'] as $dontMoveValue) { |
| 230 |
if (str_contains($excludeJsPeoSettingValue, $dontMoveValue)) { |
| 231 |
$newExcludeJs_peo[$i]['dontmove'] = 'on'; |
| 232 |
} |
| 233 |
} |
| 234 |
$i++; |
| 235 |
} |
| 236 |
|
| 237 |
/** @var string[] $excludeJsIeoSetting */ |
| 238 |
$excludeJsIeoSetting = $this->params->get($settingsMap['ieo']); |
| 239 |
$this->params->remove($settingsMap['ieo']); |
| 240 |
|
| 241 |
foreach ($excludeJsIeoSetting as $excludeJsIeoSettingValue) { |
| 242 |
$i++; |
| 243 |
$newExcludeJs_peo[$i][$settingsMap['valueType']] = $excludeJsIeoSettingValue; |
| 244 |
$newExcludeJs_peo[$i]['ieo'] = 'on'; |
| 245 |
|
| 246 |
foreach ($settingsMap['dontmove'] as $dontMoveValue) { |
| 247 |
if (str_contains($excludeJsIeoSettingValue, $dontMoveValue)) { |
| 248 |
$newExcludeJs_peo[$i]['dontmove'] = 'on'; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$this->params->set($excludeJsPeoSettingName, $newExcludeJs_peo); |
| 254 |
} |
| 255 |
|
| 256 |
update_option('jch-optimize_settings', $this->params->toArray()); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public function updateMUPlugins(): void |
| 261 |
{ |
| 262 |
$mu_folder = $this->getMUPluginDir(); |
| 263 |
|
| 264 |
$installedMU = $mu_folder . '/jch-optimize-mode-switcher.php'; |
| 265 |
|
| 266 |
|
| 267 |
if ( |
| 268 |
file_exists($installedMU) |
| 269 |
&& md5_file( |
| 270 |
JCH_PLUGIN_DIR . 'mu-plugins/jch-optimize-mode-switcher.php' |
| 271 |
) !== md5_file($installedMU) |
| 272 |
) { |
| 273 |
$this->installMUPlugin(); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
public function fixMetaFileSecurity(): void |
| 278 |
{ |
| 279 |
$metaFile = $this->helper->getMetaFile(); |
| 280 |
$metaFileDir = dirname($metaFile); |
| 281 |
|
| 282 |
if ( |
| 283 |
file_exists($metaFile) |
| 284 |
&& (!file_exists($metaFileDir . '/index.html') |
| 285 |
|| !file_exists($metaFileDir . '/.htaccess')) |
| 286 |
) { |
| 287 |
$optimizedFiles = $this->helper->getOptimizedFiles(); |
| 288 |
File::delete($metaFile); |
| 289 |
|
| 290 |
foreach ($optimizedFiles as $files) { |
| 291 |
$this->helper->markOptimized($files); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
private function rescheduleRecacheCron(): void |
| 297 |
{ |
| 298 |
$this->recache?->reSchedule(); |
| 299 |
} |
| 300 |
|
| 301 |
private function clearRecacheCron(): void |
| 302 |
{ |
| 303 |
$this->recache?->clearSchedule(); |
| 304 |
} |
| 305 |
} |
| 306 |
|