| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Dashboard; |
| 4 |
|
| 5 |
use AcyMailing\Classes\MailClass; |
| 6 |
use AcyMailing\Controllers\ConfigurationController; |
| 7 |
use AcyMailing\Controllers\MailsController; |
| 8 |
use AcyMailing\Helpers\EditorHelper; |
| 9 |
use AcyMailing\Helpers\UpdateHelper; |
| 10 |
use AcyMailing\Helpers\UpdatemeHelper; |
| 11 |
use AcyMailing\Types\StepsType; |
| 12 |
|
| 13 |
trait Walkthrough |
| 14 |
{ |
| 15 |
public function saveStepThankYou(): void |
| 16 |
{ |
| 17 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 18 |
|
| 19 |
if (!acym_isAllowed('configuration')) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$this->saveWalkthrough(['step' => 'stepSenderInformation']); |
| 24 |
$this->stepSenderInformation(); |
| 25 |
} |
| 26 |
|
| 27 |
public function saveStepSenderInformation(): void |
| 28 |
{ |
| 29 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 30 |
|
| 31 |
if (!acym_isAllowed('configuration')) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$newConfiguration = [ |
| 36 |
'from_name' => acym_getVar('string', 'from_name'), |
| 37 |
'from_email' => acym_getVar('string', 'from_email'), |
| 38 |
]; |
| 39 |
|
| 40 |
if (empty($newConfiguration['from_name']) || empty($newConfiguration['from_email'])) { |
| 41 |
acym_enqueueMessage(acym_translation('ACYM_FILL_ALL_INFORMATION'), 'error'); |
| 42 |
$this->stepSenderInformation(); |
| 43 |
|
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$this->config->saveConfig($newConfiguration); |
| 48 |
|
| 49 |
$this->saveWalkthrough(['step' => 'stepLicense']); |
| 50 |
$this->stepLicense(); |
| 51 |
} |
| 52 |
|
| 53 |
public function ajaxAttachLicense(): void |
| 54 |
{ |
| 55 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 56 |
|
| 57 |
if (!acym_isAllowed('configuration')) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$licenseKey = acym_getVar('string', 'licenseKey', ''); |
| 62 |
if (empty($licenseKey)) { |
| 63 |
acym_sendAjaxResponse(acym_translation('ACYM_LICENSE_NOT_FOUND'), [], false); |
| 64 |
} |
| 65 |
|
| 66 |
$this->config->saveConfig(['license_key' => $licenseKey]); |
| 67 |
|
| 68 |
$configurationController = new ConfigurationController(); |
| 69 |
$return = $configurationController->attachLicenseOnUpdateMe($licenseKey); |
| 70 |
|
| 71 |
if (empty($return['success'])) { |
| 72 |
$this->config->saveConfig(['license_key' => '']); |
| 73 |
} else { |
| 74 |
UpdatemeHelper::getLicenseInfo(); |
| 75 |
} |
| 76 |
|
| 77 |
$originalMessage = $return['message']; |
| 78 |
$return['message'] = $configurationController->displayMessage($originalMessage, true); |
| 79 |
$ajaxSuccess = !empty($return['message']['type']) && $return['message']['type'] !== 'error'; |
| 80 |
|
| 81 |
acym_sendAjaxResponse($return['message']['message'] ?? $originalMessage, [], $ajaxSuccess); |
| 82 |
} |
| 83 |
|
| 84 |
public function ajaxActivateCron(): void |
| 85 |
{ |
| 86 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 87 |
|
| 88 |
if (!acym_isAllowed('configuration')) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$licenseKey = $this->config->get('license_key'); |
| 93 |
if (empty($licenseKey)) { |
| 94 |
acym_sendAjaxResponse(acym_translation('ACYM_LICENSE_NOT_FOUND'), [], false); |
| 95 |
} |
| 96 |
|
| 97 |
$data = [ |
| 98 |
'domain' => ACYM_LIVE, |
| 99 |
'cms' => ACYM_CMS, |
| 100 |
'version' => $this->config->get('version', ''), |
| 101 |
'level' => $this->config->get('level', ''), |
| 102 |
'activate' => true, |
| 103 |
]; |
| 104 |
$result = UpdatemeHelper::call('api/crons/modify', 'POST', $data); |
| 105 |
|
| 106 |
$configurationController = new ConfigurationController(); |
| 107 |
$result['message'] = $configurationController->displayMessage($result['message'] ?? 'CRON_NOT_SAVED', true); |
| 108 |
$success = !empty($result['success']); |
| 109 |
|
| 110 |
if ($success) { |
| 111 |
$this->config->saveConfig(['active_cron' => 1]); |
| 112 |
} |
| 113 |
|
| 114 |
acym_sendAjaxResponse($result['message']['message'], [], $success); |
| 115 |
} |
| 116 |
|
| 117 |
public function saveStepLicense(): void |
| 118 |
{ |
| 119 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 120 |
|
| 121 |
if (!acym_isAllowed('configuration')) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$this->saveWalkthrough(['step' => 'stepFinal']); |
| 126 |
$this->stepFinal(); |
| 127 |
} |
| 128 |
|
| 129 |
public function startUsing(bool $campaigns = false): void |
| 130 |
{ |
| 131 |
if (!$campaigns) { |
| 132 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 133 |
} |
| 134 |
|
| 135 |
if (!acym_isAllowed('configuration')) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$this->config->saveConfig(['walk_through' => 0]); |
| 140 |
$redirectUrl = $campaigns ? 'campaigns' : 'dashboard'; |
| 141 |
|
| 142 |
acym_redirect(acym_completeLink($redirectUrl, false, true)); |
| 143 |
} |
| 144 |
|
| 145 |
public function tryEditor(): void |
| 146 |
{ |
| 147 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 148 |
|
| 149 |
if (!acym_isAllowed('configuration')) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$this->saveWalkthrough(['step' => 'stepEditor']); |
| 154 |
$this->stepEditor(); |
| 155 |
} |
| 156 |
|
| 157 |
public function saveAjax(): void |
| 158 |
{ |
| 159 |
wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); |
| 160 |
|
| 161 |
if (!acym_isAllowed('configuration') || $this->config->get('walk_through', 0) != 1) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$mailController = new MailsController(); |
| 166 |
$mailId = $mailController->store(true); |
| 167 |
|
| 168 |
if (!empty($mailId)) { |
| 169 |
acym_sendAjaxResponse('', ['result' => $mailId]); |
| 170 |
} else { |
| 171 |
acym_sendAjaxResponse(acym_translation('ACYM_ERROR_SAVING'), [], false); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private function walkthrough(): bool |
| 176 |
{ |
| 177 |
if ($this->config->get('walk_through', 0) != 1 || !acym_isAllowed('configuration')) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
$walkthroughParams = json_decode($this->config->get('walkthrough_params', '[]'), true); |
| 182 |
if (empty($walkthroughParams['step']) || !method_exists($this, $walkthroughParams['step'])) { |
| 183 |
$this->stepThankYou(); |
| 184 |
} else { |
| 185 |
$this->{$walkthroughParams['step']}(); |
| 186 |
} |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
private function stepThankYou(): void |
| 192 |
{ |
| 193 |
acym_setVar('layout', 'step_thank_you'); |
| 194 |
|
| 195 |
$data = [ |
| 196 |
'level' => $this->config->get('level'), |
| 197 |
]; |
| 198 |
|
| 199 |
parent::display($data); |
| 200 |
} |
| 201 |
|
| 202 |
private function stepSenderInformation(): void |
| 203 |
{ |
| 204 |
acym_setVar('layout', 'step_sender_information'); |
| 205 |
|
| 206 |
$data = [ |
| 207 |
'stepsType' => new StepsType(), |
| 208 |
'siteName' => $this->config->get('from_name', acym_getCMSConfig('sitename')), |
| 209 |
'userEmail' => $this->config->get('from_email', acym_currentUserEmail()), |
| 210 |
]; |
| 211 |
|
| 212 |
parent::display($data); |
| 213 |
} |
| 214 |
|
| 215 |
private function stepLicense(): void |
| 216 |
{ |
| 217 |
acym_setVar('layout', 'step_license'); |
| 218 |
|
| 219 |
$data = [ |
| 220 |
'stepsType' => new StepsType(), |
| 221 |
'level' => $this->config->get('level'), |
| 222 |
]; |
| 223 |
|
| 224 |
parent::display($data); |
| 225 |
} |
| 226 |
|
| 227 |
private function stepFinal(): void |
| 228 |
{ |
| 229 |
$acyMailerApiKey = $this->config->get('acymailer_apikey'); |
| 230 |
acym_setVar('layout', empty($acyMailerApiKey) ? 'step_final' : 'step_acymailer'); |
| 231 |
|
| 232 |
$data = [ |
| 233 |
'stepsType' => new StepsType(), |
| 234 |
'pricingPage' => ACYM_ACYMAILING_WEBSITE.'pricing', |
| 235 |
]; |
| 236 |
|
| 237 |
if (!empty($acyMailerApiKey)) { |
| 238 |
$data['suggestedDomain'] = acym_getDomain($this->config->get('from_email', acym_currentUserEmail())); |
| 239 |
} |
| 240 |
|
| 241 |
parent::display($data); |
| 242 |
} |
| 243 |
|
| 244 |
private function stepEditor(): void |
| 245 |
{ |
| 246 |
acym_setVar('layout', 'step_editor'); |
| 247 |
|
| 248 |
$mailClass = new MailClass(); |
| 249 |
$mail = $mailClass->getOneByName(acym_translation(UpdateHelper::FIRST_EMAIL_NAME_KEY)); |
| 250 |
|
| 251 |
if (empty($mail)) { |
| 252 |
$updateHelper = new UpdateHelper(); |
| 253 |
$updateHelper->installNotifications(); |
| 254 |
$mail = $mailClass->getOneByName(acym_translation(UpdateHelper::FIRST_EMAIL_NAME_KEY)); |
| 255 |
if (empty($mail)) { |
| 256 |
$this->startUsing(true); |
| 257 |
|
| 258 |
return; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
$editorHelper = new EditorHelper(); |
| 263 |
$editorHelper->content = $mail->body; |
| 264 |
$editorHelper->autoSave = ''; |
| 265 |
$editorHelper->settings = $mail->settings; |
| 266 |
$editorHelper->stylesheet = $mail->stylesheet ?? ''; |
| 267 |
$editorHelper->editor = 'acyEditor'; |
| 268 |
$editorHelper->mailId = $mail->id; |
| 269 |
$editorHelper->walkThrough = true; |
| 270 |
|
| 271 |
$data = [ |
| 272 |
'mail' => $mail, |
| 273 |
'social_icons' => $this->config->get('social_icons', '{}'), |
| 274 |
'editor' => $editorHelper, |
| 275 |
]; |
| 276 |
|
| 277 |
parent::display($data); |
| 278 |
} |
| 279 |
|
| 280 |
private function saveWalkthrough(array $params): void |
| 281 |
{ |
| 282 |
$newParams = json_decode($this->config->get('walkthrough_params', '[]'), true); |
| 283 |
foreach ($params as $key => $value) { |
| 284 |
$newParams[$key] = $value; |
| 285 |
} |
| 286 |
$this->config->saveConfig(['walkthrough_params' => json_encode($newParams)]); |
| 287 |
} |
| 288 |
} |
| 289 |
|