From 86cffacdbe6bbcd3fa41b7788eeac06d6215267f Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Thu, 17 Oct 2019 05:48:57 +0100 Subject: [PATCH] Add WooCommerceSettings endpoint [MAILPOET-2278] --- lib/API/JSON/v1/WoocommerceSettings.php | 33 +++++++++++++++++ lib/DI/ContainerConfigurator.php | 1 + .../API/JSON/v1/WoocommerceSettingsTest.php | 37 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 lib/API/JSON/v1/WoocommerceSettings.php create mode 100644 tests/integration/API/JSON/v1/WoocommerceSettingsTest.php diff --git a/lib/API/JSON/v1/WoocommerceSettings.php b/lib/API/JSON/v1/WoocommerceSettings.php new file mode 100644 index 0000000000..6b433d940c --- /dev/null +++ b/lib/API/JSON/v1/WoocommerceSettings.php @@ -0,0 +1,33 @@ + AccessControl::PERMISSION_MANAGE_EMAILS, + ]; + + private $allowed_settings = [ + 'woocommerce_email_base_color', + ]; + + /** @var WPFunctions */ + private $wp; + + function __construct(WPFunctions $wp) { + $this->wp = $wp; + } + + function set($data = []) { + foreach ($data as $option => $value) { + if (in_array($option, $this->allowed_settings)) { + $this->wp->updateOption($option, $value); + } + } + return $this->successResponse([]); + } +} diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index aa9501422b..22e4470012 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -84,6 +84,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\API\JSON\v1\SendingTaskSubscribers::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\Setup::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\Subscribers::class)->setPublic(true); + $container->autowire(\MailPoet\API\JSON\v1\WoocommerceSettings::class)->setPublic(true); // API response builders $container->autowire(\MailPoet\API\JSON\ResponseBuilders\NewslettersResponseBuilder::class); // Config diff --git a/tests/integration/API/JSON/v1/WoocommerceSettingsTest.php b/tests/integration/API/JSON/v1/WoocommerceSettingsTest.php new file mode 100644 index 0000000000..a519a5b2c4 --- /dev/null +++ b/tests/integration/API/JSON/v1/WoocommerceSettingsTest.php @@ -0,0 +1,37 @@ +wp = new WPFunctions(); + $this->endpoint = new WoocommerceSettings($this->wp); + } + function testItCanSetSettings() { + $this->wp->updateOption('woocommerce_email_base_color', '#ffffff'); + $response = $this->endpoint->set([ + 'woocommerce_email_base_color' => '#aaaaaa', + ]); + expect($response->status)->equals(APIResponse::STATUS_OK); + expect($this->wp->getOption('woocommerce_email_base_color'))->equals('#aaaaaa'); + } + + function testItDoesNotSetUnallowedSettings() { + $response = $this->endpoint->set([ + 'mailpoet_some_none_exting_option' => 'some value', + ]); + expect($response->status)->equals(APIResponse::STATUS_OK); + expect($this->wp->getOption('mailpoet_some_none_exting_option', null))->equals(null); + } +}