-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailchef-add-on-for-pmp.php
683 lines (588 loc) · 28.4 KB
/
emailchef-add-on-for-pmp.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
<?php
/**
* Plugin Name: Emailchef Add On for Paid Memberships Pro
* Plugin URI: https://emailchef.com/wordpress-paid-memberships-pro-emailchef-add-on/
* Description: Sync your WordPress users and members with Emailchef lists.
* Author: edisplayit
* Author URI: https://www.emailchef.com
* Version: 1.5.0
* Text Domain: emailchef-add-on-for-pmp
* Domain Path: /languages/
* Requires at least: 6.0
* Tested up to: 6.6
* Requires PHP: 7.0
* License: GPLv2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'PMPROECADDON_DIR', dirname( __FILE__ ) );
require_once( "includes/api_call.php" );
/**
* Add checkbox for plugin
* Add checkbox for plugin
*
* @param $name (string) - The existing link array
*
*/
function pmproecaddon_list_ec_plugin_display( $name ) {
$list_data = get_option( 'pmproecaddon_list_data', '' );
$list_config = get_option( 'pmproecaddon_plugin_list_config', '' );
?>
<td>
<div class="checkbox-container">
<?php foreach ( $list_data as $list ) :
$list_name = str_replace( " ", "_", $list['name'] );
$name_checkbox = $name . '_' . $list_name . "_checkbox";
$is_checked = isset( $list_config[ $name_checkbox ] ) && $list_config[ $name_checkbox ] == $list['id'];
?>
<label class="checkbox-item">
<input
class="input-checkbox"
type="checkbox"
name="<?php echo esc_attr( $name_checkbox ); ?>"
value="<?php echo esc_attr( $list['id'] ); ?>"
<?php echo $is_checked ? 'checked' : ''; ?>
>
<?php echo esc_html( $list['name'] ); ?>
</label>
<?php endforeach; ?>
</div>
</td>
<?php
}
/* MENU */
/**
* Add the admin options page
*
*
*/
function pmproecaddon_admin_add_page() {
add_options_page( 'PMPro Emailchef Options', 'PMPro Emailchef', 'manage_options', 'pmproecaddon_options', 'pmproecaddon_options_page' );
}
add_action( 'admin_menu', 'pmproecaddon_admin_add_page' );
/**
* Add menu to plugin settings
*
*
*/
function pmproecaddon_menu( $links ) {
$new_links = array(
'<a href="' . esc_url( admin_url( 'options-general.php?page=pmproecaddon_options' ) ) . '">' . esc_html__( 'Settings', 'emailchef-add-on-for-pmp' ) . '</a>',
);
return array_merge( $new_links, $links );
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'pmproecaddon_menu' );
add_action( 'admin_post_pmproecaddon_save_data', 'pmproecaddon_save_data' );
function pmproecaddon_reset_options() {
if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pmproecaddon-reset-nonce'] ) ), 'pmproecaddon-reset-nonce' )
&& isset( $_POST['plugin_reset'] )
) {
update_option( 'pmproecaddon_plugin_user_enabled', 'no' );
delete_option( 'pmproecaddon_plugin_user_ec' );
delete_option( 'pmproecaddon_plugin_pass_ec' );
delete_option( 'pmproecaddon_plugin_list_config' );
delete_option( 'pmproecaddon_plugin_list_opt_in_audiences' );
delete_option( 'pmproecaddon_plugin_list_nom_member' );
delete_option( 'pmproecaddon_require_unsuscribe_on_level' );
delete_option( 'pmproecaddon_require_update_profile' );
delete_option( 'pmproecaddon_list_data' ); // Assuming you want to reset this as well
wp_safe_redirect( add_query_arg( 'pmproecaddon_msg', 'reset', wp_get_referer() ) );
exit;
}
}
add_action( 'admin_post_pmproecaddon_reset_options', 'pmproecaddon_reset_options' );
function pmproecaddon_save_data() {
if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pmproecaddon-nonce'] ) ), 'pmproecaddon-nonce' )
&& isset( $_POST['plugin_save'] )
) {
$user_ec = sanitize_email( $_POST['user_ec'] );
$password_ec = sanitize_text_field( $_POST['pass_ec'] );
$logged_in = pmproecaddon_login( $user_ec, $password_ec );
if ( ! $logged_in ) {
update_option( 'pmproecaddon_plugin_user_enabled', 'no' );
wp_safe_redirect( add_query_arg( 'pmproecaddon_msg', 'emailchef_credentials_wrong', wp_get_referer() ) );
exit;
}
update_option( 'pmproecaddon_plugin_user_enabled', 'yes' );
update_option( 'pmproecaddon_plugin_user_ec', $user_ec );
update_option( 'pmproecaddon_plugin_pass_ec', $password_ec );
$list_config = array();
$list_opt_in_audiences = array();
$list_nom_member = array();
$list_data = get_option( 'pmproecaddon_list_data', '' );
if ( $list_data != null ) {
$membership_levels = pmpro_getAllLevels();
if ( ! empty( $membership_levels ) ) {
foreach ( $membership_levels as $membership_level ) {
foreach ( $list_data as $list ) {
$checkbox_name = str_replace( " ", "_", $membership_level->name ) . '_' . str_replace( " ", "_", $list['name'] ) . '_checkbox';
if ( isset( $_POST[ $checkbox_name ] ) ) {
$list_config[ str_replace( " ", "_", $membership_level->name . "_" . str_replace( " ", "_", $list['name'] ) ) ] = sanitize_text_field( $_POST[ $checkbox_name ] );
}
}
}
update_option( 'pmproecaddon_plugin_list_config', $list_config );
foreach ( $list_data as $list ) {
$checkbox_name = 'opt_in_audiences_' . str_replace( " ", "_", $list['name'] ) . '_checkbox';
if ( isset( $_POST[ $checkbox_name ] ) ) {
$list_nom_member[ "opt_in_audiences_" . str_replace( " ", "_", $list['name'] ) ] = sanitize_text_field( $_POST[ $checkbox_name ] );
}
}
update_option( 'pmproecaddon_plugin_list_opt_in_audiences', $list_nom_member );
foreach ( $list_data as $list ) {
$checkbox_name = 'nom_member_audiences_' . str_replace( " ", "_", $list['name'] ) . '_checkbox';
if ( isset( $_POST[ $checkbox_name ] ) ) {
$list_opt_in_audiences[ "nom_member_audiences_" . str_replace( " ", "_", $list['name'] ) ] = sanitize_text_field( $_POST[ $checkbox_name ] );
}
}
update_option( 'pmproecaddon_plugin_list_nom_member', $list_opt_in_audiences );
if ( isset( $_POST['require_unsubscribe_on_level_select'] ) ) {
$require_unsubscribe_on_leve_select = sanitize_text_field( $_POST['require_unsubscribe_on_level_select'] );
update_option( 'pmproecaddon_require_unsuscribe_on_level', $require_unsubscribe_on_leve_select );
}
if ( isset( $_POST['require_update_profile_select'] ) ) {
$require_update_profile_select = sanitize_text_field( $_POST['require_update_profile_select'] );
update_option( 'pmproecaddon_require_update_profile', $require_update_profile_select );
}
} else {
wp_safe_redirect( add_query_arg( 'pmproecaddon_msg', 'no_membership_levels', wp_get_referer() ) );
exit;
}
}
wp_safe_redirect( add_query_arg( 'pmproecaddon_msg', 'success', wp_get_referer() ) );
exit;
}
}
/**
* Add options to settings menu
*
*
*/
function pmproecaddon_options_page() {
$user_ec = get_option( 'pmproecaddon_plugin_user_ec', '' );
$password_ec = get_option( 'pmproecaddon_plugin_pass_ec', '' );
$pmproecaddon_msg = sanitize_text_field( wp_unslash( $_GET['pmproecaddon_msg'] ) );
?>
<div class="wrap pmproecaddon-options">
<img src="<?php echo plugins_url('img/logo.png', __FILE__); ?>" alt="Emailchef">
<h1><?php esc_html_e( 'Emailchef integration options and settings', 'emailchef-add-on-for-pmp' ); ?></h1>
<h2><?php esc_html_e( 'Subscribe users to one or more Emailchef lists when they sign up for your site.', 'emailchef-add-on-for-pmp' ); ?></h2>
<label><?php esc_html_e( 'If you have Paid Membership Pro installed, you can subscribe members to one or more Emailchef lists based on their membership level or specify "Opt-in Lists" that members can select at membership checkout.', 'emailchef-add-on-for-pmp' ); ?></label>
<?php
if ( ! empty( $pmproecaddon_msg ) ) {
if ( 'success' === $pmproecaddon_msg ) {
echo '<div class="updated"><p>' . esc_html__( 'Configuration saved successfully.', 'emailchef-add-on-for-pmp' ) . '</p></div>';
} elseif ( 'reset' === $pmproecaddon_msg ) {
echo '<div class="updated"><p>' . esc_html__( 'Configuration reset successfully.', 'emailchef-add-on-for-pmp' ) . '</p></div>';
} elseif ( 'emailchef_credentials_wrong' === $pmproecaddon_msg ) {
echo '<div class="error"><p>' . esc_html__( 'Emailchef credentials are wrong.', 'emailchef-add-on-for-pmp' ) . '</p></div>';
} elseif ( 'no_membership_levels' === $pmproecaddon_msg ) {
echo '<div class="error"><p>' . esc_html__( 'No available membership levels found in PMPro.', 'emailchef-add-on-for-pmp' ) . '</p></div>';
}
}
?>
<form method="post" action="<?php echo esc_url( admin_url( "admin-post.php" ) ); ?>">
<h2><?php esc_html_e( 'Login Emailchef', 'emailchef-add-on-for-pmp' ); ?></h2>
<table border="0">
<tr>
<td><label for="user_ec"><?php esc_html_e( 'User', 'emailchef-add-on-for-pmp' ); ?></label></td>
<td><input class="input-text" type="text" id="user_ec" name="user_ec"
value="<?php echo esc_attr( $user_ec ); ?>"></td>
</tr>
<tr>
<td><label for="pass_ec"><?php esc_html_e( 'Password', 'emailchef-add-on-for-pmp' ); ?></label></td>
<td><input class="input-text" type="password" id="pass_ec" name="pass_ec"
value="<?php echo esc_attr( $password_ec ); ?>"></td>
</tr>
</table>
<br>
<?php
if ( 'yes' === get_option( 'pmproecaddon_plugin_user_enabled', 'no' ) ) {
pmproecaddon_load_list_ec();
$list_data = get_option( 'pmproecaddon_list_data', '' );
$list_opt_in_audiences = get_option( 'pmproecaddon_plugin_list_opt_in_audiences', '' );
$list_nom_member = get_option( 'pmproecaddon_plugin_list_nom_member', '' );
$unsubscribe_on_level = get_option( 'pmproecaddon_require_unsuscribe_on_level', '' );
$require_update_profile = get_option( 'pmproecaddon_require_update_profile', '' );
?>
<h2><?php esc_html_e( 'General configuration', 'emailchef-add-on-for-pmp' ); ?></h2>
<table border="0">
<tr>
<td>
<label for="nonmember_audiences"><?php esc_html_e( 'Non-member Lists', 'emailchef-add-on-for-pmp' ); ?></label>
</td>
<td>
<div class="checkbox-container">
<?php foreach ( $list_data as $list ) :
$list_name = str_replace( " ", "_", $list['name'] );
$name_checkbox = "nom_member_audiences_" . $list_name . "_checkbox";
$is_checked = isset( $list_nom_member[ $name_checkbox ] ) && $list_nom_member[ $name_checkbox ] == $list['id'];
?>
<label class="checkbox-item">
<input id="nonmember_audiences" class="input-checkbox" type="checkbox"
name="<?php echo esc_attr( $name_checkbox ); ?>"
value="<?php echo esc_attr( $list['id'] ); ?>" <?php echo $is_checked ? 'checked' : ''; ?>>
<?php echo esc_html( $list['name'] ); ?>
</label>
<?php endforeach; ?>
</div>
</td>
</tr>
<tr>
<td>
<label for="optin_audiences"><?php esc_html_e( 'Opt-in Lists', 'emailchef-add-on-for-pmp' ); ?></label>
</td>
<td>
<div class="checkbox-container">
<?php foreach ( $list_data as $list ) :
$list_name = str_replace( " ", "_", $list['name'] );
$name_checkbox = "opt_in_audiences_" . $list_name . "_checkbox";
$is_checked = isset( $list_opt_in_audiences[ $name_checkbox ] ) && $list_opt_in_audiences[ $name_checkbox ] == $list['id'];
?>
<label class="checkbox-item">
<input id="optin_audiences" class="input-checkbox" type="checkbox"
name="<?php echo esc_attr( $name_checkbox ); ?>"
value="<?php echo esc_attr( $list['id'] ); ?>" <?php echo $is_checked ? 'checked' : ''; ?>>
<?php echo esc_html( $list['name'] ); ?>
</label>
<?php endforeach; ?>
</div>
</td>
</tr>
<tr>
<td>
<label for="unsubscribe_on_level_change"><?php esc_html_e( 'Unsubscribe on Level Change?', 'emailchef-add-on-for-pmp' ); ?></label>
</td>
<td>
<select class="dropdown" id="unsubscribe_on_level_change"
name="require_unsubscribe_on_level_select">
<option value="yes_only_old_levels" <?php selected( $unsubscribe_on_level, "yes_only_old_levels" ); ?>><?php esc_html_e( 'Yes (Only old level lists.)', 'emailchef-add-on-for-pmp' ); ?></option>
<option value="yes_old_level" <?php selected( $unsubscribe_on_level, "yes_old_level" ); ?>><?php esc_html_e( 'Yes (Old level and opt-in lists.)', 'emailchef-add-on-for-pmp' ); ?></option>
<option value="no" <?php selected( $unsubscribe_on_level, "no", true ); ?>><?php esc_html_e( 'No', 'emailchef-add-on-for-pmp' ); ?></option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<label><?php esc_html_e( 'Recommended: Yes. However, if you manage multiple lists in Emailchef, users will be unsubscribed from other lists when they register on your site.', 'emailchef-add-on-for-pmp' ); ?></label>
</td>
</tr>
<tr>
<td>
<label for="update_profile_save"><?php esc_html_e( 'Update on Profile Save?', 'emailchef-add-on-for-pmp' ); ?></label>
</td>
<td>
<select class="dropdown" id="update_profile_save" name="require_update_profile_select">
<option value="yes" <?php selected( $require_update_profile, "yes" ); ?>><?php esc_html_e( 'Yes', 'emailchef-add-on-for-pmp' ); ?></option>
<option value="no" <?php selected( $require_update_profile, "no", true ); ?>><?php esc_html_e( 'No', 'emailchef-add-on-for-pmp' ); ?></option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<label><?php esc_html_e( "Choosing 'No' will still update Emailchef when the user's level is changed.", 'emailchef-add-on-for-pmp' ); ?></label>
</td>
</tr>
</table>
<?php
$subscriptions = pmpro_getAllLevels();
if ( ! empty( $subscriptions ) ) {
?>
<h2><?php esc_html_e( 'Membership Levels and Lists', 'emailchef-add-on-for-pmp' ); ?></h2>
<p><?php esc_html_e( 'PMPro is installed.', 'emailchef-add-on-for-pmp' ); ?></p>
<p><?php esc_html_e( 'For each level below, choose the list(s) that a new user should be subscribed to when they register.', 'emailchef-add-on-for-pmp' ); ?></p>
<table border="0">
<?php foreach ( $subscriptions as $subscription ) : ?>
<tr>
<td><label><?php echo esc_html( $subscription->name ); ?></label></td>
<?php pmproecaddon_list_ec_plugin_display( str_replace( " ", "_", $subscription->name ) ); ?>
</tr>
<?php endforeach; ?>
</table>
<?php
} else {
echo '<p>' . esc_html__( 'No available membership levels found in PMPro.', 'emailchef-add-on-for-pmp' ) . '</p>';
}
}
?>
<br>
<?php wp_nonce_field( 'pmproecaddon-nonce', 'pmproecaddon-nonce' ); ?>
<input type="hidden" name="action" value="pmproecaddon_save_data">
<input type="submit" name="plugin_save" class="button button-primary"
value="<?php esc_attr_e( 'Save settings', 'emailchef-add-on-for-pmp' ); ?>">
</form>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="pmproecaddon-mt-20">
<input type="hidden" name="action" value="pmproecaddon_reset_options">
<?php wp_nonce_field( 'pmproecaddon-reset-nonce', 'pmproecaddon-reset-nonce' ); ?>
<input type="submit" name="plugin_reset" class="button button-secondary"
value="<?php esc_attr_e( 'Reset settings', 'emailchef-add-on-for-pmp' ); ?>"
onclick="return confirm('<?php esc_attr_e( 'Are you sure you want to reset all the settings?', 'emailchef-add-on-for-pmp' ); ?>');">
</form>
</div>
<?php
}
/** ACTION */
/**
* Update Emailchef audiences when users checkout after usermeta is saved.
*
* @param int $user_id of user who checked out.
*/
function pmproecaddon_capture_subscription_pmpro( $user_id ) {
try {
if ( pmpro_hasMembershipLevel( null, $user_id ) ) {
$current_membership = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_membership ) {
$level_name = str_replace( " ", "_", $current_membership->name );
$list_config = get_option( 'pmproecaddon_plugin_list_config', '' );
$user_email = get_userdata( $user_id )->user_email;
$user_login = get_userdata( $user_id )->user_login;
$list_data = get_option( 'pmproecaddon_list_data', '' );
update_option( 'pmproecaddon_plugin_message', "LEVEL NAME: " . $level_name );;
foreach ( $list_data as $list ) {
if ( isset( $list_config[ $level_name . "_" . str_replace( " ", "_", $list['name'] ) ] ) ) {
$id_list = $list_config[ $level_name . "_" . str_replace( " ", "_", $list['name'] ) ];
update_option( 'pmproecaddon_plugin_message', "ID: " . $id_list );;
pmproecaddon_suscribe_contact_ec( $id_list, $user_email, $user_login );
}
}
} else {
update_option( 'pmproecaddon_plugin_message', "current_membership: I don't enter" );
}
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'pmpro_after_checkout', 'pmproecaddon_capture_subscription_pmpro' );
/**
* Dispaly additional opt-in list fields on checkout
*/
function pmproecaddon_additional_lists_on_checkout() {
try {
$list_opt_in_audiences = get_option( 'pmproecaddon_plugin_list_opt_in_audiences', '' );
if ( count( $list_opt_in_audiences ) > 0 ) {
$list_data = get_option( 'pmproecaddon_list_data', '' );
?>
<h3><?php esc_html_e( 'Join our mailing list.', 'emailchef-add-on-for-pmp' ); ?></h3>
<div class="checkbox-container">
<?php foreach ( $list_data as $list ) :
$list_name = str_replace( " ", "_", $list['name'] );
$name_checkbox = "opt_in_audiences_" . $list_name . "_checkbox";
?>
<label class="checkbox-item">
<?php if ( isset( $list_opt_in_audiences[ $name_checkbox ] ) && $list_opt_in_audiences[ $name_checkbox ] == $list['id'] ) : ?>
<input type="checkbox"
name="<?php echo esc_attr( $name_checkbox ); ?>"
value="<?php echo esc_attr( $list['id'] ); ?>">
<?php echo esc_html( $list['name'] ); ?>
<?php endif; ?>
</label>
<?php endforeach; ?>
</div>
<?php
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'pmpro_checkout_after_tos_fields', 'pmproecaddon_additional_lists_on_checkout' );
/**
* Update Emailchef opt-in audiences when users checkout after usermeta is saved.
*
* @param int $user_id of user who checked out.
*/
function pmproecaddon_pmpro_after_checkout( $user_id ) {
try {
if ( pmpro_hasMembershipLevel( null, $user_id ) ) {
$current_membership = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_membership ) {
$level_name = $current_membership->name;
$list_config = get_option( 'pmproecaddon_plugin_list_config', '' );
$user_email = get_userdata( $user_id )->user_email;
$user_login = get_userdata( $user_id )->user_login;
$list_data = get_option( 'pmproecaddon_list_data', '' );
foreach ( $list_data as $list ) {
$name_checkbox = "opt_in_audiences_" . esc_html( str_replace( " ", "_", $list['name'] ) ) . "_checkbox";
if ( isset( $_REQUEST[ $name_checkbox ] ) ) {
$id_list = sanitize_text_field( $_REQUEST[ $name_checkbox ] );
pmproecaddon_suscribe_contact_ec( $id_list, $user_email, $user_login );
}
}
} else {
update_option( 'pmproecaddon_plugin_message', "current_membership: I don't enter" );
}
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'pmpro_after_checkout', 'pmproecaddon_pmpro_after_checkout', 15 );
/*
Add opt-in Lists to the user profile/edit user page.
*/
function pmproecaddon_add_custom_user_profile_fields( $user ) {
try {
$list_opt_in_audiences = get_option( 'pmproecaddon_plugin_list_opt_in_audiences', '' );
if ( count( $list_opt_in_audiences ) > 0 ) {
$list_data = get_option( 'pmproecaddon_list_data', '' );
?>
<h4><?php esc_html_e( 'Join our mailing list.', 'emailchef-add-on-for-pmp' ); ?></h4>
<div class="checkbox-container">
<?php foreach ( $list_data as $list ) :
$list_name = str_replace( " ", "_", $list['name'] );
$name_checkbox = "opt_in_audiences_" . $list_name . "_checkbox";
?>
<label class="checkbox-item">
<?php if ( isset( $list_opt_in_audiences[ $name_checkbox ] ) && $list_opt_in_audiences[ $name_checkbox ] == $list['id'] ) : ?>
<input type="checkbox"
name="<?php echo esc_attr( $name_checkbox ); ?>"
value="<?php echo esc_attr( $list['id'] ); ?>">
<?php echo esc_html( $list['name'] ); ?>
<?php endif; ?>
</label>
<?php endforeach; ?>
</div>
<?php
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'show_user_profile', 'pmproecaddon_add_custom_user_profile_fields', 12 );
add_action( 'edit_user_profile', 'pmproecaddon_add_custom_user_profile_fields', 12 );
add_action( 'pmpro_show_user_profile', 'pmproecaddon_add_custom_user_profile_fields', 12 );
// Saving additional lists on profile save.
function pmproecaddon_save_custom_user_profile_fields( $user_id ) {
try {
if ( pmpro_hasMembershipLevel( null, $user_id ) ) {
$current_membership = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_membership ) {
$level_name = $current_membership->name;
$list_config = get_option( 'pmproecaddon_plugin_list_config', '' );
$user_email = get_userdata( $user_id )->user_email;
$user_login = get_userdata( $user_id )->user_login;
$list_data = get_option( 'pmproecaddon_list_data', '' );
foreach ( $list_data as $list ) {
$name_checkbox = "opt_in_audiences_" . esc_html( str_replace( " ", "_", $list['name'] ) ) . "_checkbox";
if ( isset( $_REQUEST[ $name_checkbox ] ) ) {
$id_list = sanitize_text_field( $_REQUEST[ $name_checkbox ] );
pmproecaddon_suscribe_contact_ec( $id_list, $user_email, $user_login );
}
}
$pmproecaddon_require_update_profile = get_option( 'pmproecaddon_require_update_profile', '' );
if ( isset( $pmproecaddon_require_update_profile ) && $pmproecaddon_require_update_profile == "yes" ) {
foreach ( $list_data as $list ) {
$id_list = $list['id'];
$first_name = sanitize_text_field( $_REQUEST["first_name"] );
$last_name = sanitize_text_field( $_REQUEST["last_name"] );
pmproecaddon_update_contact( $id_list, $user_email, $user_login, $first_name, $last_name );
}
}
} else {
update_option( 'pmproecaddon_plugin_message', "current_membership: I don't enter" );
}
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'personal_options_update', 'pmproecaddon_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'pmproecaddon_save_custom_user_profile_fields' );
add_action( 'pmpro_personal_options_update', 'pmproecaddon_save_custom_user_profile_fields' );
/**
* Subscribe users to lists when they register.
*
* @param int $user_id that was registered.
*/
function pmproecaddon_user_register( $user_id ) {
try {
if ( pmpro_hasMembershipLevel( null, $user_id ) ) {
$current_membership = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_membership ) {
$level_name = $current_membership->name;
$list_config = get_option( 'pmproecaddon_plugin_list_config', '' );
$user_email = get_userdata( $user_id )->user_email;
$user_login = get_userdata( $user_id )->user_login;
$list_data = get_option( 'pmproecaddon_list_data', '' );
foreach ( $list_data as $list ) {
$name_checkbox = "opt_in_audiences_" . esc_html( str_replace( " ", "_", $list['name'] ) ) . "_checkbox";
if ( isset( $_REQUEST[ $name_checkbox ] ) ) {
$id_list = sanitize_text_field( $_REQUEST[ $name_checkbox ] );
pmproecaddon_suscribe_contact_ec( $id_list, $user_email, $user_login );
}
}
} else {
update_option( 'pmproecaddon_plugin_message', "current_membership: I don't enter" );
}
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'user_register', 'pmproecaddon_user_register' );
/**
* Subscribe new members (PMPro) when their membership level changes
*
* @param $level_id (int) -- ID of pmpro membership level.
* @param $user_id (int) -- ID for user.
*/
function pmproecaddon_pmpro_after_change_membership_level( $level_id, $user_id ) {
try {
if ( pmpro_hasMembershipLevel( null, $user_id ) ) {
$current_membership = pmpro_getMembershipLevelForUser( $user_id );
if ( $current_membership ) {
$level_name = $current_membership->name;
$unsuscribe_option = get_option( 'pmproecaddon_require_unsuscribe_on_level', '' );
if ( $unsuscribe_option == "yes_only_old_levels" ) {
/*
$list_config = get_option('pmproecaddon_plugin_list_config', '');
$user_email = get_userdata($user_id)->user_email;
$user_login = get_userdata($user_id)->user_login;
foreach ($list_config as $list) {
if (strpos(key($list), $level_name) !== 0) {
$id_list = $list;
pmproecaddon_delete_contact_ec($id_list,$user_email,$user_login);
}
}
*/
}
if ( $unsuscribe_option == "yes_old_level" ) {
}
//update_option('pmproecaddon_plugin_message', "level_name: " . $level_name . ' user_id:' . $user_id . ' level_id' . $level_id);
}
}
} catch ( Exception $e ) {
pmproecaddon_enqueue_script( $e->getMessage() );
}
}
add_action( 'pmpro_after_change_membership_level', 'pmproecaddon_pmpro_after_change_membership_level', 15, 2 );
function pmproecaddon_enqueue_script( $text ) {
if ( $text != "" ) {
$js_url = plugins_url( '/js/pmproecaddon_script.js', __FILE__ );
wp_enqueue_script( 'pmproecaddon_script', $js_url, array(), '1.0', true );
$message = 'ERROR:' . $text;
wp_localize_script( 'pmproecaddon_script', 'plugin_params', array(
'message' => $message
) );
}
}
add_action( 'wp_enqueue_scripts', 'pmproecaddon_enqueue_script' );
function pmproecaddon_admin_style() {
wp_enqueue_style( 'admin_css', plugins_url( '/css/pmproecaddon.css', __FILE__ ), false, '1.0.0' );
}
add_action( 'admin_enqueue_scripts', 'pmproecaddon_admin_style' );
function pmproecaddon_check_pmpro() {
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
/* translators: %s: plugins page */
wp_die( __( 'This plugin requires Paid Memberships Pro. <a href="%s">Please go back to the Plugins page</a>.', 'emailchef-add-on-for-pmp' ), esc_url( admin_url( 'plugins.php' ) ) );
}
}
register_activation_hook( __FILE__, 'pmproecaddon_check_pmpro' );
function pmproecaddon_load_textdomain() {
load_plugin_textdomain( 'emailchef-add-on-for-pmp', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'pmproecaddon_load_textdomain' );