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

Conversation

martynmjones
Copy link
Contributor

Changes proposed in this Pull Request:

This PR adds an option workaround for new installations of WooCommerce so that the woocommerce_newly_installed hook is triggered the first time admin_init runs after activation.

Additionally, the action name used in CouponPageMoved.php is updated to the newer woocommerce_newly_installed.

Closes #38682

How to test the changes in this Pull Request:

  1. Setup a new environment that WooCommerce has not previously been installed on
  2. Activate WooCommerce
  3. Confirm that the legacy menu WooCommerce > Coupons is not added
  4. Go to wp-admin/options.php, search for woocommerce_newly_installed, and confirm the value is no

@martynmjones martynmjones self-assigned this Jun 13, 2023
@github-actions github-actions bot added focus: react admin [team:Ghidorah] plugin: woocommerce Issues related to the WooCommerce Core plugin. labels Jun 13, 2023
@martynmjones martynmjones marked this pull request as draft June 13, 2023 15:33
@martynmjones martynmjones marked this pull request as ready for review June 14, 2023 09:54
@martynmjones martynmjones requested review from a team and Konamiman and removed request for a team June 14, 2023 13:49
@github-actions
Copy link
Contributor

github-actions bot commented Jun 14, 2023

Hi @jorgeatorres, @Konamiman,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

@Konamiman Konamiman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @martynmjones, thanks for your contribution. I'd like to request some changes, though.

As we state in our documentation, ideally any new code for WooCommerce should go into the src directory, and changes to code in includes should be kept to the minimum. I think your changes in class-wc-install.php are small enough and reasonable, so no issues here.

However, something we tend to be more strict about (but we don't have properly documented, and that's something we need to improve) is that we don't want to add new public methods to existing classes unless it's absolutely necessary, because once these are implemented we are forced to keep backwards compatibility with it.

However when adding a hook handler to a class the handler method needs to be externally accessible. To this end we have implemented the AccessiblePrivateMethods trait, which allows to use private class methods as hook handlers.

So my request for you is to please use this trait so that the newly_installed method can be private. It's quite easy, you just need to do these adjustments to your changes in class-wc-install.php:

  1. Add use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods; to the class file.
  2. Add use AccessiblePrivateMethods; right below class WC_Install {.
  3. Change the add_action( 'admin_init', array( __CLASS__, 'newly_installed' ) ); so that it's preprended with self::. So: self::add_action( 'admin_init', array( __CLASS__, 'newly_installed' ) );
  4. Change the newly_installed method so that it's private.

Please ping me once these changes are implemented. Thank you!

@Konamiman
Copy link
Contributor

By the way, I see that after firing woocommerce_newly_installed you set the "newly installed" option to "no"; wouldn't it be enough to just delete the option? Or am I missing something?

@martynmjones
Copy link
Contributor Author

martynmjones commented Jun 20, 2023

Hey @Konamiman, thanks for the review and guidance!

I've made the changes you requests so that AccessiblePrivateMethods is now being used instead of the method being public so it's ready for a second look.

By the way, I see that after firing woocommerce_newly_installed you set the "newly installed" option to "no"; wouldn't it be enough to just delete the option? Or am I missing something?

The option could be deleted and it's unlikely there would be any issues, however, as WC_Install::is_new_install() isn't guaranteed to only return true for brand new installs, setting the newly installed option to no ensures that the woocommerce_newly_installed hook will only run once and then never again in the future.

Copy link
Contributor

@Konamiman Konamiman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martynmjones Thanks, just a couple more of minor changes are needed, I tried to do these myself but looks like I don't have edit permissions in your fork:

  1. The // Register the woocommerce_newly_installed action using AccessiblePrivateMethods::add_action(). comment is redundant and can be removed (the presence of use AccessiblePrivateMethods should be enough, and the code of the trait itself has already all the relevant information about why it's used).
  2. Please change the @since x.x.x in newly_installed to @since 8.0.0.

@martynmjones
Copy link
Contributor Author

Thanks for the second look, @Konamiman! I've updated the comments as requested in ea8645b.

@jorgeatorres jorgeatorres self-requested a review July 17, 2023 19:51
Copy link
Member

@jorgeatorres jorgeatorres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @martynmjones!

@jorgeatorres jorgeatorres dismissed Konamiman’s stale review July 17, 2023 19:52

Changes have been implemented

@jorgeatorres jorgeatorres merged commit 2fcf512 into woocommerce:trunk Jul 17, 2023
18 of 19 checks passed
@github-actions github-actions bot added this to the 8.0.0 milestone Jul 17, 2023
tommyshellberg pushed a commit that referenced this pull request Aug 7, 2023
…led` action (#38694)

* Use option to track new WC installs

* Correct hook used for disabling legacy coupon menu

* Add changelog

* Fix deprecated hook version number

* Update unit tests

* Use AccessiblePrivateMethods to register action

* Update comments

---------

Co-authored-by: Martyn Jones <martyn.jones@automattic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

woocommerce_newly_installed isn't triggered
3 participants