Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Catch Exceptions Thrown When Saving Config #408

Merged
merged 1 commit into from
Feb 21, 2017

Conversation

dsix-work
Copy link
Contributor

@dsix-work dsix-work commented Feb 21, 2017

In our environment we do not allow the configuration file to be written since we are using a specially constructed file to load some parameters from environment variables. As of 0.9.5.2 this causes fatal errors on any W3TC settings page and on the plugins page in the admin interface.

This is the error:

PHP Fatal error:  Uncaught exception 'Exception' with message 'Can't write to file <strong>/var/www/html/wp-content/w3tc-config/master.php</strong>' in /var/www/html/wp-content/plugins/w3-total-cache/Util_File.php:375
Stack trace:
#0 /var/www/html/wp-content/plugins/w3-total-cache/ConfigCompiler.php(241): W3TC\Util_File::file_put_contents_atomic('/var/www/html/w...', '<?php exit; ?>{...')
#1 /var/www/html/wp-content/plugins/w3-total-cache/Config.php(291): W3TC\ConfigCompiler->save()
#2 /var/www/html/wp-content/plugins/w3-total-cache/Generic_Environment.php(30): W3TC\Config->save()
#3 /var/www/html/wp-content/plugins/w3-total-cache/Root_Environment.php(28): W3TC\Generic_Environment->fix_on_wpadmin_request(Object(W3TC\Config), false)
#4 /var/www/html/wp-content/plugins/w3-total-cache/Generic_Plugin_Admin.php(705): W3TC\Root_Environment->fix_in_wpadmin(Object(W3TC\Config))
#5 [internal function]: W3TC\Generic_Plugin_Admin->admin_notices('')
#6 /var/www/html/wp-includes/class-wp-hook.php(...

W3TC\Util_File->file_put_contents_atomic() throws generic \Exception if the temporary or final files are not writeable. This is caught in W3TC\Util_Admin->config_save() for normal configuration save operations (e.g. the Save All Settings button in the admin interface), but not caught when called from W3TC\Generic_Environment->fix_on_wpadmin_request()

This change adds a try/catch block to catch this exception and push it into Util_Environment_Exceptions for handling.

Note: The diff is messy since this involves an indentation change and one of the indented lines matches a line at the previous indentation level.

W3TC\Util_File->file_put_contents_atomic() throws generic \Exception
if the temporary or final files are not writeable.
@amiga-500 amiga-500 changed the title Catch exceptions thrown by W3TC\Config->save() Catch Exceptions Thrown When Saving Config Feb 21, 2017
@amiga-500 amiga-500 merged commit c9afab6 into szepeviktor:v0.9.5.x Feb 21, 2017
@amiga-500
Copy link
Collaborator

Much appreciated for this fix! 😄

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

Successfully merging this pull request may close these issues.

3 participants