Last active
December 6, 2023 14:59
-
-
Save DanielMarklund/2e3d282f5825cf04015a23a5a3c78234 to your computer and use it in GitHub Desktop.
Give Redis write permissions when DISALLOW_FILE_MODS is enabled in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Redis_Write_Permissions | |
* | |
* This class is responsible for enabling write permissions for the Redis object cache drop-in | |
* when the DISALLOW_FILE_MODS constant is set to true, without having to disable DISALLOW_FILE_MODS. | |
* | |
* Notes: Use with https://wordpress.org/plugins/redis-cache/ (Redis Object Cache by Till Krüss) | |
* By default, the Redis Object Cache drop-in is not writeable when DISALLOW_FILE_MODS is set to true. | |
* | |
* Installation: Create a new file in the mu-plugins folder called redis-write-permissions.php | |
* If you do not have a mu-plugins folder you can create one in the wp-content folder. | |
* Copy all of this and paste it into redis-write-permissions.php | |
* No activation is needed and the plugin will work immediately. | |
* Verify in the Redis Object Cache plugin settings that the filesystem is writeable. | |
* | |
* Thanks to: Edgards Abolins for providing the original solution (https://doinwp.com/tips/fix-for-redis-filesystem-not-writeable/). | |
* Till Krüss for providing the Redis Object Cache plugin. | |
* Templ.io for excellent managed WordPress hosting and support. | |
* | |
* Author: Daniel Leis | |
* Version: 1.0 | |
*/ | |
class Redis_Write_Permissions { | |
/** | |
* Constructor method. | |
* It checks if DISALLOW_FILE_MODS is set and true, and if so, enables write permissions for the Redis object cache drop-in. | |
*/ | |
public function __construct() { | |
if (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS) { | |
add_filter('file_mod_allowed', array($this, 'enable_redis_dropin_write_permissions'), 10, 2); | |
} | |
} | |
/** | |
* Enables Redis drop-in write permissions. | |
* | |
* This function checks if the provided context is 'object_cache_dropin'. | |
* If the context matches, it returns true, otherwise it returns the provided setting. | |
* | |
* @param mixed $setting The original setting value. | |
* @param string $context The context in which the setting is being checked. | |
* @return bool The updated setting value. | |
*/ | |
public function enable_redis_dropin_write_permissions(bool $setting, string $context) { | |
return in_array($context, ['object_cache_dropin']) ? true : $setting; | |
} | |
} | |
new Redis_Write_Permissions(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment