forked from zeen101/leaky-paywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
3056 lines (2326 loc) · 107 KB
/
functions.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package zeen101's Leaky Paywall
* @since 1.0.0
*/
if ( !function_exists( 'get_leaky_paywall_settings' ) ) {
/**
* Helper function to get zeen101's Leaky Paywall settings for current site
*
* @since 1.0.0
*
* @return mixed Value set for the issuem options.
*/
function get_leaky_paywall_settings() {
global $leaky_paywall;
return $leaky_paywall->get_settings();
}
}
if ( !function_exists( 'update_leaky_paywall_settings' ) ) {
/**
* Helper function to save zeen101's Leaky Paywall settings for current site
*
* @since 1.0.0
*
* @return mixed Value set for the issuem options.
*/
function update_leaky_paywall_settings( $settings ) {
global $leaky_paywall;
return $leaky_paywall->update_settings( $settings );
}
}
if (!function_exists('is_multisite_premium') ) {
function is_multisite_premium() {
if ( is_multisite() ) {
return true;
}
//if ( is_multisite() && function_exists( 'is_leaky_paywall_multisite' ) ) {
// return is_leaky_paywall_multisite();
//}
return false;
}
}
if ( !function_exists( 'get_leaky_paywall_subscribers_site_id_by_subscriber_id' ) ) {
function get_leaky_paywall_subscribers_site_id_by_subscriber_id( $subscriber_id, $mode = false ) {
$site_id = '';
if ( empty( $mode ) ) {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
}
if ( is_multisite_premium() ) {
global $wpdb;
$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT $wpdb->usermeta.meta_key
FROM $wpdb->usermeta
WHERE $wpdb->usermeta.meta_key LIKE %s
AND $wpdb->usermeta.meta_value = %s
",
'_issuem_leaky_paywall_' . $mode . '_subscriber_id%',
$subscriber_id
)
);
if ( !empty( $results ) ) {
foreach ( $results as $result ) {
if ( preg_match( '/_issuem_leaky_paywall_' . $mode . '_subscriber_id(_(.+))/', $result, $matches ) ) {
return $matches[2]; //should be the site ID that matches for this subscriber_id
}
}
}
}
return $site_id;
}
}
if ( !function_exists( 'get_leaky_paywall_subscribers_site_id_by_subscriber_email' ) ) {
function get_leaky_paywall_subscribers_site_id_by_subscriber_email( $subscriber_email, $mode = false ) {
$site_id = '';
if ( empty( $mode ) ) {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
}
if ( is_multisite_premium() ) {
global $wpdb;
$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT $wpdb->usermeta.meta_key
FROM $wpdb->usermeta
WHERE $wpdb->usermeta.meta_key LIKE %s
AND $wpdb->usermeta.meta_value = %s
",
'_issuem_leaky_paywall_' . $mode . '_subscriber_email%',
$subscriber_email
)
);
if ( !empty( $results ) ) {
foreach ( $results as $result ) {
if ( preg_match( '/_issuem_leaky_paywall_' . $mode . '_subscriber_email(_(.+))/', $result, $matches ) ) {
return $matches[2]; //should be the site ID that matches for this subscriber_id
}
}
}
}
return $site_id;
}
}
if ( !function_exists( 'get_leaky_paywall_subscriber_by_subscriber_id' ) ) {
function get_leaky_paywall_subscriber_by_subscriber_id( $subscriber_id, $mode = false, $blog_id = false ) {
$site = '';
if ( empty( $mode ) ) {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
}
if ( is_multisite_premium() ) {
if ( empty( $blog_id ) ) {
if ( $blog_id = get_leaky_paywall_subscribers_site_id_by_subscriber_id( $subscriber_id ) ) {
$site = '_' . $blog_id;
}
} else {
$site = '_' . $blog_id;
}
}
$args = array(
'meta_key' => '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site,
'meta_value' => $subscriber_id,
);
$users = get_users( $args );
if ( !empty( $users ) ) {
foreach ( $users as $user ) {
return $user;
}
}
return false;
}
}
if ( !function_exists( 'get_leaky_paywall_subscriber_by_subscriber_email' ) ) {
function get_leaky_paywall_subscriber_by_subscriber_email( $subscriber_email, $mode = false, $blog_id = false ) {
$site = '';
if ( is_email( $subscriber_email ) ) {
if ( empty( $mode ) ) {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
}
if ( is_multisite_premium() ) {
if ( empty( $blog_id ) ) {
if ( $blog_id = get_leaky_paywall_subscribers_site_id_by_subscriber_email( $subscriber_email ) ) {
$site = '_' . $blog_id;
}
} else {
$site = '_' . $blog_id;
}
}
$args = array(
'meta_key' => '_issuem_leaky_paywall_' . $mode . '_subscriber_email' . $site,
'meta_value' => $subscriber_email,
);
$users = get_users( $args );
if ( !empty( $users ) ) {
foreach ( $users as $user ) {
return $user;
}
}
}
return false;
}
}
if ( !function_exists( 'add_leaky_paywall_login_hash' ) ) {
/**
* Adds unique hash to login table for user's login link
*
* @since 1.0.0
*
* @param string $email address of user "logging" in
* @param string $hash of user "logging" in
* @return mixed $wpdb insert ID or false
*/
function add_leaky_paywall_login_hash( $email, $hash ) {
$expiration = apply_filters( 'leaky_paywall_login_link_expiration', 60 * 60 ); //1 hour
set_transient( '_lpl_' . $hash, $email, $expiration );
}
}
if ( !function_exists( 'is_leaky_paywall_login_hash_unique' ) ) {
/**
* Verifies hash is valid for login link
*
* @since 1.0.0
*
* @param string $hash of user "logging" in
* @return mixed $wpdb var or false
*/
function is_leaky_paywall_login_hash_unique( $hash ) {
if ( preg_match( '#^[0-9a-f]{32}$#i', $hash ) ) { //verify we get a valid 32 character md5 hash
return !( false !== get_transient( '_lpl_' . $hash ) );
}
return false;
}
}
if ( !function_exists( 'verify_leaky_paywall_login_hash' ) ) {
/**
* Verifies hash is valid length and hasn't expired
*
* @since 1.0.0
*
* @param string $hash of user "logging" in
* @return mixed $wpdb var or false
*/
function verify_leaky_paywall_login_hash( $hash ) {
if ( preg_match( '#^[0-9a-f]{32}$#i', $hash ) ) { //verify we get a valid 32 character md5 hash
return (bool) get_transient( '_lpl_' . $hash );
}
return false;
}
}
if ( !function_exists( 'get_leaky_paywall_email_from_login_hash' ) ) {
/**
* Gets logging in user's email address from login link's hash
*
* @since 1.0.0
*
* @param string $hash of user "logging" in
* @return string email from $wpdb or false if invalid hash or expired link
*/
function get_leaky_paywall_email_from_login_hash( $hash ) {
if ( preg_match( '#^[0-9a-f]{32}$#i', $hash ) ) { //verify we get a valid 32 character md5 hash
return get_transient( '_lpl_' . $hash );
}
return false;
}
}
if ( !function_exists( 'leaky_paywall_user_has_access' ) ) {
/**
* Determine if a user has access based on their expiration date and their payment status.
*
* @since 4.9.3
*
* @param object $user object from WordPress database
* @return bool true if the user has access or false if they have either expired or their payment status is set to deactived
*/
function leaky_paywall_user_has_access( $user ) {
$mode = leaky_paywall_get_current_mode();
$site = leaky_paywall_get_current_site();
$unexpired = false;
$expires = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_expires' . $site, true );
$payment_status = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, true );
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
$unexpired = true;
} else {
$date_format = get_option( 'date_format' );
$expires = mysql2date( $date_format, $expires );
if ( strtotime( $expires ) > time() ) {
$unexpired = true;
}
}
if ( $unexpired && $payment_status != 'deactivated' ) {
$has_access = true;
} else {
$has_access = false;
}
return apply_filters( 'leaky_paywall_user_has_access', $has_access, $user );
}
}
if ( !function_exists( 'leaky_paywall_get_current_mode' ) ) {
function leaky_paywall_get_current_mode() {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
return apply_filters( 'leaky_paywall_current_mode', $mode );
}
}
if ( !function_exists( 'leaky_paywall_get_current_site' ) ) {
function leaky_paywall_get_current_site() {
global $blog_id;
if ( is_multisite_premium() && !is_main_site( $blog_id ) ) {
$site = '_' . $blog_id;
} else {
$site = '';
}
return apply_filters( 'leaky_paywall_current_site', $site );
}
}
if ( !function_exists( 'leaky_paywall_get_currency' ) ) {
/**
* Get the currency value set in the Leaky Paywall settings
*
* @since 4.9.3
*
* @return string Currency code (i.e USD)
*/
function leaky_paywall_get_currency() {
$settings = get_leaky_paywall_settings();
$currency = $settings['leaky_paywall_currency'];
return apply_filters( 'leaky_paywall_currency', $currency );
}
}
if ( !function_exists( 'leaky_paywall_has_user_paid' ) ) {
/**
* Verified if user has paid through Stripe
*
* @since 1.0.0
*
* @param string $email address of user "logged" in
* @return mixed Expiration date or subscriptions status or false if not paid
*/
function leaky_paywall_has_user_paid( $email=false, $blog_id=null ) {
$settings = get_leaky_paywall_settings();
$paid = false;
$canceled = false;
$expired = false;
$sites = array( '' ); //Empty String for non-Multisite, so we cycle through "sites" one time with no $site set
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
if ( empty( $email ) ) {
$user = wp_get_current_user();
if ( $user->ID == 0 ) { // no user
return false;
}
} else {
if ( is_email( $email ) ) {
$user = get_user_by( 'email', $email );
if ( !$user ) { // no user found with that email address
return false;
}
} else {
return false;
}
}
if ( is_multisite_premium() ) {
if ( is_null( $blog_id ) ){
global $blog_id;
if ( !is_main_site( $blog_id ) ) {
$sites = array( '_all', '_' . $blog_id );
} else {
$sites = array( '_all', '_' . $blog_id, '' );
}
} else if ( is_int( $blog_id ) ) {
$sites = array( '_' . $blog_id );
} else if ( empty( $blog_id ) ) {
$sites = array( '' );
} else {
$sites = array( $blog_id );
}
}
foreach ( $sites as $site ) {
$subscriber_id = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_subscriber_id' . $site, true );
$expires = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_expires' . $site, true );
$payment_gateway = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_gateway' . $site, true );
$payment_status = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_payment_status' . $site, true );
$plan = get_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_plan' . $site, true );
if ( $payment_gateway !== 'stripe' ) {
if ( 'paypal_standard' === $payment_gateway || 'paypal-standard' === $payment_gateway ) {
if ( !empty( $plan ) && 'active' == $payment_status ) {
return 'subscription';
}
}
switch( $payment_status ) {
case 'Active':
case 'active':
case 'refunded':
case 'refund':
$expires = apply_filters( 'leaky_paywall_has_user_paid_expires', $expires, $payment_gateway, $payment_status, $subscriber_id, $plan, $expires, $user, $mode, $site );
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
return 'unlimited';
}
if ( strtotime( $expires ) < time() ) {
$expired = $expires;
} else {
$paid = true;
}
break;
case 'cancelled':
case 'canceled':
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
$expired = true;
} else {
$canceled = true;
}
case 'reversed':
case 'buyer_complaint':
case 'denied' :
case 'expired' :
case 'failed' :
case 'voided' :
case 'deactivated' :
continue;
break;
}
} else {
// check with Stripe to make sure the user has an active subscription
if ( $mode == 'test' ) {
$secret_key = isset( $settings['test_secret_key'] ) ? trim( $settings['test_secret_key'] ) : '';
} else {
$secret_key = isset( $settings['live_secret_key'] ) ? trim( $settings['live_secret_key'] ) : '';
}
\Stripe\Stripe::setApiKey( $secret_key );
try {
if ( empty( $subscriber_id ) ) {
switch( $payment_status ) {
case 'Active':
case 'active':
case 'refunded':
case 'refund':
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
return 'unlimited';
}
if ( strtotime( $expires ) < time() ) {
$expired = $expires;
} else {
$paid = true;
}
break;
case 'cancelled':
case 'canceled':
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
$expired = true;
} else {
$canceled = true;
}
break;
case 'reversed':
case 'buyer_complaint':
case 'denied' :
case 'expired' :
case 'failed' :
case 'voided' :
case 'deactivated' :
continue;
break;
}
} else {
$cu = \Stripe\Customer::retrieve( $subscriber_id );
if ( !empty( $cu ) ) {
if ( !empty( $cu->deleted ) && true === $cu->deleted ) {
$canceled = true;
}
}
if ( !empty( $plan ) ) {
if ( isset( $cu->subscriptions ) ) {
$subscriptions = $cu->subscriptions->all( array('limit' => '1') );
foreach( $subscriptions->data as $subscription ) {
if ( leaky_paywall_is_valid_stripe_subscription( $subscription ) ) {
return 'subscription';
}
}
}
}
$ch = \Stripe\Charge::all( array( 'count' => 1, 'customer' => $subscriber_id ) );
if ( empty( $expires ) || '0000-00-00 00:00:00' === $expires ) {
return 'unlimited';
} else {
if ( strtotime( $expires ) < time() ) {
if ( true === $ch->data[0]->paid && false === $ch->data[0]->refunded ) {
$expired = $expires;
}
} else {
$paid = true;
}
}
}
} catch ( Exception $e ) {
$results = '<h1>' . sprintf( __( 'Error processing request: %s', 'leaky-paywall' ), $e->getMessage() ) . '</h1>';
}
}
} // end foreach
if ( is_bool( $canceled ) && $canceled ) {
$paid = false;
}
if ( is_bool( $expired ) && $expired ) {
$paid = false;
}
return apply_filters( 'leaky_paywall_has_user_paid', $paid, $payment_gateway, $payment_status, $subscriber_id, $plan, $expires, $user, $mode, $site );
}
}
if ( !function_exists( 'leaky_paywall_set_expiration_date' ) ) {
/**
* Set a user's expiration data
* @param int $user_id the user id
* @param array $data information about the subscription
*
*/
function leaky_paywall_set_expiration_date( $user_id, $data ) {
if ( empty( $user_id ) ) {
return;
}
if ( is_multisite_premium() && !is_main_site( $data['site'] ) ) {
$site = '_' . $data['site'];
} else {
$site = '';
}
$mode = leaky_paywall_get_current_mode();
if ( isset( $data['expires'] ) && $data['expires'] ) {
$expires = $data['expires'];
} else if ( !empty( $data['interval'] ) && isset( $data['interval_count'] ) && 1 <= $data['interval_count'] ) {
$expires = date_i18n( 'Y-m-d 23:59:59', strtotime( '+' . $data['interval_count'] . ' ' . $data['interval'] ) ); //we're generous, give them the whole day!
} else {
$expires = '0000-00-00 00:00:00';
}
update_user_meta( $user_id, '_issuem_leaky_paywall_' . $mode . '_expires' . $site, apply_filters( 'leaky_paywall_set_expiration_date', $expires, $data, $user_id ) );
}
}
if ( !function_exists( 'leaky_paywall_new_subscriber' ) ) {
/**
* Adds new subscriber to subscriber table
*
* @since 1.0.0
*
* @param deprecated $hash
* @param string $email address of user "logged" in
* @param int $customer_id
* @param array $meta_args Arguments passed from type of subscriber
* @param string $login optional login name to use instead of email address
* @return mixed $wpdb insert ID or false
*/
function leaky_paywall_new_subscriber( $hash='deprecated', $email, $customer_id, $meta_args, $login='' ) {
if ( !is_email( $email ) ) {
return false;
}
$settings = get_leaky_paywall_settings();
if ( is_multisite_premium() && !is_main_site( $meta_args['site'] ) ) {
$site = '_' . $meta_args['site'];
} else {
$site = '';
}
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
$expires = '0000-00-00 00:00:00';
if ( $user = get_user_by( 'email', $email ) ) {
//the user already exists
//grab the ID for later
$user_id = $user->ID;
$userdata = get_userdata( $user_id );
} else {
//the user doesn't already exist
// if they submitted a custom login name, use that
if ( isset( $meta_args['login'] ) ) {
$login = $meta_args['login'];
}
//create a new user with their email address as their username
//grab the ID for later
if ( empty( $login ) ) {
$parts = explode( '@', $email );
$login = $parts[0];
}
//Avoid collisions
while ( $user = get_user_by( 'login', $login ) ) {
$login = $user->user_login . '_' . substr( uniqid(), 5 );
}
if ( isset( $meta_args['password'] ) ) {
$password = $meta_args['password'];
} else {
$password = wp_generate_password();
}
$userdata = array(
'user_login' => $login,
'user_email' => $email,
'user_pass' => $password,
'first_name' => isset( $meta_args['first_name'] ) ? $meta_args['first_name'] : '',
'last_name' => isset( $meta_args['last_name'] ) ? $meta_args['last_name'] : '',
'display_name' => isset( $meta_args['first_name'] ) ? $meta_args['first_name'] . ' ' . $meta_args['last_name'] : '',
'user_registered' => date_i18n( 'Y-m-d H:i:s' ),
);
$userdata = apply_filters( 'leaky_paywall_userdata_before_user_create', $userdata );
$user_id = wp_insert_user( $userdata );
}
if ( empty( $user_id ) ) {
return false;
}
leaky_paywall_set_expiration_date( $user_id, $meta_args );
unset( $meta_args['site'] );
$meta_args['created'] = date( 'Y-m-d H:i:s' );
// set free level subscribers to active
if ( $meta_args['price'] == '0' ) {
$meta_args['payment_status'] = 'active';
}
$meta = apply_filters( 'leaky_paywall_new_subscriber_meta', $meta_args, $email, $customer_id, $meta_args );
// remove any extra underscores from site variable
$site = str_replace( '__', '_', $site );
foreach( $meta as $key => $value ) {
if ( $key != 'confirm_password' || $key != 'password' ) { // do not want to store their password as plain text
update_user_meta( $user_id, '_issuem_leaky_paywall_' . $mode . '_' . $key . $site, $value );
}
}
do_action( 'leaky_paywall_new_subscriber', $user_id, $email, $meta, $customer_id, $meta_args, $userdata );
return $user_id;
}
}
if ( !function_exists( 'leaky_paywall_update_subscriber' ) ) {
/**
* Updates an existing subscriber to subscriber table
*
* @since 1.0.0
*
* @param deprecated $hash
* @param string $email address of user "logged" in
* @param int $customer_id Customer ID
* @param array $meta_args Arguments passed from type of subscriber
* @return mixed $wpdb insert ID or false
*/
function leaky_paywall_update_subscriber( $hash='deprecated', $email, $customer_id, $meta_args ) {
if ( !is_email( $email ) ) {
return false;
}
$settings = get_leaky_paywall_settings();
if ( is_multisite_premium() && !is_main_site( $meta_args['site'] ) ) {
$site = '_' . $meta_args['site'];
} else {
$site = '';
}
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
$expires = '0000-00-00 00:00:00';
if ( is_user_logged_in() && !is_admin() ) {
//Update the existing user
$user_id = get_current_user_id();
} elseif ( $user = get_user_by( 'email', $email ) ) {
//the user already exists
//grab the ID for later
$user_id = $user->ID;
} else {
return false; //User does not exist, cannot update
}
leaky_paywall_set_expiration_date( $user_id, $meta_args );
unset( $meta_args['site'] );
// if ( !empty( $meta_args['interval'] ) && isset( $meta_args['interval_count'] ) && 1 <= $meta_args['interval_count'] ) {
// $expires = date_i18n( 'Y-m-d 23:59:59', strtotime( '+' . $meta_args['interval_count'] . ' ' . $meta_args['interval'] ) ); //we're generous, give them the whole day!
// } else if ( !empty( $meta_args['expires'] ) ) {
// $expires = $meta_args['expires'];
// }
$meta = array(
'level_id' => $meta_args['level_id'],
'subscriber_id' => $customer_id,
'price' => $meta_args['price'],
'description' => $meta_args['description'],
'plan' => $meta_args['plan'],
// 'expires' => $expires,
'payment_gateway' => $meta_args['payment_gateway'],
'payment_status' => $meta_args['payment_status'],
);
$meta = apply_filters( 'leaky_paywall_update_subscriber_meta', $meta, $email, $customer_id, $meta_args );
foreach( $meta as $key => $value ) {
update_user_meta( $user_id, '_issuem_leaky_paywall_' . $mode . '_' . $key . $site, $value );
}
$user_id = wp_update_user( array( 'ID' => $user_id, 'user_email' => $email ) );
do_action( 'leaky_paywall_update_subscriber', $user_id, $email, $meta, $customer_id, $meta_args );
return $user_id;
}
}
/**
* Get all valid and active Leaky Paywall levels
*
* @since 4.9.0
*
* @return array List of active levels
*/
function leaky_paywall_get_levels() {
$settings = get_leaky_paywall_settings();
$blog_id = get_current_blog_id();
$level_list = array();
foreach( $settings['levels'] as $key => $level ) {
if ( !empty( $level['deleted'] ) ) {
continue;
}
if ( is_multisite_premium() && !empty( $level['site'] ) && 'all' != $level['site'] && $blog_id != $level['site'] ) {
continue;
}
if ( !is_numeric( $key ) ) {
continue;
}
$level_list[$key] = $level;
}
return $level_list;
}
if ( !function_exists( 'leaky_paywall_translate_payment_gateway_slug_to_name' ) ) {
function leaky_paywall_translate_payment_gateway_slug_to_name( $slug ) {
$return = 'Unknown';
switch( $slug ) {
case 'stripe':
$return = 'Stripe';
break;
case 'paypal_standard':
case 'paypal-standard':
$return = 'PayPal';
break;
case 'free_registration':
$return = 'Free Registration';
break;
case 'manual':
$return = __( 'Manually Added', 'leaky-paywall' );
break;
default:
$return = $slug;
break;
}
return apply_filters( 'leaky_paywall_translate_payment_gateway_slug_to_name', $return, $slug );
}
}
if ( !function_exists( 'leaky_paywall_cancellation_confirmation' ) ) {
/**
* Cancels a subscriber from Stripe subscription plan
*
* @since 1.0.0
*
* @return string Cancellation form output
*/
function leaky_paywall_cancellation_confirmation() {
$settings = get_leaky_paywall_settings();
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test';
$form = '';
if ( is_user_logged_in() ) {
global $blog_id;
if ( is_multisite_premium() && !is_main_site( $blog_id ) ) {
$site = '_' . $blog_id;
} else {
$site = '';
}
if ( !empty( $_REQUEST['payment_gateway'] ) ) {
$payment_gateway = $_REQUEST['payment_gateway'];
} else {
return '<p>' . __( 'No payment gateway defined.', 'leaky-paywall' ) . '</p>';
}
if ( !empty( $_REQUEST['subscriber_id'] ) ) {
$subscriber_id = $_REQUEST['subscriber_id'];
} else {
return '<p>' . __( 'No subscriber ID defined.', 'leaky-paywall' ) . '</p>';
}
if ( isset( $_REQUEST['cancel'] ) && empty( $_REQUEST['cancel'] ) ) {
$form = '<h3>' . __( 'Cancel Subscription', 'leaky-paywall' ) . '</h3>';
$cancel_description = '<p>' . __( 'Cancellations take effect at the end of your billing cycle, and we can’t give partial refunds for unused time in the billing cycle. If you still wish to cancel now, you may proceed, or you can come back later.', 'leaky-paywall' ) . '</p>';
$cancel_description .= '<p>' . sprintf( __( ' Thank you for the time you’ve spent subscribed to %s. We hope you’ll return someday. ', 'leaky-paywall' ), $settings['site_name'] ) . '</p>';
$form .= apply_filters( 'leaky_paywall_cancel_subscription_description', $cancel_description );
$form .= '<a href="' . esc_url( add_query_arg( array( 'cancel' => 'confirm' ) ) ) . '">' . __( 'Yes, cancel my subscription!', 'leaky-paywall' ) . '</a> | <a href="' . get_page_link( $settings['page_for_profile'] ) . '">' . __( 'No, get me outta here!', 'leak-paywall' ) . '</a>';
} else if ( !empty( $_REQUEST['cancel'] ) && 'confirm' === $_REQUEST['cancel'] ) {
$user = wp_get_current_user();
if ( 'stripe' === $payment_gateway ) {
try {
$secret_key = ( 'test' === $mode ) ? $settings['test_secret_key'] : $settings['live_secret_key'];
$cu = \Stripe\Customer::retrieve( $subscriber_id );
if ( !empty( $cu ) )
if ( true === $cu->deleted )
throw new Exception( __( 'Unable to find valid Stripe customer ID to unsubscribe. Please contact support', 'leaky-paywall' ) );
$subscriptions = $cu->subscriptions->all( array('limit' => '1') );
foreach( $subscriptions->data as $susbcription ) {
$sub = $cu->subscriptions->retrieve( $susbcription->id );
$results = $sub->cancel();
}
if ( !empty( $results->status ) && 'canceled' === $results->status ) {
$form .= '<p>' . sprintf( __( 'Your subscription has been successfully canceled. You will continue to have access to %s until the end of your billing cycle. Thank you for the time you have spent subscribed to our site and we hope you will return soon!', 'leaky-paywall' ), $settings['site_name'] ) . '</p>';
//We are creating plans with the site of '_all', even on single sites. This is a quick fix but needs to be readdressed.
update_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_plan' . $site, 'Canceled' );
update_user_meta( $user->ID, '_issuem_leaky_paywall_' . $mode . '_plan_all', 'Canceled' );
do_action('leaky_paywall_cancelled_subscriber', $user, 'stripe' );
} else {
$form .= '<p>' . sprintf( __( 'ERROR: An error occured when trying to unsubscribe you from your account, please try again. If you continue to have trouble, please contact us. Thank you.', 'leaky-paywall' ), $settings['site_name'] ) . '</p>';
}
$form .= '<a href="' . get_home_url() . '">' . sprintf( __( 'Return to %s...', 'leak-paywall' ), $settings['site_name'] ) . '</a>';
} catch ( Exception $e ) {
$results = '<h1>' . sprintf( __( 'Error processing request: %s', 'leaky-paywall' ), $e->getMessage() ) . '</h1>';
}
} else if ( 'paypal_standard' === $payment_gateway || 'paypal-standard' === $payment_gateway ) {