tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
/
Settings.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
Last commit date
Exceptions
4 years ago
Queue
2 years ago
Views
2 years ago
Api.php
2 years ago
Attachments.php
2 years ago
Backup.php
2 years ago
CLI.php
3 years ago
Compress.php
2 years ago
CompressDataService.php
3 years ago
CompressService.php
3 years ago
Config.php
2 years ago
Init.php
2 years ago
LastCompress.php
3 years ago
Logs.php
2 years ago
PreInit.php
3 years ago
Rest.php
2 years ago
Settings.php
3 years ago
Utils.php
1 year ago
Settings.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebIO; |
| 4 | |
| 5 | use TenWebWpTransients\OptimizerTransients; |
| 6 | |
| 7 | class Settings |
| 8 | { |
| 9 | private $settings; |
| 10 | private $aws_credentials; |
| 11 | private $stat; |
| 12 | private $request_api; |
| 13 | |
| 14 | public function __construct($request_api = true) |
| 15 | { |
| 16 | $this->request_api = $request_api; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * @param bool $force |
| 22 | * @param int $exclude_aws |
| 23 | * @param int $exclude_stat |
| 24 | * |
| 25 | * @return mixed |
| 26 | */ |
| 27 | public function getSettings($force = false, $exclude_aws = 0, $exclude_stat = 0) |
| 28 | { |
| 29 | $this->settings = get_option(TENWEBIO_PREFIX . '_settings'); |
| 30 | if (empty($this->settings) || $force) { |
| 31 | $this->getCompressSettings(0, $exclude_aws, $exclude_stat); |
| 32 | } |
| 33 | |
| 34 | return $this->settings; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param bool $force |
| 39 | * @param int $exclude_settings |
| 40 | * @param int $exclude_stat |
| 41 | * |
| 42 | * @return mixed |
| 43 | */ |
| 44 | public function getAwsCredentials($force = false, $exclude_settings = 0, $exclude_stat = 0) |
| 45 | { |
| 46 | $domain_id = get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id'); |
| 47 | $this->aws_credentials = OptimizerTransients::get(TENWEBIO_PREFIX . '_aws_credentials_' . $domain_id); |
| 48 | if (empty($this->aws_credentials) || $force) { |
| 49 | $this->getCompressSettings($exclude_settings, 0, $exclude_stat); |
| 50 | } |
| 51 | |
| 52 | return $this->aws_credentials; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param bool $force |
| 57 | * @param int $exclude_settings |
| 58 | * @param int $exclude_aws |
| 59 | * |
| 60 | * @return mixed |
| 61 | */ |
| 62 | public function getStat($force = false, $exclude_settings = 0, $exclude_aws = 0) |
| 63 | { |
| 64 | $this->stat = get_option(TENWEBIO_PREFIX . '_stat'); |
| 65 | if (empty($this->stat) || $force) { |
| 66 | $this->getCompressSettings($exclude_settings, $exclude_aws, 0); |
| 67 | } |
| 68 | |
| 69 | return $this->stat; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return void |
| 74 | */ |
| 75 | public static function purgeCompressSettings() |
| 76 | { |
| 77 | delete_option(TENWEBIO_PREFIX . '_stat'); |
| 78 | delete_option(TENWEBIO_PREFIX . '_settings'); |
| 79 | $domain_id = get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id'); |
| 80 | OptimizerTransients::delete(TENWEBIO_PREFIX . '_aws_credentials_' . $domain_id); |
| 81 | delete_transient(TENWEBIO_PREFIX . '_not_optimized_data'); |
| 82 | delete_transient(TENWEBIO_PREFIX . '_images_report'); |
| 83 | } |
| 84 | |
| 85 | public function settingActions() |
| 86 | { |
| 87 | $settings = $this->settings; |
| 88 | if ($settings["enable_auto_optimization"] == 0) { |
| 89 | wp_clear_scheduled_hook('tenwebio_compress_hook'); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param int $exclude_settings |
| 95 | * @param int $exclude_aws |
| 96 | * @param int $exclude_stat |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | |
| 101 | private function getCompressSettings($exclude_settings = 0, $exclude_aws = 0, $exclude_stat = 0) |
| 102 | { |
| 103 | if (!$this->request_api) { |
| 104 | return; |
| 105 | } |
| 106 | $api_instance = new Api(Api::API_COMPRESS_SETTINGS_ACTION); |
| 107 | $response = $api_instance->apiRequest('GET', array(), array( |
| 108 | 'exclude_aws_credentials' => $exclude_aws, |
| 109 | 'exclude_stat' => $exclude_stat, |
| 110 | 'exclude_settings' => $exclude_settings, |
| 111 | )); |
| 112 | if ($response) { |
| 113 | if (!empty($response['settings'])) { |
| 114 | update_option(TENWEBIO_PREFIX . '_settings', $response['settings']); |
| 115 | $this->settings = $response['settings']; |
| 116 | $this->settingActions(); |
| 117 | } |
| 118 | if (!empty($response['stat'])) { |
| 119 | update_option(TENWEBIO_PREFIX . '_stat', $response['stat']); |
| 120 | $this->stat = $response['stat']; |
| 121 | } |
| 122 | if (!empty($response['aws_credentials'])) { |
| 123 | $domain_id = get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id'); |
| 124 | OptimizerTransients::set(TENWEBIO_PREFIX . '_aws_credentials_' . $domain_id, $response['aws_credentials'], 17000); |
| 125 | $this->aws_credentials = $response['aws_credentials']; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |