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