Ajax
1 week ago
Dto
1 week ago
Interfaces
2 months ago
Jobs
1 week ago
Renderer
1 week ago
Service
1 day ago
Tasks
1 day ago
Traits
1 week ago
CloneOptions.php
5 months ago
FirstRun.php
4 months ago
Sites.php
1 week ago
StagingServiceProvider.php
1 week ago
CloneOptions.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging; |
| 4 | |
| 5 | use stdClass; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use WPStaging\Framework\SiteInfo; |
| 8 | |
| 9 | /** |
| 10 | * Class CloneOptions |
| 11 | * |
| 12 | * This is used to manage settings on the staging site |
| 13 | * |
| 14 | * @package WPStaging\Staging |
| 15 | */ |
| 16 | class CloneOptions |
| 17 | { |
| 18 | /** |
| 19 | * The option_name that is stored in the database to check first run is executed or not |
| 20 | * @var string |
| 21 | */ |
| 22 | const WPSTG_CLONE_SETTINGS_KEY = 'wpstg_clone_settings'; |
| 23 | |
| 24 | /** @var string */ |
| 25 | const FILTER_CLONING_TARGET_HOSTNAME = 'wpstg_cloning_target_hostname'; |
| 26 | |
| 27 | /** @var string */ |
| 28 | const FILTER_CLONING_TARGET_DIR = 'wpstg_cloning_target_dir'; |
| 29 | |
| 30 | /** |
| 31 | * Get the value of the given option, |
| 32 | * If no option given return all settings |
| 33 | * |
| 34 | * @param string|null $option |
| 35 | * |
| 36 | * @return mixed |
| 37 | */ |
| 38 | public function get($option = null, $default = null) |
| 39 | { |
| 40 | // Early bail if not a staging site |
| 41 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 42 | return $default; |
| 43 | } |
| 44 | |
| 45 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 46 | |
| 47 | // Return settings if no options given |
| 48 | if ($option === null) { |
| 49 | return $settings; |
| 50 | } |
| 51 | |
| 52 | // Early Bail: if settings is null or if settings isn't object |
| 53 | if ($settings === null || !is_object($settings)) { |
| 54 | return $default; |
| 55 | } |
| 56 | |
| 57 | // Early bail if given option not exists |
| 58 | if (!property_exists($settings, $option)) { |
| 59 | return $default; |
| 60 | } |
| 61 | |
| 62 | return $settings->{$option}; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the value of given option |
| 67 | * |
| 68 | * @param string $option |
| 69 | * @param mixed $value |
| 70 | * |
| 71 | * @return bool |
| 72 | */ |
| 73 | public function set(string $option, $value): bool |
| 74 | { |
| 75 | // Early bail if not a staging site |
| 76 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 81 | |
| 82 | // If settings is null or if settings isn't object make settings a object |
| 83 | if ($settings === null || !is_object($settings)) { |
| 84 | $settings = new stdClass(); |
| 85 | } |
| 86 | |
| 87 | $settings->{$option} = $value; |
| 88 | |
| 89 | return update_option(self::WPSTG_CLONE_SETTINGS_KEY, $settings); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Delete the given option |
| 94 | * |
| 95 | * @param string $option |
| 96 | * |
| 97 | * @return bool |
| 98 | */ |
| 99 | public function delete(string $option): bool |
| 100 | { |
| 101 | // Early bail if not a staging site |
| 102 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 107 | |
| 108 | // Early Bail: if settings is null or if settings isn't object |
| 109 | if ($settings === null || !is_object($settings)) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | // Early bail if given option not exists |
| 114 | if (!property_exists($settings, $option)) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | unset($settings->{$option}); |
| 119 | |
| 120 | return update_option(self::WPSTG_CLONE_SETTINGS_KEY, $settings); |
| 121 | } |
| 122 | } |
| 123 |