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

wp-now: Constant WP_DEBUG already defined #135

Open
swissspidy opened this issue Dec 22, 2023 · 1 comment
Open

wp-now: Constant WP_DEBUG already defined #135

swissspidy opened this issue Dec 22, 2023 · 1 comment
Labels
Bug Something isn't working wp-now

Comments

@swissspidy
Copy link
Member

swissspidy commented Dec 22, 2023

I followed the example from the docs to set WP_DEBUG and WP_DEBUG_LOG constants using a Blueprint file like so:

{
	"steps": [
		{
			"step": "defineWpConfigConsts",
			"consts": {
				"WP_DEBUG": true,
				"WP_DEBUG_LOG": true
			},
			"virtualize": true
		}
	]
}

But with this I'm getting this PHP warning:

Warning: Constant WP_DEBUG already defined in /var/www/html/wp-config.php on line 82

Same with virtualize: false, so it's not that.

Problem is I have to define WP_DEBUG as it's otherwise false by default and I don't get anything logged.

With this config the logging works, but the warning is always displayed. WP_DEBUG_DISPLAY does not help.

adamziel added a commit to WordPress/wordpress-playground that referenced this issue Dec 23, 2023
Wraps all define calls in a conditional `if(!defined())` to ensure that the default
wp-config.php constants will not class with the ones defined with the `defineWpConfigConsts`
Blueprint step.

Related to WordPress/playground-tools#135
adamziel added a commit to WordPress/wordpress-playground that referenced this issue Jan 8, 2024
…ConfigConsts step (#902)

## Summary

This PR enhances the defineWpConfigConsts step by allowing constants to
be defined in two ways:

1. By rewriting wp-config.php (default behavior)
    * Existing define() calls are rewritten with new constant values
    * New define() calls are prepended
    * All calls wrapped in if(!defined()) to avoid conflicts
2. By calling `define()` before script execution (previous behavior)
    * Constants defined directly via PHP before script execution
    * Does not modify wp-config.php
    * May conflict with existing defines in wp-config.php (limitation)

Related to #901
Related to WordPress/playground-tools#135
Closes #900

## wp-config.php rewriting details 

The following `wp-config.php`:

```php
<?php
define('WP_DEBUG', true);
// The third define() argument is also supported:
define('SAVEQUERIES', false, true);

// Expression are wrapped in `if(!defined())` guards
define(true ? 'WP_DEBUG_LOG' : 'WP_DEBUG_LOG', 123);

// Guarded expressions shouldn't be wrapped twice
if(!defined(1 ? 'A' : 'B')) {
    define(1 ? 'A' : 'B', 0);
}

// More advanced expression
define((function() use($x) {
    return [$x, 'a'];
})(), 123);
```

When rewritten like this:

```php
rewrite_wp_config_to_define_constants($content, [
    'WP_DEBUG' => false,
    'WP_DEBUG_LOG' => true,
    'SAVEQUERIES' => true,
    'NEW_CONSTANT' => "new constant",
]);
```

Will become:

```php
<?php
define('WP_DEBUG_LOG',true);
define('NEW_CONSTANT','new constant');
?><?php
define('WP_DEBUG',false);
// The third define() argument is also supported:
define('SAVEQUERIES',true, true);

// Expression are wrapped in `if(!defined())` guards
if(!defined($const ? 'WP_DEBUG_LOG' : 'WP_DEBUG_LOG')) {
     define($const ? 'WP_DEBUG_LOG' : 'WP_DEBUG_LOG', 123);
}

// Guarded expressions shouldn't be wrapped twice
if(!defined(1 ? 'A' : 'B')) {
    define(1 ? 'A' : 'B', 0);
}

// More advanced expression
if(!defined((function() use($x) {
   return [$x, 'a'];
})())) {
    define((function() use($x) {
        return [$x, 'a'];
    })(), 123);
}
```

## Testing instructions

Go to the following URL without this PR applied and confirm there are
warnings like Warning: Constant WP_DEBUG already defined:


http://localhost:5400/website-server/#{%22landingPage%22:%22/%22,%22phpExtensionBundles%22:[%22kitchen-sink%22],%22preferredVersions%22:{%22php%22:%228.0%22,%22wp%22:%225.9%22},%22steps%22:[{%22step%22:%22defineWpConfigConsts%22,%22consts%22:{%22WP_DEBUG%22:true}},{%22step%22:%22defineWpConfigConsts%22,%22consts%22:{%22WP_DEBUG_ONLY%22:%22a%22}}]}

Now go there with this PR applied and confirm the warnings are gone. The
Blueprint encoded in the URL above calls the `defineWpConfigConsts` step
twice just to make sure a duplicate and conflicting `define()` call
won't be added.

cc @sejas
@adamziel adamziel added Bug Something isn't working wp-now labels Jan 11, 2024
@adamziel
Copy link
Collaborator

WordPress/wordpress-playground#902 enables rewriting wp-config.php instead of defining them in a temporary file that's loaded before wp-config.php. What do you think about such an approach @sejas @danielbachhuber?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working wp-now
Projects
None yet
Development

No branches or pull requests

2 participants