/
patreon_frontend.php
1604 lines (1102 loc) · 56.4 KB
/
patreon_frontend.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
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
die;
}
class Patreon_Frontend {
public static $messages_map = array();
public static $allowed_toggles = array();
public static $current_user_logged_into_patreon = -1;
function __construct() {
add_action( 'login_enqueue_scripts', array( $this,'patreonEnqueueCss' ), 10 );
add_action( 'wp_enqueue_scripts', array( $this,'patreonEnqueueCss' ) );
add_action( 'wp_head', array( $this, 'patreonPrintCss' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'patreonEnqueueJs' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'patreonEnqueueAdminCss' ) );
add_action( 'login_form', array( $this, 'showPatreonMessages' ) );
add_action( 'login_form', array( $this, 'displayPatreonLoginButtonInLoginForm' ) );
add_action( 'register_form', array( $this, 'showPatreonMessages' ) );
add_action( 'register_form', array( $this, 'displayPatreonLoginButtonInLoginForm' ) );
add_filter( 'the_content', array( $this, 'protectContentFromUsers'), PHP_INT_MAX - 5 );
// This filter will inject currency sign into label over button until proper internationalization is done
add_filter( 'ptrn/label_text_over_universal_button', array( $this, 'replace_in_currency_sign'), PHP_INT_MAX - 5 );
add_filter( 'ptrn/valid_patron_final_footer', array( $this, 'replace_in_currency_sign'), PHP_INT_MAX - 5 );
add_shortcode( 'patreon_login_button', array( $this,'LoginButtonShortcode' ) );
add_filter('get_avatar', array( $this, 'show_patreon_avatar' ), 10, 5);
self::$messages_map = array(
'patreon_cant_login_strict_oauth' => PATREON_CANT_LOGIN_STRICT_OAUTH,
'login_with_wordpress' => PATREON_LOGIN_WITH_WORDPRESS_NOW,
'patreon_cant_login_api_error' => PATREON_CANT_LOGIN_DUE_TO_API_ERROR,
'patreon_cant_login_api_error_credentials' => PATREON_CANT_LOGIN_DUE_TO_API_ERROR_CHECK_CREDENTIALS,
'patreon_no_locking_level_set_for_this_post' => PATREON_NO_LOCKING_LEVEL_SET_FOR_THIS_POST,
'patreon_no_post_id_to_unlock_post' => PATREON_NO_POST_ID_TO_UNLOCK_POST,
'patreon_weird_redirection_at_login' => PATREON_WEIRD_REDIRECTION_AT_LOGIN,
'patreon_could_not_create_wp_account' => PATREON_COULDNT_CREATE_WP_ACCOUNT,
'patreon_api_credentials_missing' => PATREON_API_CREDENTIALS_MISSING,
'admin_login_with_patreon_disabled' => PATREON_ADMIN_LOGIN_WITH_PATREON_DISABLED,
'email_exists_login_with_wp_first' => PATREON_EMAIL_EXISTS_LOGIN_WITH_WP_FIRST,
'login_with_patreon_disabled' => PATREON_LOGIN_WITH_PATREON_DISABLED,
'admin_bypass_filter_message' => PATREON_ADMIN_BYPASSES_FILTER_MESSAGE,
'no_code_receved_from_patreon' => PATREON_NO_CODE_RECEIVED_FROM_PATREON,
'no_patreon_action_provided_for_flow' => PATREON_NO_FLOW_ACTION_PROVIDED,
'patreon_direct_unlocks_not_turned_on' => PATREON_DIRECT_UNLOCKS_NOT_ON,
'patreon_couldnt_acquire_user_details' => PATREON_COULDNT_ACQUIRE_USER_DETAILS,
'pretty_permalinks_are_required' => PATREON_PRETTY_PERMALINKS_NEED_TO_BE_ENABLED,
'error_missing_credentials' => PATREON_ERROR_MISSING_CREDENTIALS,
'no_auth_for_client_creation' => PATREON_NO_AUTH_FOR_CLIENT_CREATION,
'failure_obtaining_credentials' => PATREON_NO_ACQUIRE_CLIENT_DETAILS,
'error_missing_credentials' => PATREON_NO_CREDENTIALS_RECEIVED,
'patreon_admin_message_default_title' => PATREON_ADMIN_MESSAGE_DEFAULT_TITLE,
'patreon_admin_message_default_content' => PATREON_ADMIN_MESSAGE_DEFAULT_CONTENT,
'client_delete_error_title' => PATREON_ADMIN_MESSAGE_CLIENT_DELETE_ERROR_TITLE,
'client_delete_error_content' => PATREON_ADMIN_MESSAGE_CLIENT_DELETE_ERROR_CONTENT,
'client_reconnect_delete_error_title' => PATREON_ADMIN_MESSAGE_CLIENT_RECONNECT_DELETE_ERROR_TITLE,
'client_reconnect_delete_error_content' => PATREON_ADMIN_MESSAGE_CLIENT_RECONNECT_DELETE_ERROR_CONTENT,
'all_post_category_fields_must_be_selected' => PATREON_ALL_POST_CATEGORY_FIELDS_MUST_BE_SELECTED,
);
self::$allowed_toggles = array(
'patreon-wordpress-advanced-options-toggle' => '',
);
}
function patreonEnqueueJs() {
wp_register_script( 'patreon-wordpress-js', PATREON_PLUGIN_ASSETS . '/js/app.js', array( 'jquery' ) );
wp_enqueue_script( 'patreon-wordpress-js', PATREON_PLUGIN_ASSETS . '/js/app.js', array( 'jquery' ), '1.0', true );
}
function patreonEnqueueAdminCss() {
wp_register_style( 'patreon-wordpress-admin-css', PATREON_PLUGIN_ASSETS . '/css/admin.css', false );
wp_enqueue_style( 'patreon-wordpress-admin-css', PATREON_PLUGIN_ASSETS . '/css/admin.css', PATREON_WORDPRESS_VERSION );
}
function patreonEnqueueCss() {
wp_register_style( 'patreon-wordpress-css', PATREON_PLUGIN_ASSETS.'/css/app.css', false );
wp_enqueue_style('patreon-wordpress-css', PATREON_PLUGIN_ASSETS.'/css/app.css' );
}
function patreonPrintCss() {
// Why we print out css direclty in header is that we want to account for any potential WP content directory location than default
echo '<style>';
echo "@font-face {
font-family: 'Libre Franklin Extra Bold';
src: url('" . PATREON_PLUGIN_ASSETS . "/fonts/librefranklin-extrabold-webfont.woff2') format('woff2'),
url('" . PATREON_PLUGIN_ASSETS . "/fonts/librefranklin-extrabold-webfont.woff') format('woff');
font-weight: bold;
}";
echo '</style>';
}
public static function displayPatreonCampaignBanner( $patreon_level = false, $args = false ) {
global $wp;
// Allow 3rd party plugins to override interface - this will abort interface generation and replace it with the code that returns from this filter, and also allow 3rd party code to still apply rest of this function's filters without causing recursion
$override_interface = array();
$override_interface = apply_filters( 'ptrn/override_interface_template', $patreon_level, $args );
if ( is_array( $override_interface ) AND isset( $override_interface['override'] ) ) {
return $override_interface['interface'];
}
$post = false;
// Get the post from post id if it is supplied
if ( isset( $args['post_id'] ) ) {
$post = get_post( $args['post_id'] );
}
if ( !$args OR !is_array( $args ) ) {
global $post;
$args = array();
}
$login_with_patreon = get_option( 'patreon-enable-login-with-patreon', false );
$client_id = get_option( 'patreon-client-id', false );
// Check existence of a custom patreon banners as saved in plugin options
$custom_universal_banner = get_option( 'patreon-custom-universal-banner', false );
// Default custom text banner
$contribution_required = PATREON_TEXT_LOCKED_POST;
if ( $custom_universal_banner AND $custom_universal_banner !='' ) {
// Custom banner exists and it is not empty. Override the message
$contribution_required = $custom_universal_banner;
}
if ( $patreon_level != false ) {
$contribution_required = str_replace( '%%pledgelevel%%', $patreon_level, $contribution_required );
$contribution_required = apply_filters( 'ptrn/contribution_required', $contribution_required, 'main_banner_message',$patreon_level, $post );
}
if ( $client_id ) {
// Wrap message and buttons in divs for responsive interface mechanics
$contribution_required = '<div class="patreon-locked-content-message">' . $contribution_required . '</div>';
// But hide the custom banner if no custom banner was saved
if ( !$custom_universal_banner OR $custom_universal_banner =='' ) {
$contribution_required = '';
}
// Still apply the filters so it can be modified by 3rd party code
$contribution_required = apply_filters( 'ptrn/final_state_main_banner_message', $contribution_required, $patreon_level, $post );
$button_args = array();
if ( isset( $args['direct_unlock'] ) ) {
// This is a direct unlock intent that does not seek to particularly unlock a post. It can be anything. Set the relevant vars for universal button function:
$button_args['direct_unlock'] = $args['direct_unlock'];
$button_args['redirect'] = $args['redirect'];
}
if ( is_feed() ) {
$button_args['is_feed'] = true;
}
$universal_button = self::patreonMakeUniversalButton( $patreon_level, false, false, false, $button_args );
$universal_button = apply_filters( 'ptrn/final_state_universal_button', '<div class="patreon-universal-button">' . $universal_button . '</div>', $patreon_level, $post );
$text_over_universal_button = apply_filters( 'ptrn/final_state_label_over_universal_button', self::getLabelOverUniversalButton( $patreon_level, $args ), $patreon_level, $post );
$text_under_universal_button = apply_filters( 'ptrn/final_state_label_under_universal_button', self::getLabelUnderUniversalButton( $patreon_level, false, false, $args ), $patreon_level, $post );
if ( is_feed() ) {
$text_under_universal_button = PATREON_FEED_ACTION_TEXT;
}
// Wrap all of them in a responsive div
$campaign_banner = '<div class="patreon-campaign-banner">'.
$contribution_required.
'<div class="patreon-patron-button-wrapper">'.
'<div class="patreon-text-over-button">'.
$text_over_universal_button.
'</div>'.
$universal_button.
'<div class="patreon-text-under-button">'.
$text_under_universal_button.
'</div>'.
'</div>'.
'</div>';
// This extra button is solely here to test whether new wrappers cause any design issues in different themes. For easy comparison with existing unwrapped button. Remove when confirmed.
$campaign_banner = apply_filters( 'ptrn/campaign_banner', $campaign_banner, $patreon_level, $post );
return $campaign_banner;
}
}
public static function getLabelOverUniversalButton( $patreon_level, $args = false ) {
$label = PATREON_TEXT_OVER_BUTTON_1;
$user_logged_into_patreon = self::isUserLoggedInPatreon();
$is_patron = Patreon_Wordpress::isPatron( wp_get_current_user() );
$messages = self::processPatreonMessages();
$user = wp_get_current_user();
$declined = Patreon_Wordpress::checkDeclinedPatronage( $user );
$user_patronage = Patreon_Wordpress::getUserPatronage();
// Get the creator or page name which is going to be used for interface text
$creator_full_name = self::make_creator_name_for_interface();
// Add 's to make it "creator's Patreon" when placed into label
$creator_full_name .= "'s";
// Override entire text if the creator set a custom site/creator name string:
$patreon_custom_page_name = get_option( 'patreon-custom-page-name', false );
if ( $patreon_custom_page_name AND $patreon_custom_page_name != '' ) {
$creator_full_name = $patreon_custom_page_name;
}
// Get lock or not details if it is not given. If post id given, use it.
if ( !isset( $args['lock'] ) ) {
if ( isset( $args['post_id'] ) ) {
$lock_or_not = Patreon_Wordpress::lock_or_not( $args['post_id'] );
}
else {
$lock_or_not = Patreon_Wordpress::lock_or_not();
}
}
// If lock or not details were not given, merge the incoming args with what we just got.
if( $args AND is_array( $args ) AND !isset( $args['lock'] ) ) {
$args = $lock_or_not + $args;
}
// If no args given, just feed lock or not:
if ( !isset( $args ) ) {
$args = $lock_or_not;
}
$post_id = false;
if ( isset( $args['post_id'] ) ) {
$post = get_post( $args['post_id'] );
}
else {
global $post;
}
if ( $args['reason'] == 'user_not_logged_in' ) {
$label = PATREON_TEXT_OVER_BUTTON_1;
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
$label = PATREON_TEXT_OVER_BUTTON_7;
}
if( isset( $args['post_total_patronage_level'] ) AND $args['post_total_patronage_level'] > 0 ) {
$label = PATREON_TEXT_OVER_BUTTON_12;
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
// Double condition - has both active patron and total patronage conditions. Override text
$label = PATREON_TEXT_OVER_BUTTON_13;
}
}
}
if ( $args['reason'] == 'not_a_patron' ) {
$label = PATREON_TEXT_OVER_BUTTON_1;
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
$label = PATREON_TEXT_OVER_BUTTON_7;
}
if( isset( $args['post_total_patronage_level'] ) AND $args['post_total_patronage_level'] > 0 ) {
$label = PATREON_TEXT_OVER_BUTTON_9;
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
// Double condition - has both active patron and total patronage conditions. Override text
$label = PATREON_TEXT_OVER_BUTTON_10;
}
}
}
if ( $args['reason'] == 'payment_declined' ) {
$label = PATREON_TEXT_OVER_BUTTON_3;
}
if ( $args['reason'] == 'active_pledge_not_enough' ) {
$label = PATREON_TEXT_OVER_BUTTON_1A;
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
$label = PATREON_TEXT_OVER_BUTTON_8;
}
if( isset( $args['post_total_patronage_level'] ) AND $args['post_total_patronage_level'] > 0 ) {
$label = PATREON_TEXT_OVER_BUTTON_9;
// Double condition - override the text
if ( isset( $args['patreon_active_patrons_only'] ) AND $args['patreon_active_patrons_only'] == 1 ) {
$label = PATREON_TEXT_OVER_BUTTON_10;
}
}
}
if ( $args['reason'] == 'not_active_patron_at_post_date' ) {
$label = PATREON_TEXT_OVER_BUTTON_8;
if( isset( $args['post_total_patronage_level'] ) AND $args['post_total_patronage_level'] > 0 ) {
$label = PATREON_TEXT_OVER_BUTTON_14;
}
}
// We init vars so picky users will not see undefined var/index errors:
$post_total_patronage_level = '';
if( isset( $args['post_total_patronage_level'] ) ) {
$post_total_patronage_level = $args['post_total_patronage_level'];
}
// Get creator url
$creator_url = get_option( 'patreon-creator-url', false );
$creator_url = apply_filters( 'ptrn/creator_profile_link_in_text_over_interface', $creator_url );
$utm_content = 'creator_profile_link_in_text_over_interface';
$filterable_utm_params = 'utm_term=&utm_content=' . $utm_content;
$filterable_utm_params = apply_filters( 'ptrn/utm_params_for_creator_profile_link_in_text_over_interface', $filterable_utm_params );
// Set default for content url
$content_url = site_url();
// Override if content url exists
if ( $post ) {
$content_url = get_permalink( $post );
}
$utm_params = 'utm_source=' . urlencode( $content_url ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=' . get_option( 'patreon-campaign-id' ) . '&' . $filterable_utm_params;
// Simple check to see if creator url has ? (for non vanity urls)
$append_with = '?';
if ( strpos( $creator_url, '?' ) !== false ) {
$append_with = '&';
}
$creator_url .= $append_with . $utm_params;
if ( get_option( 'patreon-creator-has-tiers', 'yes' ) == 'yes' ) {
// Get Patreon creator tiers
$tiers = get_option( 'patreon-creator-tiers', false );
foreach( $tiers['included'] as $key => $value ) {
// If its not a reward element, continue, just to make sure
if(
!isset( $tiers['included'][$key]['type'] )
OR ( $tiers['included'][$key]['type'] != 'reward'
AND $tiers['included'][$key]['type'] != 'tier' )
) {
continue;
}
$reward = $tiers['included'][$key];
// Special conditions for label for element 0, which is 'everyone' and '1, which is 'patron only'
if ( $reward['id'] == -1 ) {
$tier_title = PATREON_TEXT_EVERYONE;
}
if ( $reward['id'] == 0 ) {
$tier_title = PATREON_TEXT_ANY_PATRON;
}
if ( ( $reward['attributes']['amount_cents'] / 100 ) >= $patreon_level ) {
// Matching level was present, but now found. Set selected and toggle flag.
// selected = selected for XHTML compatibility
// Use title if it exists, description if it does not.
if ( isset( $reward['attributes']['title'] ) ) {
$tier_title = $reward['attributes']['title'];
}
if ( $tier_title == '' ) {
/// Detect if this is an old non bas64 desc
if (base64_decode($reward['attributes']['description'], true) === false) {
$tier_title = $reward['attributes']['description'];
}
else {
$tier_title = base64_decode( $reward['attributes']['description'] );
}
}
// If the title is too long, snip it
if ( strlen( $tier_title ) > 23 ) {
$tier_title = substr( $tier_title , 0 , 23 ) .'...';
}
$tier_title = '"' . $tier_title . '"';
break;
}
}
}
else {
// Creator has no tiers, possibly lite plan. Override text here.
$tier_title = '$'.$patreon_level;
}
// Exception - when content is locked for 'any patron' and the user is not a patron, the interface text shows $0.01. For this special case, check and manipulate the chosen label to avoid this:
if ( $label == PATREON_TEXT_OVER_BUTTON_1 AND $patreon_level == 0.01 AND !$is_patron ) {
$label = PATREON_TEXT_OVER_BUTTON_15;
}
$label = apply_filters( 'ptrn/label_text_over_universal_button_raw', $label, $args['reason'], $user_logged_into_patreon, $is_patron, $args, $post, $creator_full_name, $patreon_level, $post_total_patronage_level, $creator_url, strip_tags( $tier_title ), self::patreonMakeCacheableFlowLink( $post ) );
$label = str_replace( '%%creator_link%%', $creator_url, $label );
$label = str_replace( '%%creator%%', $creator_full_name, $label );
$label = str_replace( '%%pledgelevel%%', $patreon_level, $label );
$label = str_replace( '%%tier_level%%', strip_tags( $tier_title ), $label );
$label = str_replace( '%%flow_link%%', self::patreonMakeCacheableFlowLink( $post ), $label );
$label = str_replace( '%%total_pledge%%', $post_total_patronage_level, $label );
return $messages . apply_filters( 'ptrn/label_text_over_universal_button', str_replace( '%%pledgelevel%%',$patreon_level, $label ), $args['reason'], $user_logged_into_patreon, $is_patron, $patreon_level, $args );
}
public static function getLabelUnderUniversalButton( $patreon_level, $state = false, $post = false, $args = false ) {
$label = '';
$user_logged_into_patreon = self::isUserLoggedInPatreon();
$is_patron = Patreon_Wordpress::isPatron( wp_get_current_user() );
$messages = self::processPatreonMessages();
$user = wp_get_current_user();
$declined = Patreon_Wordpress::checkDeclinedPatronage( $user );
$user_patronage = Patreon_Wordpress::getUserPatronage();
// Get the creator or page name which is going to be used for interface text
$creator_full_name = self::make_creator_name_for_interface();
// Add 's to make it "creator's Patreon" when placed into label
$creator_full_name .= "'s";
// Override entire text if the creator set a custom site/creator name string:
$patreon_custom_page_name = get_option( 'patreon-custom-page-name', false );
if ( $patreon_custom_page_name AND $patreon_custom_page_name != '' ) {
$creator_full_name = $patreon_custom_page_name;
}
// Get lock or not details if it is not given. If post id given, use it.
if ( !isset( $args['lock'] ) ) {
if ( isset( $args['post_id'] ) ) {
$lock_or_not = Patreon_Wordpress::lock_or_not( $args['post_id'] );
}
else {
$lock_or_not = Patreon_Wordpress::lock_or_not();
}
}
// If lock or not details were not given, merge the incoming args with what we just got.
if( $args AND is_array( $args ) AND !isset( $args['lock'] ) ) {
$args = $lock_or_not + $args;
}
if ( $args['reason'] == 'user_not_logged_in' ) {
$label = PATREON_TEXT_UNDER_BUTTON_2;
}
if ( $args['reason'] == 'not_a_patron' ) {
$label = PATREON_TEXT_UNDER_BUTTON_2;
}
if ( $args['reason'] == 'payment_declined' ) {
$label = PATREON_TEXT_UNDER_BUTTON_3;
}
if ( $args['reason'] == 'active_pledge_not_enough' ) {
$label = PATREON_TEXT_UNDER_BUTTON_2;
}
if ( $args['reason'] == 'not_active_patron_at_post_date' ) {
$label = PATREON_TEXT_UNDER_BUTTON_2;
}
$post_total_patronage_level = '';
if( isset( $args['post_total_patronage_level'] ) ) {
$post_total_patronage_level = $args['post_total_patronage_level'];
}
$flow_link = self::patreonMakeCacheableFlowLink();
// Change with login link if user is not logged in - this gives non logged in patrons an easy way to login/refresh the content without having to go to pledge flow again
if ( $args['reason'] == 'user_not_logged_in' ) {
$flow_link = self::patreonMakeCacheableLoginLink();
}
$label = str_replace( '%%creator%%', $creator_full_name, $label );
$label = str_replace( '%%pledgelevel%%', $patreon_level, $label );
$label = str_replace( '%%flow_link%%', $flow_link, $label );
$label = str_replace( '%%total_pledge%%', $post_total_patronage_level, $label );
return apply_filters( 'ptrn/label_text_under_universal_button', $label, $args['reason'], $user_logged_into_patreon, $is_patron, $patreon_level, $state, $args);
}
public static function showPatreonMessages() {
echo self::processPatreonMessages();
}
public static function processPatreonMessages() {
$patreon_error = '';
if ( isset( $_REQUEST['patreon_error'] ) ) {
// If any specific error message is sent from Patreon, prepare it
$patreon_error = ' - Patreon returned: ' . preg_replace("/[^A-Za-z0-9 ]/", '', $_REQUEST['patreon_error'] );
}
if ( isset( $_REQUEST['patreon_message'] ) ) {
return '<p class="patreon_message">' . apply_filters( 'ptrn/error_message', self::$messages_map[ $_REQUEST['patreon_message'] ] . $patreon_error ) . '</p>';
}
if ( isset( $GLOBALS['patreon_notice'] ) ) {
return '<p class="patreon_message">' . apply_filters( 'ptrn/patreon_notice', $GLOBALS['patreon_notice'] ).'</p>';
}
return '';
}
public static function patreonMakeUniversalButton( $min_cents = false, $state = false, $post = false, $client_id = false, $args = false ) {
// This very customizable function takes numerous parameters to customize universal flow links and creates the desired link
// If no post is given, get the active post:
if ( !$post ) {
global $post;
}
// If it is a direct unlock request, unset the post
if ( isset( $args['direct_unlock'] ) ) {
$post = false;
// Set the post to the id if it is given:
if ( isset( $args['post_id'] ) AND $args['post_id'] != '' ) {
$post = get_post( $args['post_id'] );
}
}
$send_pledge_level = 1;
if ( $min_cents ) {
$send_pledge_level = $min_cents * 100;;
}
if ( !$client_id ) {
$client_id = get_option( 'patreon-client-id', false );
}
// If we werent given any state vars to send, initialize the array
if ( !$state ) {
$state = array();
}
// Get the address of the current page, and save it as final redirect uri.
// Start with home url for redirect. If post is valid, get permalink.
$final_redirect = home_url();
if ( $post ) {
$final_redirect = get_permalink( $post->ID );
}
$state['final_redirect_uri'] = $final_redirect;
// $href = self::MakeUniversalFlowLink($send_pledge_level,$state,$client_id);
// We changed the above universal flow link maker to a function which will create cache-able links
// Some of the vars in current function which the earlier function used may not be needed now - clean up later #REVISIT
$flow_link_args = array();
if ( isset( $args['direct_unlock'] ) ) {
// If direct unlock request is given, set cacheable flow link vars.
$flow_link_args['direct_unlock'] = $args['direct_unlock'];
$flow_link_args['redirect'] = $args['redirect'];
if ( isset( $args['post_id'] ) ) {
$flow_link_args['post_id'] = $args['post_id'];
}
}
$href = self::patreonMakeCacheableFlowLink( $post, $flow_link_args );
$label_text = self::patreonMakeUniversalButtonLabel();
$button = self::patreonMakeUniversalButtonImage( $label_text );
if ( isset( $args['is_feed'] ) ) {
return '';
}
return apply_filters( 'ptrn/patron_button', '<a href="' . $href . '">' . $button . '</a>', $min_cents );
}
public static function patreonMakeCacheableLoginLink() {
global $wp;
$current_url = home_url( $wp->request );
$flow_link = site_url() . '/patreon-flow/?patreon-login=yes&patreon-final-redirect=' . urlencode( $current_url );
return $flow_link;
}
public static function patreonMakeCacheableFlowLink( $post = false, $args = false ) {
if ( !$post ) {
global $post;
}
$unlock_post_id = '';
if ( isset( $post ) AND isset( $post->ID ) ) {
$unlock_post_id = $post->ID;
}
$flow_link = site_url() . '/patreon-flow/?patreon-unlock-post=' . $unlock_post_id;
if ( isset( $args['direct_unlock'] ) ) {
$append_post_id = '';
// If direct unlock request is given, override all :
if( isset( $args['post_id'] ) ) {
$append_post_id = '&patreon-post-id=' . $args['post_id'];
}
$flow_link = site_url() . '/patreon-flow/?patreon-direct-unlock=' . $args['direct_unlock'] . $append_post_id . '&patreon-redirect=' . urlencode( base64_encode( $args['redirect'] ) );
}
return $flow_link;
}
public static function patreonMakeCacheableImageFlowLink( $attachment_id, $post_id = false ) {
if ( !$post_id ) {
global $post;
}
$unlock_post_id = $post_id;
if ( !$unlock_post_id AND ( isset( $post ) AND isset( $post->ID ) ) ) {
$unlock_post_id = $post->ID;
}
$flow_link = site_url() . '/patreon-flow/?patreon-unlock-post=' . $unlock_post_id . '&patreon-unlock-image=' . $attachment_id;
return $flow_link;
}
public static function patreonMakeUniversalButtonImage( $label ) {
return '<div class="patreon-responsive-button-wrapper"><div class="patreon-responsive-button"><img class="patreon_logo" src="' . PATREON_PLUGIN_ASSETS . '/img/patreon-logomark-on-coral.svg" alt="' . $label . '" /> ' . $label . '</div></div>';
}
public static function MakeUniversalFlowLink( $pledge_level, $state = false, $client_id = false, $post = false, $args = false ) {
if ( !$post AND !isset( $args['direct_unlock'] ) ) {
global $post;
}
if ( !$client_id ) {
$client_id = get_option( 'patreon-client-id', false );
}
// If we werent given any state vars to send, initialize the array
if ( !$state ) {
$state = array();
// Get the address of the current page, and save it as final redirect uri.
// Start with home url for redirect. If post is valid, get permalink.
$final_redirect = home_url();
if ( $post ) {
$final_redirect = get_permalink( $post->ID );
}
// We dont want to redirect people to login page. So check if we are there.
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
$final_redirect = site_url();
}
$state['final_redirect_uri'] = $final_redirect;
}
$redirect_uri = site_url() . '/patreon-authorization/';
$v2_params = '&scope=identity%20identity[email]';
$send_post_id = false;
if ( $post ) {
$send_post_id = $post->ID;
}
$pledge_level = apply_filters( 'ptrn/patron_link_pledge_level', $pledge_level, $send_post_id, $args );
$href = 'https://www.patreon.com/oauth2/become-patron?response_type=code&min_cents=' . $pledge_level . '&client_id=' . $client_id . $v2_params . '&redirect_uri=' . $redirect_uri . '&state=' . urlencode( base64_encode( json_encode( $state ) ) );
// 3rd party dev goodie! Apply custom filters so they can manipulate the url:
$href = apply_filters( 'ptrn/patron_link', $href );
$utm_content = 'post_unlock_button';
if ( isset( $args ) AND isset( $args['link_interface_item']) AND $args['link_interface_item'] == 'image_unlock_button' ) {
$utm_content = 'image_unlock_button';
}
if ( isset( $args ) AND isset($args['link_interface_item']) AND $args['link_interface_item'] == 'direct_unlock_button' ) {
$utm_content = 'direct_unlock_button';
}
$filterable_utm_params = 'utm_term=&utm_content=' . $utm_content;
$filterable_utm_params = apply_filters( 'ptrn/utm_params_for_patron_link', $filterable_utm_params );
// Set default for content url
$content_url = site_url();
// Override if content url exists
if ( $post ) {
$content_url = get_permalink( $post );
}
$utm_params = 'utm_source=' . urlencode( $content_url ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=' . get_option( 'patreon-campaign-id' ) . '&' . $filterable_utm_params;
return $href . '&' . $utm_params;
}
public static function patreonMakeUniversalButtonLabel() {
// Default label:
$label = apply_filters( 'ptrn/universal_button_label', PATREON_TEXT_UNLOCK_WITH_PATREON );
$user_logged_into_patreon = self::isUserLoggedInPatreon();
$is_patron = Patreon_Wordpress::isPatron();
// Change this after getting info about which value confirms user's payment is declined. The only different button label is for that condition.
return $label;
}
public static function isUserLoggedInPatreon() {
if ( self::$current_user_logged_into_patreon != -1 ) {
return self::$current_user_logged_into_patreon;
}
$user_logged_into_patreon = false;
if ( is_user_logged_in() ) {
// User is logged into WP. Check if user has valid patreon data :
$user = wp_get_current_user();
if ( $user ) {
// REVISIT - whats below may be a concern - it connects to API to check for valid user for every generation of button. If we could cache it it would be better
$user_response = Patreon_Wordpress::getPatreonUser( $user );
if ( $user_response ) {
// This is a user logged into Patreon.
$user_logged_into_patreon = true;
}
}
}
return self::$current_user_logged_into_patreon = $user_logged_into_patreon;
}
public static function patreonMakeLoginLink( $client_id=false, $state=false, $post=false, $args=false ) {
if ( !$post ) {
global $post;
}
if ( !$client_id ) {
$client_id = get_option( 'patreon-client-id', false );
}
$redirect_uri = site_url() . '/patreon-authorization/';
// If we werent given any state vars to send, initialize the array
if ( !$state ) {
$state = array();
// Get the address of the current page, and save it as final redirect uri.
// Start with home url for redirect. If post is valid, get permalink.
$final_redirect = home_url();
if ( $post ) {
$final_redirect = get_permalink( $post->ID );
}
// We dont want to redirect people to login page. So check if we are there.
if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
$final_redirect = site_url();
}
$state['final_redirect_uri'] = $final_redirect;
}
$redirect_uri = site_url() . '/patreon-authorization/';
$v2_params = '&scope=' . 'identity+' . urlencode( 'identity[email]' );
$href = 'https://www.patreon.com/oauth2/authorize?response_type=code&client_id='
. $client_id . $v2_params
. '&redirect_uri=' . urlencode($redirect_uri)
. '&state=' . urlencode( base64_encode( json_encode( $state ) ) );
$href = apply_filters( 'ptrn/login_link', $href );
$filterable_utm_params = 'utm_term=&utm_content=login_button';
$filterable_utm_params = apply_filters( 'ptrn/utm_params_for_login_link', $filterable_utm_params );
// Set default for content url
$content_url = site_url();
// Override if content url exists
if ( $post ) {
$content_url = get_permalink( $post );
}
$utm_params = 'utm_source=' . urlencode( $content_url ) . '&utm_medium=patreon_wordpress_plugin&utm_campaign=' . get_option( 'patreon-campaign-id' ) . '&' . $filterable_utm_params;
return $href . '&' . $utm_params;
}
public static function patreonMakeLoginButton( $client_id = false ) {
if ( !$client_id ) {
$client_id = get_option( 'patreon-client-id', false );
}
// Check if user is logged in to WP, for determination of label text
// Set login label to default
$login_label = apply_filters( 'ptrn/login_button_label', PATREON_TEXT_CONNECT );
if ( is_user_logged_in() ) {
// User is logged into WP. Check if user has valid patreon data :
$user = wp_get_current_user();
if ( $user ) {
$user_response = Patreon_Wordpress::getPatreonUser( $user );
// ^ REVISIT - whats above may be a concern - it connects to API to check for valid user for every generation of button. If we could cache it it would be better
if ( $user_response ) {
// This is a user logged into Patreon. use refresh text
$login_label = PATREON_TEXT_REFRESH;
}
}
}
$href = self::patreonMakeCacheableLoginLink();
return apply_filters( 'ptrn/login_button', '<a href="' . $href . '" class="ptrn-login"><div class="patreon-responsive-button-wrapper"><div class="patreon-responsive-button"><img class="patreon_logo" src="' . PATREON_PLUGIN_ASSETS . '/img/patreon-logomark-on-coral.svg" alt=""> ' . $login_label . '</div></div></a>', $href );
}
public static function lock_this_post( $post = false ) {
if ( !$post ) {
global $post;
}
// Just bail out if this is not the main query for content
if ( !is_main_query() ) {
return false;
}
$post_types = get_post_types( array( 'public'=>true ), 'names' );
if ( in_array( get_post_type( $post ), $post_types ) ) {
$exclude = array(
);
// Enables 3rd party plugins to modify the post types excluded from locking
$exclude = apply_filters( 'ptrn/filter_excluded_posts', $exclude );
if ( in_array( get_post_type( $post ), $exclude ) ) {
return false;
}
// First check if entire site is locked, get the level for locking.
$patreon_level = get_option( 'patreon-lock-entire-site', false );
// Check if specific level is given for this post:
$post_level = get_post_meta( $post->ID, 'patreon-level', true );
// get post meta returns empty if no value is found. If so, set the value to 0.
if ( $post_level == '' ) {
$post_level = 0;
}
// Check if both post level and site lock level are set to 0 or nonexistent. If so return normal content.
if( $post_level == 0
&& ( !$patreon_level
|| $patreon_level == 0 )
) {
return false;
}
// If we are at this point, then this post is protected.
// Below define can be defined in any plugin to bypass core locking function and use a custom one from plugin
// It is independent of the plugin load order since it checks if it is defined.
// It can be defined by any plugin until right before the_content filter is run.
if ( apply_filters( 'ptrn/bypass_filtering', defined( 'PATREON_BYPASS_FILTERING' ) ) ) {
return false;
}
if ( current_user_can( 'manage_options' ) ) {
// Here we need to put a notification to admins so they will know they can see the content because they are admin_login_with_patreon_disabled
return false;
}
// Passed checks. If post level is not 0, override patreon level and hence site locking value with post's. This will allow Creators to lock entire site and then set a different value for individual posts for access. Ie, site locking is $5, but one particular post can be $10, and it will require $10 to see.
if ( $post_level != 0 ) {
$patreon_level = $post_level;
}
$user = wp_get_current_user();
$user_pledge_relationship_start = Patreon_Wordpress::get_user_pledge_relationship_start();
$user_patronage = Patreon_Wordpress::getUserPatronage();
$user_lifetime_patronage = Patreon_Wordpress::get_user_lifetime_patronage();
$declined = Patreon_Wordpress::checkDeclinedPatronage($user);