-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscribers.php
More file actions
471 lines (439 loc) · 18.7 KB
/
Copy pathsubscribers.php
File metadata and controls
471 lines (439 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/**
* Subscribers page
*
* @package MSKD
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $wpdb;
// Load the List Provider service.
require_once MSKD_PLUGIN_DIR . 'includes/services/class-mskd-list-provider.php';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce not needed for reading view state.
$current_action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : 'list';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce not needed for reading view state.
$subscriber_id = isset( $_GET['id'] ) ? sanitize_text_field( wp_unslash( $_GET['id'] ) ) : '';
// Get all lists for dropdown (database + external).
$lists = MSKD_List_Provider::get_all_lists();
// Get subscriber for editing (only database subscribers are editable).
$subscriber = null;
$subscriber_lists = array();
if ( 'edit' === $current_action && $subscriber_id ) {
// Check if this is an external subscriber (not editable).
if ( MSKD_List_Provider::is_external_id( $subscriber_id ) ) {
wp_redirect( admin_url( 'admin.php?page=mskd-subscribers' ) );
exit;
}
$subscriber = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}mskd_subscribers WHERE id = %d",
intval( $subscriber_id )
)
);
$subscriber_lists = $wpdb->get_col(
$wpdb->prepare(
"SELECT list_id FROM {$wpdb->prefix}mskd_subscriber_list WHERE subscriber_id = %d",
intval( $subscriber_id )
)
);
}
?>
<div class="wrap mskd-wrap">
<h1>
<?php esc_html_e( 'Subscribers', 'mail-system' ); ?>
<?php if ( 'list' === $current_action ) : ?>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers&action=add' ) ); ?>" class="page-title-action">
<?php esc_html_e( 'Add new', 'mail-system' ); ?>
</a>
<?php endif; ?>
</h1>
<?php settings_errors( 'mskd_messages' ); ?>
<?php if ( 'add' === $current_action || 'edit' === $current_action ) : ?>
<!-- Add/Edit Form -->
<div class="mskd-form-wrap">
<h2><?php echo esc_html( 'add' === $current_action ? esc_html__( 'Add subscriber', 'mail-system' ) : esc_html__( 'Edit subscriber', 'mail-system' ) ); ?></h2>
<form method="post" action="">
<?php wp_nonce_field( 'add' === $current_action ? 'mskd_add_subscriber' : 'mskd_edit_subscriber', 'mskd_nonce' ); ?>
<?php if ( 'edit' === $current_action ) : ?>
<input type="hidden" name="subscriber_id" value="<?php echo esc_attr( $subscriber_id ); ?>">
<?php endif; ?>
<table class="form-table">
<tr>
<th scope="row">
<label for="email"><?php esc_html_e( 'Email', 'mail-system' ); ?> *</label>
</th>
<td>
<input type="email" name="email" id="email" class="regular-text" required
value="<?php echo $subscriber ? esc_attr( $subscriber->email ) : ''; ?>">
</td>
</tr>
<tr>
<th scope="row">
<label for="first_name"><?php esc_html_e( 'First name', 'mail-system' ); ?></label>
</th>
<td>
<input type="text" name="first_name" id="first_name" class="regular-text"
value="<?php echo $subscriber ? esc_attr( $subscriber->first_name ) : ''; ?>">
</td>
</tr>
<tr>
<th scope="row">
<label for="last_name"><?php esc_html_e( 'Last name', 'mail-system' ); ?></label>
</th>
<td>
<input type="text" name="last_name" id="last_name" class="regular-text"
value="<?php echo $subscriber ? esc_attr( $subscriber->last_name ) : ''; ?>">
</td>
</tr>
<tr>
<th scope="row">
<label for="status"><?php esc_html_e( 'Status', 'mail-system' ); ?></label>
</th>
<td>
<select name="status" id="status">
<option value="active" <?php selected( $subscriber ? $subscriber->status : 'active', 'active' ); ?>>
<?php echo esc_html( _x( 'Active', 'subscriber status', 'mail-system' ) ); ?>
</option>
<option value="inactive" <?php selected( $subscriber ? $subscriber->status : '', 'inactive' ); ?>>
<?php echo esc_html( _x( 'Inactive', 'subscriber status', 'mail-system' ) ); ?>
</option>
<option value="unsubscribed" <?php selected( $subscriber ? $subscriber->status : '', 'unsubscribed' ); ?>>
<?php echo esc_html( _x( 'Unsubscribed', 'subscriber status', 'mail-system' ) ); ?>
</option>
</select>
</td>
</tr>
<tr>
<th scope="row">
<label><?php esc_html_e( 'Lists', 'mail-system' ); ?></label>
</th>
<td>
<?php
// Only show database lists for subscriber assignment (external lists manage their own subscribers).
$database_lists = array_filter(
$lists,
function ( $list_item ) {
return 'database' === $list_item->source;
}
);
?>
<?php if ( ! empty( $database_lists ) ) : ?>
<?php foreach ( $database_lists as $list ) : ?>
<label style="display: block; margin-bottom: 5px;">
<input type="checkbox" name="lists[]" value="<?php echo esc_attr( $list->id ); ?>"
<?php checked( in_array( (string) $list->id, array_map( 'strval', $subscriber_lists ), true ) ); ?>>
<?php echo esc_html( $list->name ); ?>
</label>
<?php endforeach; ?>
<?php else : ?>
<p class="description"><?php esc_html_e( 'No lists created.', 'mail-system' ); ?></p>
<?php endif; ?>
<?php
// Show external lists as info (not selectable).
$external_lists = array_filter(
$lists,
function ( $list_item ) {
return 'external' === $list_item->source;
}
);
if ( ! empty( $external_lists ) ) :
?>
<p class="description" style="margin-top: 10px;">
<?php esc_html_e( 'Automated lists (membership managed by external plugins):', 'mail-system' ); ?>
<?php
$external_names = array_map(
function ( $list ) {
return esc_html( $list->name ) . ' (' . esc_html( $list->provider ) . ')';
},
$external_lists
);
echo esc_html( implode( ', ', $external_names ) );
?>
</p>
<?php endif; ?>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="<?php echo 'add' === $current_action ? 'mskd_add_subscriber' : 'mskd_edit_subscriber'; ?>"
class="button button-primary"
value="<?php echo 'add' === $current_action ? esc_attr__( 'Add subscriber', 'mail-system' ) : esc_attr__( 'Save changes', 'mail-system' ); ?>">
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers' ) ); ?>" class="button">
<?php esc_html_e( 'Cancel', 'mail-system' ); ?>
</a>
</p>
</form>
</div>
<?php else : ?>
<!-- Subscribers List -->
<?php
// Pagination.
$items_per_page = 20;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce not needed for reading pagination.
$current_page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
// Filter by status.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce not needed for reading status filter.
$status_filter = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : '';
// Get total counts for proper pagination.
$db_count = MSKD_List_Provider::get_database_subscriber_count( $status_filter );
$external_subs = MSKD_List_Provider::get_external_subscribers( array( 'status' => $status_filter ) );
$ext_count = count( $external_subs );
$total_items = $db_count + $ext_count;
$total_pages = ceil( $total_items / $items_per_page );
$has_external = $ext_count > 0;
// Calculate pagination across database + external subscribers.
$offset = ( $current_page - 1 ) * $items_per_page;
$all_subscribers = array();
if ( $offset < $db_count ) {
// We need some database subscribers for this page.
$db_subscribers = MSKD_List_Provider::get_database_subscribers(
array(
'status' => $status_filter,
'per_page' => $items_per_page,
'page' => $current_page,
)
);
$all_subscribers = $db_subscribers;
$remaining = $items_per_page - count( $db_subscribers );
// If we have room left on this page, add external subscribers.
if ( $remaining > 0 && $ext_count > 0 ) {
$all_subscribers = array_merge( $all_subscribers, array_slice( $external_subs, 0, $remaining ) );
}
} else {
// We've passed all database subscribers, show external only.
$ext_offset = $offset - $db_count;
if ( $ext_offset < $ext_count ) {
$all_subscribers = array_slice( $external_subs, $ext_offset, $items_per_page );
}
}
// Get database lists for batch action dropdown.
// Get list assignments for all subscribers on this page.
$subscriber_service = new \MSKD\Services\Subscriber_Service();
$subscriber_ids = array_filter(
array_map(
function ( $s ) {
$is_editable = isset( $s->is_editable ) ? $s->is_editable : true;
return isset( $s->id ) && $is_editable ? (int) $s->id : null;
},
$all_subscribers
)
);
$subscriber_lists_map = ! empty( $subscriber_ids ) ? $subscriber_service->batch_get_lists( $subscriber_ids ) : array();
$database_lists = array_filter(
$lists,
function ( $list_item ) {
return 'database' === $list_item->source;
}
);
// Editable (database) subscribers on this page can be selected for bulk actions,
// independent of whether any lists exist.
$has_editable_subscribers = ! empty( $subscriber_ids );
// Calculate stats for the statistics box.
$total_subs_count = MSKD_List_Provider::get_total_subscriber_count();
$active_subs_count = MSKD_List_Provider::get_total_subscriber_count( 'active' );
$inactive_subs_count = MSKD_List_Provider::get_total_subscriber_count( 'inactive' );
$unsub_subs_count = MSKD_List_Provider::get_total_subscriber_count( 'unsubscribed' );
?>
<!-- Statistics Box -->
<div class="mskd-subscribers-stats">
<div class="mskd-stat-card">
<span class="mskd-stat-label"><?php esc_html_e( 'Total Subscribers', 'mail-system' ); ?></span>
<span class="mskd-stat-value"><?php echo esc_html( $total_subs_count ); ?></span>
</div>
<div class="mskd-stat-card">
<span class="mskd-stat-label"><?php esc_html_e( 'Active', 'mail-system' ); ?></span>
<span class="mskd-stat-value mskd-text-active"><?php echo esc_html( $active_subs_count ); ?></span>
</div>
<div class="mskd-stat-card">
<span class="mskd-stat-label"><?php esc_html_e( 'Inactive', 'mail-system' ); ?></span>
<span class="mskd-stat-value mskd-text-inactive"><?php echo esc_html( $inactive_subs_count ); ?></span>
</div>
<div class="mskd-stat-card">
<span class="mskd-stat-label"><?php esc_html_e( 'Unsubscribed', 'mail-system' ); ?></span>
<span class="mskd-stat-value mskd-text-unsubscribed"><?php echo esc_html( $unsub_subs_count ); ?></span>
</div>
</div>
<!-- Filters -->
<ul class="subsubsub">
<li>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers' ) ); ?>"
class="<?php echo empty( $status_filter ) ? 'current' : ''; ?>">
<?php esc_html_e( 'All', 'mail-system' ); ?>
</a> |
</li>
<li>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers&status=active' ) ); ?>"
class="<?php echo 'active' === $status_filter ? 'current' : ''; ?>">
<?php esc_html_e( 'Active', 'mail-system' ); ?>
</a> |
</li>
<li>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers&status=inactive' ) ); ?>"
class="<?php echo 'inactive' === $status_filter ? 'current' : ''; ?>">
<?php esc_html_e( 'Inactive', 'mail-system' ); ?>
</a> |
</li>
<li>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers&status=unsubscribed' ) ); ?>"
class="<?php echo 'unsubscribed' === $status_filter ? 'current' : ''; ?>">
<?php esc_html_e( 'Unsubscribed', 'mail-system' ); ?>
</a>
</li>
</ul>
<!-- Bulk Actions -->
<?php if ( $has_editable_subscribers ) : ?>
<div class="tablenav top mskd-bulk-actions-bar">
<div class="alignleft actions bulkactions mskd-bulk-actions-row">
<select name="mskd_bulk_action" id="mskd-bulk-action" class="mskd-bulk-action-select">
<option value=""><?php esc_html_e( 'Bulk actions', 'mail-system' ); ?></option>
<?php if ( ! empty( $database_lists ) ) : ?>
<option value="assign_lists"><?php esc_html_e( 'Add to lists', 'mail-system' ); ?></option>
<option value="remove_lists"><?php esc_html_e( 'Remove from lists', 'mail-system' ); ?></option>
<?php endif; ?>
<option value="set_active"><?php esc_html_e( 'Set active', 'mail-system' ); ?></option>
<option value="set_inactive"><?php esc_html_e( 'Set inactive', 'mail-system' ); ?></option>
<option value="delete"><?php esc_html_e( 'Delete', 'mail-system' ); ?></option>
</select>
<div id="mskd-bulk-list-wrapper" class="mskd-bulk-list-wrapper" style="display: none;">
<select name="mskd_bulk_list_ids[]" id="mskd-bulk-list-ids" class="mskd-slimselect-bulk-lists" multiple="multiple">
<?php foreach ( $database_lists as $list ) : ?>
<option value="<?php echo esc_attr( $list->id ); ?>">
<?php echo esc_html( $list->name ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="button" id="mskd-bulk-apply" class="button action" style="display: none;">
<?php esc_html_e( 'Apply', 'mail-system' ); ?>
</button>
<span id="mskd-bulk-result" class="mskd-bulk-result"></span>
</div>
<div class="alignleft mskd-selected-count" style="margin-left: 10px; line-height: 30px;">
<span id="mskd-selected-count">0</span> <?php esc_html_e( 'selected', 'mail-system' ); ?>
</div>
</div>
<?php endif; ?>
<table class="wp-list-table widefat fixed striped mskd-subscribers-table">
<thead>
<tr>
<?php if ( $has_editable_subscribers ) : ?>
<td id="cb" class="manage-column column-cb check-column">
<input type="checkbox" id="mskd-select-all" />
</td>
<?php endif; ?>
<th scope="col"><?php esc_html_e( 'Email', 'mail-system' ); ?></th>
<th scope="col"><?php esc_html_e( 'Status', 'mail-system' ); ?></th>
<th scope="col"><?php esc_html_e( 'Date', 'mail-system' ); ?></th>
<th scope="col"><?php esc_html_e( 'Lists', 'mail-system' ); ?></th>
<th scope="col"><?php esc_html_e( 'Actions', 'mail-system' ); ?></th>
</tr>
</thead>
<tbody>
<?php if ( ! empty( $all_subscribers ) ) : ?>
<?php foreach ( $all_subscribers as $sub ) : ?>
<?php
$is_external = isset( $sub->source ) && 'external' === $sub->source;
$is_editable = isset( $sub->is_editable ) ? $sub->is_editable : true;
?>
<tr<?php echo $is_external ? ' class="mskd-external-list"' : ''; ?> data-subscriber-id="<?php echo esc_attr( $sub->id ); ?>">
<?php if ( $has_editable_subscribers ) : ?>
<th scope="row" class="check-column">
<?php if ( $is_editable && ! $is_external ) : ?>
<input type="checkbox" name="mskd_subscriber_ids[]" class="mskd-subscriber-checkbox" value="<?php echo esc_attr( $sub->id ); ?>" />
<?php endif; ?>
</th>
<?php endif; ?>
<td>
<strong><?php echo esc_html( $sub->email ); ?></strong>
<?php if ( $is_external ) : ?>
<span class="mskd-badge mskd-badge-external" title="<?php esc_attr_e( 'External subscriber from plugin', 'mail-system' ); ?>">
<?php esc_html_e( 'External', 'mail-system' ); ?>
</span>
<?php endif; ?>
</td>
<td class="mskd-status-column">
<span class="mskd-status mskd-status-<?php echo esc_attr( $sub->status ); ?>">
<?php
$statuses = array(
'active' => _x( 'Active', 'subscriber status', 'mail-system' ),
'inactive' => _x( 'Inactive', 'subscriber status', 'mail-system' ),
'unsubscribed' => _x( 'Unsubscribed', 'subscriber status', 'mail-system' ),
);
echo esc_html( $statuses[ $sub->status ] ?? $sub->status );
?>
</span>
</td>
<td>
<?php if ( isset( $sub->created_at ) && $sub->created_at ) : ?>
<?php echo esc_html( date_i18n( 'd.m.Y', strtotime( $sub->created_at ) ) ); ?>
<?php else : ?>
<span class="mskd-readonly-text">—</span>
<?php endif; ?>
</td>
<td class="mskd-lists-column">
<?php
$lists_for_sub = $subscriber_lists_map[ $sub->id ] ?? array();
if ( ! empty( $lists_for_sub ) ) :
foreach ( $lists_for_sub as $list_item ) :
?>
<div class="mskd-list-badge"><?php echo esc_html( $list_item->name ); ?></div>
<?php
endforeach;
else :
?>
<span class="mskd-text-muted">—</span>
<?php endif; ?>
</td>
<td>
<?php if ( $is_editable ) : ?>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mskd-subscribers&action=edit&id=' . $sub->id ) ); ?>">
<?php esc_html_e( 'Edit', 'mail-system' ); ?>
</a> |
<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=mskd-subscribers&action=delete_subscriber&id=' . $sub->id ), 'delete_subscriber_' . $sub->id ) ); ?>"
class="mskd-delete-link" style="color: #a00;">
<?php esc_html_e( 'Delete', 'mail-system' ); ?>
</a>
<?php else : ?>
<span class="mskd-readonly-text" title="<?php esc_attr_e( 'External subscribers cannot be edited', 'mail-system' ); ?>">
<?php esc_html_e( 'Read-only', 'mail-system' ); ?>
</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="<?php echo $has_editable_subscribers ? '6' : '5'; ?>"><?php esc_html_e( 'No subscribers found.', 'mail-system' ); ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>
<!-- Pagination -->
<?php if ( $total_pages > 1 ) : ?>
<div class="tablenav bottom">
<div class="tablenav-pages">
<?php
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- paginate_links returns safe HTML.
echo paginate_links(
array(
'base' => add_query_arg( 'paged', '%#%' ),
'format' => '',
'prev_text' => esc_html__( '«', 'mail-system' ),
'next_text' => esc_html__( '»', 'mail-system' ),
'total' => $total_pages,
'current' => $current_page,
)
);
?>
</div>
</div>
<?php endif; ?>
<?php if ( $has_external ) : ?>
<p class="description" style="margin-top: 15px;">
<span class="dashicons dashicons-info" style="color: #0073aa;"></span>
<?php esc_html_e( 'External subscribers are managed by third-party plugins and appear as read-only.', 'mail-system' ); ?>
</p>
<?php endif; ?>
<?php endif; ?>
</div>