PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.5
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.5
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Core / AcymController.php

AcymController.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.5, at back/Core/AcymController.php

474 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Core;
4
5 abstract class AcymController extends AcymObject
6 {
7 private $currentClass = null;
8 private string $sessionName;
9
10 protected array $breadcrumb = [];
11 protected array $loadScripts = [];
12 protected array $publicFrontTasks = [];
13 protected array $allowedTasks = [];
14 protected array $menuAlias = [];
15 protected string $taskCalled = '';
16 protected string $menuClass = '';
17
18 public string $defaulttask = 'listing';
19 public string $name = '';
20
21 public function __construct()
22 {
23 parent::__construct();
24
25 $classname = get_class($this);
26 $classname = substr($classname, strrpos($classname, '\\') + 1);
27 $this->name = strtolower(
28 substr(
29 $classname,
30 0,
31 strpos($classname, 'Controller')
32 )
33 );
34
35 $currentClassName = 'AcyMailing\\Classes\\'.rtrim(ucfirst(str_replace(['Front', 'front'], '', $this->name)), 's').'Class';
36 if (class_exists($currentClassName)) {
37 $this->currentClass = new $currentClassName();
38 }
39 $this->sessionName = 'acym_filters_'.$this->name;
40 $this->taskCalled = acym_getVar('string', 'task', '');
41
42 $this->breadcrumb['AcyMailing'] = acym_completeLink('dashboard');
43 }
44
45 private function initSession(): void
46 {
47 $controllerSession = acym_getVar('array', $this->sessionName, [], 'SESSION');
48 if (empty($controllerSession)) {
49 acym_setSession($this->sessionName, []);
50 }
51 }
52
53 /**
54 * @param mixed $default
55 *
56 * @return mixed
57 */
58 public function getVarFiltersListing(string $type, string $varName, $default, bool $overrideIfNull = false)
59 {
60 if ($this->taskCalled === 'clearFilters') {
61 return $default;
62 }
63
64 $this->initSession();
65
66 $isVarInRequest = !is_null(acym_getVar('raw', $varName, null, '', ACYM_ALLOWRAW));
67 $returnValue = $isVarInRequest ? acym_getVar($type, $varName) : null;
68
69 if (is_null($returnValue) && $overrideIfNull) {
70 $returnValue = $default;
71 }
72
73 if (!is_null($returnValue)) {
74 $controllerSession = acym_getVar('array', $this->sessionName, [], 'SESSION');
75 $controllerSession[$varName] = $returnValue;
76 acym_setSession($this->sessionName, $controllerSession);
77
78 return $returnValue;
79 }
80
81 $filters = acym_getVar('array', $this->sessionName, [], 'SESSION');
82 if (!empty($filters[$varName])) {
83 return $filters[$varName];
84 }
85
86 return $default;
87 }
88
89 public function setVarFiltersListing(string $varName, int $value): void
90 {
91 acym_setVar($varName, $value);
92 $this->initSession();
93 $controllerSession = acym_getVar('array', $this->sessionName, [], 'SESSION');
94 $controllerSession[$varName] = $value;
95 acym_setSession($this->sessionName, $controllerSession);
96 }
97
98 /**
99 * Called using data-task
100 */
101 public function clearFilters(): void
102 {
103 acym_setSession($this->sessionName, []);
104
105 $taskToCall = acym_getVar('string', 'cleartask', $this->defaulttask);
106 if (in_array($taskToCall, ['campaigns_auto', 'welcome', 'unsubscribe', $this->defaulttask])) {
107 $this->call($taskToCall);
108 }
109 }
110
111 public function call(string $task, bool $isFront = false): void
112 {
113 if (
114 !in_array($task, $this->publicFrontTasks, true)
115 && !acym_isAllowed($this->name, $task)
116 ) {
117 acym_enqueueMessage(acym_translation('ACYM_ACCESS_DENIED'), 'warning');
118 acym_redirect($isFront ? ACYM_LIVE : acym_completeLink('dashboard'));
119
120 return;
121 }
122
123 // If task doesn't exist, redirect to default task + add message
124 if (!is_callable([$this, $task])) {
125 acym_enqueueMessage(acym_translation('ACYM_NON_EXISTING_PAGE'), 'warning');
126 $task = $this->defaulttask;
127 acym_setVar('task', $task);
128 }
129
130 // Call the task
131 $this->$task();
132 }
133
134 public function loadScripts(string $task): void
135 {
136 if (empty($this->loadScripts)) return;
137
138 $scripts = [];
139 if (!empty($this->loadScripts['all'])) {
140 $scripts = $this->loadScripts['all'];
141 }
142
143 if (!empty($task) && !empty($this->loadScripts[$task])) {
144 $scripts = array_merge($scripts, $this->loadScripts[$task]);
145 }
146
147 if (empty($scripts)) return;
148
149 if (in_array('editor-wysid', $scripts)) {
150 acym_addStyle(false, ACYM_CSS.'editorWYSID.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'editorWYSID.min.css'));
151 acym_addScript(false, ACYM_JS.'editor_wysid_utils.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'editor_wysid_utils.min.js'));
152
153 acym_addScript(
154 true,
155 'const ACYM_UNSPLASH_KEY = "'.addslashes($this->config->get('unsplash_key', '')).'";
156 const ACYM_GIPHY_KEY = "'.addslashes($this->config->get('giphy_key', '')).'";
157 const ACYM_SAVE_THUMBNAIL = '.(acym_isAdmin() ? (int)$this->config->get('save_thumbnail', 0) : 0).';'
158 );
159
160 // Automatically add editor dependencies
161 $scripts = array_merge($scripts, ['colorpicker', 'datepicker', 'thumbnail', 'foundation-email', 'parse-css', 'vue-prism-editor', 'masonry']);
162
163 if (empty($scripts['vue-applications'])) {
164 $scripts['vue-applications'] = ['code_editor'];
165 } else {
166 $scripts['vue-applications'][] = 'code_editor';
167 }
168 }
169
170 if (in_array('sankey', $scripts)) {
171 acym_addScript(false, ACYM_JS.'libraries/sankey.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'sankey.min.js'));
172 }
173
174 if (in_array('colorpicker', $scripts)) {
175 acym_addScript(false, ACYM_JS.'libraries/spectrum.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'spectrum.min.js'));
176 acym_addStyle(false, ACYM_CSS.'libraries/spectrum.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'spectrum.min.css'));
177 }
178
179 if (in_array('datepicker', $scripts)) {
180 // Must be loaded in the right order
181 if (ACYM_CMS === 'joomla') {
182 acym_addScript(false, ACYM_JS.'libraries/moment.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'moment.min.js'));
183 }
184 acym_addScript(false, ACYM_JS.'libraries/rome.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'rome.min.js'), [
185 'dependencies' => ['moment'],
186 ]);
187 acym_addScript(false, ACYM_JS.'libraries/material-datetime-picker.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'material-datetime-picker.min.js'));
188 acym_addStyle(false, ACYM_CSS.'libraries/material-datetime-picker.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'material-datetime-picker.min.css'));
189 }
190
191 if (in_array('dtextPicker', $scripts)) {
192 acym_addScript(false, ACYM_JS.'dtext_picker.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'dtext_picker.min.js'));
193 acym_addStyle(false, ACYM_CSS.'dtext_picker.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'dtext_picker.min.css'));
194 }
195
196 if (in_array('thumbnail', $scripts)) {
197 acym_addScript(false, ACYM_JS.'libraries/html2canvas.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'html2canvas.min.js'));
198 }
199
200 if (in_array('foundation-email', $scripts)) {
201 acym_addStyle(false, ACYM_CSS.'libraries/foundation_email.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'foundation_email.min.css'));
202 acym_addStyle(true, acym_getEmailCssFixes());
203 }
204
205 if (in_array('parse-css', $scripts)) {
206 acym_addScript(false, ACYM_JS.'libraries/parse-css.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'parse-css.min.js'));
207 }
208
209 if (in_array('masonry', $scripts)) {
210 acym_addScript(false, ACYM_JS.'libraries/masonry.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'masonry.min.js'));
211 }
212
213 if (!empty($scripts['vue-applications'])) {
214 //vuejs javascript library
215 acym_addScript(false, ACYM_JS.'libraries/vuejs.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'vuejs.min.js'));
216 acym_addScript(false, ACYM_JS.'libraries/vue-infinite-scroll.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'vue-infinite-scroll.min.js'));
217 //All the component created to use in vue app (ex: select2 component)
218 acym_addScript(false, ACYM_JS.'vue/vue_components.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'vue'.DS.'vue_components.min.js'));
219 foreach ($scripts['vue-applications'] as $script) {
220 acym_addScript(false, ACYM_JS.'vue/'.$script.'.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'vue'.DS.$script.'.min.js'));
221 }
222 }
223
224 if (in_array('vue-prism-editor', $scripts)) {
225 acym_addScript(false, ACYM_JS.'libraries/prism-editor.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'prism-editor.min.js'));
226 acym_addScript(false, ACYM_JS.'libraries/prism.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'prism.min.js'));
227 acym_addStyle(false, ACYM_CSS.'libraries/prism.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'prism.min.css'));
228 acym_addScript(false, ACYM_JS.'libraries/beautify-html.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'beautify-html.min.js'));
229 }
230 }
231
232 public function setDefaultTask(string $task): void
233 {
234 $this->defaulttask = $task;
235 }
236
237 public function getName(): string
238 {
239 return $this->name;
240 }
241
242 public function display(array $data = []): void
243 {
244 if (acym_isAdmin()) {
245 if (!acym_isNoTemplate()) {
246 $data['breadcrumb'] = $this->breadcrumb;
247 }
248 $viewNamespace = 'AcyMailing\\Views\\';
249 } else {
250 $viewNamespace = 'AcyMailing\\FrontViews\\';
251 }
252
253 $viewName = ucfirst($this->getName());
254 $viewNamespace .= $viewName.'\\'.$viewName.'View';
255 $view = new $viewNamespace();
256 $view->display($this, $data);
257 }
258
259 /**
260 * Called using data-task
261 */
262 public function cancel(): void
263 {
264 acym_setVar('layout', 'listing');
265 $this->display();
266 }
267
268 public function listing(): void
269 {
270 acym_setVar('layout', 'listing');
271
272 $this->display();
273 }
274
275 public function edit(): void
276 {
277 $nextstep = acym_getVar('string', 'nextstep', '');
278 if (empty($nextstep)) {
279 $nextstep = acym_getVar('string', 'step', '');
280 }
281
282 if (empty($nextstep)) {
283 acym_setVar('layout', 'edit');
284
285 $this->display();
286 } else {
287 if (!is_callable([$this, $nextstep])) {
288 acym_enqueueMessage(acym_translation('ACYM_NON_EXISTING_PAGE'), 'warning');
289 $this->listing();
290
291 return;
292 }
293
294 acym_setVar('step', $nextstep);
295
296 $this->$nextstep();
297 }
298 }
299
300 public function apply(): void
301 {
302 if (method_exists($this, 'store')) {
303 $this->store();
304 }
305
306 $this->edit();
307 }
308
309 public function add(): void
310 {
311 acym_setVar('cid', []);
312 acym_setVar('layout', 'form');
313
314 $this->display();
315 }
316
317 public function save(): void
318 {
319 $step = acym_getVar('string', 'step', '');
320
321 if (!empty($step)) {
322 $saveMethod = 'save'.ucfirst($step);
323 if (!method_exists($this, $saveMethod)) {
324 die('Save method '.esc_html($saveMethod).' not found');
325 }
326
327 $this->$saveMethod();
328
329 return;
330 }
331
332 if (method_exists($this, 'store')) {
333 $this->store();
334 }
335
336 $this->listing();
337 }
338
339 public function delete(): void
340 {
341 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
342 $ids = acym_getVar('array', 'elements_checked', []);
343 $allChecked = acym_getVar('string', 'checkbox_all');
344 $currentPage = explode('_', acym_getVar('string', 'page', ''));
345 $pageNumber = $this->getVarFiltersListing('int', end($currentPage).'_pagination_page', 1);
346
347 if (!empty($ids) && !empty($this->currentClass)) {
348 $this->currentClass->delete($ids);
349 if ($allChecked === 'on') {
350 $this->setVarFiltersListing(end($currentPage).'_pagination_page', $pageNumber - 1);
351 }
352 }
353
354 if (!acym_getVar('bool', 'no_listing', false)) {
355 $this->listing();
356 }
357 }
358
359 public function setActive(): void
360 {
361 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
362 $ids = acym_getVar('array', 'elements_checked', []);
363
364 if (!empty($ids) && !empty($this->currentClass)) {
365 $this->currentClass->setActive($ids);
366 }
367
368 $this->listing();
369 }
370
371 public function setInactive(): void
372 {
373 wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');
374 $ids = acym_getVar('array', 'elements_checked', []);
375
376 if (!empty($ids) && !empty($this->currentClass)) {
377 $this->currentClass->setInactive($ids);
378 }
379
380 $this->listing();
381 }
382
383 public function getMatchingElementsFromData(array $requestData, string &$status, int &$page, string $class = ''): array
384 {
385 $className = 'AcyMailing\\Classes\\'.ucfirst(strtolower($class)).'Class';
386 $classElement = empty($class) ? $this->currentClass : new $className();
387 $matchingElement = $classElement->getMatchingElements($requestData);
388
389 // No result and no search used, we revert to default listing (all)
390 if (empty($matchingElement['elements'])) {
391 if (!empty($status) && empty($requestData['search']) && empty($requestData['tag'])) {
392 $status = '';
393 $requestData['status'] = $status;
394 } elseif (!empty($requestData['offset'])) {
395 $page = 1;
396 $requestData['offset'] = 0;
397 } else {
398 return $matchingElement;
399 }
400
401 $matchingElement = $classElement->getMatchingElements($requestData);
402 }
403
404 $matchingElement['total']->total = intval($matchingElement['total']->total);
405
406 return $matchingElement;
407 }
408
409 /**
410 * Used in front/acym.php
411 */
412 public function checkTaskFront(string $task = ''): void
413 {
414 // For cron tasks created by users on their own server, for Joomla 4
415 if ($this->getName() === 'cron') {
416 $task = 'cron';
417 }
418
419 if (empty($task) && !empty($this->defaulttask)) {
420 $task = $this->defaulttask;
421 }
422
423 // Handle whitelisted tasks
424 if (in_array($task, $this->publicFrontTasks)) {
425 $this->$task();
426
427 return;
428 }
429
430 // No front-end management for WordPress users
431 if (empty(acym_currentUserId()) || ACYM_CMS !== 'joomla' || !acym_level(ACYM_ENTERPRISE)) {
432 acym_redirect(acym_rootURI(), 'Front-end management not available');
433 }
434
435 // If there is no menu, someone typed the URL manually
436 $currentMenu = acym_getMenu();
437 if (empty($currentMenu)) {
438 acym_redirect(acym_rootURI(), 'Direct access denied');
439 }
440
441 // We whitelist specific routes when having access to specific menus
442 if ($this->isTaskAllowed($currentMenu->link, $task)) {
443 $this->$task();
444
445 return;
446 }
447
448 // Blacklist tasks by default
449 acym_redirect(acym_rootURI(), 'Unknown route, access denied');
450 }
451
452 private function isTaskAllowed(string $menuUrl, string $task): bool
453 {
454 if (!empty($this->allowedTasks[$menuUrl]) && in_array($task, $this->allowedTasks[$menuUrl])) {
455 return true;
456 }
457
458 if (!empty($this->menuAlias[$menuUrl])) {
459 $menuUrl = $this->menuAlias[$menuUrl];
460
461 return $this->isTaskAllowed($menuUrl, $task);
462 }
463
464 return false;
465 }
466
467 protected function prepareMultilingualOption(array &$data): void
468 {
469 if (!acym_isMultilingual()) return;
470
471 $data['translation_languages'] = acym_getMultilingualLanguages();
472 }
473 }
474