| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manages recipient subscriptions across notification modules. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component; |
| 9 |
|
| 10 |
use WP_Defender\Component; |
| 11 |
use WP_Defender\Model\Notification as Model_Notification; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handles recipient persistence for notification modules. |
| 15 |
*/ |
| 16 |
class Recipients extends Component { |
| 17 |
|
| 18 |
/** |
| 19 |
* Notification service. |
| 20 |
* |
| 21 |
* @var Notification |
| 22 |
*/ |
| 23 |
private Notification $notification; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize dependencies. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
$this->notification = wd_di()->get( Notification::class ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Save recipient subscriptions. |
| 34 |
* |
| 35 |
* @param array $statuses Notification slugs to subscribe to. |
| 36 |
* @param array $subscriber Subscriber data. |
| 37 |
* @param array $unsubscribed Notification slugs to unsubscribe from. |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function upsert_recipient_to_modules( array $statuses, array $subscriber, array $unsubscribed = array() ): void { |
| 41 |
$name = $subscriber['name']; |
| 42 |
$email = $subscriber['email']; |
| 43 |
$in_house = isset( $subscriber['id'] ); |
| 44 |
$user_id = $in_house ? (int) $subscriber['id'] : 0; |
| 45 |
$user = $in_house ? get_userdata( $user_id ) : false; |
| 46 |
$key = $in_house && $user ? $user->user_email : $email; |
| 47 |
$module_map = $this->notification->get_module_map(); |
| 48 |
|
| 49 |
$saved_models = $this->upsert_subscribed_modules( |
| 50 |
$statuses, |
| 51 |
$subscriber, |
| 52 |
$key, |
| 53 |
$name, |
| 54 |
$in_house, |
| 55 |
$user_id, |
| 56 |
$module_map |
| 57 |
); |
| 58 |
$this->unsubscribe_recipient_from_modules( $unsubscribed, $key, $in_house, $module_map ); |
| 59 |
|
| 60 |
if ( array() !== $saved_models ) { |
| 61 |
if ( 1 === count( $saved_models ) ) { |
| 62 |
$this->notification->send_email( $subscriber, $saved_models[0]->export() ); |
| 63 |
} else { |
| 64 |
$this->notification->send_invite_all_email( $subscriber, $saved_models ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add or reactivate a recipient in selected modules. |
| 71 |
* |
| 72 |
* @param array $statuses Notification slugs to subscribe to. |
| 73 |
* @param array $subscriber Subscriber data. |
| 74 |
* @param string $key Recipient lookup key. |
| 75 |
* @param string $name Recipient display name. |
| 76 |
* @param bool $in_house Whether the recipient is a WordPress user. |
| 77 |
* @param int $user_id WordPress user ID for in-house recipients. |
| 78 |
* @param array $module_map Pre-built slug → model index. |
| 79 |
* @return array Saved notification models. |
| 80 |
*/ |
| 81 |
private function upsert_subscribed_modules( |
| 82 |
array $statuses, |
| 83 |
array $subscriber, |
| 84 |
string $key, |
| 85 |
string $name, |
| 86 |
bool $in_house, |
| 87 |
int $user_id, |
| 88 |
array $module_map |
| 89 |
): array { |
| 90 |
$saved_models = array(); |
| 91 |
|
| 92 |
foreach ( $statuses as $slug ) { |
| 93 |
$model = $module_map[ sanitize_key( $slug ) ] ?? null; |
| 94 |
if ( ! $model instanceof Model_Notification ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$changed = $this->upsert_recipient_to_module( |
| 99 |
$model, |
| 100 |
$subscriber, |
| 101 |
$key, |
| 102 |
$name, |
| 103 |
$in_house, |
| 104 |
$user_id |
| 105 |
); |
| 106 |
|
| 107 |
if ( ! $changed ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$model->save(); |
| 112 |
$saved_models[] = $model; |
| 113 |
} |
| 114 |
|
| 115 |
return $saved_models; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add or reactivate a recipient in one module. |
| 120 |
* |
| 121 |
* @param Model_Notification $model Notification model. |
| 122 |
* @param array $subscriber Subscriber data. |
| 123 |
* @param string $key Recipient lookup key. |
| 124 |
* @param string $name Recipient display name. |
| 125 |
* @param bool $in_house Whether the recipient is a WordPress user. |
| 126 |
* @param int $user_id WordPress user ID for in-house recipients. |
| 127 |
* @return bool Whether the recipient data changed. |
| 128 |
*/ |
| 129 |
private function upsert_recipient_to_module( |
| 130 |
Model_Notification $model, |
| 131 |
array $subscriber, |
| 132 |
string $key, |
| 133 |
string $name, |
| 134 |
bool $in_house, |
| 135 |
int $user_id |
| 136 |
): bool { |
| 137 |
if ( $in_house && 0 === $user_id ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
$exists = $in_house |
| 142 |
? isset( $model->in_house_recipients[ $key ] ) |
| 143 |
: isset( $model->out_house_recipients[ $key ] ); |
| 144 |
|
| 145 |
if ( $exists ) { |
| 146 |
$changed = $this->update_existing_recipient( $model, $in_house, $key, $name ); |
| 147 |
if ( $in_house ) { |
| 148 |
$this->maybe_update_wp_user_profile( $user_id, $name, $subscriber ); |
| 149 |
} |
| 150 |
|
| 151 |
return $changed; |
| 152 |
} |
| 153 |
|
| 154 |
if ( $in_house ) { |
| 155 |
$list = $model->in_house_recipients; |
| 156 |
$list[ $key ] = array( |
| 157 |
'id' => $user_id, |
| 158 |
'name' => $name, |
| 159 |
'email' => $key, |
| 160 |
'status' => Model_Notification::USER_SUBSCRIBE_NA, |
| 161 |
); |
| 162 |
$model->in_house_recipients = $list; |
| 163 |
} else { |
| 164 |
$list = $model->out_house_recipients; |
| 165 |
$list[ $key ] = array( |
| 166 |
'name' => $name, |
| 167 |
'email' => $key, |
| 168 |
'status' => Model_Notification::USER_SUBSCRIBE_NA, |
| 169 |
); |
| 170 |
$model->out_house_recipients = $list; |
| 171 |
} |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Update an existing recipient record in one module. |
| 178 |
* |
| 179 |
* @param Model_Notification $model Notification model. |
| 180 |
* @param bool $in_house Whether the recipient is a WordPress user. |
| 181 |
* @param string $key Recipient lookup key. |
| 182 |
* @param string $name Recipient display name. |
| 183 |
* @return bool Whether the recipient data changed. |
| 184 |
*/ |
| 185 |
private function update_existing_recipient( Model_Notification $model, bool $in_house, string $key, string $name ): bool { |
| 186 |
$changed = false; |
| 187 |
$recipients = $in_house ? $model->in_house_recipients : $model->out_house_recipients; |
| 188 |
|
| 189 |
if ( $recipients[ $key ]['name'] !== $name ) { |
| 190 |
$recipients[ $key ]['name'] = $name; |
| 191 |
$changed = true; |
| 192 |
} |
| 193 |
|
| 194 |
if ( Model_Notification::USER_SUBSCRIBE_CANCELED === $recipients[ $key ]['status'] ) { |
| 195 |
$recipients[ $key ]['status'] = Model_Notification::USER_SUBSCRIBE_NA; |
| 196 |
$changed = true; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $changed ) { |
| 200 |
if ( $in_house ) { |
| 201 |
$model->in_house_recipients = $recipients; |
| 202 |
} else { |
| 203 |
$model->out_house_recipients = $recipients; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return $changed; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Mark a recipient as unsubscribed in selected modules. |
| 212 |
* |
| 213 |
* @param array $unsubscribed Notification slugs to unsubscribe from. |
| 214 |
* @param string $key Recipient lookup key. |
| 215 |
* @param bool $in_house Whether the recipient is a WordPress user. |
| 216 |
* @param array $module_map Pre-built slug → model index. |
| 217 |
* @return void |
| 218 |
*/ |
| 219 |
private function unsubscribe_recipient_from_modules( array $unsubscribed, string $key, bool $in_house, array $module_map ): void { |
| 220 |
foreach ( $unsubscribed as $slug ) { |
| 221 |
$model = $module_map[ sanitize_key( $slug ) ] ?? null; |
| 222 |
if ( ! $model instanceof Model_Notification ) { |
| 223 |
continue; |
| 224 |
} |
| 225 |
|
| 226 |
$recipient = $in_house |
| 227 |
? ( $model->in_house_recipients[ $key ] ?? null ) |
| 228 |
: ( $model->out_house_recipients[ $key ] ?? null ); |
| 229 |
|
| 230 |
if ( |
| 231 |
! is_array( $recipient ) |
| 232 |
|| Model_Notification::USER_SUBSCRIBE_CANCELED === $recipient['status'] |
| 233 |
) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 237 |
if ( $in_house ) { |
| 238 |
$model->in_house_recipients[ $key ]['status'] = Model_Notification::USER_SUBSCRIBE_CANCELED; |
| 239 |
} else { |
| 240 |
$model->out_house_recipients[ $key ]['status'] = Model_Notification::USER_SUBSCRIBE_CANCELED; |
| 241 |
} |
| 242 |
$model->save(); |
| 243 |
$this->notification->send_unsubscribe_email( $model, $recipient['email'], $in_house, $recipient['name'] ); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Update a WordPress user profile. |
| 249 |
* |
| 250 |
* @param int $user_id WordPress user ID. |
| 251 |
* @param string $display_name User display name. |
| 252 |
* @param array $subscriber Subscriber data. |
| 253 |
* @return void |
| 254 |
*/ |
| 255 |
private function maybe_update_wp_user_profile( int $user_id, string $display_name, array $subscriber ): void { |
| 256 |
if ( 0 === $user_id || ! current_user_can( 'edit_user', $user_id ) ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
$first_name = $subscriber['first_name'] ?? null; |
| 261 |
$last_name = $subscriber['last_name'] ?? null; |
| 262 |
|
| 263 |
if ( null === $first_name && null === $last_name && '' === $display_name ) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
$args = array( 'ID' => $user_id ); |
| 268 |
if ( '' !== $display_name ) { |
| 269 |
$args['display_name'] = $display_name; |
| 270 |
} |
| 271 |
if ( null !== $first_name ) { |
| 272 |
$args['first_name'] = $first_name; |
| 273 |
} |
| 274 |
if ( null !== $last_name ) { |
| 275 |
$args['last_name'] = $last_name; |
| 276 |
} |
| 277 |
|
| 278 |
wp_update_user( $args ); |
| 279 |
} |
| 280 |
} |
| 281 |
|