From 70625490aed79c37e0222c1ab067b563dfe269b4 Mon Sep 17 00:00:00 2001 From: Moon Date: Mon, 4 Mar 2024 07:57:16 -0800 Subject: [PATCH] Prevent fresh_site option from being set to 0 after WooCommerce installation (#45232) * Prevent fresh_site option from being set to 0 after publishing default pages * Add changefile(s) from automation for the following project(s): woocommerce * Add changefile(s) from automation for the following project(s): woocommerce * Typo fix * Add is_site_fresh function * Update comment * Add a test for is_fresh_site * Check fresh_site option first * Add test for checking fresh_site=1 * Fix lint errors * Fix lint errors --------- Co-authored-by: github-actions --- ...2-update-45219-disable-fresh_site_deletion | 4 ++ .../woocommerce/includes/class-wc-install.php | 4 ++ .../woocommerce/src/Admin/WCAdminHelper.php | 26 ++++++++++++ .../woocommerce-admin/wc-admin-helper.php | 41 +++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 plugins/woocommerce/changelog/45232-update-45219-disable-fresh_site_deletion diff --git a/plugins/woocommerce/changelog/45232-update-45219-disable-fresh_site_deletion b/plugins/woocommerce/changelog/45232-update-45219-disable-fresh_site_deletion new file mode 100644 index 00000000000..ab2ff58b068 --- /dev/null +++ b/plugins/woocommerce/changelog/45232-update-45219-disable-fresh_site_deletion @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Prevent fresh_site option from being set to 0 after WooCommerce installation. \ No newline at end of file diff --git a/plugins/woocommerce/includes/class-wc-install.php b/plugins/woocommerce/includes/class-wc-install.php index ac9700aa970..9510cd4899f 100644 --- a/plugins/woocommerce/includes/class-wc-install.php +++ b/plugins/woocommerce/includes/class-wc-install.php @@ -792,6 +792,10 @@ class WC_Install { * Create pages that the plugin relies on, storing page IDs in variables. */ public static function create_pages() { + // WordPress sets fresh_site to 0 after a page gets published. + // Prevent fresh_site option from being set to 0 so that we can use it for further customizations. + remove_action( 'publish_page', '_delete_option_fresh_site', 0 ); + // Set the locale to the store locale to ensure pages are created in the correct language. wc_switch_to_site_locale(); diff --git a/plugins/woocommerce/src/Admin/WCAdminHelper.php b/plugins/woocommerce/src/Admin/WCAdminHelper.php index 6306cf615cb..4e4ad1796c4 100644 --- a/plugins/woocommerce/src/Admin/WCAdminHelper.php +++ b/plugins/woocommerce/src/Admin/WCAdminHelper.php @@ -96,4 +96,30 @@ class WCAdminHelper { } return false; } + + /** + * Test if the site is fresh. A fresh site must meet the following requirements. + * + * - The current user was registered less than 1 month ago. + * - fresh_site option must be 1 + * + * @return bool + */ + public static function is_site_fresh() { + $fresh_site = get_option( 'fresh_site' ); + if ( '1' !== $fresh_site ) { + return false; + } + + $current_userdata = get_userdata( get_current_user_id() ); + // Return false if we can't get user meta data for some reason. + if ( ! $current_userdata ) { + return false; + } + + $date = new \DateTime( $current_userdata->user_registered ); + $month_ago = new \DateTime( '-1 month' ); + + return $date > $month_ago; + } } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php index 98e97baa3bd..03d23a3ac32 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/wc-admin-helper.php @@ -135,4 +135,45 @@ class WC_Admin_Tests_Admin_Helper extends WP_UnitTestCase { '2 month old store not within 6+ months?' => array( 2 * MONTH_IN_SECONDS, 'month-6+', false ), ); } + + /** + * Test is_fresh_site with registered date. + */ + public function test_is_fresh_site_user_registered_less_than_a_month() { + update_option( 'fresh_site', '1' ); + $user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + wp_set_current_user( $user ); + $this->assertTrue( WCAdminHelper::is_site_fresh() ); + + // Update registered date to January. + // The function should return false. + wp_update_user( + array( + 'ID' => $user, + 'user_registered' => '2024-01-27 20:56:29', + ) + ); + $this->assertFalse( WCAdminHelper::is_site_fresh() ); + } + + /** + * Test is_fresh_site with fresh_site option. + */ + public function test_is_fresh_site_fresh_site_option_must_be_1() { + update_option( 'fresh_site', '0' ); + $user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + wp_set_current_user( $user ); + $this->assertFalse( WCAdminHelper::is_site_fresh() ); + + update_option( 'fresh_site', '1' ); + $this->assertTrue( WCAdminHelper::is_site_fresh() ); + } }