| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Integrations; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Helpers\ArrayHelper; |
| 20 |
|
| 21 |
class AcyMailing extends Integration |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Subscribe user to AcyMailing. |
| 25 |
* |
| 26 |
* @param string $email |
| 27 |
* @param array $params |
| 28 |
* @param array $list_ids |
| 29 |
* @param bool $doubleOptin |
| 30 |
*/ |
| 31 |
public function subscribe($email = null, $params = [], $list_ids = [], $doubleOptin = false) |
| 32 |
{ |
| 33 |
if (!$this->checkValidVersion()) |
| 34 |
{ |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
if (!is_array($list_ids)) |
| 39 |
{ |
| 40 |
$list_ids = [$list_ids]; |
| 41 |
} |
| 42 |
|
| 43 |
// Ensure List ID is correct |
| 44 |
$listClass = new \AcyMailing\Classes\ListClass(); |
| 45 |
if (count($list_ids)) |
| 46 |
{ |
| 47 |
foreach ($list_ids as $list_id) |
| 48 |
{ |
| 49 |
if ($listClass->getOneById($list_id)) |
| 50 |
{ |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
$error = new \WP_Error('acym_list_invalid', 'AcyMailing list does not exist.'); |
| 55 |
$this->throwError($error); |
| 56 |
return; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Create user object |
| 61 |
$user = new \stdClass(); |
| 62 |
$user->cms_id = acym_currentUserId(); |
| 63 |
$user->email = $email; |
| 64 |
$user->source = $this->getSource(); |
| 65 |
$user->confirmed = $doubleOptin ? 0 : 1; |
| 66 |
|
| 67 |
$user_fields = array_change_key_case($params); |
| 68 |
|
| 69 |
$user->name = isset($user_fields['name']) ? $user_fields['name'] : ''; |
| 70 |
|
| 71 |
// Create user |
| 72 |
$userClass = new \AcyMailing\Classes\UserClass(); |
| 73 |
|
| 74 |
// Check if exists |
| 75 |
$existing_user = $userClass->getOneByEmail($email); |
| 76 |
|
| 77 |
if ($existing_user) |
| 78 |
{ |
| 79 |
$user->id = $existing_user->id; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
// Save user to database only if it's a new user. |
| 84 |
if (!$user->id = $userClass->save($user)) |
| 85 |
{ |
| 86 |
$error = new \WP_Error('acym_user_creation_fail', 'AcyMailing cannot create user.'); |
| 87 |
$this->throwError($error); |
| 88 |
return; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Send confirmation (double opt-in) |
| 93 |
if (!$user->confirmed) |
| 94 |
{ |
| 95 |
$mailerHelper = new \AcyMailing\Helpers\MailerHelper(); |
| 96 |
$mailerHelper->checkConfirmField = false; |
| 97 |
$mailerHelper->checkEnabled = false; |
| 98 |
$mailerHelper->report = $userClass->config->get('confirm_message', 0); |
| 99 |
|
| 100 |
$mailClass = new \AcyMailing\Classes\MailClass(); |
| 101 |
$confirmationEmail = $mailClass->getOneByName('acy_confirm'); |
| 102 |
$mailerHelper->sendOne($confirmationEmail->id, $user); |
| 103 |
} |
| 104 |
|
| 105 |
// Add custom fields to user |
| 106 |
$fieldClass = new \AcyMailing\Classes\FieldClass(); |
| 107 |
|
| 108 |
$acym_fields = $fieldClass->getAllFieldsForModuleFront(); |
| 109 |
|
| 110 |
unset($user_fields['name']); // Name is already used during user creation. |
| 111 |
|
| 112 |
$fields_to_store = []; |
| 113 |
|
| 114 |
foreach ($user_fields as $paramKey => $paramValue) |
| 115 |
{ |
| 116 |
// Check if paramKey it's a custom field |
| 117 |
$field_found = array_filter($acym_fields, function($field) use($paramKey) { |
| 118 |
return (strtolower($field->name) == $paramKey || $field->id == $paramKey); |
| 119 |
}); |
| 120 |
|
| 121 |
if ($field_found) |
| 122 |
{ |
| 123 |
// Get the 1st occurence |
| 124 |
$field = array_shift($field_found); |
| 125 |
|
| 126 |
// AcyMailing 6 needs field's ID to recognize a field. |
| 127 |
$fields_to_store[$field->id] = $paramValue; |
| 128 |
|
| 129 |
// $paramValue output: array(1) { [0]=> string(2) "gr" } |
| 130 |
// AcyMailing will get the key as the value instead of "gr" |
| 131 |
// We combine to remove the keys in order to keep the values |
| 132 |
if (is_array($paramValue)) |
| 133 |
{ |
| 134 |
$fields_to_store[$field->id] = array_combine($fields_to_store[$field->id], $fields_to_store[$field->id]); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
$fields_to_store = array_filter($fields_to_store); |
| 140 |
|
| 141 |
if ($fields_to_store) |
| 142 |
{ |
| 143 |
$fieldClass->store($user->id, $fields_to_store); |
| 144 |
} |
| 145 |
|
| 146 |
// Subscribe user to the list |
| 147 |
$userClass->subscribe([$user->id], $list_ids, true, !$doubleOptin); |
| 148 |
|
| 149 |
// Set that the request was successful. |
| 150 |
$this->setSuccessful(true); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
private function getSource() |
| 156 |
{ |
| 157 |
$metadata = $this->getMetadata(); |
| 158 |
|
| 159 |
$default = 'FirePlugins AcyMailing Integration'; |
| 160 |
|
| 161 |
return isset($metadata['source']) && !empty($metadata['source']) ? $metadata['source'] : $default; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns all available lists |
| 166 |
* |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
public function getLists() |
| 170 |
{ |
| 171 |
if (!$this->checkValidVersion()) |
| 172 |
{ |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// Get all lists |
| 177 |
$sql = "SELECT id, name FROM #__acym_list AS list WHERE type = 'standard' ORDER BY id DESC"; |
| 178 |
|
| 179 |
return json_decode(wp_json_encode(acym_loadObjectList($sql)), true); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Check whether we have a valid version, else throw error. |
| 184 |
* |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
private function checkValidVersion() |
| 188 |
{ |
| 189 |
if (!function_exists('acym_config')) |
| 190 |
{ |
| 191 |
$error = new \WP_Error('acym_missing', 'AcyMailing 6 or 7 does not exist.'); |
| 192 |
$this->throwError($error); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
$config = acym_config(); |
| 197 |
|
| 198 |
$version = $config->get('version'); |
| 199 |
|
| 200 |
if (version_compare($version, '6.0.0', 'lt')) |
| 201 |
{ |
| 202 |
$error = new \WP_Error('acym_old_version', 'AcyMailing 6 or newer is required.'); |
| 203 |
$this->throwError($error); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
} |