| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Applies notification and email digest settings from the settings form payload. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_SettingsNotificationPolicy { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Logging */ |
| 13 |
private $logger; |
| 14 |
|
| 15 |
/** @param ABJ_404_Solution_Logging $logger */ |
| 16 |
public function __construct($logger) { |
| 17 |
$this->logger = $logger; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* @param array<string, mixed> $options |
| 22 |
* @param array<string, mixed> $postData |
| 23 |
* @return string Any translated validation messages. |
| 24 |
*/ |
| 25 |
public function apply(array &$options, array $postData): string { |
| 26 |
$message = ""; |
| 27 |
|
| 28 |
$message .= $this->applyAdminNotificationThreshold($options, $postData); |
| 29 |
if (isset($postData['admin_notification_email'])) { |
| 30 |
$options['admin_notification_email'] = trim( |
| 31 |
wp_kses_post(is_string($postData['admin_notification_email']) ? $postData['admin_notification_email'] : '') |
| 32 |
); |
| 33 |
} |
| 34 |
$message .= $this->applyNotificationFrequency($options, $postData); |
| 35 |
$message .= $this->applyDigestLimit($options, $postData); |
| 36 |
|
| 37 |
return $message; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param array<string, mixed> $options |
| 42 |
* @param array<string, mixed> $postData |
| 43 |
*/ |
| 44 |
private function applyAdminNotificationThreshold(array &$options, array $postData): string { |
| 45 |
if (!isset($postData['admin_notification'])) { |
| 46 |
return ""; |
| 47 |
} |
| 48 |
|
| 49 |
$rawAdminNotification = is_scalar($postData['admin_notification']) ? $postData['admin_notification'] : ''; |
| 50 |
if (is_numeric($rawAdminNotification) && (int)$rawAdminNotification >= 0) { |
| 51 |
$options['admin_notification'] = (int)$rawAdminNotification; |
| 52 |
return ""; |
| 53 |
} |
| 54 |
|
| 55 |
if (is_numeric($rawAdminNotification)) { |
| 56 |
return __('Error: Admin notification threshold must be a non-negative number', '404-solution') . ".<BR/>"; |
| 57 |
} |
| 58 |
|
| 59 |
return ""; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param array<string, mixed> $options |
| 64 |
* @param array<string, mixed> $postData |
| 65 |
*/ |
| 66 |
private function applyNotificationFrequency(array &$options, array $postData): string { |
| 67 |
if (!isset($postData['admin_notification_frequency'])) { |
| 68 |
return ""; |
| 69 |
} |
| 70 |
|
| 71 |
$allowedFrequencies = array('instant', 'daily', 'weekly', 'never'); |
| 72 |
$freq = sanitize_text_field( |
| 73 |
is_string($postData['admin_notification_frequency']) ? $postData['admin_notification_frequency'] : '' |
| 74 |
); |
| 75 |
if (!in_array($freq, $allowedFrequencies, true)) { |
| 76 |
return __('Error: Invalid email notification frequency selected', '404-solution') . ".<BR/>"; |
| 77 |
} |
| 78 |
|
| 79 |
$options['admin_notification_frequency'] = $freq; |
| 80 |
$emailDigest = new ABJ_404_Solution_EmailDigest( |
| 81 |
abj_service('logs_repository'), |
| 82 |
ABJ_404_Solution_StatsRepositoryResolver::resolve(__CLASS__), |
| 83 |
$this->logger |
| 84 |
); |
| 85 |
// Pass $freq directly rather than letting scheduleNextDigest() re-read |
| 86 |
// the options repository: the caller (PluginLogicSettingsUpdate) |
| 87 |
// persists $options AFTER apply() returns, so a re-fetch here would |
| 88 |
// still see the pre-save frequency. |
| 89 |
$emailDigest->scheduleNextDigest($freq); |
| 90 |
return ""; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param array<string, mixed> $options |
| 95 |
* @param array<string, mixed> $postData |
| 96 |
*/ |
| 97 |
private function applyDigestLimit(array &$options, array $postData): string { |
| 98 |
if (!isset($postData['admin_notification_digest_limit'])) { |
| 99 |
return ""; |
| 100 |
} |
| 101 |
|
| 102 |
if (is_numeric($postData['admin_notification_digest_limit']) && $postData['admin_notification_digest_limit'] >= 1) { |
| 103 |
$options['admin_notification_digest_limit'] = absint($postData['admin_notification_digest_limit']); |
| 104 |
return ""; |
| 105 |
} |
| 106 |
|
| 107 |
return __('Error: Digest limit must be a number greater than or equal to 1', '404-solution') . ".<BR/>"; |
| 108 |
} |
| 109 |
} |
| 110 |
|