PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 All 68 releases
logtivity / functions / functions.php

functions.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at functions/functions.php

272 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, [email protected]
6 * @copyright 2024-2026 Logtivity. All rights reserved
7 * @license https://www.gnu.org/licenses/gpl.html GNU/GPL
8 *
9 * This file is part of Logtivity.
10 *
11 * Logtivity is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Logtivity is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Logtivity. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25 if (function_exists('logtivity_dd') == false) {
26 /**
27 *
28 * Dump and Die variable or whatever
29 *
30 * @param $dump
31 *
32 */
33 function logtivity_dd($dump)
34 {
35 if (!defined('WP_DEBUG') || !WP_DEBUG) {
36 return;
37 }
38
39 echo '<pre>';
40 echo esc_html(wp_json_encode($dump, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
41 echo '</pre>';
42 wp_die();
43 }
44
45 /**
46 * Global function for easy use in themes/plugins for logging custom actions
47 *
48 * @param ?string $action
49 * @param ?string $meta
50 * @param ?string $user_id
51 *
52 * @return ?mixed
53 * @deprecated v3.1.8 - use Logtivity::log() instead
54 */
55 function logtivity_log(?string $action = null, ?string $meta = null, ?string $user_id = null): void
56 {
57 Logtivity::log($action, $meta, $user_id)->send();
58 }
59
60 /**
61 * Load a view and pass variables into it
62 *
63 * To output a view you would want to echo it
64 *
65 * @param string $fileName excluding file extension
66 * @param array $vars
67 *
68 * @return string
69 */
70 function logtivity_view(string $fileName, array $vars = []): string
71 {
72 $options = $vars['options'] ?? [];
73 $log = $vars['log'] ?? null;
74 $logs = $vars['logs'] ?? [];
75 $message = $vars['message'] ?? null;
76 $meta = $vars['meta'] ?? null;
77 $hasNextPage = $vars['hasNextPage'] ?? null;
78 $display = $vars['display'] ?? false;
79 $logo = $vars['logo'] ?? true;
80 unset($vars);
81
82 ob_start();
83
84 include(__DIR__ . '/../views/' . str_replace('.', '/', $fileName) . '.php');
85
86 return ob_get_clean();
87 }
88
89 /**
90 * @param ?int $postId
91 *
92 * @return string
93 */
94 function logtivity_get_the_title(?int $postId): string
95 {
96 $wpTexturize = remove_filter('the_title', 'wptexturize');
97
98 $title = get_the_title($postId);
99
100 if ($wpTexturize) {
101 add_filter('the_title', 'wptexturize');
102 }
103
104 return $title;
105 }
106
107 /**
108 * @return string
109 */
110 function logtivity_get_api_url(): string
111 {
112 if (defined('LOGTIVITY_API_URL')) {
113 $customUrl = rtrim(sanitize_url(LOGTIVITY_API_URL), '/');
114 }
115
116 return $customUrl ?? logtivity_get_production_api_url();
117 }
118
119 /**
120 * @return string
121 */
122 function logtivity_get_production_api_url(): string
123 {
124 return 'https://api.logtivity.io';
125 }
126
127 /**
128 * @return string
129 */
130 function logtivity_get_app_url(): string
131 {
132 if (defined('LOGTIVITY_APP_URL')) {
133 $customUrl = rtrim(sanitize_url(LOGTIVITY_APP_URL), '/');
134 }
135
136 return $customUrl ?? logtivity_get_production_app_url();
137 }
138
139 /**
140 * @return string
141 */
142 function logtivity_get_production_app_url(): string
143 {
144 return 'https://app.logtivity.io';
145 }
146
147 /**
148 * @return bool
149 */
150 function logtivity_is_production(): bool
151 {
152 return logtivity_get_app_url() === logtivity_get_production_app_url()
153 && logtivity_get_api_url() === logtivity_get_production_api_url();
154 }
155
156 /**
157 * True when exactly one of the app/api URLs has been overridden away from
158 * production. This is a configuration error: the two endpoints must belong
159 * to the same environment.
160 *
161 * @return bool
162 */
163 function logtivity_url_environment_mismatch(): bool
164 {
165 $appIsProduction = logtivity_get_app_url() === logtivity_get_production_app_url();
166 $apiIsProduction = logtivity_get_api_url() === logtivity_get_production_api_url();
167
168 return $appIsProduction !== $apiIsProduction;
169 }
170
171 /**
172 * @return bool
173 */
174 function logtivity_has_site_url_changed(): bool
175 {
176 $hash = (new Logtivity_Options())->urlHash();
177
178 return $hash != md5(home_url());
179 }
180
181
182 /**
183 * @param array $array
184 *
185 * @return bool
186 */
187 function logtivity_is_associative(array $array): bool
188 {
189 foreach ($array as $key => $value) {
190 if (is_string($key)) {
191 return true;
192 }
193 }
194
195 return false;
196 }
197
198 /**
199 * Get all known capabilities
200 *
201 * @return array
202 */
203 function logtivity_get_capabilities(): array
204 {
205 $capabilities = [];
206 $roles = wp_roles();
207 if ($roles) {
208 foreach ($roles->roles as $role) {
209 $capabilities = array_merge($capabilities, $role['capabilities']);
210 }
211 }
212
213 return $capabilities;
214 }
215
216 /**
217 * @param string $label
218 *
219 * @return string
220 */
221 function logtivity_logger_label(string $label): string
222 {
223 global $wpdb;
224
225 return ucwords(
226 str_replace(
227 ['_', '-'], ' ',
228 str_replace($wpdb->get_blog_prefix(), '', $label)
229 )
230 );
231 }
232
233 /**
234 * @param mixed $value
235 *
236 * @return string
237 */
238 function logtivity_logger_value($value): string
239 {
240 $value = is_string($value) ? maybe_unserialize($value) : $value;
241
242 switch (gettype($value)) {
243 case 'NULL':
244 return 'NULL';
245
246 case 'object':
247 $value = get_object_vars($value);
248 // fall through to array type
249 case 'array':
250 return wp_json_encode($value, JSON_UNESCAPED_SLASHES) ?: '';
251
252 case 'boolean':
253 return $value ? 'True' : 'False';
254
255 default:
256 return empty($value) ? '[Empty]' : $value;
257 }
258 }
259
260 /**
261 * @param string $time
262 *
263 * @return string
264 */
265 function logtivity_datetime(string $time): string
266 {
267 $format = get_option('date_format') . ' ' . get_option('time_format');
268
269 return get_date_from_gmt($time, $format);
270 }
271 }
272