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
Conversation
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: |
There was a problem hiding this 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
:
- Add
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
to the class file. - Add
use AccessiblePrivateMethods;
right belowclass WC_Install {
. - Change the
add_action( 'admin_init', array( __CLASS__, 'newly_installed' ) );
so that it's preprended withself::
. So:self::add_action( 'admin_init', array( __CLASS__, 'newly_installed' ) );
- Change the
newly_installed
method so that it's private.
Please ping me once these changes are implemented. Thank you!
By the way, I see that after firing |
Hey @Konamiman, thanks for the review and guidance! I've made the changes you requests so that
The option could be deleted and it's unlikely there would be any issues, however, as |
There was a problem hiding this 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:
- The
// Register the woocommerce_newly_installed action using AccessiblePrivateMethods::add_action().
comment is redundant and can be removed (the presence ofuse AccessiblePrivateMethods
should be enough, and the code of the trait itself has already all the relevant information about why it's used). - Please change the
@since x.x.x
innewly_installed
to@since 8.0.0
.
Thanks for the second look, @Konamiman! I've updated the comments as requested in ea8645b. |
There was a problem hiding this 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!
…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>
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 timeadmin_init
runs after activation.Additionally, the action name used in
CouponPageMoved.php
is updated to the newerwoocommerce_newly_installed
.Closes #38682
How to test the changes in this Pull Request:
WooCommerce > Coupons
is not addedwp-admin/options.php
, search forwoocommerce_newly_installed
, and confirm the value isno