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 <github-actions@github.com>
This commit is contained in:
parent
f24eb2efa3
commit
70625490ae
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: update
|
||||
|
||||
Prevent fresh_site option from being set to 0 after WooCommerce installation.
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue