| 1 |
<?php |
| 2 |
/* |
| 3 |
* Plugin Name: Reset Customizer |
| 4 |
* Version: 1.1.7 |
| 5 |
* Plugin URI: https://webd.uk/support/ |
| 6 |
* Description: Adds a reset button to each section in the customizer and easily backup / restore / migrate customizer settings |
| 7 |
* Author: Webd Ltd |
| 8 |
* Author URI: https://webd.uk |
| 9 |
* License: GPLv2 or later |
| 10 |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
* Text Domain: reset-customizer |
| 12 |
*/ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit('This isn\'t the page you\'re looking for. Move along, move along.'); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if (!class_exists('reset_customizer_class')) { |
| 23 |
|
| 24 |
class reset_customizer_class { |
| 25 |
|
| 26 |
public static $version = '1.1.7'; |
| 27 |
|
| 28 |
function __construct() { |
| 29 |
|
| 30 |
if (is_admin()) { |
| 31 |
|
| 32 |
add_action('admin_menu', array($this, 'rc_settings_menu')); |
| 33 |
add_action('wp_ajax_rc_download_theme_mods', array($this,'rc_download_theme_mods')); |
| 34 |
add_action('wp_ajax_rc_delete_theme_mods', array($this,'rc_delete_theme_mods')); |
| 35 |
add_action('wp_ajax_rc_restore_theme_mods', array($this,'rc_restore_theme_mods')); |
| 36 |
add_action('wp_ajax_rc_download_theme_mods', array($this,'rc_download_theme_mods')); |
| 37 |
add_action('wp_ajax_rc_delete_theme_mod', array($this,'rc_delete_theme_mod')); |
| 38 |
add_action('wp_ajax_rc_get_control_defaults', array($this,'rc_get_control_defaults')); |
| 39 |
add_action('customize_controls_enqueue_scripts', array($this, 'rc_enqueue_customize_controls_js')); |
| 40 |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'rc_add_plugin_action_link')); |
| 41 |
add_action('admin_notices', 'rcCommon::admin_notices'); |
| 42 |
add_action('wp_ajax_dismiss_rc_notice_handler', 'rcCommon::ajax_notice_handler'); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
function rc_add_plugin_action_link($links) { |
| 49 |
|
| 50 |
$settings_links = rcCommon::plugin_action_links(add_query_arg('page', 'reset_customizer', admin_url('themes.php'))); |
| 51 |
|
| 52 |
return array_merge($settings_links, $links); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
function rc_enqueue_customize_controls_js() { |
| 57 |
|
| 58 |
wp_enqueue_script('rc-customize-controls', plugins_url('js/customize-controls.js', __FILE__), array('jquery', 'customize-controls'), rcCommon::plugin_version(), true); |
| 59 |
$l10n = array(); |
| 60 |
$l10n['resetPrefix'] = __('Reset', 'reset-customizer'); |
| 61 |
$l10n['resetSuffix'] = __('Defaults', 'reset-customizer'); |
| 62 |
$l10n['nonce'] = wp_create_nonce('get-control-defaults'); |
| 63 |
$l10n['errorNotice'] = __('Something went wrong!', 'reset-customizer'); |
| 64 |
wp_localize_script( |
| 65 |
'rc-customize-controls', |
| 66 |
'resetCustomizer', |
| 67 |
$l10n |
| 68 |
); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
function rc_settings_menu() { |
| 73 |
|
| 74 |
add_theme_page(__('Reset Customizer', 'reset-customizer'), __('Backup Customizer', 'reset-customizer'), 'manage_options', 'reset_customizer', array($this, 'rc_settings_page'), 2); |
| 75 |
add_action('admin_init', array($this, 'rc_register_settings')); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
function rc_settings_page() { |
| 80 |
|
| 81 |
?> |
| 82 |
<div class="wrap"> |
| 83 |
<h1><?php esc_html_e('Reset Customizer', 'reset-customizer'); ?></h1> |
| 84 |
<p><?php esc_html_e('All sets of Customizer Theme Modifications found in your database are listed below. You can download (backup), delete or even restore a set to the active theme. Deleting a set of theme modifications will reset all modifications for that theme. Restoring a set of theme modifications will completely overwrite the existing modifications for the active theme.', 'reset-customizer'); ?></p> |
| 85 |
<?php |
| 86 |
|
| 87 |
$all_options = wp_load_alloptions(); |
| 88 |
$theme_mods = array(); |
| 89 |
|
| 90 |
foreach ($all_options as $key => $value) { |
| 91 |
|
| 92 |
if (substr($key, 0, 11) === 'theme_mods_') { |
| 93 |
|
| 94 |
$theme_mods[substr($key, 11)] = $value; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
if ($theme_mods) { |
| 101 |
|
| 102 |
?> |
| 103 |
<table class="wp-list-table widefat striped"> |
| 104 |
<thead> |
| 105 |
<tr> |
| 106 |
<th class="manage-column column-name column-primary"><?php esc_html_e('Theme', 'reset-customizer'); ?></th> |
| 107 |
<th class="manage-column column-actions"><?php esc_html_e('Actions', 'reset-customizer'); ?></th> |
| 108 |
</tr> |
| 109 |
</thead> |
| 110 |
<tbody> |
| 111 |
<?php |
| 112 |
|
| 113 |
$i = 0; |
| 114 |
|
| 115 |
foreach ($theme_mods as $key => $value) { |
| 116 |
|
| 117 |
$i++; |
| 118 |
|
| 119 |
?> |
| 120 |
<tr class="theme-mods-<?php echo esc_attr(htmlentities($key)); ?>"> |
| 121 |
<td class="plugin-title column-primary"> |
| 122 |
<strong><?php echo esc_html(htmlentities($key)); ?></strong> |
| 123 |
<?php |
| 124 |
|
| 125 |
if ($key == get_stylesheet()) { |
| 126 |
|
| 127 |
echo 'Active Theme'; |
| 128 |
|
| 129 |
} elseif ($key == get_template()) { |
| 130 |
|
| 131 |
echo 'Parent Theme'; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
?> |
| 136 |
</td> |
| 137 |
<td class="column-actions"> |
| 138 |
<span class="rc-download button button-small" data-theme="<?php echo esc_html($key); ?>"><?php esc_html_e('Download', 'reset-customizer'); ?></span> |
| 139 |
<span class="rc-delete button button-small" data-theme="<?php echo esc_html($key); ?>"><?php esc_html_e('Delete', 'reset-customizer'); ?></span> |
| 140 |
</td> |
| 141 |
</tr> |
| 142 |
|
| 143 |
<?php |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
?> |
| 148 |
</tbody> |
| 149 |
</table> |
| 150 |
<?php |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
?> |
| 155 |
<input type="file" id="rc-json-file" accept=".json" style="display: none;" /> |
| 156 |
<p><span class="rc-restore button button-small" data-theme="<?php echo esc_html($key); ?>"><?php esc_html_e('Restore', 'reset-customizer'); ?></span></p> |
| 157 |
<script type="text/javascript"> |
| 158 |
(function($) { |
| 159 |
$('.rc-download').click(function() { |
| 160 |
var data = { |
| 161 |
action: 'rc_download_theme_mods', |
| 162 |
security: '<?php echo esc_attr(wp_create_nonce('download-theme-mods')); ?>', |
| 163 |
theme: $(this).data('theme') |
| 164 |
}; |
| 165 |
$.ajax({ |
| 166 |
url: ajaxurl, |
| 167 |
dataType: 'text', |
| 168 |
data: data, |
| 169 |
type: 'POST', |
| 170 |
success: function(themeModsJson) { |
| 171 |
var themeModsFile = new Blob( |
| 172 |
[themeModsJson], { |
| 173 |
type : "application/json;charset=utf-8" |
| 174 |
} |
| 175 |
); |
| 176 |
var today = new Date(); |
| 177 |
var dd = today.getDate(); |
| 178 |
var mm = today.getMonth() + 1; |
| 179 |
var yyyy = today.getFullYear(); |
| 180 |
if (dd < 10) { |
| 181 |
dd = '0' + dd; |
| 182 |
} |
| 183 |
if (mm < 10) { |
| 184 |
mm = '0' + mm; |
| 185 |
} |
| 186 |
today = yyyy + '_' + mm + '_' + dd; |
| 187 |
var downloadLink = document.createElement('a'); |
| 188 |
var downloadURL = window.URL.createObjectURL(themeModsFile); |
| 189 |
downloadLink.href = downloadURL; |
| 190 |
downloadLink.download = data.theme + '_' + today + '.json'; |
| 191 |
document.body.append(downloadLink); |
| 192 |
downloadLink.click(); |
| 193 |
downloadLink.remove(); |
| 194 |
window.URL.revokeObjectURL(downloadURL); |
| 195 |
}, |
| 196 |
error: function() { |
| 197 |
alert('<?php esc_html_e('Something went wrong!', 'reset-customizer'); ?>'); |
| 198 |
} |
| 199 |
}); |
| 200 |
}); |
| 201 |
$('.rc-delete').click(function() { |
| 202 |
var confirmText = '<?php |
| 203 |
/* translators: current theme slug */ |
| 204 |
esc_html_e('Are you sure you want to delete all the %s theme modifications?', 'reset-customizer'); ?>'; |
| 205 |
if (confirm(confirmText.replace('%s', $(this).data('theme')))) { |
| 206 |
var data = { |
| 207 |
action: 'rc_delete_theme_mods', |
| 208 |
security: '<?php echo esc_attr(wp_create_nonce('delete-theme-mods')); ?>', |
| 209 |
theme: $(this).data('theme') |
| 210 |
}; |
| 211 |
$.ajax({ |
| 212 |
url: ajaxurl, |
| 213 |
data: data, |
| 214 |
type: 'POST', |
| 215 |
success: function() { |
| 216 |
$('.theme-mods-' + data.theme).fadeTo('slow', 0, function() { |
| 217 |
$('.theme-mods-' + data.theme).remove(); |
| 218 |
}); |
| 219 |
}, |
| 220 |
error: function() { |
| 221 |
alert('<?php esc_attr_e('Something went wrong!', 'reset-customizer'); ?>'); |
| 222 |
} |
| 223 |
}); |
| 224 |
} |
| 225 |
}); |
| 226 |
$('.rc-restore').click(function() { |
| 227 |
document.getElementById('rc-json-file').click(); |
| 228 |
}); |
| 229 |
$('#rc-json-file').change(function() { |
| 230 |
var confirmText = '<?php |
| 231 |
/* translators: current theme slug */ |
| 232 |
esc_attr_e('Are you sure you want to upload %s to the active theme?', 'reset-customizer'); ?>'; |
| 233 |
if (confirm(confirmText.replace('%s', $('#rc-json-file').prop('files')[0].name))) { |
| 234 |
var data = new FormData(); |
| 235 |
data.append('action', 'rc_restore_theme_mods'); |
| 236 |
data.append('security', '<?php echo esc_attr(wp_create_nonce('restore-theme-mods')); ?>'); |
| 237 |
data.append('file', $('#rc-json-file').prop('files')[0]); |
| 238 |
$.ajax({ |
| 239 |
url: ajaxurl, |
| 240 |
data: data, |
| 241 |
type: 'POST', |
| 242 |
contentType: false, |
| 243 |
processData: false, |
| 244 |
success: function() { |
| 245 |
$('#rc-json-file').val(''); |
| 246 |
alert('Theme modifications have been successfully restored to the active theme.'); |
| 247 |
}, |
| 248 |
error: function() { |
| 249 |
$('#rc-json-file').val(''); |
| 250 |
alert('<?php esc_attr_e('Something went wrong!', 'reset-customizer'); ?>'); |
| 251 |
} |
| 252 |
}); |
| 253 |
} |
| 254 |
}); |
| 255 |
})(jQuery); |
| 256 |
</script> |
| 257 |
<!-- <form action="options.php" method="post"> |
| 258 |
<?php |
| 259 |
|
| 260 |
settings_fields('reset_customizer'); |
| 261 |
|
| 262 |
?> |
| 263 |
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_attr_e('Save Changes','reset-customizer'); ?>"></p> |
| 264 |
</form> --> |
| 265 |
<?php |
| 266 |
|
| 267 |
$active_theme_mods = get_theme_mods(); |
| 268 |
|
| 269 |
if ($active_theme_mods) { |
| 270 |
|
| 271 |
ksort($active_theme_mods); |
| 272 |
|
| 273 |
if (!class_exists('WP_Customize_Manager')) { |
| 274 |
|
| 275 |
require_once(ABSPATH . 'wp-includes/class-wp-customize-manager.php'); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
?> |
| 280 |
<h2><?php esc_html_e('Active Theme Mods', 'reset-customizer'); ?></h2> |
| 281 |
|
| 282 |
<p><span id="toggle-active-theme-mods" class="button button-small" data-mod="<?php echo esc_html($key); ?>"><?php esc_html_e('Show', 'reset-customizer'); ?></span></p> |
| 283 |
|
| 284 |
<table id="active-theme-mods" class="wp-list-table widefat striped" style="display: none;"> |
| 285 |
<thead> |
| 286 |
<tr> |
| 287 |
<td class="manage-column column-cb check-column"></td> |
| 288 |
<th class="manage-column column-name column-primary">Mod</th> |
| 289 |
<th class="manage-column column-description">Value</th> |
| 290 |
</tr> |
| 291 |
</thead> |
| 292 |
<tbody> |
| 293 |
<?php |
| 294 |
|
| 295 |
$i = 0; |
| 296 |
|
| 297 |
foreach ($active_theme_mods as $key => $value) { |
| 298 |
|
| 299 |
$i++; |
| 300 |
?> |
| 301 |
|
| 302 |
<tr class="theme-mod-<?php echo esc_attr(htmlentities($key)); ?>"> |
| 303 |
<td class="check-column"><?php echo esc_html($i); ?>)</td> |
| 304 |
<td class="plugin-title column-primary"><?php echo esc_html($key); ?> <span class="rc-delete-mod button button-small" data-mod="<?php echo esc_html($key); ?>"><?php esc_html_e('Delete', 'reset-customizer'); ?></span></td> |
| 305 |
<td class="column-description"><pre></pre><?php |
| 306 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 307 |
echo esc_html(print_r($value, true)); ?></pre></td> |
| 308 |
</tr> |
| 309 |
|
| 310 |
<?php |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
?> |
| 315 |
</tbody> |
| 316 |
</table> |
| 317 |
<script type="text/javascript"> |
| 318 |
(function($) { |
| 319 |
$('#toggle-active-theme-mods').click(function() { |
| 320 |
$('#active-theme-mods').toggle('slow', function() { |
| 321 |
$('#toggle-active-theme-mods').text(function(i, text){ |
| 322 |
return text === 'Show' ? 'Hide' : 'Show'; |
| 323 |
}); |
| 324 |
}); |
| 325 |
}); |
| 326 |
$('.rc-delete-mod').click(function() { |
| 327 |
var confirmText = '<?php |
| 328 |
/* translators: theme mod key */ |
| 329 |
esc_attr_e('Are you sure you want to delete theme mod "%s"?', 'reset-customizer'); ?>'; |
| 330 |
if (confirm(confirmText.replace('%s', $(this).data('mod')))) { |
| 331 |
var data = { |
| 332 |
action: 'rc_delete_theme_mod', |
| 333 |
security: '<?php echo esc_attr(wp_create_nonce('delete-theme-mod')); ?>', |
| 334 |
mod: $(this).data('mod') |
| 335 |
}; |
| 336 |
$.ajax({ |
| 337 |
url: ajaxurl, |
| 338 |
data: data, |
| 339 |
type: 'POST', |
| 340 |
success: function() { |
| 341 |
$('.theme-mod-' + data.mod).fadeTo('slow', 0, function() { |
| 342 |
$('.theme-mod-' + data.mod).remove(); |
| 343 |
}); |
| 344 |
}, |
| 345 |
error: function() { |
| 346 |
alert('<?php esc_attr_e('Something went wrong!', 'reset-customizer'); ?>'); |
| 347 |
} |
| 348 |
}); |
| 349 |
} |
| 350 |
}); |
| 351 |
})(jQuery); |
| 352 |
</script> |
| 353 |
<?php |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
?> |
| 358 |
</div> |
| 359 |
<?php |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
function rc_register_settings() { |
| 364 |
|
| 365 |
register_setting( |
| 366 |
'reset_customizer', |
| 367 |
'reset_customizer', |
| 368 |
array($this, 'rc_options_validate') |
| 369 |
); |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
function rc_options_validate($input) { |
| 374 |
|
| 375 |
$options = get_option('reset_customizer'); |
| 376 |
|
| 377 |
return $options; |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
function rc_download_theme_mods() { |
| 382 |
|
| 383 |
check_ajax_referer('download-theme-mods', 'security'); |
| 384 |
|
| 385 |
if (current_user_can('manage_options') && isset($_POST['theme']) && sanitize_key($_POST['theme'])) { |
| 386 |
|
| 387 |
$theme_mods = get_option('theme_mods_' . sanitize_key($_POST['theme'])); |
| 388 |
|
| 389 |
if ($theme_mods) { |
| 390 |
|
| 391 |
wp_send_json($theme_mods); |
| 392 |
|
| 393 |
} else { |
| 394 |
|
| 395 |
wp_send_json_error(); |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
} else { |
| 400 |
|
| 401 |
wp_send_json_error(); |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
wp_die(); |
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
function rc_delete_theme_mods() { |
| 410 |
|
| 411 |
check_ajax_referer('delete-theme-mods', 'security'); |
| 412 |
|
| 413 |
if (current_user_can('manage_options') && isset($_POST['theme']) && sanitize_key($_POST['theme'])) { |
| 414 |
|
| 415 |
if (delete_option('theme_mods_' . sanitize_key($_POST['theme']))) { |
| 416 |
|
| 417 |
wp_send_json_success(); |
| 418 |
|
| 419 |
} else { |
| 420 |
|
| 421 |
wp_send_json_error(); |
| 422 |
|
| 423 |
} |
| 424 |
|
| 425 |
} else { |
| 426 |
|
| 427 |
wp_send_json_error(); |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
wp_die(); |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
function rc_restore_theme_mods() { |
| 436 |
|
| 437 |
check_ajax_referer('restore-theme-mods', 'security'); |
| 438 |
|
| 439 |
if (isset($_FILES['file']['tmp_name']) && isset($_FILES['file']['type']) && $_FILES['file']['type'] == 'application/json' && current_user_can('manage_options')) { |
| 440 |
|
| 441 |
$json_data = file_get_contents(sanitize_text_field($_FILES['file']['tmp_name'])); |
| 442 |
$theme_mods = false; |
| 443 |
|
| 444 |
if ($json_data) { |
| 445 |
|
| 446 |
$theme_mods = json_decode($json_data, true); |
| 447 |
|
| 448 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 449 |
|
| 450 |
$theme_mods = false; |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
if ($theme_mods) { |
| 457 |
|
| 458 |
$old_theme_mods = get_option('theme_mods_' . get_stylesheet()); |
| 459 |
|
| 460 |
if ($old_theme_mods) { |
| 461 |
|
| 462 |
update_option('theme_mods_' . get_stylesheet() . '_bak', $old_theme_mods); |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
update_option('theme_mods_' . get_stylesheet(), $theme_mods); |
| 467 |
|
| 468 |
wp_send_json_success(); |
| 469 |
|
| 470 |
} else { |
| 471 |
|
| 472 |
wp_send_json_error(); |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
} else { |
| 477 |
|
| 478 |
wp_send_json_error(); |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
wp_die(); |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
function rc_delete_theme_mod() { |
| 487 |
|
| 488 |
check_ajax_referer('delete-theme-mod', 'security'); |
| 489 |
|
| 490 |
if (current_user_can('manage_options') && isset($_POST['mod']) && sanitize_key($_POST['mod'])) { |
| 491 |
|
| 492 |
remove_theme_mod(sanitize_key($_POST['mod'])); |
| 493 |
|
| 494 |
wp_send_json_success(); |
| 495 |
|
| 496 |
} else { |
| 497 |
|
| 498 |
wp_send_json_error(); |
| 499 |
|
| 500 |
} |
| 501 |
|
| 502 |
wp_die(); |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
public function rc_get_control_defaults() { |
| 507 |
|
| 508 |
check_ajax_referer('get-control-defaults'); |
| 509 |
|
| 510 |
$controls = array(); |
| 511 |
|
| 512 |
if (isset($_POST['controls'])) { |
| 513 |
|
| 514 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 515 |
$controls = wp_unslash($_POST['controls']); |
| 516 |
|
| 517 |
} |
| 518 |
|
| 519 |
if ( |
| 520 |
current_user_can('manage_options') && |
| 521 |
is_array($controls) && |
| 522 |
$controls |
| 523 |
) { |
| 524 |
|
| 525 |
global $wp_customize; |
| 526 |
$control_defaults = array(); |
| 527 |
|
| 528 |
foreach ($controls as $control) { |
| 529 |
|
| 530 |
$control = sanitize_key($control); |
| 531 |
$setting = $wp_customize->get_setting($control); |
| 532 |
|
| 533 |
if ($setting && isset($setting->default)) { |
| 534 |
|
| 535 |
$control_defaults[$control] = $setting->default; |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
} |
| 540 |
|
| 541 |
wp_send_json_success($control_defaults); |
| 542 |
|
| 543 |
} else { |
| 544 |
|
| 545 |
wp_send_json_error(); |
| 546 |
|
| 547 |
} |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
} |
| 552 |
|
| 553 |
if (!class_exists('rcCommon')) { |
| 554 |
|
| 555 |
require_once(dirname(__FILE__) . '/includes/class-rc-common.php'); |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
$reset_customizer_object = new reset_customizer_class(); |
| 560 |
|
| 561 |
} |
| 562 |
|
| 563 |
?> |
| 564 |
|