forked from zeen101/leaky-paywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber-table.php
585 lines (455 loc) · 19.4 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?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.
*/
if( !class_exists( 'WP_List_Table' ) )
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
class Leaky_Paywall_Subscriber_List_Table extends WP_List_Table {
function ajax_user_can() {
return current_user_can( 'manage_network_users' );
}
public function prepare_items() {
global $usersearch, $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'] ) ? '*' . $_REQUEST['s'] . '*' : '';
// $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
$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'] = $_REQUEST['orderby'] = 'ID';
}
if ( !isset( $_REQUEST['order'] ) ) {
$_GET['order'] = $_REQUEST['order'] = 'DESC';
}
// $args['count_total'] = false;
}
if ( $this->is_site_users ) {
$args['blog_id'] = $this->site_id;
}
if ( !empty( $_REQUEST['orderby'] ) ) {
$args['orderby'] = $_REQUEST['orderby'];
}
if ( !empty( $_REQUEST['order'] ) )
$args['order'] = $_REQUEST['order'];
// Query the user IDs for this page
// global $blog_id;
// $results = leaky_paywall_subscriber_query( $args, $blog_id );
$settings = get_leaky_paywall_settings();
$mode = leaky_paywall_get_current_mode();
$site = leaky_paywall_get_current_site();
// if ( is_multisite_premium() && is_main_site( $blog_id ) ) {
// $results = array_merge( $results, leaky_paywall_subscriber_query( $args, false ) );
// }
// $this->items = $results;
//
$args['meta_query'] = array(
array(
'key' => '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site,
'compare' => 'EXISTS',
),
);
// search by subscriber id
if ( isset( $_GET['custom_field_search'] ) && 'on' == $_GET['custom_field_search'] ) {
// if is custom field, then do the following
$args['meta_query']['relation'] = 'AND';
$args['meta_query'][] = array(
'key' => '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site,
'value' => $_GET['s'],
'compare' => 'LIKE'
);
unset( $args['search'] );
}
if ( isset( $_GET['filter-level'] ) && 'lpsubs' == $_GET['user-type'] ) {
$level = esc_attr( $_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'] ) && 'lpsubs' == $_GET['user-type'] ) {
$status = esc_attr( $_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();
// $args['number'] = 0;
// $this->set_pagination_args( array(
// 'total_items' => count( leaky_paywall_subscriber_query( $args ) ),
// 'per_page' => $users_per_page,
// ) );
$this->set_pagination_args( array(
'total_items' => $wp_user_search->get_total(),
'per_page' => $users_per_page,
) );
}
public function new_prepare_items()
{
global $role, $usersearch;
$usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
$role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
$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();
if ( 'none' === $role ) {
$args = array(
'number' => $users_per_page,
'offset' => ( $paged-1 ) * $users_per_page,
'include' => wp_get_users_with_no_role( $this->site_id ),
'search' => $usersearch,
'fields' => 'all_with_meta'
);
} else {
$args = array(
'number' => $users_per_page,
'offset' => ( $paged-1 ) * $users_per_page,
'role' => $role,
'search' => $usersearch,
'fields' => 'all_with_meta'
);
}
if ( '' !== $args['search'] )
$args['search'] = '*' . $args['search'] . '*';
if ( $this->is_site_users )
$args['blog_id'] = $this->site_id;
if ( isset( $_REQUEST['orderby'] ) )
$args['orderby'] = $_REQUEST['orderby'];
if ( isset( $_REQUEST['order'] ) )
$args['order'] = $_REQUEST['order'];
/**
* Filters the query arguments used to retrieve users for the current users list table.
*
* @since 4.4.0
*
* @param array $args Arguments passed to WP_User_Query to retrieve items for the current
* users list table.
*/
$args = apply_filters( 'users_list_table_query_args', $args );
// Query the user IDs for this page
$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,
) );
}
function no_items() {
_e( 'No Leaky Paywall subscribers found.' );
}
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' ),
'price' => __( 'Price', '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' ),
);
$users_columns = apply_filters( 'leaky_paywall_subscribers_columns', $users_columns );
return $users_columns;
}
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 ),
'price' => array( 'price', 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;
}
function user_views() {
$user_type = !empty( $_GET['user-type'] ) ? $_GET['user-type'] : 'lpsubs';
echo '<div class="alignleft actions">';
echo '<label for="user-type-selector" class="screen-reader-text">' . __( 'Select User Type' ) . '</label>';
echo '<select name="user-type" id="user-type-selector">';
echo '<option value="lpsubs" ' . selected( 'lpsubs', $user_type, false ) . '>' . __( 'Leaky Paywall Subscribers' ) . '</option>';
echo '<option value="wpusers" ' . selected( 'wpusers', $user_type, false ) . '>' . __( 'All WordPress Users' ) . '</option>';
echo '</select>';
submit_button( __( 'Apply' ), 'primary', false, false );
echo '</div>';
}
public function extra_tablenav( $which ) {
if ( $which != 'top' ) {
return;
}
if ( isset( $_GET['user-type'] ) && $_GET['user-type'] != 'lpsubs' ) {
return;
}
$levels = leaky_paywall_get_levels();
$lev = isset( $_GET['filter-level'] ) ? esc_attr( $_GET['filter-level'] ) : 'all';
$stat = isset( $_GET['filter-status'] ) ? esc_attr( $_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="' . $key . '">' . $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 $key; ?>" <?php selected( $stat, $key ); ?>><?php echo $value; ?></option>
<?php
} ?>
</select>
<input name="filter_action" id="subscriber-query-submit" class="button" value="Filter" type="submit">
</div>
<?php
}
function display_rows() {
global $current_site, $blog_id;
$settings = get_leaky_paywall_settings();
if ( is_multisite_premium() ) {
global $blog_id;
if ( !is_main_site( $blog_id ) ) {
$sites = array( '_all', '_' . $blog_id );
} else {
$sites = array( '_all', '_' . $blog_id, '' );
}
} else {
// create a generic array so that single site installs will iterate through the sites loop below
$sites = array( '_all' );
}
$alt = '';
foreach ( $this->items as $user ) {
$user = get_user_by( 'email', $user->user_email );
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
foreach( $sites as $site ) {
$alt = ( 'alternate' == $alt ) ? '' : 'alternate';
if ( is_multisite_premium() ) {
$payment_gateway = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_gateway' . $site, true );
} else {
$payment_gateway = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_gateway', true );
}
if ( !empty( $_GET['user-type'] ) && 'wpusers' !== $_GET['user-type'] ) {
if ( empty( $payment_gateway ) ) {
continue;
}
}
?>
<tr class="<?php echo $alt; ?>">
<?php
list( $columns, $hidden ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class='$column_name column-$column_name'";
$style = '';
if ( in_array( $column_name, $hidden ) )
$style = ' style="display:none;"';
$attributes = "$class$style";
switch ( $column_name ) {
case 'wp_user_login':
echo "<td $attributes>"; ?>
<strong><?php echo $user->user_login; ?></strong>
<?php
// if the user switching plugin is activated, add switch to link to LP subscriber table for easier testing
if ( class_exists( 'user_switching' ) ) {
if ( $link = user_switching::maybe_switch_url( $user ) ) {
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', urlencode( $user->user_email ) ) );
echo "<td $attributes>"; ?>
<strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_email; ?></a></strong>
</td>
<?php
break;
case 'name':
echo "<td $attributes>"; ?>
<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>
</td>
<?php
break;
case 'level_id':
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 );
}
$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', 'issuem-leaky-paywall' );
} else {
$level_name = stripcslashes( $settings['levels'][$level_id]['label'] );
}
echo "<td $attributes>" . $level_name . '</td>';
break;
case 'susbcriber_id':
if ( is_multisite_premium() ) {
echo "<td $attributes>" . get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true ) . '</td>';
} else {
echo "<td $attributes>" . get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_subscriber_id', true ) . '</td>';
}
break;
case 'price':
if ( is_multisite_premium() ) {
echo "<td $attributes>" . number_format( (float)get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_price' . $site, true ), '2' ) . '</td>';
} else {
echo "<td $attributes>" . number_format( (float)get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_price', true ), '2' ) . '</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', 'issuem-leaky-paywall' );
} else if ( 'paypal_standard' === $payment_gateway || 'paypal-standard' === $payment_gateway ) {
$plan = sprintf( __( 'Recurring every %s', 'issuem-leaky-paywall' ), str_replace( array( 'D', 'W', 'M', 'Y' ), array( 'Days', 'Weeks', 'Months', 'Years' ), $plan ) );
}
echo "<td $attributes>" . $plan . '</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 = get_option( 'date_format' );
$created = mysql2date( $date_format, $created );
echo "<td $attributes>" . $created . '</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', 'issuem-leaky-paywall' );
} else {
$date_format = get_option( 'date_format' );
$expires = mysql2date( $date_format, $expires );
}
echo "<td $attributes>" . $expires . '</td>';
break;
case 'has_access':
$has_access = leaky_paywall_user_has_access( $user );
if ( $has_access ) {
echo '<td ' . $attributes . '>Yes</td>';
} else {
echo '<td ' . $attributes . '>No</td>';
}
break;
case 'gateway':
echo "<td $attributes>" . leaky_paywall_translate_payment_gateway_slug_to_name( $payment_gateway ) . '</td>';
break;
case 'status':
if ( is_multisite_premium() ) {
echo "<td $attributes>" . get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, true ) . '</td>';
} else {
echo "<td $attributes>" . get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status', true ) . '</td>';
}
break;
default:
echo "<td $attributes>" . apply_filters( 'manage_leaky_paywall_subscribers_custom_column', ' ', $column_name, $user->ID ) . '</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';
if ( ! empty( $_REQUEST['orderby'] ) )
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
if ( ! empty( $_REQUEST['order'] ) )
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
if ( ! empty( $_REQUEST['post_mime_type'] ) )
echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
if ( ! empty( $_REQUEST['detached'] ) )
echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
?>
<p class="search-box">
<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
<input type="checkbox" name="custom_field_search"> Search custom fields</input><br>
<input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" />
<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
</p>
<?php
}
}