Compression
2 years ago
Database
2 years ago
AbstractServiceProvider.php
1 year ago
Archiver.php
1 year ago
BackupAssets.php
2 years ago
BackupMetadataEditor.php
3 years ago
BackupSigner.php
2 years ago
BackupsFinder.php
1 year ago
Extractor.php
2 years ago
FileBackupService.php
1 year ago
FileBackupServiceProvider.php
1 year ago
ServiceInterface.php
1 year ago
ZlibCompressor.php
1 year ago
ZlibCompressor.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Service; |
| 4 | |
| 5 | use WPStaging\Backup\Service\Compression\CompressionInterface; |
| 6 | use WPStaging\Framework\Facades\Hooks; |
| 7 | |
| 8 | class ZlibCompressor |
| 9 | { |
| 10 | const FILTER_ZLIB_COMPRESSION_ENABLED = 'wpstg.backup.compression.zlib.enabled'; |
| 11 | |
| 12 | /** @var CompressionInterface */ |
| 13 | protected $service; |
| 14 | |
| 15 | public function __construct(CompressionInterface $service) |
| 16 | { |
| 17 | $this->service = $service; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @return bool Whether the server supports compression. |
| 22 | */ |
| 23 | public function supportsCompression(): bool |
| 24 | { |
| 25 | return function_exists('gzcompress') && function_exists('gzuncompress'); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @see \WPStaging\Backup\BackupServiceProvider::registerClasses For the filter. |
| 30 | * @return bool Whether the user can use compression. |
| 31 | */ |
| 32 | public function canUseCompression(): bool |
| 33 | { |
| 34 | static $canUseCompression = null; |
| 35 | |
| 36 | if (!is_null($canUseCompression)) { |
| 37 | return $canUseCompression; |
| 38 | } |
| 39 | |
| 40 | $isPro = defined('WPSTGPRO_VERSION'); |
| 41 | $license = get_option('wpstg_license_status'); |
| 42 | |
| 43 | $hasActiveLicense = is_object($license) && property_exists($license, 'license') && $license->license === 'valid'; |
| 44 | |
| 45 | $canUseCompression = $this->supportsCompression() && $isPro && ($hasActiveLicense || wpstg_is_local()); |
| 46 | |
| 47 | return $canUseCompression; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return bool True if compression is enabled, false if not. |
| 52 | */ |
| 53 | public function isCompressionEnabled(): bool |
| 54 | { |
| 55 | static $isEnabled = null; |
| 56 | |
| 57 | // Early bail: if compression feature not enabled. |
| 58 | if (!$this->isCompressionFeatureEnabled()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | if (is_null($isEnabled)) { |
| 63 | $settings = (object)get_option('wpstg_settings', []); |
| 64 | $isEnabled = $settings->enableCompression ?? false; |
| 65 | } |
| 66 | |
| 67 | $canUseCompression = $this->canUseCompression(); |
| 68 | |
| 69 | return Hooks::applyFilters(self::FILTER_ZLIB_COMPRESSION_ENABLED, $isEnabled && $canUseCompression); |
| 70 | } |
| 71 | |
| 72 | public function getService(): CompressionInterface |
| 73 | { |
| 74 | return $this->service; |
| 75 | } |
| 76 | |
| 77 | /** @return bool */ |
| 78 | protected function isCompressionFeatureEnabled(): bool |
| 79 | { |
| 80 | $enabled = (bool)Hooks::applyFilters('wpstg.tests.backup.enable_compression', defined('WPSTG_ENABLE_COMPRESSION') && WPSTG_ENABLE_COMPRESSION); |
| 81 | return $enabled; |
| 82 | } |
| 83 | } |
| 84 |