From c5d110ef3bbbcdc7acd93ba6607ab2ca5aba12a1 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 25 Jan 2024 18:19:41 -0500 Subject: [PATCH] Register the site when opting in if unregistered. Fixes #109. --- src/Telemetry/Opt_In/Status.php | 9 ++++++++- tests/wpunit/StatusTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Telemetry/Opt_In/Status.php b/src/Telemetry/Opt_In/Status.php index a97c1fa..3334952 100644 --- a/src/Telemetry/Opt_In/Status.php +++ b/src/Telemetry/Opt_In/Status.php @@ -11,6 +11,7 @@ use StellarWP\Telemetry\Config; use StellarWP\Telemetry\Core; +use StellarWP\Telemetry\Telemetry\Telemetry; /** * Class for handling the Opt-in status for the site. @@ -240,7 +241,13 @@ public function set_status( bool $status, string $stellar_slug = '' ) { $option['plugins'][ $stellar_slug ]['optin'] = $status; - return update_option( $this->get_option_name(), $option ); + $updated = update_option( $this->get_option_name(), $option ); + + if ( $updated && $status && ! $this->get_token() ) { + Config::get_container()->get( Opt_In_Subscriber::class )->opt_in( $stellar_slug ); + } + + return $updated; } /** diff --git a/tests/wpunit/StatusTest.php b/tests/wpunit/StatusTest.php index 3b971cd..d6fd44a 100644 --- a/tests/wpunit/StatusTest.php +++ b/tests/wpunit/StatusTest.php @@ -6,7 +6,9 @@ use Codeception\TestCase\WPTestCase; use StellarWP\Telemetry\Config; use StellarWP\Telemetry\Core; +use StellarWP\Telemetry\Opt_In\Opt_In_Subscriber; use StellarWP\Telemetry\Opt_In\Status; +use StellarWP\Telemetry\Tests\Container; use StellarWP\Telemetry\Tests\Support\Traits\With_Test_Container; use StellarWP\Telemetry\Tests\Support\Traits\With_Uopz; @@ -511,4 +513,20 @@ public function test_it_returns_false_if_plugin_not_in_option() { $this->assertFalse( $plugin_removed ); } + + public function test_it_registers_the_site_when_opting_in() { + + $subscriber_mock = $this->createMock( Opt_In_Subscriber::class ); + $subscriber_mock->expects( $this->once() ) + ->method( 'opt_in' ) + ->with( 'the-events-calendar' ); + + $this->set_container(); + $container = new Container(); + $container->bind(Opt_In_Subscriber::class, $subscriber_mock ); + Config::set_container($container); + + $status = new Status(); + $status->set_status( true, 'the-events-calendar' ); + } }