| 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\Controller; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Joomla\Input\Input; |
| 17 |
use FilesystemIterator; |
| 18 |
use JchOptimize\Core\Admin\AdminHelper; |
| 19 |
use JchOptimize\Core\Admin\Icons; |
| 20 |
use JchOptimize\Core\Model\CacheMaintainer; |
| 21 |
use JchOptimize\Core\Mvc\Controller; |
| 22 |
use JchOptimize\Core\PageCache\CaptureCache; |
| 23 |
use JchOptimize\Core\Platform\PathsInterface; |
| 24 |
use JchOptimize\Core\Registry; |
| 25 |
use JchOptimize\Core\Settings; |
| 26 |
use JchOptimize\WordPress\Contracts\WillEnqueueAssets; |
| 27 |
use JchOptimize\WordPress\Model\NotificationIcons; |
| 28 |
use JchOptimize\WordPress\Model\PageCache as PageCacheModel; |
| 29 |
use JchOptimize\WordPress\View\MainHtml; |
| 30 |
use RecursiveDirectoryIterator; |
| 31 |
use RecursiveIteratorIterator; |
| 32 |
use UnexpectedValueException; |
| 33 |
|
| 34 |
use function admin_url; |
| 35 |
use function array_filter; |
| 36 |
use function count; |
| 37 |
use function get_option; |
| 38 |
use function in_array; |
| 39 |
use function is_dir; |
| 40 |
use function pathinfo; |
| 41 |
use function strtolower; |
| 42 |
|
| 43 |
use const PATHINFO_EXTENSION; |
| 44 |
|
| 45 |
class Main extends Controller implements WillEnqueueAssets |
| 46 |
{ |
| 47 |
use AuthorizesAdminRequestsTrait; |
| 48 |
|
| 49 |
public function __construct( |
| 50 |
private MainHtml $view, |
| 51 |
private Icons $icons, |
| 52 |
private NotificationIcons $notificationIcons, |
| 53 |
?Input $input |
| 54 |
) { |
| 55 |
parent::__construct($input); |
| 56 |
} |
| 57 |
|
| 58 |
public function execute(): bool |
| 59 |
{ |
| 60 |
$this->authorizeManageOptions(); |
| 61 |
if (JCH_PRO) { |
| 62 |
$this->getContainer()->get(CaptureCache::class)->updateHtaccess(); |
| 63 |
} |
| 64 |
|
| 65 |
[$cacheSize, $cacheFiles] = $this->getContainer()->get(CacheMaintainer::class)->getCacheSize(); |
| 66 |
$lastCleared = (int)get_option('jch_optimize_cache_last_cleared', 0); |
| 67 |
$pageCacheItems = $this->getContainer()->get(PageCacheModel::class)->getItems(); |
| 68 |
$adminHelper = $this->getContainer()->get(AdminHelper::class); |
| 69 |
$paths = $this->getContainer()->get(PathsInterface::class); |
| 70 |
$params = $this->getContainer()->get(Registry::class); |
| 71 |
$optimizedImages = count(array_filter($adminHelper->getOptimizedFiles())); |
| 72 |
$webpImages = $this->countFilesByExtension($paths->nextGenImagesPath(), ['webp']); |
| 73 |
$avifImages = $this->countFilesByExtension($paths->nextGenImagesPath(), ['avif']); |
| 74 |
|
| 75 |
$this->view->setData([ |
| 76 |
'tab' => 'main', |
| 77 |
'icons' => $this->icons, |
| 78 |
'notifications' => $this->notificationIcons->getNotificationIcons(), |
| 79 |
'optimizationSnapshot' => [ |
| 80 |
'cacheSize' => $cacheSize, |
| 81 |
'cacheFiles' => $cacheFiles, |
| 82 |
'lastCleared' => $lastCleared, |
| 83 |
'pageCacheCount' => count($pageCacheItems), |
| 84 |
'optimizedImages' => $optimizedImages, |
| 85 |
'webpImages' => $webpImages, |
| 86 |
'avifImages' => $avifImages, |
| 87 |
'webpAvifEnabled' => $params->isEnabled(Settings::LOAD_AVIF_WEBP_IMAGES), |
| 88 |
'optimizeImagesUrl' => admin_url('options-general.php?page=jch_optimize&tab=optimizeimages'), |
| 89 |
'pageCacheUrl' => admin_url('options-general.php?page=jch_optimize&tab=pagecache'), |
| 90 |
'configurationsUrl' => admin_url('options-general.php?page=jch_optimize&tab=configurations'), |
| 91 |
], |
| 92 |
]); |
| 93 |
|
| 94 |
echo $this->view->render(); |
| 95 |
|
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
public function enqueueAssets(): void |
| 100 |
{ |
| 101 |
$this->view->loadResources(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @param string[] $extensions |
| 106 |
*/ |
| 107 |
private function countFilesByExtension(string $path, array $extensions): int |
| 108 |
{ |
| 109 |
if (!is_dir($path)) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
try { |
| 114 |
$files = new RecursiveIteratorIterator( |
| 115 |
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
| 116 |
); |
| 117 |
} catch (UnexpectedValueException) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
$count = 0; |
| 122 |
|
| 123 |
foreach ($files as $file) { |
| 124 |
if ( |
| 125 |
$file->isFile() |
| 126 |
&& in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $extensions, true) |
| 127 |
) { |
| 128 |
$count++; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return $count; |
| 133 |
} |
| 134 |
} |
| 135 |
|