Optimizer.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Optimizer; |
| 4 | |
| 5 | use WPStaging\Framework\Filesystem\Filesystem; |
| 6 | |
| 7 | // No Direct Access |
| 8 | if (!defined("WPINC")) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Optimizer |
| 14 | */ |
| 15 | class Optimizer |
| 16 | { |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | private $mudir; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $source; |
| 26 | |
| 27 | /** |
| 28 | * @var string |
| 29 | */ |
| 30 | private $dest; |
| 31 | |
| 32 | /** |
| 33 | * Optimizer constructor. |
| 34 | * |
| 35 | * If changes are made to this, also check uninstall.php! |
| 36 | */ |
| 37 | public function __construct() |
| 38 | { |
| 39 | $this->mudir = ( defined('WPMU_PLUGIN_DIR') && defined('WPMU_PLUGIN_URL') ) ? WPMU_PLUGIN_DIR : trailingslashit(WP_CONTENT_DIR) . 'mu-plugins'; |
| 40 | |
| 41 | $this->source = trailingslashit(WPSTG_PLUGIN_DIR) . 'Backend/Optimizer/wp-staging-optimizer.php'; |
| 42 | $this->dest = trailingslashit($this->mudir) . 'wp-staging-optimizer.php'; |
| 43 | } |
| 44 | |
| 45 | public function installOptimizer(): bool |
| 46 | { |
| 47 | if (file_exists($this->dest) && $this->mustUpdateOptimizer() === false) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if ((new Filesystem())->mkdir($this->mudir)) { |
| 52 | return @copy($this->source, $this->dest); |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check if the Optimizer must use plugin must be updated |
| 60 | * @return bool |
| 61 | */ |
| 62 | private function mustUpdateOptimizer(): bool |
| 63 | { |
| 64 | $isVersionNumber = defined('WPSTG_OPTIMIZER_VERSION') ? WPSTG_OPTIMIZER_VERSION : false; |
| 65 | |
| 66 | $update = false; |
| 67 | |
| 68 | if ($isVersionNumber === false) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | $mustVersionNumber = defined('WPSTG_OPTIMIZER_MUVERSION') ? WPSTG_OPTIMIZER_MUVERSION : false; |
| 73 | |
| 74 | if ($mustVersionNumber) { |
| 75 | $update = version_compare($isVersionNumber, $mustVersionNumber, '!='); |
| 76 | } |
| 77 | |
| 78 | return $update; |
| 79 | } |
| 80 | } |
| 81 |