CloneOptions.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\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\Framework\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 | */ |
| 21 | const WPSTG_CLONE_SETTINGS_KEY = 'wpstg_clone_settings'; |
| 22 | |
| 23 | /** |
| 24 | * Get the value of the given option, |
| 25 | * If no option given return all settings |
| 26 | * |
| 27 | * @param string $option |
| 28 | * |
| 29 | * @return mixed |
| 30 | */ |
| 31 | public function get($option = null) |
| 32 | { |
| 33 | // Early bail if not a staging site |
| 34 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 39 | |
| 40 | // Return settings if no options given |
| 41 | if ($option === null) { |
| 42 | return $settings; |
| 43 | } |
| 44 | |
| 45 | // Early Bail: if settings is null or if settings isn't object |
| 46 | if ($settings === null || !is_object($settings)) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | // Early bail if given option not exists |
| 51 | if (!property_exists($settings, $option)) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | return $settings->{$option}; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the value of given option |
| 60 | * |
| 61 | * @param string $option |
| 62 | * @param mixed $value |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function set($option, $value) |
| 67 | { |
| 68 | // Early bail if not a staging site |
| 69 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 74 | |
| 75 | // If settings is null or if settings isn't object make settings a object |
| 76 | if ($settings === null || !is_object($settings)) { |
| 77 | $settings = new stdClass(); |
| 78 | } |
| 79 | |
| 80 | $settings->{$option} = $value; |
| 81 | |
| 82 | return update_option(self::WPSTG_CLONE_SETTINGS_KEY, $settings); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Delete the given option |
| 87 | * |
| 88 | * @param string $option |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function delete($option) |
| 93 | { |
| 94 | // Early bail if not a staging site |
| 95 | if (!WPStaging::make(SiteInfo::class)->isStagingSite()) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | $settings = get_option(self::WPSTG_CLONE_SETTINGS_KEY, null); |
| 100 | |
| 101 | // Early Bail: if settings is null or if settings isn't object |
| 102 | if ($settings === null || !is_object($settings)) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Early bail if given option not exists |
| 107 | if (!property_exists($settings, $option)) { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | unset($settings->{$option}); |
| 112 | |
| 113 | return update_option(self::WPSTG_CLONE_SETTINGS_KEY, $settings); |
| 114 | } |
| 115 | } |
| 116 |