-
Notifications
You must be signed in to change notification settings - Fork 29
/
subscriber-table.php
564 lines (463 loc) · 18.8 KB
/
subscriber-table.php
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
/*************************** LOAD THE BASE CLASS *******************************
* ******************************************************************************
* The WP_List_Table class isn't automatically available to plugins, so we need
* to check if it's available and load it if necessary.
*
* @package Leaky Paywall
*/
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* This class builds the subscriber table
*
* @since 1.0.0
*/
class Leaky_Paywall_Subscriber_List_Table extends WP_List_Table {
/**
* Ajax user can
*/
public function ajax_user_can() {
return current_user_can( 'manage_network_users' );
}
/**
* Prepare items for table
*/
public function prepare_items() {
global $wpdb;
/**
* REQUIRED. Now we need to define our column headers. This includes a complete
* array of columns to be displayed (slugs & titles), a list of columns
* to keep hidden, and a list of columns that are sortable. Each of these
* can be defined in another method (as we've done here) before being
* used to build the value for our _column_headers property.
*/
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
/**
* REQUIRED. Finally, we build an array to be used by the class for column
* headers. The $this->_column_headers property takes an array which contains
* 3 other arrays. One for all columns, one for hidden columns, and one
* for sortable columns.
*/
$this->_column_headers = array( $columns, $hidden, $sortable );
$usersearch = isset( $_REQUEST['s'] ) ? '*' . sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) . '*' : '';
$per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
$users_per_page = $this->get_items_per_page( $per_page );
$paged = $this->get_pagenum();
$args = array(
'number' => $users_per_page,
'offset' => ( $paged - 1 ) * $users_per_page,
'search' => $usersearch,
);
// If a search is not being performed, show only the latest users with no paging in order.
// to avoid expensive count queries.
if ( ! $usersearch ) {
if ( ! isset( $_REQUEST['orderby'] ) ) {
$_GET['orderby'] = 'ID';
}
if ( ! isset( $_REQUEST['order'] ) ) {
$_GET['order'] = 'DESC';
}
}
if ( $this->is_site_users ) {
$args['blog_id'] = $this->site_id;
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
$args['orderby'] = sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) );
} else {
$args['orderby'] = 'ID';
}
if ( ! empty( $_REQUEST['order'] ) ) {
$args['order'] = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) );
} else {
$args['order'] = 'DESC';
}
$settings = get_leaky_paywall_settings();
$mode = leaky_paywall_get_current_mode();
$site = leaky_paywall_get_current_site();
$args['meta_query'] = array(
array(
'key' => '_issuem_leaky_paywall_' . $mode . '_level_id' . $site,
'compare' => 'EXISTS',
),
);
if ( isset( $_GET['custom_field_search'] ) && 'on' === $_GET['custom_field_search'] ) {
$args['meta_query']['relation'] = 'AND';
$args['meta_query'][] = array(
'key' => '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site,
'value' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '',
'compare' => 'LIKE',
);
unset( $args['search'] );
}
if ( isset( $_GET['filter-level'] ) && isset( $_GET['user-type'] ) && 'lpsubs' === $_GET['user-type'] ) {
$level = sanitize_text_field( wp_unslash( $_GET['filter-level'] ) );
if ( 'all' !== $level ) {
$args['meta_query'][] = array(
'key' => '_issuem_leaky_paywall_' . $mode . '_level_id' . $site,
'value' => $level,
'compare' => 'LIKE',
);
}
}
if ( isset( $_GET['filter-status'] ) && isset( $_GET['user-type'] ) && 'lpsubs' === $_GET['user-type'] ) {
$status = sanitize_text_field( wp_unslash( $_GET['filter-status'] ) );
if ( 'all' !== $status ) {
$args['meta_query'][] = array(
'key' => '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site,
'value' => $status,
'compare' => 'LIKE',
);
}
}
if ( ! empty( $_GET['user-type'] ) && 'lpsubs' !== $_GET['user-type'] ) {
unset( $args['meta_query'] );
}
$wp_user_search = new WP_User_Query( $args );
$this->items = $wp_user_search->get_results();
$this->set_pagination_args(
array(
'total_items' => $wp_user_search->get_total(),
'per_page' => $users_per_page,
)
);
}
/**
* No items text
*/
public function no_items() {
esc_attr_e( 'No Leaky Paywall subscribers found.' );
}
/**
* Get columns
*/
public function get_columns() {
$users_columns = array(
'wp_user_login' => __( 'WordPress Username', 'leaky-paywall' ),
'email' => __( 'E-mail', 'leaky-paywall' ),
'name' => __( 'Name', 'leaky-paywall' ),
'level_id' => __( 'Level ID', 'leaky-paywall' ),
'susbcriber_id' => __( 'Subscriber ID', 'leaky-paywall' ),
'plan' => __( 'Plan', 'leaky-paywall' ),
'created' => __( 'Created', 'leaky-paywall' ),
'expires' => __( 'Expires', 'leaky-paywall' ),
'has_access' => __( 'Has Access', 'leaky-paywall' ),
'gateway' => __( 'Gateway', 'leaky-paywall' ),
'status' => __( 'Payment Status', 'leaky-paywall' ),
'notes' => __( 'Notes', 'leaky-paywall' ),
);
$users_columns = apply_filters( 'leaky_paywall_subscribers_columns', $users_columns );
return $users_columns;
}
/**
* Get sortable columns
*/
public function get_sortable_columns() {
$sortable_columns = array(
'wp_user_login' => array( 'wp_user_login', false ),
'email' => array( 'email', false ),
'level_id' => array( 'level_id', false ),
'susbcriber_id' => array( 'susbcriber_id', false ),
'plan' => array( 'plan', false ),
'gateway' => array( 'payment_gateway', false ),
'status' => array( 'payment_status', false ),
);
$sortable_columns = apply_filters( 'leaky_paywall_subscribers_sortable_columns', $sortable_columns );
return $sortable_columns;
}
/**
* Select for user type
*/
public function user_views() {
$user_type = ! empty( $_GET['user-type'] ) ? sanitize_text_field( wp_unslash( $_GET['user-type'] ) ) : 'lpsubs';
echo '<div class="alignleft actions">';
echo '<label for="user-type-selector" class="screen-reader-text">' . esc_html__( 'Select User Type' ) . '</label>';
echo '<select name="user-type" id="user-type-selector">';
echo '<option value="lpsubs" ' . selected( 'lpsubs', $user_type, false ) . '>' . esc_html__( 'Leaky Paywall Subscribers' ) . '</option>';
echo '<option value="wpusers" ' . selected( 'wpusers', $user_type, false ) . '>' . esc_html__( 'All WordPress Users' ) . '</option>';
echo '</select>';
submit_button( __( 'Apply' ), 'primary', false, false );
echo '</div>';
}
/**
* Display subscriber table navigation
*
* @param string $which The location of the nav.
*/
public function extra_tablenav( $which ) {
if ( 'top' !== $which ) {
return;
}
if ( isset( $_GET['user-type'] ) && 'lpsubs' !== $_GET['user-type'] ) {
return;
}
$levels = leaky_paywall_get_levels();
$lev = isset( $_GET['filter-level'] ) ? sanitize_text_field( wp_unslash( $_GET['filter-level'] ) ) : 'all';
$stat = isset( $_GET['filter-status'] ) ? sanitize_text_field( wp_unslash( $_GET['filter-status'] ) ) : 'all';
?>
<div class="alignleft actions">
<label for="filter-by-level" class="screen-reader-text">Filter by level</label>
<select name="filter-level" id="filter-by-level">
<option value="all" <?php selected( $lev, 'all' ); ?>>All Levels</option>
<?php
foreach ( $levels as $key => $level ) {
echo '<option ' . selected( $key, $lev, false ) . ' value="' . esc_attr( $key ) . '">' . esc_html(stripslashes( $level['label'] ) ) . '</option>';
}
?>
</select>
<?php
$status_filter_args = apply_filters(
'leaky_paywall_status_filter_args',
array(
'all' => 'All Statuses',
'active' => 'Active',
'deactivated' => 'Deactivated',
'canceled' => 'Canceled',
)
);
?>
<label for="filter-by-status" class="screen-reader-text">Filter by status</label>
<select name="filter-status" id="filter-by-status">
<?php
foreach ( $status_filter_args as $key => $value ) {
?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $stat, $key ); ?>><?php echo esc_html( $value ); ?></option>
<?php
}
?>
</select>
<input name="filter_action" id="subscriber-query-submit" class="button" value="Filter" type="submit">
</div>
<?php
}
/**
* Display rows in subscriber table
*/
public function display_rows() {
$settings = get_leaky_paywall_settings();
$mode = leaky_paywall_get_current_mode();
$site = leaky_paywall_get_current_site();
$alt = '';
foreach ( $this->items as $user ) {
if ( ! $user->user_email ) {
continue;
}
$user = get_user_by( 'email', $user->user_email );
$alt = ( 'alternate' === $alt ) ? '' : 'alternate';
$payment_gateway = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_gateway' . $site, true );
if ( ! empty( $_GET['user-type'] ) && 'wpusers' !== $_GET['user-type'] ) {
if ( empty( $payment_gateway ) ) {
$payment_gateway = 'manual';
}
}
if ( is_multisite_premium() ) {
$level_id = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_level_id' . $site, true );
} else {
$level_id = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_level_id', true );
}
?>
<tr class="<?php echo esc_attr( $alt ); ?>">
<?php
list($columns, $hidden) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "$column_name column-$column_name";
$style = '';
if ( in_array( $column_name, $hidden, true ) ) {
$style = 'display:none;';
}
switch ( $column_name ) {
case 'wp_user_login':
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
?>
<strong><?php echo esc_html( $user->user_login ); ?></strong>
<?php
// if the user switching plugin is activated, add switch to link to LP subscriber table for easier testing.
if ( method_exists( 'user_switching', 'maybe_switch_url' ) ) {
if ( is_object( $user ) ) {
$link = user_switching::maybe_switch_url( $user );
if ( $link ) {
echo '<br><a href="' . esc_url( $link ) . '">' . esc_html__( 'Switch To', 'leaky-paywall' ) . '</a>';
}
}
}
?>
</td>
<?php
break;
case 'email':
$edit_link = esc_url( add_query_arg( 'edit', rawurlencode( $user->user_email ) ) );
$edit_wp_link = admin_url() . 'user-edit.php?user_id=' . $user->ID;
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
?>
<strong><?php echo esc_html( $user->user_email ); ?></strong>
<br><a href="<?php echo esc_url( $edit_link ); ?>" class="edit">Edit LP Sub</a> | <a href="<?php echo esc_url( $edit_wp_link ); ?>">Edit WP user</a>
</td>
<?php
break;
case 'name':
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
?>
<?php echo esc_attr( $user->first_name ); ?> <?php echo esc_attr( $user->last_name ); ?>
</td>
<?php
break;
case 'level_id':
$level_id = apply_filters( 'get_leaky_paywall_users_level_id', $level_id, $user, $mode, $site );
$level_id = apply_filters( 'get_leaky_paywall_subscription_level_level_id', $level_id );
if ( false === $level_id || empty( $settings['levels'][ $level_id ]['label'] ) ) {
$level_name = __( 'Undefined', 'leaky-paywall' );
} else {
$level_name = stripcslashes( $settings['levels'][ $level_id ]['label'] );
}
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( $level_name );
echo '</td>';
break;
case 'susbcriber_id':
$subscriber_id = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true);
$stripe_url = $mode == 'test' ? 'https://dashboard.stripe.com/test/customers/' : 'https://dashboard.stripe.com/customers/';
if ( strpos( $subscriber_id, 'cus_' ) !== false ) {
$output = '<a target="_blank" href="' . $stripe_url . $subscriber_id . '">' . $subscriber_id . '</a>';
} else {
$output = $subscriber_id;
}
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo wp_kses_post( $output );
echo '</td>';
break;
case 'plan':
if ( is_multisite_premium() ) {
$plan = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_plan' . $site, true );
} else {
$plan = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_plan', true );
}
if ( empty( $plan ) ) {
$plan = __( 'Non-Recurring', 'leaky-paywall' );
} elseif ( 'paypal_standard' === $payment_gateway || 'paypal-standard' === $payment_gateway ) {
/* Translators: %s: type of time */
$plan = sprintf( __( 'Recurring every %s', 'leaky-paywall' ), str_replace( array( 'D', 'W', 'M', 'Y' ), array( 'Days', 'Weeks', 'Months', 'Years' ), $plan ) );
}
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( $plan );
echo '</td>';
break;
case 'created':
if ( is_multisite_premium() ) {
$created = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_created' . $site, true );
} else {
$created = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_created', true );
}
$created = apply_filters( 'do_leaky_paywall_profile_shortcode_created_column', $created, $user, $mode, $site, $level_id );
$date_format = 'F j, Y';
if ( is_numeric( $created ) && (int)$created == $created ) {
// its a timestamp
$formatted_created = gmdate( $date_format, $created );
} else {
// its a date format
$formatted_created = mysql2date( $date_format, $created );
}
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( $formatted_created );
echo '</td>';
break;
case 'expires':
if ( is_multisite_premium() ) {
$expires = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_expires' . $site, true );
} else {
$expires = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_expires', true );
}
$expires = apply_filters( 'do_leaky_paywall_profile_shortcode_expiration_column', $expires, $user, $mode, $site, $level_id );
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires || 'Never' === $expires ) {
$expires = __( 'Never', 'leaky-paywall' );
} else {
$date_format = 'F j, Y';
$expires = mysql2date( $date_format, $expires );
}
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( $expires );
echo '</td>';
break;
case 'has_access':
$has_access = leaky_paywall_user_has_access( $user );
if ( $has_access ) {
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr__( 'Yes', 'leaky-paywall' );
echo '</td>';
} else {
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr__( 'No', 'leaky-paywall' );
echo '</td>';
}
break;
case 'gateway':
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( leaky_paywall_translate_payment_gateway_slug_to_name( $payment_gateway ) );
echo '</td>';
break;
case 'status':
if ( is_multisite_premium() ) {
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, true ) );
echo '</td>';
} else {
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status', true ) );
echo '</td>';
}
break;
case 'notes':
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( get_user_meta( $user->ID, '_leaky_paywall_subscriber_notes', true ) );
echo '</td>';
break;
default:
echo '<td class="' . esc_attr( $class ) . '" style="' . esc_attr( $style ) . '">';
echo esc_attr( apply_filters( 'manage_leaky_paywall_subscribers_custom_column', ' ', $column_name, $user->ID ) );
echo '</td>';
break;
}
}
?>
</tr>
<?php
}
}
/**
* Displays the search box.
*
* @since 3.1.0
*
* @param string $text The 'submit' button label.
* @param string $input_id ID attribute value for the search input field.
*/
public function search_box( $text, $input_id ) {
if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
return;
}
$input_id = $input_id . '-search-input';
$search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
if ( ! empty( $_REQUEST['orderby'] ) ) {
echo '<input type="hidden" name="orderby" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) ) . '" />';
}
if ( ! empty( $_REQUEST['order'] ) ) {
echo '<input type="hidden" name="order" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ) . '" />';
}
if ( ! empty( $_REQUEST['post_mime_type'] ) ) {
echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['post_mime_type'] ) ) ) . '" />';
}
if ( ! empty( $_REQUEST['detached'] ) ) {
echo '<input type="hidden" name="detached" value="' . esc_attr( sanitize_text_field( wp_unslash( $_REQUEST['detached'] ) ) ) . '" />';
}
?>
<p class="search-box">
<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_html( $text ); ?>:</label>
<input type="checkbox" name="custom_field_search"> Search custom fields<br>
<input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php echo esc_attr( $search_query ); ?>" />
<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
</p>
<?php
}
}