-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathclass-reader-activation.php
More file actions
2969 lines (2681 loc) · 106 KB
/
class-reader-activation.php
File metadata and controls
2969 lines (2681 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
/**
* Reader Activation (publicly rebranded as "Audience Management").
*
* @package Newspack
*/
namespace Newspack;
use Newspack\Recaptcha;
use Newspack\Reader_Activation\Sync;
use Newspack\Renewal;
use Newspack\WooCommerce_My_Account;
defined( 'ABSPATH' ) || exit;
/**
* Reader Activation Class.
*/
final class Reader_Activation {
const OPTIONS_PREFIX = 'newspack_reader_activation_';
const AUTH_READER_COOKIE = 'np_auth_reader';
const AUTH_INTENTION_COOKIE = 'np_auth_intention';
const SCRIPT_HANDLE = 'newspack-reader-activation';
const AUTH_SCRIPT_HANDLE = 'newspack-reader-auth';
const NEWSLETTERS_SCRIPT_HANDLE = 'newspack-newsletters-signup';
/**
* Reader user meta keys.
*/
const READER = 'np_reader';
const EMAIL_VERIFIED = 'np_reader_email_verified';
const WITHOUT_PASSWORD = 'np_reader_without_password';
const REGISTRATION_METHOD = 'np_reader_registration_method';
const CONNECTED_ACCOUNT = 'np_reader_connected_account';
const READER_SAVED_GENERIC_DISPLAY_NAME = 'np_reader_saved_generic_display_name';
/**
* Unverified email rate limiting
*/
const LAST_EMAIL_DATE = 'np_reader_last_email_date';
const EMAIL_INTERVAL = 10; // 10 seconds
/**
* Auth form.
*/
const AUTH_FORM_ACTION = 'reader-activation-auth-form';
const AUTH_FORM_OPTIONS = [
'signin',
'register',
'link',
'pwd',
];
/**
* Registration methods that don't require account verification.
*/
const SSO_REGISTRATION_METHODS = [ 'google' ];
/**
* OAuth routes to intercept for RAS login redirection.
*/
const OAUTH_REDIRECT_ROUTES = [ '/oauth/authorize' ];
/**
* Newsletters signup form.
*/
const NEWSLETTERS_SIGNUP_FORM_ACTION = 'reader-activation-newsletters-signup';
/**
* Whether the session is authenticating a newly registered reader
*
* @var bool
*/
private static $is_new_reader_auth = false;
/**
* UI labels for reader activation flows.
*
* @var mixed[]
*/
private static $reader_activation_labels = [];
/**
* Initialize hooks.
*/
public static function init() {
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
\add_action( 'wp_footer', [ __CLASS__, 'render_auth_modal' ] );
\add_action( 'wp_footer', [ __CLASS__, 'render_newsletters_signup_modal' ] );
\add_action( 'wp_ajax_newspack_reader_activation_newsletters_signup', [ __CLASS__, 'newsletters_signup' ] );
\add_action( 'woocommerce_customer_reset_password', [ __CLASS__, 'login_after_password_reset' ] );
if ( self::is_enabled() ) {
\add_action( 'rest_api_init', [ __CLASS__, 'register_routes' ] );
\add_action( 'clear_auth_cookie', [ __CLASS__, 'clear_auth_intention_cookie' ] );
\add_action( 'clear_auth_cookie', [ __CLASS__, 'clear_auth_reader_cookie' ] );
\add_action( 'set_auth_cookie', [ __CLASS__, 'clear_auth_intention_cookie' ] );
\add_filter( 'login_form_defaults', [ __CLASS__, 'add_auth_intention_to_login_form' ], 20 );
\add_action( 'wp_login', [ __CLASS__, 'login_set_reader_cookie' ], 10, 2 );
\add_action( 'resetpass_form', [ __CLASS__, 'set_reader_verified' ] );
\add_action( 'password_reset', [ __CLASS__, 'set_reader_verified' ] );
\add_action( 'password_reset', [ __CLASS__, 'set_reader_has_password' ] );
\add_action( 'profile_update', [ __CLASS__, 'maybe_set_reader_has_password' ], 10, 3 );
\add_action( 'newspack_magic_link_authenticated', [ __CLASS__, 'set_reader_verified' ] );
\add_action( 'auth_cookie_expiration', [ __CLASS__, 'auth_cookie_expiration' ], 10, 3 );
\add_action( 'init', [ __CLASS__, 'setup_nav_menu' ] );
\add_action( 'wc_get_template', [ __CLASS__, 'replace_woocommerce_auth_form' ], 10, 2 );
\add_action( 'template_redirect', [ __CLASS__, 'process_auth_form' ] );
\add_filter( 'woocommerce_new_customer_data', [ __CLASS__, 'canonize_user_data' ] );
\add_filter( 'wp_pre_insert_user_data', [ __CLASS__, 'validate_user_data' ], 10, 4 );
\add_filter( 'woocommerce_add_error', [ __CLASS__, 'better_display_name_error' ] );
\add_filter( 'amp_native_post_form_allowed', '__return_true' );
\add_filter( 'woocommerce_email_actions', [ __CLASS__, 'disable_woocommerce_new_user_email' ] );
\add_filter( 'retrieve_password_notification_email', [ __CLASS__, 'password_reset_configuration' ], 10, 4 );
\add_action( 'lostpassword_post', [ __CLASS__, 'set_password_reset_mail_content_type' ] );
\add_filter( 'lostpassword_errors', [ __CLASS__, 'rate_limit_lost_password' ], 10, 2 );
\add_filter( 'newspack_esp_sync_contact', [ __CLASS__, 'set_mailchimp_sync_contact_status' ], 10, 2 );
\add_filter( 'login_url', [ __CLASS__, 'redirect_oauth_to_ras_login' ], 10, 3 );
/**
* If RAS is enabled, we assume that any user created by Woo is a reader, without a password and unverified.
*/
\add_action( 'woocommerce_created_customer', [ __CLASS__, 'add_default_reader_metadata' ] );
\add_action( 'woocommerce_payment_complete', [ __CLASS__, 'verify_reader_account' ] );
}
}
/**
* Enqueue front-end scripts.
*/
public static function enqueue_scripts() {
$authenticated_email = self::get_logged_in_reader_email_address();
$script_dependencies = [];
$script_data = [
'auth_intention_cookie' => self::AUTH_INTENTION_COOKIE,
'cid_cookie' => NEWSPACK_CLIENT_ID_COOKIE_NAME,
'is_logged_in' => \is_user_logged_in(),
'authenticated_email' => $authenticated_email,
'otp_auth_action' => Magic_Link::OTP_AUTH_ACTION,
'otp_rate_interval' => Magic_Link::RATE_INTERVAL,
'auth_action_result' => Magic_Link::AUTH_ACTION_RESULT,
'account_url' => function_exists( 'wc_get_account_endpoint_url' ) ? \wc_get_account_endpoint_url( 'dashboard' ) : '',
'is_ras_enabled' => self::is_enabled(),
];
if ( Recaptcha::can_use_captcha() ) {
$recaptcha_version = Recaptcha::get_setting( 'version' );
$script_dependencies[] = Recaptcha::SCRIPT_HANDLE;
if ( 'v3' === $recaptcha_version ) {
$script_data['captcha_site_key'] = Recaptcha::get_site_key();
}
}
Newspack::load_common_assets();
/**
* Reader Activation Frontend Library.
*/
\wp_enqueue_script(
self::SCRIPT_HANDLE,
Newspack::plugin_url() . '/dist/reader-activation.js',
$script_dependencies,
NEWSPACK_PLUGIN_VERSION,
[
'strategy' => 'async',
'in_footer' => true,
]
);
\wp_localize_script(
self::SCRIPT_HANDLE,
'newspack_ras_config',
$script_data
);
\wp_script_add_data( self::SCRIPT_HANDLE, 'async', true );
\wp_script_add_data( self::SCRIPT_HANDLE, 'amp-plus', true );
/**
* Filters whether to enqueue the reader auth scripts.
*
* @param bool $allow_reg_block_render Whether to allow the registration block to render.
*/
if ( apply_filters( 'newspack_reader_activation_should_render_auth', true ) ) {
/**
* Reader Authentication
*/
\wp_enqueue_script(
self::AUTH_SCRIPT_HANDLE,
Newspack::plugin_url() . '/dist/reader-auth.js',
[ self::SCRIPT_HANDLE ],
NEWSPACK_PLUGIN_VERSION,
[
'strategy' => 'async',
'in_footer' => true,
]
);
\wp_localize_script( self::AUTH_SCRIPT_HANDLE, 'newspack_reader_activation_labels', self::get_reader_activation_labels() );
\wp_script_add_data( self::AUTH_SCRIPT_HANDLE, 'async', true );
\wp_script_add_data( self::AUTH_SCRIPT_HANDLE, 'amp-plus', true );
\wp_enqueue_style(
self::AUTH_SCRIPT_HANDLE,
Newspack::plugin_url() . '/dist/reader-auth.css',
[],
NEWSPACK_PLUGIN_VERSION
);
}
if ( self::is_newsletters_signup_available() ) {
/**
* Newsletters Signup.
*/
\wp_enqueue_script(
self::NEWSLETTERS_SCRIPT_HANDLE,
Newspack::plugin_url() . '/dist/newsletters-signup.js',
[ self::SCRIPT_HANDLE ],
NEWSPACK_PLUGIN_VERSION,
[
'strategy' => 'async',
'in_footer' => true,
]
);
\wp_localize_script(
self::NEWSLETTERS_SCRIPT_HANDLE,
'newspack_reader_activation_newsletters',
[
'newspack_ajax_url' => admin_url( 'admin-ajax.php' ),
]
);
\wp_script_add_data( self::NEWSLETTERS_SCRIPT_HANDLE, 'async', true );
\wp_enqueue_style(
self::NEWSLETTERS_SCRIPT_HANDLE,
Newspack::plugin_url() . '/dist/newsletters-signup.css',
[],
NEWSPACK_PLUGIN_VERSION
);
}
}
/**
* Register routes.
*/
public static function register_routes() {
\register_rest_route(
NEWSPACK_API_NAMESPACE,
'/reader-newsletter-signup-lists/(?P<email_address>[\a-z]+)',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'api_render_newsletters_signup_form' ],
'permission_callback' => '__return_true',
'args' => [
'email_address' => [
'type' => 'string',
'sanitize_callback' => 'sanitize_email',
],
],
]
);
}
/**
* Get labels for reader activation flows.
*
* @param string|null $key Key of the label to return (optional).
*
* @return mixed[]|string The label string or an array of labels keyed by string.
*/
private static function get_reader_activation_labels( $key = null ) {
if ( empty( self::$reader_activation_labels ) ) {
$default_labels = [
'title' => __( 'Sign in', 'newspack-plugin' ),
'invalid_email' => __( 'Please enter a valid email address.', 'newspack-plugin' ),
'invalid_password' => __( 'Please enter a password.', 'newspack-plugin' ),
'invalid_display' => __( 'Display name cannot match your email address. Please choose a different display name.', 'newspack-plugin' ),
'blocked_popup' => __( 'The popup has been blocked. Allow popups for the site and try again.', 'newspack-plugin' ),
'code_sent' => __( 'Code sent! Check your inbox.', 'newspack-plugin' ),
'code_resent' => __( 'Code resent! Check your inbox.', 'newspack-plugin' ),
'create_account' => __( 'Create an account', 'newspack-plugin' ),
'signin' => [
'title' => __( 'Sign in', 'newspack-plugin' ),
'success_title' => __( 'Success! You’re signed in.', 'newspack-plugin' ),
'success_message' => __( 'Login successful!', 'newspack-plugin' ),
'continue' => __( 'Continue', 'newspack-plugin' ),
'resend_code' => __( 'Resend code', 'newspack-plugin' ),
'otp' => __( 'Email me a one-time code instead', 'newspack-plugin' ),
'otp_title' => __( 'Enter the code sent to your email', 'newspack-plugin' ),
'forgot_password' => __( 'Forgot password', 'newspack-plugin' ),
'create_account' => __( 'Create an account', 'newspack-plugin' ),
'register' => __( 'Sign in to an existing account', 'newspack-plugin' ),
'go_back' => __( 'Go back', 'newspack-plugin' ),
'set_password' => __( 'Set a password (optional)', 'newspack-plugin' ),
],
'register' => [
'title' => __( 'Create an account', 'newspack-plugin' ),
'success_title' => __( 'Success! Your account was created and you’re signed in.', 'newspack-plugin' ),
'success_description' => __( 'In the future, you’ll sign in with a magic link, or a code sent to your email. If you’d rather use a password, you can set one below.', 'newspack-plugin' ),
],
'verify' => __( 'Thank you for verifying your account!', 'newspack-plugin' ),
'magic_link' => __( 'Please check your inbox for an authentication link.', 'newspack-plugin' ),
'password_reset_interval' => __( 'Please wait a moment before requesting another password reset email.', 'newspack-plugin' ),
'account_link' => [
'signedin' => __( 'My Account', 'newspack-plugin' ),
'signedout' => __( 'Sign In', 'newspack-plugin' ),
],
'newsletters_cta' => __( 'Subscribe to our newsletter', 'newspack-plugin' ),
'newsletters_confirmation' => sprintf(
// Translators: %s is the site name.
__( 'Thanks for supporting %s.', 'newspack-plugin' ),
get_option( 'blogname' )
),
'newsletters_continue' => __( 'Continue', 'newspack-plugin' ),
'newsletters_details' => sprintf(
// Translators: %s is the site name.
__( 'Get the best of %s directly in your email inbox.', 'newspack-plugin' ),
get_bloginfo( 'name' )
),
'newsletters_success' => __( 'Signup successful!', 'newspack-plugin' ),
'newsletters_title' => __( 'Sign up for newsletters', 'newspack-plugin' ),
'auth_form_action' => self::AUTH_FORM_ACTION,
// Subscription tiers labels.
'sign_in_to_upgrade' => __( 'Sign in to upgrade', 'newspack-plugin' ),
'register_to_upgrade' => __( 'Register to upgrade', 'newspack-plugin' ),
];
/**
* Filters the global labels for reader activation auth flow.
*
* @param mixed[] $labels Labels keyed by name.
*/
$filtered_labels = apply_filters( 'newspack_reader_activation_auth_labels', $default_labels );
foreach ( $default_labels as $key => $label ) {
if ( isset( $filtered_labels[ $key ] ) ) {
if ( is_array( $label ) && is_array( $filtered_labels[ $key ] ) ) {
self::$reader_activation_labels[ $key ] = array_merge( $label, $filtered_labels[ $key ] );
} elseif ( is_string( $label ) && is_string( $filtered_labels[ $key ] ) ) {
self::$reader_activation_labels[ $key ] = $filtered_labels[ $key ];
} else {
// If filtered label type doesn't match, fallback to default.
self::$reader_activation_labels[ $key ] = $label;
}
} else {
self::$reader_activation_labels[ $key ] = $label;
}
}
}
if ( ! $key ) {
return self::$reader_activation_labels;
}
return self::$reader_activation_labels[ $key ] ?? '';
}
/**
* Get settings config with default values.
*
* @return mixed[] Settings default values keyed by their name.
*/
private static function get_settings_config() {
$settings_config = [
'enabled' => false,
'enabled_account_link' => true,
'account_link_menu_locations' => [ 'tertiary-menu' ],
'newsletters_label' => self::get_reader_activation_labels( 'newsletters_cta' ),
'use_custom_lists' => false,
'newsletter_lists' => [],
'newsletter_list_initial_size' => self::get_newsletters_list_initial_size(),
'terms_text' => '',
'terms_url' => '',
'sync_esp' => true,
'metadata_prefix' => Sync\Metadata::get_prefix(),
'metadata_fields' => Sync\Metadata::get_fields(),
'sync_esp_delete' => true,
'active_campaign_master_list' => '',
'constant_contact_list_id' => '',
'mailchimp_audience_id' => '',
'mailchimp_reader_default_status' => 'transactional',
'sender_name' => Emails::get_from_name(),
'sender_email_address' => Emails::get_from_email(),
'contact_email_address' => Emails::get_reply_to_email(),
'woocommerce_registration_required' => false,
'woocommerce_checkout_privacy_policy_text' => self::get_checkout_privacy_policy_text(),
'woocommerce_post_checkout_success_text' => self::get_post_checkout_success_text(),
'woocommerce_post_checkout_registration_success_text' => self::get_post_checkout_registration_success_text(),
'woocommerce_enable_subscription_confirmation' => false,
'woocommerce_subscription_confirmation_text' => self::get_subscription_confirmation_text(),
'woocommerce_enable_terms_confirmation' => false,
'woocommerce_terms_confirmation_text' => self::get_terms_confirmation_text(),
'woocommerce_terms_confirmation_url' => self::get_terms_confirmation_url(),
'oauth_redirect_to_ras' => false,
];
/**
* Filters the global settings config for reader activation.
*
* @param mixed[] $settings_config Settings default values keyed by their name.
*/
return apply_filters( 'newspack_reader_activation_settings_config', $settings_config );
}
/**
* Get reader activation global settings.
*
* @return mixed[] Global settings keyed by their option name.
*/
public static function get_settings() {
$config = self::get_settings_config();
$settings = [];
foreach ( $config as $key => $default_value ) {
$settings[ $key ] = self::get_setting( $key );
}
return $settings;
}
/**
* Get a setting value.
*
* @param string $name Setting name.
*
* @return mixed Setting value.
*/
public static function get_setting( $name ) {
$config = self::get_settings_config();
if ( ! isset( $config[ $name ] ) ) {
return null;
}
$value = \get_option( self::OPTIONS_PREFIX . $name, $config[ $name ] );
// Use default value type for casting bool option value.
if ( is_bool( $config[ $name ] ) ) {
$value = (bool) $value;
}
return apply_filters( 'newspack_reader_activation_setting', $value, $name );
}
/**
* Update a setting value.
*
* @param string $key Option name.
* @param mixed $value Option value.
*
* @return bool True if the value was updated, false otherwise.
*/
public static function update_setting( $key, $value ) {
$config = self::get_settings_config();
if ( ! isset( $config[ $key ] ) ) {
return false;
}
if ( is_bool( $value ) ) {
$value = intval( $value );
}
/**
* Fires just before a setting is updated
*
* @param string $key Option name.
* @param mixed $value Option value.
*/
do_action( 'newspack_reader_activation_update_setting', $key, $value );
if ( 'metadata_prefix' === $key ) {
return Sync\Metadata::update_prefix( $value );
}
if ( 'metadata_fields' === $key ) {
return Sync\Metadata::update_fields( $value );
}
return \update_option( self::OPTIONS_PREFIX . $key, $value );
}
/**
* Activate RAS features and publish RAS prompts + segments.
*/
public static function activate() {
if ( ! method_exists( 'Newspack_Popups_Presets', 'activate_ras_presets' ) ) {
return new \WP_Error( 'newspack_reader_activation_missing_dependencies', __( 'Newspack Campaigns plugin is required to activate Reader Activation features.', 'newspack-plugin' ) );
}
$activated = \Newspack_Popups_Presets::activate_ras_presets();
if ( $activated ) {
self::skip( 'ras_campaign', false );
}
return $activated;
}
/**
* Check if the required Woo plugins are active.
*
* @return boolean True if all required plugins are active, otherwise false.
*/
public static function is_woocommerce_active() {
$is_active = Donations::is_woocommerce_suite_active();
if ( \is_wp_error( $is_active ) ) {
return false;
}
return $is_active;
}
/**
* Is the Newspack Newsletters plugin configured with an ESP?
*/
public static function is_esp_configured() {
$newsletters_configuration_manager = Configuration_Managers::configuration_manager_class_for_plugin_slug( 'newspack-newsletters' );
if ( ! $newsletters_configuration_manager->is_esp_set_up() ) {
return false;
}
$lists = $newsletters_configuration_manager->get_enabled_lists();
if ( empty( $lists ) || ! is_array( $lists ) ) {
return false;
}
// Can be considered fully configured if the ESP is setup and there's at least one active list.
foreach ( $lists as $list ) {
if ( $list['active'] ) {
return true;
}
}
return false;
}
/**
* Get the master list ID for the ESP.
*
* @param string $provider Optional ESP provider. Defaults to the configured ESP.
*
* @return string|bool Master list ID or false if not set or not available.
*/
public static function get_esp_master_list_id( $provider = '' ) {
if ( empty( $provider ) && class_exists( 'Newspack_Newsletters' ) ) {
$provider = \Newspack_Newsletters::service_provider();
}
switch ( $provider ) {
case 'active_campaign':
return self::get_setting( 'active_campaign_master_list' );
case 'constant_contact':
return self::get_setting( 'constant_contact_list_id' );
case 'mailchimp':
$audience_id = self::get_setting( 'mailchimp_audience_id' );
/** Attempt to use list ID from "Mailchimp for WooCommerce" */
if ( ! $audience_id && function_exists( 'mailchimp_get_list_id' ) ) {
$audience_id = \mailchimp_get_list_id();
}
return ! empty( $audience_id ) ? $audience_id : false;
default:
return false;
}
}
/**
* Set the contact metadata status for Mailchimp.
*
* @param array $contact The contact data to sync.
*
* @return array Modified contact data.
*/
public static function set_mailchimp_sync_contact_status( $contact ) {
$allowed_statuses = [
'transactional',
'subscribed',
];
$default_status = self::get_setting( 'mailchimp_reader_default_status' );
$status = in_array( $default_status, $allowed_statuses, true ) ? $default_status : 'transactional';
$contact['metadata']['status_if_new'] = $status;
return $contact;
}
/**
* Get the newsletter lists that should be rendered during registration.
*
* @return array
*/
public static function get_registration_newsletter_lists() {
if ( ! class_exists( '\Newspack_Newsletters_Subscription' ) ) {
return [];
}
$registration_lists = self::get_available_newsletter_lists();
/**
* Filters the newsletters lists that should be rendered during registration.
*
* @param array $registration_lists Array of newsletter lists.
*/
return apply_filters( 'newspack_registration_newsletters_lists', $registration_lists );
}
/**
* Get the newsletter lists that should be rendered after checkout.
*
* @param string $email_address Email address. Optional.
*
* @return array
*/
public static function get_post_checkout_newsletter_lists( $email_address = '' ) {
$available_lists = self::get_available_newsletter_lists( $email_address );
$registration_lists = [];
if ( empty( $available_lists ) ) {
return [];
}
foreach ( $available_lists as $list_id => $list ) {
$registration_lists[ $list_id ] = $list;
}
/**
* Filters the newsletters lists that should be rendered after checkout.
*
* @param array $registration_lists Array of newsletter lists.
* @param string $email_address Email address.
*/
return apply_filters( 'newspack_post_registration_newsletters_lists', $registration_lists, $email_address );
}
/**
* Get all available newsletter lists.
*
* @param string $email_address Email address. Optional.
*
* @return array
*/
public static function get_available_newsletter_lists( $email_address = '' ) {
if ( ! method_exists( 'Newspack_Newsletters_Subscription', 'get_lists' ) ) {
return [];
}
$use_custom_lists = self::get_setting( 'use_custom_lists' );
$available_lists = \Newspack_Newsletters_Subscription::get_lists_config();
$registration_lists = [];
if ( \is_wp_error( $available_lists ) ) {
return [];
}
if ( ! $use_custom_lists ) {
$registration_lists = $available_lists;
} else {
$lists = self::get_setting( 'newsletter_lists' );
if ( empty( $lists ) ) {
return [];
}
foreach ( $lists as $list ) {
if ( isset( $available_lists[ $list['id'] ] ) ) {
$registration_lists[ $list['id'] ] = $available_lists[ $list['id'] ];
$registration_lists[ $list['id'] ]['checked'] = $list['checked'] ?? false;
}
}
}
// Filter out any lists the reader is already signed up for if an email address is provided.
if ( $email_address && method_exists( '\Newspack_Newsletters_Subscription', 'get_contact_lists' ) ) {
$current_lists = \Newspack_Newsletters_Subscription::get_contact_lists( $email_address );
if ( ! \is_wp_error( $current_lists ) && is_array( $current_lists ) ) {
$filtered_lists = [];
foreach ( $registration_lists as $list ) {
// Skip any lists the reader is already signed up for.
if ( in_array( $list['id'], $current_lists, true ) ) {
continue;
}
$filtered_lists[ $list['id'] ] = $list;
}
$registration_lists = $filtered_lists;
}
}
/**
* Filters the newsletters lists that should be rendered during registration.
*
* @param array $registration_lists Array of newsletter lists.
*/
return apply_filters( 'newspack_registration_newsletters_lists', $registration_lists );
}
/**
* Are all Reader Revenue features configured and ready to use?
* Platform must be "Newspack" and all donation settings must be configured.
*/
public static function is_reader_revenue_ready() {
$ready = false;
$donation_settings = Donations::get_donation_settings();
if ( \is_wp_error( $donation_settings ) ) {
return $ready;
}
if ( Donations::is_platform_wc() ) {
$ready = true;
} elseif ( Donations::is_platform_nrh() && NRH::get_setting( 'nrh_organization_id' ) && method_exists( '\Newspack_Popups_Settings', 'donor_landing_page' ) && \Newspack_Popups_Settings::donor_landing_page() ) {
$ready = true;
}
return $ready;
}
/**
* Get an array of required plugins for satisfying Reader Revenue prerequisites.
* WooCommerce and Woo Subscriptions are required for Newspack, but not for NRH.
*/
public static function get_reader_revenue_required_plugins() {
$required_plugins = [
'newspack-blocks' => class_exists( '\Newspack_Blocks' ),
];
if ( Donations::is_platform_wc() ) {
$required_plugins['woocommerce'] = class_exists( 'WooCommerce' );
$required_plugins['woocommerce-subscriptions'] = class_exists( 'WCS_Query' );
}
return $required_plugins;
}
/**
* Are the Legal Pages settings configured?
* Allows for blank values.
*
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_terms_configured( $skip = false ) {
$terms_text = \get_option( self::OPTIONS_PREFIX . 'terms_text', false );
$terms_url = \get_option( self::OPTIONS_PREFIX . 'terms_url', false );
$is_valid = is_string( $terms_text ) && is_string( $terms_url );
if ( $skip ) {
return $is_valid || self::is_skipped( 'terms_conditions' );
}
if ( $is_valid ) {
self::skip( 'terms_conditions', false );
}
return $is_valid;
}
/**
* Are Transaction Email settings configured?
*/
public static function is_transactional_email_configured() {
$sender_name = \get_option( self::OPTIONS_PREFIX . 'sender_name', false );
$sender_email = \get_option( self::OPTIONS_PREFIX . 'sender_email_address', false );
$contact_email_address = \get_option( self::OPTIONS_PREFIX . 'contact_email_address', false );
return ! empty( $sender_name ) && ! empty( $sender_email ) && ! empty( $contact_email_address );
}
/**
* Is reCAPTCHA enabled?
*
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_recaptcha_enabled( $skip = false ) {
$is_valid = method_exists( '\Newspack\Recaptcha', 'can_use_captcha' ) && \Newspack\Recaptcha::can_use_captcha();
if ( $skip ) {
return $is_valid || self::is_skipped( 'recaptcha' );
}
if ( $is_valid ) {
self::skip( 'recaptcha', false );
}
return $is_valid;
}
/**
* Is the RAS campaign configured?
*
* @param bool $skip Whether to skip the check.
*
* @return bool
*/
public static function is_ras_campaign_configured( $skip = false ) {
$is_valid = class_exists( 'Newspack_Popups_Presets' ) && get_option( \Newspack_Popups_Presets::NEWSPACK_POPUPS_RAS_LAST_UPDATED, false );
if ( $skip ) {
return $is_valid || self::is_skipped( 'ras_campaign' );
}
if ( $is_valid ) {
self::skip( 'ras_campaign', false );
}
return $is_valid;
}
/**
* Are all prerequisites for Reader Activation complete?
*
* @return bool
*/
public static function is_ras_ready_to_configure() {
$is_ready = self::is_terms_configured( true ) && self::is_esp_configured() && self::is_transactional_email_configured() && self::is_recaptcha_enabled( true ) && self::is_woocommerce_active();
// If all requirements are met or skipped, and RAS isn't yet enabled, enable it.
if ( $is_ready && self::is_ras_campaign_configured( true ) && ! self::is_enabled() ) {
self::update_setting( 'enabled', true );
}
return $is_ready;
}
/**
* Has the given prerequisite been skipped?
*
* @param string $prerequisite The prerequisite to check.
*
* @return bool
*/
public static function is_skipped( $prerequisite ) {
// Legacy option name compabitility.
$legacy_is_skipped = false;
if ( 'ras_campaign' === $prerequisite ) {
$legacy_is_skipped = get_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION, false ) === '1';
}
return boolval( get_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped', $legacy_is_skipped ) );
}
/**
* Skip or unskip the given prerequisite.
*
* @param string $prerequisite The prerequisite to skip.
* @param bool $skip If true, skip the prerequisite. If false, unskip it.
*
* @return bool True if updated, false if not.
*/
public static function skip( $prerequisite, $skip = true ) {
if ( ( $skip && self::is_skipped( $prerequisite ) ) || ( ! $skip && ! self::is_skipped( $prerequisite ) ) ) {
return true;
}
$updated = $skip ? update_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped', '1' ) : delete_option( self::OPTIONS_PREFIX . $prerequisite . '_skipped' );
// Legacy option name compabitility.
if ( 'ras_campaign' === $prerequisite && ! $skip && ! $updated ) {
$updated = delete_option( Audience_Wizard::SKIP_CAMPAIGN_SETUP_OPTION );
}
// If all requirements are met or skipped, and RAS isn't yet enabled, enable it.
if ( $skip && self::is_ras_ready_to_configure() && self::is_ras_campaign_configured( true ) && ! self::is_enabled() ) {
self::update_setting( 'enabled', true );
}
return $updated;
}
/**
* Get the status of the prerequisites for enabling reader activation.
*
* @return array Array of prerequisites to complete.
*/
public static function get_prerequisites_status() {
$prerequisites = [
'terms_conditions' => [
'active' => self::is_terms_configured(),
'label' => __( 'Legal Pages', 'newspack-plugin' ),
'description' => __( 'Displaying legal pages like Privacy Policy and Terms of Service on your site is recommended for allowing readers to register and access their account.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'warning' => __( 'Privacy policies that tell users how you collect and use their data are essential for running a trustworthy website. While rules and regulations can differ by country, certain legal pages might be required by law.', 'newspack-plugin' ),
'fields' => [
'terms_text' => [
'label' => __( 'Legal Pages Disclaimer Text', 'newspack-plugin' ),
'description' => __( 'Legal pages disclaimer text to display on registration.', 'newspack-plugin' ),
],
'terms_url' => [
'label' => __( 'Legal Pages URL', 'newspack-plugin' ),
'description' => __( 'URL to the page containing the privacy policy or terms of service.', 'newspack-plugin' ),
],
],
'skippable' => true,
'is_skipped' => self::is_skipped( 'terms_conditions' ),
],
'esp' => [
'active' => self::is_esp_configured(),
'plugins' => [
'newspack-newsletters' => class_exists( '\Newspack_Newsletters' ),
],
'label' => __( 'Email Service Provider (ESP)', 'newspack-plugin' ),
'description' => __( 'Connect to your ESP to register readers with their email addresses and send newsletters.', 'newspack-plugin' ),
'instructions' => __( 'Connect to your email service provider (ESP) and enable at least one subscription list.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'href' => \admin_url( 'edit.php?post_type=newspack_nl_cpt&page=newspack-newsletters' ),
'action_text' => __( 'ESP settings' ),
],
'emails' => [
'active' => self::is_transactional_email_configured(),
'label' => __( 'Transactional Emails', 'newspack-plugin' ),
'description' => __( 'Your sender name and email address determines how readers find emails related to their account in their inbox. To customize the content of these emails, visit Advanced Settings below.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'fields' => [
'sender_name' => [
'label' => __( 'Sender Name', 'newspack-plugin' ),
'description' => __( 'Name to use as the sender of transactional emails.', 'newspack-plugin' ),
],
'sender_email_address' => [
'label' => __( 'Sender Email Address', 'newspack-plugin' ),
'description' => __( 'Email address to use as the sender of transactional emails.', 'newspack-plugin' ),
],
'contact_email_address' => [
'label' => __( 'Contact Email Address', 'newspack-plugin' ),
'description' => __( 'This email will be used as "Reply-To" for transactional emails as well.', 'newspack-plugin' ),
],
],
],
'recaptcha' => [
'active' => self::is_recaptcha_enabled(),
'label' => __( 'reCAPTCHA', 'newspack-plugin' ),
'description' => __( 'Connecting to a Google reCAPTCHA account enables enhanced anti-spam for all Newspack sign-up blocks.', 'newspack-plugin' ),
'instructions' => __( 'Enable reCAPTCHA and enter your account credentials.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'href' => \admin_url( '/admin.php?page=newspack-settings&scrollTo=newspack-settings-recaptcha' ),
'action_text' => __( 'reCAPTCHA settings' ),
'skippable' => true,
'is_skipped' => self::is_skipped( 'recaptcha' ),
],
'reader_revenue' => [
'active' => self::is_reader_revenue_ready(),
'plugins' => self::get_reader_revenue_required_plugins(),
'label' => __( 'Reader Revenue', 'newspack-plugin' ),
'description' => __( 'Setting suggested donation amounts is required for enabling a streamlined donation experience.', 'newspack-plugin' ),
'instructions' => __( 'Set platform to "Newspack" or "News Revenue Hub" and configure your default donation settings. If using News Revenue Hub, set an Organization ID and a Donor Landing Page in News Revenue Hub Settings.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'href' => \admin_url( '/admin.php?page=newspack-audience#/payment' ),
'action_text' => __( 'Reader Revenue settings' ),
],
'ras_campaign' => [
'active' => self::is_ras_campaign_configured(),
'plugins' => [
'newspack-popups' => class_exists( '\Newspack_Popups_Model' ),
],
'label' => __( 'Audience Management Campaign', 'newspack-plugin' ),
'description' => __( 'Building a set of prompts with default segments and settings allows for an improved experience optimized for audience management.', 'newspack-plugin' ),
'help_url' => 'https://help.newspack.com/engagement/audience-management-system/',
'href' => self::is_ras_campaign_configured() ? admin_url( '/admin.php?page=newspack-audience-campaigns' ) : admin_url( '/admin.php?page=newspack-audience#/campaign' ),
'action_enabled' => self::is_ras_ready_to_configure(),
'action_text' => __( 'Audience Management campaign', 'newspack-plugin' ),
'disabled_text' => __( 'Waiting for all settings to be ready', 'newspack-plugin' ),
'skippable' => true,
'is_skipped' => self::is_skipped( 'ras_campaign' ),
],
];
return $prerequisites;
}
/**
* Whether reader activation features should be enabled.
*
* @return bool True if reader activation is enabled.
*/
public static function is_enabled() {
if ( defined( 'IS_TEST_ENV' ) && IS_TEST_ENV ) {
return true;
}
$is_enabled = (bool) \get_option( self::OPTIONS_PREFIX . 'enabled', false );
/**
* Filters whether reader activation is enabled.
*
* @param bool $is_enabled Whether reader activation is enabled.
*/
return \apply_filters( 'newspack_reader_activation_enabled', $is_enabled );
}
/**
* Whether or not to render the Registration block front-end.
* This must be allowed to render before RAS is enabled in the context of previews.
*
* @return boolean
*/
public static function allow_reg_block_render() {
if ( ! class_exists( '\Newspack_Popups' ) ) {
return self::is_enabled();
}
// If RAS is not enabled yet, allow to render when previewing a campaign prompt.
return self::is_enabled() || ( method_exists( '\Newspack_Popups', 'is_preview_request' ) && \Newspack_Popups::is_preview_request() );
}
/**
* Add auth intention email to login form defaults.
*
* @param array $defaults Login form defaults.
*
* @return array
*/
public static function add_auth_intention_to_login_form( $defaults ) {
$email = self::get_auth_intention_value();
if ( ! empty( $email ) ) {
$defaults['label_username'] = __( 'Email address', 'newspack-plugin' );
$defaults['value_username'] = $email;
}
return $defaults;
}