Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activation option workaround to trigger woocommerce_newly_installed action #38694

Merged
merged 7 commits into from Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-38682-newly-installed-hook
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Trigger "woocommerce_newly_installed" hook for new installations
41 changes: 31 additions & 10 deletions plugins/woocommerce/includes/class-wc-install.php
Expand Up @@ -234,12 +234,20 @@ class WC_Install {
),
);

/**
* Option name used to track new installations of WooCommerce.
*
* @var string
*/
const NEWLY_INSTALLED_OPTION = 'woocommerce_newly_installed';

/**
* Hook in tabs.
*/
public static function init() {
add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
add_action( 'init', array( __CLASS__, 'manual_database_update' ), 20 );
add_action( 'admin_init', array( __CLASS__, 'newly_installed' ) );
add_action( 'admin_init', array( __CLASS__, 'wc_admin_db_update_notice' ) );
add_action( 'admin_init', array( __CLASS__, 'add_admin_note_after_page_created' ) );
add_action( 'woocommerce_run_update_callback', array( __CLASS__, 'run_update_callback' ) );
Expand All @@ -252,6 +260,25 @@ public static function init() {
add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) );
}

/**
* Trigger `woocommerce_newly_installed` action for new installations.
*
* @since x.x.x
*/
public static function newly_installed() {
if ( 'yes' === get_option( self::NEWLY_INSTALLED_OPTION, false ) ) {
/**
* Run when WooCommerce has been installed for the first time.
*
* @since 6.5.0
*/
do_action( 'woocommerce_newly_installed' );
do_action_deprecated( 'woocommerce_admin_newly_installed', array(), '6.5.0', 'woocommerce_newly_installed' );

update_option( self::NEWLY_INSTALLED_OPTION, 'no' );
}
}

/**
* Check WooCommerce version and run the updater is required.
*
Expand All @@ -270,16 +297,6 @@ public static function check_version() {
*/
do_action( 'woocommerce_updated' );
do_action_deprecated( 'woocommerce_admin_updated', array(), $wc_code_version, 'woocommerce_updated' );
// If there is no woocommerce_version option, consider it as a new install.
if ( ! $wc_version ) {
/**
* Run when WooCommerce has been installed for the first time.
*
* @since 6.5.0
*/
do_action( 'woocommerce_newly_installed' );
do_action_deprecated( 'woocommerce_admin_newly_installed', array(), $wc_code_version, 'woocommerce_newly_installed' );
}
}
}

Expand Down Expand Up @@ -391,6 +408,10 @@ public static function install() {
set_transient( 'wc_installing', 'yes', MINUTE_IN_SECONDS * 10 );
wc_maybe_define_constant( 'WC_INSTALLING', true );

if ( self::is_new_install() && ! get_option( self::NEWLY_INSTALLED_OPTION, false ) ) {
update_option( self::NEWLY_INSTALLED_OPTION, 'yes' );
}

WC()->wpdb_table_fix();
self::remove_admin_notices();
self::create_tables();
Expand Down
Expand Up @@ -35,7 +35,7 @@ public function init() {

add_action( 'admin_init', [ $this, 'possibly_add_note' ] );
add_action( 'admin_init', [ $this, 'redirect_to_coupons' ] );
add_action( 'woocommerce_admin_newly_installed', [ $this, 'disable_legacy_menu_for_new_install' ] );
add_action( 'woocommerce_newly_installed', [ $this, 'disable_legacy_menu_for_new_install' ] );
}

/**
Expand Down
Expand Up @@ -132,13 +132,19 @@ public function test_missed_version_number_update() {
* Test the following options are created.
*
* - woocommerce_admin_install_timestamp
* - WC_Install::NEWLY_INSTALLED_OPTION
*
* @return void
*/
public function test_options_are_set() {
delete_transient( 'wc_installing' );
WC_Install::install();
$options = array( 'woocommerce_admin_install_timestamp' );

$options = array(
'woocommerce_admin_install_timestamp',
WC_Install::NEWLY_INSTALLED_OPTION,
);

foreach ( $options as $option ) {
$this->assertNotFalse( get_option( $option ) );
}
Expand Down Expand Up @@ -168,13 +174,19 @@ public function test_woocommerce_updated_action() {
}

/**
* Test woocommerce_newly_installed action gets fired.
* Test woocommerce_newly_installed action gets fired and the option is set to 'no'.
*
* @return void
*/
public function test_woocommerce_newly_installed_action() {
delete_option( 'woocommerce_version' );
WC_Install::check_version();
$this->assertTrue( did_action( 'woocommerce_newly_installed' ) > 0 );
update_option( WC_Install::NEWLY_INSTALLED_OPTION, 'yes' );

// Call twice to ensure `woocommerce_newly_installed` is only triggered once.
WC_Install::newly_installed();
WC_Install::newly_installed();

$this->assertTrue( 1 === did_action( 'woocommerce_newly_installed' ) );
$this->assertEquals( get_option( WC_Install::NEWLY_INSTALLED_OPTION ), 'no' );
}

/**
Expand Down