PluginProbe
404 Solution / 4.1.19
404 Solution v4.1.19
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / ajax / Ajax_RestoreDefaults.php

Ajax_RestoreDefaults.php in 404 Solution 4.1.19, at includes/ajax/Ajax_RestoreDefaults.php

68 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 if (!defined('ABSPATH')) {
5 exit;
6 }
7
8 /**
9 * AJAX handler for restoring plugin settings to canonical defaults.
10 *
11 * Entry point: wp_ajax_abj404_restore_defaults.
12 * Gates: valid nonce ('abj404_restore_defaults') AND plugin admin capability.
13 * Action: writes PluginLogic::getDefaultOptions() through PluginLogic::updateOptions(),
14 * preserving the DB_VERSION key so a settings restore does not trigger a schema downgrade.
15 */
16 class ABJ_404_Solution_Ajax_RestoreDefaults {
17 use ABJ_404_Solution_AjaxSecurityTrait;
18
19 /** @var self|null */
20 private static $instance = null;
21
22 /** @return self */
23 public static function getInstance(): self {
24 if (self::$instance == null) {
25 self::$instance = new ABJ_404_Solution_Ajax_RestoreDefaults();
26 }
27 return self::$instance;
28 }
29
30 /**
31 * Initialize AJAX handler.
32 * @return void
33 */
34 static function init(): void {
35 $me = ABJ_404_Solution_Ajax_RestoreDefaults::getInstance();
36 ABJ_404_Solution_WPUtils::safeAddAction('wp_ajax_abj404_restore_defaults',
37 array($me, 'handleRestoreDefaults'));
38 }
39
40 /**
41 * Handle the restore-defaults AJAX request. Verifies nonce + admin
42 * capability, then overwrites abj404_settings with getDefaultOptions().
43 * DB_VERSION is preserved from the current settings so a settings reset
44 * does not look like a schema downgrade to DatabaseUpgradesEtc.
45 * @return void
46 */
47 function handleRestoreDefaults(): void {
48 self::requireAdminWithNonce('abj404_restore_defaults');
49
50 $abj404logic = abj_service('plugin_logic');
51
52 $defaults = $abj404logic->getDefaultOptions();
53
54 // Preserve DB_VERSION so the restore does not appear as a schema
55 // downgrade to the upgrade engine.
56 $current = $abj404logic->getOptions(true);
57 if (is_array($current) && array_key_exists('DB_VERSION', $current)) {
58 $defaults['DB_VERSION'] = $current['DB_VERSION'];
59 }
60
61 $abj404logic->updateOptions($defaults);
62
63 wp_send_json_success(array(
64 'message' => __('Settings restored to defaults.', '404-solution'),
65 ));
66 }
67 }
68