Fix: Prevent constant redefinition warning for white label configuration
Users defining WP_ROCKET_PLUGIN_NAME in wp-config.php for white labeling (per documentation) trigger a PHP warning when the plugin redefines it.
Changes
- Wrapped constant definitions in conditional checks across 2 initialization paths:
-
inc/main.php- Primary plugin init -
inc/Engine/Activation/Activation.php- Activation flow
-
Before:
define( 'WP_ROCKET_PLUGIN_NAME', 'WP Rocket' );
define( 'WP_ROCKET_PLUGIN_SLUG', sanitize_key( WP_ROCKET_PLUGIN_NAME ) );
After:
if ( ! defined( 'WP_ROCKET_PLUGIN_NAME' ) ) {
define( 'WP_ROCKET_PLUGIN_NAME', 'WP Rocket' );
}
if ( ! defined( 'WP_ROCKET_PLUGIN_SLUG' ) ) {
define( 'WP_ROCKET_PLUGIN_SLUG', sanitize_key( WP_ROCKET_PLUGIN_NAME ) );
}
- Added unit test covering default and pre-defined constant scenarios
Original prompt
This section details on the original issue you should resolve
<issue_title>Defining WP_ROCKET_PLUGIN_NAME in wp-config.php causes PHP Warning</issue_title> <issue_description>Describe the bug When defining WP_ROCKET_PLUGIN_NAME in wp-config.php to change the WP Rocket name to something else in the WP Admin, the following error is logged:
[12-Dec-2024 04:03:30 UTC] PHP Warning: Constant WP_ROCKET_PLUGIN_NAME already defined in /home/user/example.com/wp-content/plugins/wp-rocket/inc/main.php on line 40To Reproduce
- Add
define( 'WP_ROCKET_PLUGIN_NAME', 'Site Cache' );to wp-config.php- Clear cache and view some pages
- Check error log
Expected behavior Here, we should check if
WP_ROCKET_PLUGIN_NAMEis already defined and only define it if not. Something like the following:if ( ! defined( 'WP_ROCKET_PLUGIN_NAME' ) ) { define( 'WP_ROCKET_PLUGIN_NAME', 'WP Rocket' ); }AC
- Safeguard the constant from prompting a warning if already declared.
Additional context Ticket: https://secure.helpscout.net/conversation/2789608151/530064</issue_description>
Comments on the Issue (you are @copilot in this section)
@wordpressfan Thanks @joejoe04May I ask, what is the reason behind having our constant inside
wp-config.php?From my point of view, I think those constants are forming the base functionality of the plugin so changing them will change the core functionality, and showing a warning is a good thing :)
It's a @wp-media/product call but I can't think of this one as an issue.</comment_new> <comment_new>
@wordpressfan@joejoe04 I'm not sure what decisions were made about it previously, but it's currently in our documentation for white labeling WP Rocket in the WP Admin.
You're right, product can make the call.
If we're not going to support doing this, then we would need to also remove it from the doc.</comment_new> <comment_new>
Thanks @joejoe04 I wasn't aware that we have something like that in our docs :) In this case, you are correct.</comment_new> <comment_new>@wordpressfan @joejoe04 Well, maybe not. You would know better than I if allowing that constant to be changed might lead to unexpected bad outcomes. If so, then maybe we shouldn't allow it to be changed or have it in the doc. What do you think?</comment_new> <comment_new>@DahmaniAdame It's resulting from us instructing users to use a constant. We should have a way to prevent the warning. </comment_new>
- Fixes wp-media/wp-rocket#7183
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
@copilot You can remove your change from inc/deprecated/*
@copilot You can remove your change from
inc/deprecated/*
Done. Reverted changes to inc/deprecated/3.6.php in commit b4b15a5.
Coverage summary from Codacy
See diff coverage on Codacy
| Coverage variation | Diff coverage |
|---|---|
| Report missing for 162086ed3b70b81b118367ea3be0d48066667da4[^1] | :x: 0.00% (target: 50.00%) |
Coverage variation details
| Coverable lines | Covered lines | Coverage | |
|---|---|---|---|
| Common ancestor commit (162086ed3b70b81b118367ea3be0d48066667da4) | Report Missing | Report Missing | Report Missing |
| Head commit (4f74f7b241fd4f56d9352456226494668073320a) | 41879 | 18886 | 45.10% |
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>
Diff coverage details
| Coverable lines | Covered lines | Diff coverage | |
|---|---|---|---|
| Pull request (#7951) | 8 | 0 | 0.00% |
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%
See your quality gate settings Change summary preferences
[^1]: Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
Working as expected https://wpmediaqa.testrail.io/index.php?/cases/view/21724 (explored too on multisite and apache server)