flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-persistence
/
src
/
Adapter
/
WooCommerce
/
WooCommerceSettingsContainer.php
flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-persistence
/
src
/
Adapter
/
WooCommerce
Last commit date
WooCommerceSettingsContainer.php
1 week ago
WooCommerceShippingInstanceContainer.php
1 week ago
WooCommerceSettingsContainer.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Persistence\Adapter\WooCommerce; |
| 4 | |
| 5 | use FSVendor\WPDesk\Persistence\ElementNotExistsException; |
| 6 | use FSVendor\WPDesk\Persistence\PersistentContainer; |
| 7 | /** |
| 8 | * Can store data using WooCommerce settings options. |
| 9 | * Use when want the abstract access to \WC_Settings_API. |
| 10 | * |
| 11 | * @package WPDesk\Persistence\WooCommerce |
| 12 | */ |
| 13 | final class WooCommerceSettingsContainer implements PersistentContainer |
| 14 | { |
| 15 | /** @var \WC_Settings_API */ |
| 16 | private $settings; |
| 17 | public function __construct(\WC_Settings_API $settings) |
| 18 | { |
| 19 | $this->settings = $settings; |
| 20 | } |
| 21 | public function get($id) |
| 22 | { |
| 23 | if (!$this->has($id)) { |
| 24 | throw new ElementNotExistsException(sprintf('Element %s not exists!', $id)); |
| 25 | } |
| 26 | return $this->settings->get_option($id); |
| 27 | } |
| 28 | public function has($id) |
| 29 | { |
| 30 | return isset($this->settings->settings[$id]); |
| 31 | } |
| 32 | public function set($id, $value) |
| 33 | { |
| 34 | if (version_compare(\WC_VERSION, '3.4', '>=')) { |
| 35 | $this->settings->update_option($id, $value); |
| 36 | } else { |
| 37 | $this->settings->settings[$id] = $value; |
| 38 | $this->update_db_using_wordpress(); |
| 39 | } |
| 40 | } |
| 41 | /** |
| 42 | * WC_Settings_API is so great that sometimes we have to manually update the data using WP update_option. |
| 43 | * |
| 44 | * @see \WC_Settings_API::process_admin_options |
| 45 | */ |
| 46 | private function update_db_using_wordpress() |
| 47 | { |
| 48 | update_option($this->settings->get_option_key(), apply_filters('woocommerce_settings_api_sanitized_fields_' . $this->settings->id, $this->settings->settings), 'yes'); |
| 49 | } |
| 50 | public function delete($id) |
| 51 | { |
| 52 | $form_fields = $this->settings->get_form_fields(); |
| 53 | if (isset($form_fields[$id])) { |
| 54 | $this->settings->settings[$id] = $this->settings->get_field_default($form_fields[$id]); |
| 55 | } else { |
| 56 | unset($this->settings->settings[$id]); |
| 57 | } |
| 58 | $this->update_db_using_wordpress(); |
| 59 | } |
| 60 | } |
| 61 |