From ab9173c062528a0a1f717aa7dda97e76d3bc035f 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 494a00a..30396dc 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 371dea9..5ca78fa 100644 --- a/tests/wpunit/StatusTest.php +++ b/tests/wpunit/StatusTest.php @@ -6,7 +6,9 @@ use lucatume\WPBrowser\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' ); + } }