Optimizer.php
96 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 | /** |
| 46 | * Install Optimizer |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function installOptimizer(): bool |
| 51 | { |
| 52 | if (file_exists($this->dest) && $this->mustUpdateOptimizer() === false) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (file_exists($this->dest) && !is_writable($this->dest)) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (!(new Filesystem())->mkdir($this->mudir)) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // Avoid emitting E_WARNING from copy() when destination is not writable. |
| 65 | if (!is_writable($this->mudir)) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return @copy($this->source, $this->dest); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check if the Optimizer must use plugin must be updated |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | private function mustUpdateOptimizer(): bool |
| 78 | { |
| 79 | $isVersionNumber = defined('WPSTG_OPTIMIZER_VERSION') ? WPSTG_OPTIMIZER_VERSION : false; |
| 80 | |
| 81 | $update = false; |
| 82 | |
| 83 | if ($isVersionNumber === false) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | $mustVersionNumber = defined('WPSTG_OPTIMIZER_MUVERSION') ? WPSTG_OPTIMIZER_MUVERSION : false; |
| 88 | |
| 89 | if ($mustVersionNumber) { |
| 90 | $update = version_compare($isVersionNumber, $mustVersionNumber, '!='); |
| 91 | } |
| 92 | |
| 93 | return $update; |
| 94 | } |
| 95 | } |
| 96 |