| 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 |
|