| 1 |
<?php |
| 2 |
/** |
| 3 |
* Builds recipient directory data for 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 search and frontend directory data. |
| 15 |
*/ |
| 16 |
class Recipient_Directory 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 |
* Search WordPress users by name or email. |
| 34 |
* |
| 35 |
* @param string $search Search term. |
| 36 |
* @return array User result rows. |
| 37 |
*/ |
| 38 |
public function search_users( string $search ): array { |
| 39 |
if ( strlen( $search ) < 3 ) { |
| 40 |
return array(); |
| 41 |
} |
| 42 |
|
| 43 |
$wp_users = get_users( |
| 44 |
array( |
| 45 |
'search' => "*{$search}*", |
| 46 |
'search_columns' => array( 'user_login', 'user_email', 'display_name' ), |
| 47 |
'number' => 20, |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
$results = array(); |
| 52 |
foreach ( $wp_users as $user ) { |
| 53 |
$results[] = array( |
| 54 |
'id' => (string) $user->ID, |
| 55 |
'name' => $user->display_name, |
| 56 |
'email' => $user->user_email, |
| 57 |
'role' => ! empty( $user->roles ) ? $user->roles[0] : '', |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
return $results; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Delete a recipient from all notification modules. |
| 66 |
* |
| 67 |
* @param string $recipient_id Recipient ID or out-of-house email. |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function delete_recipient( string $recipient_id ): void { |
| 71 |
$is_in_house = is_numeric( $recipient_id ); |
| 72 |
$email = ''; |
| 73 |
if ( $is_in_house ) { |
| 74 |
$user_data = get_userdata( (int) $recipient_id ); |
| 75 |
$email = $user_data ? $user_data->user_email : ''; |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $this->notification->get_modules_as_objects() as $model ) { |
| 79 |
$changed = false; |
| 80 |
|
| 81 |
if ( $is_in_house ) { |
| 82 |
if ( '' !== $email && isset( $model->in_house_recipients[ $email ] ) ) { |
| 83 |
unset( $model->in_house_recipients[ $email ] ); |
| 84 |
$changed = true; |
| 85 |
} |
| 86 |
} elseif ( isset( $model->out_house_recipients[ $recipient_id ] ) ) { |
| 87 |
unset( $model->out_house_recipients[ $recipient_id ] ); |
| 88 |
$changed = true; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $changed ) { |
| 92 |
$model->save(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get recipients across all modules. |
| 99 |
* |
| 100 |
* @return array Recipient data. |
| 101 |
*/ |
| 102 |
public function get_all_recipients(): array { |
| 103 |
$seen = array(); |
| 104 |
$result = array(); |
| 105 |
$modules = $this->notification->get_modules_as_objects(); |
| 106 |
$avatar_map = $this->get_in_house_avatar_map( $modules ); |
| 107 |
|
| 108 |
foreach ( $modules as $model ) { |
| 109 |
$this->collect_in_house_recipients( $result, $seen, $model, $avatar_map ); |
| 110 |
$this->collect_out_house_recipients( $result, $seen, $model ); |
| 111 |
} |
| 112 |
|
| 113 |
return array_values( $result ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Collect in-house recipients for one module. |
| 118 |
* |
| 119 |
* @param array $result Accumulated recipients list. |
| 120 |
* @param array $seen Deduplication index. |
| 121 |
* @param Model_Notification $model Notification model. |
| 122 |
* @param array $avatar_map Avatar URLs keyed by email. |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
private function collect_in_house_recipients( array &$result, array &$seen, Model_Notification $model, array $avatar_map ): void { |
| 126 |
$is_module_active = Model_Notification::STATUS_ACTIVE === $model->status; |
| 127 |
foreach ( $model->in_house_recipients as $recipient ) { |
| 128 |
$key = 'u_' . ( $recipient['id'] ?? $recipient['email'] ); |
| 129 |
$base = array( |
| 130 |
'id' => (string) ( $recipient['id'] ?? $recipient['email'] ), |
| 131 |
'avatar' => $avatar_map[ $recipient['email'] ] ?? '', |
| 132 |
'role' => $recipient['role'] ?? '', |
| 133 |
'_inHouse' => true, |
| 134 |
); |
| 135 |
$this->collect_recipient( $result, $seen, $key, $recipient, $model->slug, $base, $is_module_active ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Collect out-of-house recipients for one module. |
| 141 |
* |
| 142 |
* @param array $result Accumulated recipients list. |
| 143 |
* @param array $seen Deduplication index. |
| 144 |
* @param Model_Notification $model Notification model. |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
private function collect_out_house_recipients( array &$result, array &$seen, Model_Notification $model ): void { |
| 148 |
$is_module_active = Model_Notification::STATUS_ACTIVE === $model->status; |
| 149 |
foreach ( $model->out_house_recipients as $recipient ) { |
| 150 |
$key = 'o_' . $recipient['email']; |
| 151 |
$base = array( |
| 152 |
'id' => $recipient['email'], |
| 153 |
'avatar' => '', |
| 154 |
'role' => '', |
| 155 |
'_inHouse' => false, |
| 156 |
); |
| 157 |
$this->collect_recipient( $result, $seen, $key, $recipient, $model->slug, $base, $is_module_active ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get avatar URLs for all in-house recipient emails. |
| 163 |
* |
| 164 |
* @param array $modules Notification modules. |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
private function get_in_house_avatar_map( array $modules ): array { |
| 168 |
$in_house_emails = array(); |
| 169 |
foreach ( $modules as $model ) { |
| 170 |
foreach ( $model->in_house_recipients as $recipient ) { |
| 171 |
if ( ! empty( $recipient['email'] ) ) { |
| 172 |
$in_house_emails[ $recipient['email'] ] = true; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$avatar_map = array(); |
| 178 |
foreach ( array_keys( $in_house_emails ) as $email ) { |
| 179 |
$avatar_map[ $email ] = get_avatar_url( $email ); |
| 180 |
} |
| 181 |
|
| 182 |
return $avatar_map; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Add a recipient entry to the result or append its module slug if already seen. |
| 187 |
* |
| 188 |
* @param array $result Accumulated recipients list. |
| 189 |
* @param array $seen Deduplication index. |
| 190 |
* @param string $key Unique recipient key. |
| 191 |
* @param array $recipient Recipient data from the model. |
| 192 |
* @param string $slug Notification module slug. |
| 193 |
* @param array $base Type-specific fields. |
| 194 |
* @param bool $is_module_active Whether the notification module is enabled. |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
private function collect_recipient( array &$result, array &$seen, string $key, array $recipient, string $slug, array $base, bool $is_module_active = true ): void { |
| 198 |
$status = $recipient['status'] ?? ''; |
| 199 |
$canceled = Model_Notification::USER_SUBSCRIBE_CANCELED === $status; |
| 200 |
$add_slug = $is_module_active && ! $canceled; |
| 201 |
|
| 202 |
if ( isset( $seen[ $key ] ) ) { |
| 203 |
if ( $add_slug ) { |
| 204 |
$result[ $seen[ $key ] ]['statuses'][] = $slug; |
| 205 |
} |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$seen[ $key ] = count( $result ); |
| 210 |
$result[] = $base + array( |
| 211 |
'name' => $recipient['name'], |
| 212 |
'email' => $recipient['email'], |
| 213 |
'status' => $status, |
| 214 |
'statuses' => $add_slug ? array( $slug ) : array(), |
| 215 |
); |
| 216 |
} |
| 217 |
} |
| 218 |
|