| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Applies boolean settings while respecting simple vs advanced settings mode. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_SettingsBooleanModePolicy { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_ContentRepositoryInterface */ |
| 13 |
private $contentRepo; |
| 14 |
|
| 15 |
/** @var object|null */ |
| 16 |
private $modePreference; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param ABJ_404_Solution_ContentRepositoryInterface $contentRepo |
| 20 |
* @param object|null $modePreference Optional service exposing getMode(). |
| 21 |
*/ |
| 22 |
public function __construct($contentRepo, $modePreference = null) { |
| 23 |
$this->contentRepo = $contentRepo; |
| 24 |
$this->modePreference = $modePreference; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param array<string, mixed> $options |
| 29 |
* @param array<string, mixed> $postData |
| 30 |
* @return string Empty string; kept for section-updater message contract. |
| 31 |
*/ |
| 32 |
public function apply(array &$options, array $postData): string { |
| 33 |
$settingsMode = $this->settingsMode(); |
| 34 |
$allBooleanOptions = array( |
| 35 |
'remove_matches', |
| 36 |
'debug_mode', |
| 37 |
'suggest_cats', |
| 38 |
'suggest_tags', |
| 39 |
'auto_redirects', |
| 40 |
'auto_slugs', |
| 41 |
'auto_cats', |
| 42 |
'auto_tags', |
| 43 |
'auto_trash_redirect', |
| 44 |
'capture_404', |
| 45 |
'send_error_logs', |
| 46 |
'log_raw_ips', |
| 47 |
'redirect_all_requests', |
| 48 |
'update_suggest_url', |
| 49 |
'suggest_minscore_enabled', |
| 50 |
'auto_trash_junk_urls', |
| 51 |
); |
| 52 |
$simpleModeOptions = array('auto_redirects', 'capture_404', 'auto_trash_junk_urls'); |
| 53 |
$optionsToProcess = $settingsMode === 'simple' ? $simpleModeOptions : $allBooleanOptions; |
| 54 |
|
| 55 |
foreach ($optionsToProcess as $optionName) { |
| 56 |
$newVal = (array_key_exists($optionName, $postData) && $postData[$optionName] == "1") ? 1 : 0; |
| 57 |
if (!array_key_exists($optionName, $options) || $options[$optionName] != $newVal) { |
| 58 |
$this->contentRepo->deleteSpellingCache(); |
| 59 |
} |
| 60 |
$options[$optionName] = $newVal; |
| 61 |
} |
| 62 |
|
| 63 |
if ($settingsMode === 'simple') { |
| 64 |
$autoRedirectsValue = isset($options['auto_redirects']) ? $options['auto_redirects'] : 0; |
| 65 |
$options['auto_cats'] = $autoRedirectsValue; |
| 66 |
$options['auto_tags'] = $autoRedirectsValue; |
| 67 |
} |
| 68 |
|
| 69 |
return ""; |
| 70 |
} |
| 71 |
|
| 72 |
private function settingsMode(): string { |
| 73 |
if ($this->modePreference === null) { |
| 74 |
$this->modePreference = abj_service('settings_mode_preference'); |
| 75 |
} |
| 76 |
return is_callable(array($this->modePreference, 'getMode')) ? |
| 77 |
(string)call_user_func(array($this->modePreference, 'getMode')) : 'advanced'; |
| 78 |
} |
| 79 |
} |
| 80 |
|