Skip to content

Instantly share code, notes, and snippets.

@gaambo
Last active February 3, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaambo/12877a59aa4193a9a496b6519b91fa49 to your computer and use it in GitHub Desktop.
Save gaambo/12877a59aa4193a9a496b6519b91fa49 to your computer and use it in GitHub Desktop.
nippet to allow setting UpdraftPlus settings via wp-config.php constants
<?php
/**
* Allows setting UpdraftPlus settings via wp-config.php constants
*
* Usefull for setting AWS S3 Access/Secret Keys in a constant instead of saving it in the database
* Set the constant UPDRAFT_S3_SETTINGS with configuration like this:
* ```php
* define('UPDRAFT_S3_SETTINGS', [
* 'accesskey' => 'accesskey',
* 'secretkey' => 'secretkey',
* 'path' => '$BUCKET/$DIR'
* ]);
* ```
*
* For available keys setup a job via UI and look at the serialized value in the database
* accesskey, secretkey, path ($BUCKET/$DIR)
*/
/**
* Merges the settings when get for backend UI display (prepare for template)
* Hooked on `updraftplus_options_s3_options`
*/
function mwp_mergeUpdraftSettingsTemplate($options)
{
if (!defined('UPDRAFT_S3_SETTINGS') || !UPDRAFT_S3_SETTINGS) {
return $options;
}
$options = array_merge($options, UPDRAFT_S3_SETTINGS);
return $options;
}
add_filter('updraftplus_options_s3_options', 'mwp_mergeUpdraftSettingsTemplate');
/**
* Merges the settings when get for checking anywhere by Updraft
* Hooked on `updraftplus_get_option`
*/
function mwp_mergeUpdraftSettingsOption($value, $option) {
if ($option !== 'updraft_s3' && $option !== 'updraft_s3generic') {
return $value;
}
if (!defined('UPDRAFT_S3_SETTINGS') || !UPDRAFT_S3_SETTINGS) {
return $value;
}
if (!isset($value['settings']) || !$value['settings']) { // no instance created yet
return $value;
}
foreach ($value['settings'] as $instanceId => $instanceSettings) {
foreach ($instanceSettings as $key => $val) {
if (!isset(UPDRAFT_S3_SETTINGS[$key])) {
continue;
}
$value['settings'][$instanceId][$key] = UPDRAFT_S3_SETTINGS[$key]; // overwrite with constant value
}
}
return $value;
}
add_filter('updraftplus_get_option', 'mwp_mergeUpdraftSettingsOption', 10, 2);
/**
* Prevents from overwriting UpdraftPlus settings defined via constants in the backend UI
*
* Loops through all jobs and optionKeys and checks if they are defined in the constant,
* if yes an empty string is saved to the database
*
* Hooked on `updraftplus_update_option`
*/
function mwp_mergeUpdraftSettingsOnSave($value, $option)
{
if ($option !== 'updraft_s3' && $option !== 'updraft_s3generic') {
return $value;
}
if (!defined('UPDRAFT_S3_SETTINGS') || !UPDRAFT_S3_SETTINGS) {
return $value;
}
foreach ($value['settings'] as $instanceId => $instanceSettings) {
foreach ($instanceSettings as $key => $val) {
if (!isset(UPDRAFT_S3_SETTINGS[$key])) {
continue;
}
$value['settings'][$instanceId][$key] = ''; // reset to empty
}
}
return $value;
}
add_filter('updraftplus_update_option', 'mwp_mergeUpdraftSettingsOnSave', 10, 2);
/**
* Makes the constant-defined settings readonly/disabled in the backend UI
*
* Hooked on `admin_footer-settings_page_updraftplus`
*/
function mwp_disableUpdraftSettingsInputs()
{
if (!defined('UPDRAFT_S3_SETTINGS') || !UPDRAFT_S3_SETTINGS) {
return;
}
$readableKeysSelectorString = implode(',', array_map(function ($k) {
return "[id^='updraft_s3_{$k}_']";
}, array_keys(UPDRAFT_S3_SETTINGS)));
?>
<script>
(function($) {
$(function() {
$("<?php echo $readableKeysSelectorString; ?>").attr("readonly", true).attr("disabled", true);
});
})(jQuery)
</script>
<?php
}
add_action('admin_footer-settings_page_updraftplus', 'mwp_disableUpdraftSettingsInputs');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment