diff --git a/plugins/woocommerce/changelog/add-lys-default-values b/plugins/woocommerce/changelog/add-lys-default-values new file mode 100644 index 00000000000..3239cd2bfdf --- /dev/null +++ b/plugins/woocommerce/changelog/add-lys-default-values @@ -0,0 +1,4 @@ +Significance: patch +Type: add + +Add default option values for Launch your store task diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php index 0a6cb7cffd8..4c52111de91 100644 --- a/plugins/woocommerce/includes/class-woocommerce.php +++ b/plugins/woocommerce/includes/class-woocommerce.php @@ -24,6 +24,8 @@ use Automattic\WooCommerce\Internal\Utilities\WebhookUtil; use Automattic\WooCommerce\Internal\Admin\Marketplace; use Automattic\WooCommerce\Proxies\LegacyProxy; use Automattic\WooCommerce\Utilities\{ LoggingUtil, TimeUtil }; +use Automattic\WooCommerce\Admin\WCAdminHelper; +use Automattic\WooCommerce\Admin\Features\Features; /** * Main WooCommerce Class. @@ -253,6 +255,11 @@ final class WooCommerce { add_action( 'woocommerce_installed', array( $this, 'add_woocommerce_remote_variant' ) ); add_action( 'woocommerce_updated', array( $this, 'add_woocommerce_remote_variant' ) ); + if ( Features::is_enabled( 'launch-your-store' ) ) { + add_action( 'woocommerce_newly_installed', array( $this, 'add_lys_default_values' ) ); + add_action( 'woocommerce_updated', array( $this, 'add_lys_default_values' ) ); + } + // These classes set up hooks on instantiation. $container = wc_get_container(); $container->get( ProductDownloadDirectories::class ); @@ -307,6 +314,35 @@ final class WooCommerce { } } + /** + * Set default option values for launch your store task. + */ + public function add_lys_default_values() { + $is_new_install = current_action() === 'woocommerce_newly_installed'; + + $coming_soon = $is_new_install ? 'yes' : 'no'; + $launch_status = $is_new_install ? 'unlaunched' : 'launched'; + $store_pages_only = WCAdminHelper::is_site_fresh() ? 'no' : 'yes'; + $private_link = 'yes'; + $share_key = wp_generate_password( 32, false ); + + if ( false === get_option( 'woocommerce_coming_soon', false ) ) { + update_option( 'woocommerce_coming_soon', $coming_soon ); + } + if ( false === get_option( 'woocommerce_store_pages_only', false ) ) { + update_option( 'woocommerce_store_pages_only', $store_pages_only ); + } + if ( false === get_option( 'woocommerce_private_link', false ) ) { + update_option( 'woocommerce_private_link', $private_link ); + } + if ( false === get_option( 'woocommerce_share_key', false ) ) { + update_option( 'woocommerce_share_key', $share_key ); + } + if ( false === get_option( 'launch-status', false ) ) { + update_option( 'launch-status', $launch_status ); + } + } + /** * Ensures fatal errors are logged so they can be picked up in the status report. * diff --git a/plugins/woocommerce/src/Admin/API/Options.php b/plugins/woocommerce/src/Admin/API/Options.php index 1cd59c9bfc0..5a36cbcefc3 100644 --- a/plugins/woocommerce/src/Admin/API/Options.php +++ b/plugins/woocommerce/src/Admin/API/Options.php @@ -220,6 +220,11 @@ class Options extends \WC_REST_Data_Controller { 'woocommerce_admin_customize_store_completed', 'woocommerce_admin_customize_store_completed_theme_id', 'woocommerce_admin_customize_store_survey_completed', + 'woocommerce_coming_soon', + 'woocommerce_store_pages_only', + 'woocommerce_private_link', + 'woocommerce_share_key', + 'launch-status', // WC Test helper options. 'wc-admin-test-helper-rest-api-filters', 'wc_admin_helper_feature_values', diff --git a/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php b/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php index a048cd20669..e27603f4b35 100644 --- a/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php +++ b/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php @@ -4,6 +4,19 @@ * Unit tests for the WooCommerce class. */ class WooCommerce_Test extends \WC_Unit_Test_Case { + /** + * Setup test data. Called before every test. + */ + public function setUp(): void { + parent::setUp(); + + $this->user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + wp_set_current_user( $this->user ); + } /** * Test that the $api property is defined, public and initialized correctly. @@ -14,4 +27,83 @@ class WooCommerce_Test extends \WC_Unit_Test_Case { $this->assertTrue( $property->isPublic() ); $this->assertInstanceOf( WC_API::class, $property->getValue( WC() ) ); } + + /** + * Tear down. + */ + public function tearDown(): void { + parent::tearDown(); + + delete_option( 'woocommerce_coming_soon' ); + delete_option( 'woocommerce_store_pages_only' ); + delete_option( 'woocommerce_private_link' ); + delete_option( 'woocommerce_share_key' ); + delete_option( 'launch-status' ); + } + + /** + * Test for add_lys_default_values method on fresh installation. + */ + public function test_add_lys_default_values_on_fresh_installation() { + update_option( 'fresh_site', '1' ); + + $this->set_current_action( 'woocommerce_newly_installed' ); + ( WooCommerce::instance() )->add_lys_default_values(); + + $this->assertEquals( 'yes', get_option( 'woocommerce_coming_soon' ) ); + $this->assertEquals( 'no', get_option( 'woocommerce_store_pages_only' ) ); + $this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) ); + $this->assertNotEmpty( get_option( 'woocommerce_share_key' ) ); + $this->assertMatchesRegularExpression( '/^[a-zA-Z0-9]{32}$/', get_option( 'woocommerce_share_key' ) ); + $this->assertEquals( 'unlaunched', get_option( 'launch-status' ) ); + } + + /** + * Test for add_lys_default_values method on WooCommerce update. + */ + public function test_add_lys_default_values_on_woocommerce_update() { + update_option( 'fresh_site', '0' ); + + $this->set_current_action( 'woocommerce_updated' ); + ( WooCommerce::instance() )->add_lys_default_values(); + + $this->assertEquals( 'no', get_option( 'woocommerce_coming_soon' ) ); + $this->assertEquals( 'yes', get_option( 'woocommerce_store_pages_only' ) ); + $this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) ); + $this->assertNotEmpty( get_option( 'woocommerce_share_key' ) ); + $this->assertMatchesRegularExpression( '/^[a-zA-Z0-9]{32}$/', get_option( 'woocommerce_share_key' ) ); + $this->assertEquals( 'launched', get_option( 'launch-status' ) ); + } + + /** + * Test for add_lys_default_values method when options are already set. + * + */ + public function test_add_lys_default_values_when_options_are_already_set() { + update_option( 'fresh_site', '0' ); + update_option( 'woocommerce_coming_soon', 'yes' ); + update_option( 'woocommerce_store_pages_only', 'no' ); + update_option( 'woocommerce_private_link', 'yes' ); + update_option( 'woocommerce_share_key', 'test' ); + update_option( 'launch-status', 'unlaunched' ); + + $this->set_current_action( 'woocommerce_updated' ); + ( WooCommerce::instance() )->add_lys_default_values(); + + $this->assertEquals( 'yes', get_option( 'woocommerce_coming_soon' ) ); + $this->assertEquals( 'no', get_option( 'woocommerce_store_pages_only' ) ); + $this->assertEquals( 'yes', get_option( 'woocommerce_private_link' ) ); + $this->assertEquals( 'test', get_option( 'woocommerce_share_key' ) ); + $this->assertEquals( 'unlaunched', get_option( 'launch-status' ) ); + } + + /** + * Helper method to set the current action for testing. + * + * @param string $action The action to set. + */ + private function set_current_action( $action ) { + global $wp_current_filter; + $wp_current_filter[] = $action; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } }