PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / trunk
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress vtrunk
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 / global / global.php

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

244 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || die('Restricted Access');
3
4 use AcyMailing\Classes\ConfigurationClass;
5 use AcyMailing\Helpers\SecurityHelper;
6
7 /**
8 * @param ?mixed $arg
9 */
10 function acym_dump($arg, bool $ajax = false, array $options = []): void
11 {
12 $indent = $options['indent'] ?? true;
13 $htmlentities = $options['htmlentities'] ?? true;
14
15 ob_start();
16 if (is_object($arg) && isset($arg->config)) {
17 $safeArg = clone $arg;
18 unset($safeArg->config);
19 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump -- For debug during support only.
20 var_dump($safeArg);
21 } else {
22 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump -- For debug during support only.
23 var_dump($arg);
24 }
25 $result = ob_get_clean();
26
27 if ($ajax) {
28 $logsPath = ACYM_ROOT.ACYM_LOGS_FOLDER.'debug.log';
29 acym_createDir(dirname($logsPath), true, true);
30
31 if (($options['clear_file'] ?? false) === true) {
32 file_put_contents($logsPath, '');
33 }
34
35 file_put_contents($logsPath, $result, FILE_APPEND);
36 } else {
37 $style = $indent ? 'margin-left: 220px;' : '';
38 echo '<pre style="'.esc_attr($style).'">';
39 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- For debug during support only.
40 echo $htmlentities ? htmlentities($result) : $result;
41 echo '</pre>';
42 }
43 }
44
45 function acym_debug(bool $file = false, bool $indent = true)
46 {
47 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Debug for support tickets.
48 $debug = debug_backtrace();
49 $takenPath = [];
50 foreach ($debug as $step) {
51 if (empty($step['file']) || empty($step['line'])) continue;
52 $takenPath[] = $step['file'].' => '.$step['line'];
53 }
54 acym_dump(implode("\n", $takenPath), $file, ['indent' => $indent]);
55 }
56
57 function acym_config(bool $reload = false): ConfigurationClass
58 {
59 static $configClass = null;
60 if ($configClass === null || $reload) {
61 $configClass = new ConfigurationClass();
62 $configClass->load();
63 }
64
65 return $configClass;
66 }
67
68 /**
69 * @param mixed $messages The messages displayed, either a string or an array or strings
70 * @param string $type Type of the message: success, error, warning, info
71 * @param bool $close Allow or not to close the message zone
72 */
73 function acym_display($messages, string $type = 'success', bool $close = true): void
74 {
75 if (empty($messages)) return;
76 if (!is_array($messages)) $messages = [$messages];
77 $config = acym_config();
78 $remindme = json_decode($config->get('remindme', '[]'), true);
79 foreach ($messages as $id => $message) {
80 if (strpos($message, 'acym__do__not__remindme') !== false) {
81 if (preg_match('/title="(.*)"/Ui', $message, $matches) && in_array($matches[1], $remindme)) {
82 continue;
83 }
84 }
85
86 echo '<div class="acym__message grid-x acym__message__'.esc_attr($type).'">';
87
88 if (is_array($message)) $message = implode('</div><div>', $message);
89
90 echo '<div class="cell auto"><div>'.wp_kses($message, SecurityHelper::ALLOWED_HTML_MESSAGE).'</div></div>';
91
92 if ($close && strpos($message, 'acym__do__not__remindme') === false) {
93 echo '<i data-id="'.esc_attr($id).'" class="cell shrink acym__message__close acymicon-close"></i>';
94 }
95 echo '</div>';
96 }
97 }
98
99 function acym_increasePerf(): int
100 {
101 // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- This is for big regex, the default value is 100 000.
102 @ini_set('pcre.backtrack_limit', 1000000);
103
104 // Increase the max exec time to be able to handle long processes such as the send process
105 $maxExecutionTime = (int)ini_get('max_execution_time');
106
107 if (empty($maxExecutionTime) || $maxExecutionTime < 600) {
108 // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Needed for long processes like email sending.
109 return set_time_limit(600) ? 600 : $maxExecutionTime;
110 }
111
112 return $maxExecutionTime;
113 }
114
115 function acym_session(): void
116 {
117 if (empty(session_id()) || session_status() !== PHP_SESSION_ACTIVE) {
118 @session_start();
119 }
120 }
121
122 function acym_getCID(string $field = ''): int
123 {
124 $oneResult = acym_getVar('array', 'cid', [], '');
125 $oneResult = intval(reset($oneResult));
126 if (!empty($oneResult) || empty($field)) {
127 return $oneResult;
128 }
129
130 $oneResult = acym_getVar('int', $field, 0, '');
131
132 return intval($oneResult);
133 }
134
135 function acym_header(string $header, bool $replace = true): void
136 {
137 if (headers_sent()) {
138 return;
139 }
140
141 header($header, $replace);
142 }
143
144 function acym_getSocialMedias(): array
145 {
146 return json_decode(ACYM_SOCIAL_MEDIA, true);
147 }
148
149 function acym_isAcyCheckerInstalled(): bool
150 {
151 $installed = ACYM_CMS === 'joomla' && acym_isExtensionActive('com_acychecker');
152
153 return $installed || (ACYM_CMS === 'wordpress' && acym_isExtensionActive('acychecker/acychecker.php'));
154 }
155
156 function acym_getErrorLogFilename(string $prefix = ''): string
157 {
158 if (!empty($prefix)) {
159 $prefix .= '_';
160 }
161
162 return $prefix.'errors.log';
163 }
164
165 function acym_logError(string $message, string $prefix = '', int $maxLines = 0)
166 {
167 $reportPath = acym_getLogPath(acym_getErrorLogFilename($prefix), true);
168
169 if ($maxLines > 0 && file_exists($reportPath)) {
170 $lines = file($reportPath);
171 if (!empty($lines)) {
172 $lines = array_slice($lines, -$maxLines);
173 file_put_contents($reportPath, implode("\n", $lines));
174 }
175 }
176
177 $lr = "\r\n";
178 file_put_contents(
179 $reportPath,
180 $lr.acym_getDate(time()).': '.$message,
181 FILE_APPEND
182 );
183 }
184
185 function acym_isLogFileErrorExist(string $prefix = ''): bool
186 {
187 $reportPath = acym_getLogPath(acym_getErrorLogFilename($prefix));
188
189 return file_exists($reportPath);
190 }
191
192 function acym_getJsonData(): array
193 {
194 $rawData = file_get_contents('php://input');
195 $decodedData = @json_decode($rawData, true);
196
197 return empty($decodedData) ? [] : $decodedData;
198 }
199
200 function acym_displayFreeTrialMessage(): void
201 {
202 if (!acym_isAdmin()) {
203 return;
204 }
205
206 $config = acym_config();
207
208 if ($config->get('isTrial', 0) != 1) {
209 return;
210 }
211
212 $expirationDate = $config->get('expirationdate', 0);
213 $href = ACYM_ACYMAILING_WEBSITE.'account/license';
214 if ($config->get('expirationdate', 0) > time()) {
215 $days = ceil(($expirationDate - time()) / 86400);
216 $buttonFullAccess = '<a target="_blank" class="button button-secondary margin-left-1 shrink margin-bottom-0" href="'.$href.'">'.acym_translation(
217 'ACYM_GET_FULL_ACCESS'
218 ).'</a>';
219 $message = '<span class="shrink">'.acym_translationSprintf('ACYM_FREE_TRIAL_EXPIRATION_X_DAYS', $days).'</span>';
220 $type = 'warning';
221 } else {
222 $buttonFullAccess = '<a target="_blank" class="button acym__button__upgrade margin-left-1 shrink margin-bottom-0" href="'.$href.'">'.acym_translation(
223 'ACYM_GET_FULL_ACCESS'
224 ).'</a>';
225 $message = '<span class="shrink">'.acym_translation('ACYM_FREE_TRIAL_ENDED').'</span>';
226 $type = 'error';
227 }
228 acym_enqueueMessage('<div class="cell grid-x acym_vcenter">'.$message.$buttonFullAccess.'</div>', $type, false);
229 }
230
231 function acym_removeDashboardNotification(string $name): void
232 {
233 $config = acym_config();
234 $existingNotifications = json_decode($config->get('dashboard_notif', '[]'), true) ?? [];
235
236 foreach ($existingNotifications as $key => $existingNotification) {
237 if (is_array($existingNotification) && $existingNotification['name'] === $name) {
238 unset($existingNotifications[$key]);
239 }
240 }
241
242 $config->saveConfig(['dashboard_notif' => json_encode($existingNotifications)], false);
243 }
244