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

Constants are working in theory, but ignored in real life #783

Open
i2dcarrasco opened this issue Nov 25, 2024 · 7 comments
Open

Constants are working in theory, but ignored in real life #783

i2dcarrasco opened this issue Nov 25, 2024 · 7 comments
Assignees

Comments

@i2dcarrasco
Copy link

Hello,

I am using this module from a time ago in some Wordpress installations. For now was working because the settings were set directly on the plugin configuration page but now by requirements of our environments I need to set it using constants. The reason is mainly because the DB of the environment will be replaced by the DB in another environment from time to time, so the configuration params will come with that dump (that's a problem).

The problem is that in theory the constants are working because the configuration page shows the settings greyed out and with the correct configurations that I set via those constants, but in real life the settings seems to be ignored and instead is using the database settings.

My constants:

define('WP_STATELESS_MEDIA_BUCKET', getenv('WP_STATELESS_MEDIA_BUCKET'));
define('WP_STATELESS_MEDIA_KEY_FILE_PATH', getenv('WP_STATELESS_MEDIA_KEY_FILE_PATH'));
define('WP_STATELESS_MEDIA_CUSTOM_DOMAIN', getenv('WP_STATELESS_MEDIA_CUSTOM_DOMAIN') ? getenv('WP_STATELESS_MEDIA_CUSTOM_DOMAIN') : null);

Steps I have follow to trigger the issue:

  • I have copied the DB from my PRO environment to this one (PRE).
  • I have changed the storage name to avoid unwanted uploads to the PRO env.
  • An error message was shown in the top of the page, which is correct because the PRO service account cannot write on the PRE storage.
  • I have enabled the contstants to replace the wrong settings by the correct ones. The settings page shows the correct data so the constants seems to be working.
  • The error message has not gone and everytime I upload a file the link is the local one instead the storage link.
  • After a long time trying to debug the problem, I have decided to disable the constants related to the Service Account and Storage Name, and I've place the correct json via the settings page (the storage name was modified before).
  • The error has gone and the upload seems to work, but not fully. The domain field is also ignored and it's using the PRO setting which is in the database instead the one that I am providing via the constant.
  • Well, that is a big problem so I have decided to clear all the settings related with the storage, the service account and the domain in the settings page (after disabling the constants again).
  • After removing all, I have enabled the contstants again to see if maybe were ignored because there were settings in the database but no, the settings provided by constants are ignored even now with the related settings empty. The error I get now is that the storage setting cannot be empty when in the settings page I can clearly see that is filled by the name provided in the constant.

A summary can be the title: The settings provided by the constans are readed and considered in the settings page but not in the rest of the plugin, so it's not working as expected.

Best regards.

@planv
Copy link
Contributor

planv commented Nov 25, 2024

Hello, @i2dcarrasco

Could you please copy the info from the Media / Stateless Setting / Status page and post it here?

Thanks!

@i2dcarrasco
Copy link
Author

i2dcarrasco commented Nov 27, 2024

Hello,

The status right now without constants is:

### Server

Server architecture: Linux 6.1.85+ x86_64
Web server: nginx/1.24.0
MySQL version: 8.0.31-google
PHP Version: 8.1.29
PHP Memory Limit: 2G
PHP Max Input Vars: 1000
PHP Max Post Size: 32M
PHP Time Limit: 600
Max Upload Size: 8M
Allow URL-aware fopen Wrappers: Yes
Loaded Extensions: Core, date, libxml, pcre, zlib, filter, hash, json, readline, Reflection, SPL, session, cgi-fcgi, bcmath, ctype, curl, dom, fileinfo, gd, iconv, intl, mbstring, openssl, standard, soap, sockets, tokenizer, xml, xmlwriter, zip, exif, mysqlnd, xmlreader, mysqli, apcu, msgpack, igbinary, brotli, ddtrace, ddappsec, redis, Zend OPcache

### WordPress

Version: 6.5.4
Multisite: Yes
Memory Limit: 64M

### WP-Stateless

Version: 4.0.4
Database Version: 1.1
Mode: CDN
File URL Replacement: Disabled
Supported File Types: jpg jpeg png gif pdf
Bucket: Not set
Bucket Accessible: Yes
Service Account JSON: Not set
Cache-Control: Default: public, max-age=36000, must-revalidate
Delete GCS File: Enable
Folder: sites/%site_id%/%date_year/date_month%
Domain: Not set
Cache-Busting: Disable
Dynamic Image Support: Disable
Use Post Meta: Don't override

After enabling the constants:

### Server

Server architecture: Linux 6.1.85+ x86_64
Web server: nginx/1.24.0
MySQL version: 8.0.31-google
PHP Version: 8.1.29
PHP Memory Limit: 2G
PHP Max Input Vars: 1000
PHP Max Post Size: 32M
PHP Time Limit: 600
Max Upload Size: 8M
Allow URL-aware fopen Wrappers: Yes
Loaded Extensions: Core, date, libxml, pcre, zlib, filter, hash, json, readline, Reflection, SPL, session, cgi-fcgi, bcmath, ctype, curl, dom, fileinfo, gd, iconv, intl, mbstring, openssl, standard, soap, sockets, tokenizer, xml, xmlwriter, zip, exif, mysqlnd, xmlreader, mysqli, apcu, msgpack, igbinary, brotli, ddtrace, ddappsec, redis, Zend OPcache

### WordPress

Version: 6.5.4
Multisite: Yes
Memory Limit: 64M

### WP-Stateless

Version: 4.0.4
Database Version: 1.1
Mode: CDN (Constant)
File URL Replacement: Disabled
Supported File Types: jpg jpeg png gif pdf
Bucket: Set (Constant)
Bucket Accessible: Yes
Service Account JSON: Set (Constant)
Cache-Control: Default: public, max-age=36000, must-revalidate
Delete GCS File: Enable
Folder: sites/%site_id%/%date_year/date_month%
Domain: Set (Constant)
Cache-Busting: Disable
Dynamic Image Support: Disable
Use Post Meta: Don't override

It says "bucket accesible" and "set by constants", but when I go to a site to upload any file, I can see this:
Captura-desde-2024-11-27-13-16-00.png

If I upload any file the upload is done locally instead to the storage.

My Wordpress is multisite, I don't know if that can be the problem.

The above error is now that I have emptied all the configurations, but after emtying it I was able to see how it was trying to use the stored settings instead the constant provided settings (just like now that is using the empty stored settings). If I go to the settings page in the site were I am trying to upload the files, I can see how the settings are also filled with the correct data like in the network settings page.

Best regards.

@i2dcarrasco
Copy link
Author

No news about this?

@balexey88
Copy link
Contributor

Hello @i2dcarrasco ,

Thank you for the provided info.

We've made a couple of tests on our multisite environment; the constants work as expected. But there are a couple of notes related to this:

  • Bucket value is considered set when the value is not empty, so even one space ' ' is considered as Set.
  • The Bucket Accessible value relies on transient, so it is possible that it does not reflect the current state.

Both issues are related to the Status page, and we will fix them in nearby releases.

The source of the issue could be related to your env variables. You pass the constant values using getenv(), which behaves differently depending on the platform and PHP config options. Besides, if you are running, for example, PHP-FPM, it has its own environment with its own environment variables.

I would suggest focusing on the WP_STATELESS_MEDIA_BUCKET only for now and see if we can resolve the issue.

Could you please try the following:

  • Set the constants using exact values without getenv like :
define('WP_STATELESS_MEDIA_BUCKET', 'insert_your_bucket_name');
  • Output the values of your environment variables like:
var_dump( getenv('WP_STATELESS_MEDIA_BUCKET') );

or

error_log( getenv('WP_STATELESS_MEDIA_BUCKET') );
  • If you are using any caching please disable it during the tests.

Please let us know the results so we can move further.
Thank you!

@balexey88 balexey88 self-assigned this Dec 13, 2024
@i2dcarrasco
Copy link
Author

Hello,

Thanks for your info, but I have already checked all that things. I know that the childs of the PHP-FPM has its own env variables, and there's an option to control if you want to preserve the OS env variables. We have already setted that option to preserve the current env variables and even we are using env variables to configure other things in the wp-config.php file (all working).
Also I have checked that the variables are right with the var_dump and echo, and even I am able to see the correct values in the control panel of the plugin so they are correctly retrieved. The problem is not an incorrect value in the control panel or something related with the env variables itself, is that the values shown in the control panel when the values are set by env variables is not respected. The plugin instead always tries to use the previously setted values when the env variables were not set.
I can disable the env variables and set an incorrect value for that settings, and the error is that it has no permissions. Setting the env variables again I got the same error. Now, doing exactly the same but having the setting empty, I got the "Setting cannot be empty" error with and without the env variables, so my intuition makes me think that the plugin is ignoring the settings provided by constants (envs).
Maybe it only happens when the settings are previously setted and no when the test environment is clean, I don't fully know the way that have you tested. I only knows the symptoms that I am observing on out environment: The settings works when they are setted in the panel directly and stored in the database, but don't works when they are setted via constants.

Best regards.

@balexey88
Copy link
Contributor

Hello @i2dcarrasco,

I think I found the source of the confusion here. In multisite WP, there are global network-wide WP-Stateless settings and each site in the network has its own settings.

Suppose I have a WP multisite with site1.com, site2.com, etc. When you upload a file on site1.com it checks:

  1. The WP_STATELESS_MEDIA_BUCKET constant in the config file;
  2. Network settings, found under Network Admin / Settings / Stateless Settings;
  3. Site-specific settings, found under Media / Stateless Settings in the Admin Panel of the particular site.

Suppose I have bucket-1 and bucket-2.

  • When I set bucket-1 under network settings - all the files uploaded on site1.com and site2.com go to bucket-1.
  • When I define a constant define('WP_STATELESS_MEDIA_BUCKET', 'bucket-2'); - all the files go to bucket-2.
  • When the Bucket setting is empty under network settings and the constant is not defined, and you try to upload a file on site1.com - it will use its site-specific Bucket setting. If it's empty - there will be a warning that the bucket is not set, or maybe it stores the wrong value (e.g., from your production DB), and the upload will try to use it.

So, when using multisite WP, please pay attention to the Network Settings as well as the site-specific settings.

I hope this can help us to resolve the issue.

@i2dcarrasco
Copy link
Author

i2dcarrasco commented Jan 20, 2025

Hello,

Not really, that is not the real problem. I am using the same storage for all the sites, so I have set the configuration under the Network settings. The real problem is that those settings are used in all sites even when you are trying to change those settings via constants. The admin panel shows the correct settings set by the constants, but the plugin doesn't respect that configuration and instead uses the "old" settings (the one set by the network settings).

Is something like this:

Adjusted Settings What I see in the Admin What the plugin seems to use Is correct?
Network Network setting Network settings Of course
Constants Constants settings Network settings No way

The WP is multisite but all the sites are under the same domain, so I have never configured any setting via "per-site" settings page, all the changes were done in the network page.

The problem is what I say above: When the constants are disabled, the network settings are shown in all the sites, used in all sites and that is right, but when constants are set the constants settings are shown in all the sites (which it's correct), but the real settings used by the plugin seems to be the Network settings (which now are not visible under the admin page becuase are overrided by the constants).

If the admin page shows an storage name and all the configurations set by constants in all the sites (including the network settings page), and at the same time shows a message saying that there is no storage name set in the settings page, then there is a problem with the way it read the settings because is not considering the constants and it is just looking into the old Network Settings.

I don't know if under a clean Wordpress without any stored setting also happens. Maybe the problem only triggers when there are settings stored into the database.

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants