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