PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.0.1
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 / Helpers / EditorHelper.php

EditorHelper.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.0.1, at back/Helpers/EditorHelper.php

402 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Helpers;
4
5 use AcyMailing\Classes\MailClass;
6 use AcyMailing\Classes\ZoneClass;
7 use AcyMailing\Core\AcymObject;
8 use AcyMailing\Types\DtextType;
9 use Joomla\CMS\Editor\Editor as Editor;
10 use Joomla\CMS\Factory;
11
12 class EditorHelper extends AcymObject
13 {
14 var $width = '95%';
15 var $height = '600';
16
17 var $cols = 100;
18 var $rows = 30;
19
20 var $myEditor;
21 var $editor = '';
22 var $name = 'editor_content';
23 var $settings = 'editor_settings';
24 var $stylesheet = 'editor_stylesheet';
25 var $thumbnail = 'editor_thumbnail';
26 var $content = '';
27 var $editorContent = '';
28 var $editorConfig = [];
29 var $mailId = 0;
30 var $automation = false;
31 var $walkThrough = false;
32 var $emailsTest;
33
34 var $data = [];
35 var $defaultTemplate = '';
36
37 public $autoSave;
38
39 public function display()
40 {
41 if ($this->isDragAndDrop()) {
42 ob_start();
43 include acym_getPartial('editor', 'default_template');
44 $this->defaultTemplate = ob_get_clean();
45
46 acym_disableCmsEditor();
47 $currentEmail = acym_currentUserEmail();
48 $this->emailsTest = [$currentEmail => $currentEmail];
49 acym_addScript(false, ACYM_JS.'tinymce/tinymce.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'tinymce/tinymce.min.js'));
50
51 $data = $this->data;
52 $mailId = empty($data['mail']->mail_id) ? (empty($data['mail']->id) ? 0 : $data['mail']->id) : $data['mail']->mail_id;
53 $data['tabHelper'] = new TabHelper();
54 $data['plugins'] = acym_trigger('dynamicText', [$mailId]);
55 usort(
56 $data['plugins'],
57 function ($a, $b) {
58 return strtolower($a->name ?? '') > strtolower($b->name ?? '') ? 1 : -1;
59 }
60 );
61
62 $zoneClass = new ZoneClass();
63 $data['custom_zones'] = $zoneClass->getAll();
64 usort($data['custom_zones'], function ($a, $b) {
65 return strtolower($a->name) > strtolower($b->name) ? 1 : -1;
66 });
67
68 acym_setVar('mail_id', $mailId);
69 acym_setVar('mail_type', empty($data['mail']->type) ? '' : $data['mail']->type);
70 acym_setVar('automation', $this->automation ? 1 : 0);
71
72 include acym_getPartial('editor', 'editor_wysid');
73 } else {
74
75 if (acym_isLeftMenuNecessary()) echo '</div>';
76
77 echo '</div><div class="acym_no_foundation">';
78
79 $dtextType = new DtextType();
80 $dtextType->display(['withButton' => false]);
81
82 $method = 'displayWordPress';
83 $this->$method();
84
85 if (acym_isLeftMenuNecessary()) echo '<div>';
86 }
87 }
88
89 public function isDragAndDrop()
90 {
91 return strpos($this->content, 'acym__wysid__template') !== false || $this->editor == 'acyEditor';
92 }
93
94 private function displayJoomla()
95 {
96 $dtextType = new DtextType();
97 $dtextType->displayButton(
98 [
99 'icon' => 'acymicon-chevrons',
100 'text' => acym_translation('ACYM_INSERT_DYNAMIC_TEXT'),
101 'class' => 'button button-secondary margin-bottom-0 margin-top-1',
102 ]
103 );
104
105 $this->editor = acym_getCMSConfig('editor', 'tinymce');
106
107 if (!class_exists('Joomla\CMS\Editor\Editor')) {
108 $this->myEditor = Factory::getEditor($this->editor);
109 } else {
110 $this->myEditor = Editor::getInstance($this->editor);
111 }
112 $this->myEditor->initialise();
113
114 $this->editorConfig['extended_elements'] = 'table[background|cellspacing|cellpadding|width|align|bgcolor|border|style|class|id],tr[background|width|bgcolor|style|class|id|valign],td[background|width|align|bgcolor|valign|colspan|rowspan|height|style|class|id|nowrap]';
115
116 if (!empty($this->mailId)) {
117 $cssurl = acym_completeLink((acym_isAdmin() ? '' : 'front').'mails&task=loadCSS&id='.$this->mailId.'&time='.time());
118 $classMail = new MailClass();
119 $filepath = $classMail->createTemplateFile($this->mailId);
120
121 if ($this->editor === 'tinymce') {
122 $this->editorConfig['content_css_custom'] = $cssurl.'&local=http';
123 $this->editorConfig['content_css'] = '0';
124
125 if (!ACYM_J40) {
126 $access = [];
127 for ($i = 1 ; $i < 20 ; $i++) {
128 $access[] = $i;
129 }
130
131 $this->editorConfig['configuration'] = (object)[
132 'toolbars' => (object)['AcyCustomCSS' => []],
133 'setoptions' => [
134 'AcyCustomCSS' => (object)[
135 'access' => $access,
136 'content_css' => '0',
137 'content_css_custom' => $cssurl.'&local=http',
138 ],
139 ],
140 ];
141 }
142 } elseif ($this->editor === 'jckeditor' || $this->editor === 'fckeditor') {
143 $this->editorConfig['content_css_custom'] = $filepath;
144 $this->editorConfig['content_css'] = '0';
145 $this->editorConfig['editor_css'] = '0';
146 } else {
147 $fileurl = str_replace(DS, '/', ACYM_MEDIA_FOLDER).'templates/css/template_'.$this->mailId.'.css?time='.time();
148 $this->editorConfig['custom_css_url'] = $cssurl;
149 $this->editorConfig['custom_css_file'] = $fileurl;
150 $this->editorConfig['custom_css_path'] = $filepath;
151 acym_setVar('acycssfile', $fileurl);
152 }
153 }
154
155 if (empty($this->editorContent)) {
156 $this->content = acym_escape($this->content);
157 ob_start();
158
159 echo $this->myEditor->display(
160 $this->name,
161 $this->content,
162 $this->width,
163 $this->height,
164 $this->cols,
165 $this->rows,
166 ['pagebreak', 'readmore'],
167 null,
168 'com_content',
169 null,
170 $this->editorConfig
171 );
172
173 $this->editorContent = ob_get_clean();
174 }
175
176 if (method_exists($this->myEditor, 'save')) {
177 acym_addScript(true, 'function acyOnSaveEditor(){'.$this->myEditor->save($this->name).'}');
178 }
179
180 echo $this->editorContent;
181 }
182
183 private function displayWordPress()
184 {
185 add_filter('mce_external_plugins', [$this, 'addPlugins']);
186 add_filter('mce_buttons', [$this, 'addButtons']);
187 add_filter('mce_buttons_2', [$this, 'addButtonsToolbar']);
188 add_action('media_buttons', [$this, 'addDtextButton']);
189
190 $mailClass = new MailClass();
191
192 $mail = $mailClass->getOneById($this->mailId);
193 $stylesheet = empty($mail) ? '' : trim(preg_replace('/\s\s+/', ' ', $mailClass->buildCSS($mail->stylesheet)));
194 $stylesheet = str_replace(['"', "\r\n", "\n"], ['\"', '', ''], $stylesheet);
195
196 $options = [
197 'editor_css' => '<style type="text/css">
198 .alignleft{float:left;margin:0.5em 1em 0.5em 0;}
199 .aligncenter{display: block;margin-left: auto;margin-right: auto;}
200 .alignright{float: right;margin: 0.5em 0 0.5em 1em;}
201 </style>',
202 'editor_height' => $this->height,
203 'textarea_rows' => $this->rows,
204 'wpautop' => false,
205 'tinymce' => [
206 'content_css' => '',
207 'content_style' => '.alignleft{float:left;margin:0.5em 1em 0.5em 0;} .aligncenter{display: block;margin-left: auto;margin-right: auto;} .alignright{float: right;margin: 0.5em 0 0.5em 1em;}'.$stylesheet,
208 ],
209 ];
210
211 wp_editor($this->content, $this->name, $options);
212 }
213
214 public function addDtextButton($editor_id = 'content')
215 {
216 static $instance = 0;
217 ++$instance;
218
219 $img = '<i class="acymicon-chevrons"></i> ';
220
221 printf(
222 '<button type="button" class="button" id="acym__dtext__button" data-editor="%s">%s</button>',
223 esc_attr($editor_id),
224 $img.acym_translation('ACYM_INSERT_DYNAMIC_TEXT')
225 );
226 }
227
228 private function getWYSIDSettings(): string
229 {
230 $ctrl = acym_getVar('string', 'ctrl');
231 if ($this->isResetCampaign() || !in_array($ctrl, ['dashboard', 'mails', 'campaigns', 'frontmails', 'frontcampaigns'])) {
232 return '{}';
233 }
234
235 $mailId = acym_getVar('int', 'from', 0);
236 if (empty($mailId)) {
237 if ($this->settings !== 'editor_settings') {
238 return $this->settings;
239 }
240
241 $mailId = acym_getVar('int', 'id');
242 $campaignId = acym_getVar('int', 'campaignId');
243 if (!empty($campaignId) && $ctrl === 'campaigns') {
244 $mailId = acym_loadResult('SELECT mail_id FROM #__acym_campaign WHERE id = '.intval($campaignId));
245 }
246
247 if (empty($mailId)) {
248 return '{}';
249 }
250 }
251
252 $settings = acym_loadResult('SELECT settings FROM #__acym_mail WHERE id = '.intval($mailId));
253
254 return empty($settings) ? '{}' : $settings;
255 }
256
257 private function getWYSIDStylesheet(): string
258 {
259 $ctrl = acym_getVar('string', 'ctrl');
260 if ($this->isResetCampaign() || !in_array($ctrl, ['mails', 'campaigns', 'frontmails', 'frontcampaigns'])) {
261 return '';
262 }
263
264 $mailId = acym_getVar('int', 'from', 0);
265 if (empty($mailId)) {
266 if ($this->stylesheet !== 'editor_stylesheet') {
267 return $this->stylesheet;
268 }
269
270 $mailId = acym_getVar('int', 'id');
271 $campaignId = acym_getVar('int', 'campaignId');
272 if (!empty($campaignId) && in_array($ctrl, ['campaigns', 'frontcampaigns'])) {
273 $mailId = acym_loadResult('SELECT mail_id FROM #__acym_campaign WHERE id = '.intval($campaignId));
274 }
275 }
276
277 $notification = acym_getVar('string', 'notification');
278 if (!empty($mailId)) {
279 $stylesheet = acym_loadResult('SELECT stylesheet FROM #__acym_mail WHERE id = '.intval($mailId));
280 } elseif (!empty($notification)) {
281 $stylesheet = acym_loadResult(
282 'SELECT stylesheet
283 FROM #__acym_mail
284 WHERE `type` = '.acym_escapeDB(MailClass::TYPE_NOTIFICATION).'
285 AND `name` = '.acym_escapeDB($notification)
286 );
287 }
288
289 return empty($stylesheet) ? '' : $stylesheet;
290 }
291
292 private function isResetCampaign()
293 {
294 $fromId = acym_getVar('int', 'from', 0);
295
296 return -1 == $fromId;
297 }
298
299 private function getWYSIDThumbnail()
300 {
301 if ($this->thumbnail != 'editor_thumbnail') return $this->thumbnail;
302
303 $id = acym_getVar('int', 'id');
304 if (empty($id)) return null;
305
306 $thumbnail = acym_loadResult('SELECT thumbnail FROM #__acym_mail WHERE id = '.intval($id));
307
308 return empty($thumbnail) ? '' : $thumbnail;
309 }
310
311 private function addButtonAtPosition(&$buttons, $newButton, $after)
312 {
313 $position = array_search($after, $buttons);
314
315 if ($position === false) {
316 array_push($buttons, 'separator', $newButton);
317 } else {
318 array_splice($buttons, $position + 1, 0, $newButton);
319 }
320 }
321
322 public function addPlugins($plugins)
323 {
324 $plugins['table'] = ACYM_JS.'tinymce/table.min.js';
325
326 return $plugins;
327 }
328
329 public function addButtons($buttons)
330 {
331 $position = array_search('wp_more', $buttons);
332 if ($position !== false) {
333 $buttons[$position] = '';
334 }
335
336 array_unshift($buttons, 'separator', 'fontsizeselect');
337 array_unshift($buttons, 'separator', 'fontselect');
338 array_push($buttons, 'separator', 'table');
339
340 $this->addButtonAtPosition($buttons, 'alignjustify', 'alignright');
341 $this->addButtonAtPosition($buttons, 'underline', 'italic');
342 $this->addButtonAtPosition($buttons, 'strikethrough', 'underline');
343
344 return $buttons;
345 }
346
347 public function addButtonsToolbar($buttons)
348 {
349 $position = array_search('strikethrough', $buttons);
350 if ($position !== false) {
351 $buttons[$position] = '';
352 }
353 $this->addButtonAtPosition($buttons, 'backcolor', 'forecolor');
354
355 return $buttons;
356 }
357
358 public function getSettingsStyle($settings)
359 {
360 if (empty($settings)) return '';
361 if (is_string($settings) && substr($settings, 0, 2) === '{"') {
362 $settings = json_decode($settings, true);
363 }
364
365 if (!is_array($settings)) return '';
366
367 $styles = '';
368 foreach ($settings as $element => $rules) {
369 $values = '';
370 foreach ($rules as $ruleName => $value) {
371 if ($ruleName == 'overridden') continue;
372 $values .= $ruleName.': '.$value.';';
373 }
374 $styles .= '#acym__wysid__template .acym__wysid__column__element__td .acym__wysid__tinymce--text '.$element.':not(.acym__wysid__content-no-settings-style){';
375 $styles .= $values;
376 $styles .= '}';
377
378 $styles .= '#acym__wysid__template .acym__wysid__column__element__td .acymailing_content '.$element.':not(.acym__wysid__content-no-settings-style){';
379 $styles .= $values;
380 $styles .= '}';
381 }
382
383 return $styles;
384 }
385
386 private function getDefaultColors()
387 {
388 $mailSettings = new \stdClass();
389 if (isset($this->data['mail']->mail_settings) && !empty($this->data['mail']->mail_settings)) {
390 $mailSettings = json_decode($this->data['mail']->mail_settings, false);
391 } elseif (isset($this->data['mailInformation']->mail_settings) && !empty($this->data['mailInformation']->mail_settings)) {
392 $mailSettings = json_decode($this->data['mailInformation']->mail_settings, false);
393 }
394
395 if (!empty($mailSettings->mainColors)) {
396 return $mailSettings->mainColors;
397 }
398
399 return '';
400 }
401 }
402