Skip to content

Instantly share code, notes, and snippets.

@gaambo
Created February 2, 2021 12:08
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/d3bba5427d10d5c495838fd31685975f to your computer and use it in GitHub Desktop.
Save gaambo/d3bba5427d10d5c495838fd31685975f to your computer and use it in GitHub Desktop.
Snippet to allow setting BackWPup settings via wp-config.php constants
<?php
/**
* Allows setting BackWPup 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 BACKWPUP_SETTINGS with configuration per jobid(!) like this:
* ```php
* define('BACKWPUP_SETTINGS', [
* '1' => [ // jobid
* 's3accesskey' => 'accesskey',
* 's3secretkey' => 'secretkey'
* ]
* ]);
* ```
*
* For available keys setup a job via UI and look at the serialized value in the database
* s3region, s3accesskey, s3secretkey, s3bucket, s3dir
*
* Hooked on `site_option_backwpup_jobs`
* because BackWPup uses site_option and site_option calls this filter even if it's not a multisite
*/
function mwp_mergeBackWPupSettings($value)
{
if (!is_array($value)) {
return $value;
}
if (!defined('BACKWPUP_SETTINGS') || !BACKWPUP_SETTINGS) {
return $value;
}
foreach ($value as $k => $job) {
$jobid = $job['jobid'];
if (!isset(BACKWPUP_SETTINGS[$jobid])) {
continue;
}
$value[$k] = array_merge($job, BACKWPUP_SETTINGS[$jobid]);
}
return $value;
}
add_filter('site_option_backwpup_jobs', 'mwp_mergeBackWPupSettings');
/**
* Prevents from overwriting BackWPup 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 `pre_update_site_option_backwpup_jobs`
* because BackWPup uses site_option and site_option calls this filter even if it's not a multisite
*/
function mwp_mergeBackWPupSettingsOnSave($value)
{
if (!is_array($value)) {
return $value;
}
if (!defined('BACKWPUP_SETTINGS') || !BACKWPUP_SETTINGS) {
return $value;
}
foreach ($value as $k => $job) {
$jobid = $job['jobid'];
if (!isset(BACKWPUP_SETTINGS[$jobid])) {
continue;
}
foreach ($job as $optionKey => $optionValue) {
if (isset(BACKWPUP_SETTINGS[$jobid][$optionKey])) {
$value[$k][$optionKey] = ''; // reset to empty
}
}
}
return $value;
}
add_filter('pre_update_site_option_backwpup_jobs', 'mwp_mergeBackWPupSettingsOnSave'); #
/**
* Makes the constant-defined settings readonly/disabled in the backend UI
*
*/
function mwp_printBackWPupJavaScript()
{
$jobid = (int)$_GET['jobid'];
if (!defined('BACKWPUP_SETTINGS') || !BACKWPUP_SETTINGS || !isset(BACKWPUP_SETTINGS[$jobid])) {
return;
}
$readableKeys = array_keys(BACKWPUP_SETTINGS[$jobid]);
if (empty($readableKeys)) {
return;
}
$readableKeysSelectorString = implode(',', array_map(function ($k) {
return "#$k, input[name='$k']";
}, $readableKeys));
?>
<script>
(function($) {
$("<?php echo $readableKeysSelectorString; ?>").attr("readonly", true).attr("disabled", true);
})(jQuery)
</script>
<?php
}
add_action('admin_footer-backwpup-pro_page_backwpupeditjob', 'mwp_printBackWPupJavaScript');
add_action('admin_footer-backwpup_page_backwpupeditjob', 'mwp_printBackWPupJavaScript');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment