| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\WpInit; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('Restricted Access'); |
| 6 |
|
| 7 |
use AcyMailing\Classes\UserClass; |
| 8 |
use AcyMailing\Helpers\RegacyHelper; |
| 9 |
|
| 10 |
class UserSync |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
// Let the users opt in to the AcyMailing lists |
| 15 |
add_action('register_form', [$this, 'addRegistrationFields']); |
| 16 |
add_action('edit_user_profile', [$this, 'addProfileFields'], 10, 1); |
| 17 |
add_action('show_user_profile', [$this, 'addProfileFields'], 10, 1); |
| 18 |
|
| 19 |
// Hooks to create/update an Acy user when a WP user is created/updated |
| 20 |
add_action('user_register', [$this, 'synchSaveUsers'], 10, 1); |
| 21 |
add_action('profile_update', [$this, 'synchSaveUsers'], 10, 2); |
| 22 |
add_action('delete_user', [$this, 'synchDeleteUsers']); |
| 23 |
} |
| 24 |
|
| 25 |
public function addRegistrationFields($externalPluginConfig = '') |
| 26 |
{ |
| 27 |
$config = acym_config(); |
| 28 |
|
| 29 |
$displayOnExternalPlugin = true; |
| 30 |
if (!empty($externalPluginConfig)) $displayOnExternalPlugin = $config->get($externalPluginConfig, 0) == 1; |
| 31 |
|
| 32 |
if (!$config->get('regacy', 0) || !$displayOnExternalPlugin) return; |
| 33 |
|
| 34 |
$regacyHelper = new RegacyHelper(); |
| 35 |
if (!$regacyHelper->prepareLists(['formatted' => true])) return; |
| 36 |
|
| 37 |
?> |
| 38 |
<div class="acym__regacy"> |
| 39 |
<label class="acym__regacy__label"><?php echo esc_html($regacyHelper->label); ?></label> |
| 40 |
<div class="acym__regacy__values"> |
| 41 |
<?php |
| 42 |
echo wp_kses( |
| 43 |
$regacyHelper->listsHtml, |
| 44 |
[ |
| 45 |
'table' => ['class' => [], 'style' => []], |
| 46 |
'tr' => ['style' => []], |
| 47 |
'td' => ['style' => []], |
| 48 |
'input' => [ |
| 49 |
'type' => [], |
| 50 |
'name' => [], |
| 51 |
'id' => [], |
| 52 |
'value' => [], |
| 53 |
'class' => [], |
| 54 |
'checked' => [], |
| 55 |
], |
| 56 |
'label' => ['for' => [], 'class' => []], |
| 57 |
] |
| 58 |
); |
| 59 |
?> |
| 60 |
</div> |
| 61 |
</div> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
public function addProfileFields() |
| 66 |
{ |
| 67 |
$config = acym_config(); |
| 68 |
if (!$config->get('regacy', 0)) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$regacyHelper = new RegacyHelper(); |
| 73 |
if (!$regacyHelper->prepareLists([])) { |
| 74 |
return; |
| 75 |
} |
| 76 |
?> |
| 77 |
<h2><?php echo esc_html(acym_translation('ACYM_SUBSCRIPTION')); ?></h2> |
| 78 |
<table class="form-table"> |
| 79 |
<tbody> |
| 80 |
<?php |
| 81 |
foreach ($regacyHelper->lists as $listId => $oneList) { |
| 82 |
?> |
| 83 |
<tr> |
| 84 |
<th scope="row"> |
| 85 |
<label class="acym__regacy__lists__label" for="acym__regacy__lists-<?php echo intval($listId); ?>"> |
| 86 |
<?php echo esc_html($oneList['name']); ?> |
| 87 |
</label> |
| 88 |
</th> |
| 89 |
<td> |
| 90 |
<input name="regacy_visible_lists_checked[]" |
| 91 |
type="checkbox" |
| 92 |
id="acym__regacy__lists-<?php echo intval($listId); ?>" |
| 93 |
value="<?php echo intval($listId); ?>" <?php checked($oneList['checked']); ?>> |
| 94 |
</td> |
| 95 |
</tr> |
| 96 |
<?php |
| 97 |
} |
| 98 |
?> |
| 99 |
</tbody> |
| 100 |
</table> |
| 101 |
<input type="hidden" value="<?php echo esc_attr(implode(',', array_keys($regacyHelper->lists))); ?>" name="regacy_visible_lists" /> |
| 102 |
<input type="hidden" value="WordPress user profile" name="acy_source" /> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
public function synchSaveUsers($userId, $oldUser = null) |
| 107 |
{ |
| 108 |
if (empty($userId)) return; |
| 109 |
|
| 110 |
$isnew = empty($oldUser); |
| 111 |
$cmsUser = get_user_by('id', $userId); |
| 112 |
if (empty($cmsUser->user_email)) return; |
| 113 |
|
| 114 |
$user = [ |
| 115 |
'email' => $cmsUser->user_email, |
| 116 |
'id' => $cmsUser->ID, |
| 117 |
'block' => 0, |
| 118 |
]; |
| 119 |
|
| 120 |
if (!empty($cmsUser->display_name)) { |
| 121 |
$user['name'] = $cmsUser->display_name; |
| 122 |
} elseif (!empty($cmsUser->user_nicename)) { |
| 123 |
$user['name'] = $cmsUser->user_nicename; |
| 124 |
} |
| 125 |
|
| 126 |
$oldUser = empty($oldUser->user_email) ? null : ['email' => $oldUser->user_email]; |
| 127 |
|
| 128 |
$userClass = new UserClass(); |
| 129 |
$userClass->synchSaveCmsUser($user, $isnew, $oldUser); |
| 130 |
} |
| 131 |
|
| 132 |
public function synchDeleteUsers($userId) |
| 133 |
{ |
| 134 |
$cmsUser = get_user_by('id', $userId); |
| 135 |
if (empty($cmsUser->user_email)) return; |
| 136 |
|
| 137 |
$userClass = new UserClass(); |
| 138 |
$userClass->synchDeleteCmsUser($cmsUser->user_email); |
| 139 |
} |
| 140 |
} |
| 141 |
|