PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.9
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.9
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 1.6.0 All 67 releases
logtivity / functions / functions.php

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

260 lines 6.2 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 echo '<pre>';
36 var_export($dump);
37 echo '</pre>';
38 die();
39 }
40
41 /**
42 * Global function for easy use in themes/plugins for logging custom actions
43 *
44 * @param ?string $action
45 * @param ?string $meta
46 * @param ?string $user_id
47 *
48 * @return ?mixed
49 * @deprecated v3.1.8 - use Logtivity::log() instead
50 */
51 function logtivity_log(?string $action = null, ?string $meta = null, ?string $user_id = null): void
52 {
53 Logtivity::log($action, $meta, $user_id)->send();
54 }
55
56 /**
57 * Load a view and pass variables into it
58 *
59 * To output a view you would want to echo it
60 *
61 * @param string $fileName excluding file extension
62 * @param array $vars
63 *
64 * @return string
65 */
66 function logtivity_view(string $fileName, array $vars = []): string
67 {
68 extract($vars);
69 unset($vars);
70
71 ob_start();
72
73 include(__DIR__ . '/../views/' . str_replace('.', '/', $fileName) . '.php');
74
75 return ob_get_clean();
76 }
77
78 /**
79 * @param ?int $postId
80 *
81 * @return string
82 */
83 function logtivity_get_the_title(?int $postId): string
84 {
85 $wpTexturize = remove_filter('the_title', 'wptexturize');
86
87 $title = get_the_title($postId);
88
89 if ($wpTexturize) {
90 add_filter('the_title', 'wptexturize');
91 }
92
93 return $title;
94 }
95
96 /**
97 * @return string
98 */
99 function logtivity_get_api_url(): string
100 {
101 if (defined('LOGTIVITY_API_URL')) {
102 $customUrl = rtrim(sanitize_url(LOGTIVITY_API_URL), '/');
103 }
104
105 return $customUrl ?? logtivity_get_production_api_url();
106 }
107
108 /**
109 * @return string
110 */
111 function logtivity_get_production_api_url(): string
112 {
113 return 'https://api.logtivity.io';
114 }
115
116 /**
117 * @return string
118 */
119 function logtivity_get_app_url(): string
120 {
121 if (defined('LOGTIVITY_APP_URL')) {
122 $customUrl = rtrim(sanitize_url(LOGTIVITY_APP_URL), '/');
123 }
124
125 return $customUrl ?? logtivity_get_production_app_url();
126 }
127
128 /**
129 * @return string
130 */
131 function logtivity_get_production_app_url(): string
132 {
133 return 'https://app.logtivity.io';
134 }
135
136 /**
137 * @return bool
138 */
139 function logtivity_is_production(): bool
140 {
141 return logtivity_get_app_url() === logtivity_get_production_app_url()
142 && logtivity_get_api_url() === logtivity_get_production_api_url();
143 }
144
145 /**
146 * True when exactly one of the app/api URLs has been overridden away from
147 * production. This is a configuration error: the two endpoints must belong
148 * to the same environment.
149 *
150 * @return bool
151 */
152 function logtivity_url_environment_mismatch(): bool
153 {
154 $appIsProduction = logtivity_get_app_url() === logtivity_get_production_app_url();
155 $apiIsProduction = logtivity_get_api_url() === logtivity_get_production_api_url();
156
157 return $appIsProduction !== $apiIsProduction;
158 }
159
160 /**
161 * @return bool
162 */
163 function logtivity_has_site_url_changed(): bool
164 {
165 $hash = (new Logtivity_Options())->urlHash();
166
167 return $hash != md5(home_url());
168 }
169
170
171 /**
172 * @param array $array
173 *
174 * @return bool
175 */
176 function logtivity_is_associative(array $array): bool
177 {
178 foreach ($array as $key => $value) {
179 if (is_string($key)) {
180 return true;
181 }
182 }
183
184 return false;
185 }
186
187 /**
188 * Get all known capabilities
189 *
190 * @return array
191 */
192 function logtivity_get_capabilities(): array
193 {
194 $capabilities = [];
195 if ($roles = wp_roles()) {
196 foreach ($roles->roles as $role) {
197 $capabilities = array_merge($capabilities, $role['capabilities']);
198 }
199 }
200
201 return $capabilities;
202 }
203
204 /**
205 * @param string $label
206 *
207 * @return string
208 */
209 function logtivity_logger_label(string $label): string
210 {
211 global $wpdb;
212
213 return ucwords(
214 str_replace(
215 ['_', '-'], ' ',
216 str_replace($wpdb->get_blog_prefix(), '', $label)
217 )
218 );
219 }
220
221 /**
222 * @param mixed $value
223 *
224 * @return string
225 */
226 function logtivity_logger_value($value): string
227 {
228 $value = is_string($value) ? maybe_unserialize($value) : $value;
229
230 switch (gettype($value)) {
231 case 'NULL':
232 return 'NULL';
233
234 case 'object':
235 $value = get_object_vars($value);
236 // fall through to array type
237 case 'array':
238 return print_r($value, true);
239
240 case 'boolean':
241 return $value ? 'True' : 'False';
242
243 default:
244 return empty($value) ? '[Empty]' : $value;
245 }
246 }
247
248 /**
249 * @param string $time
250 *
251 * @return string
252 */
253 function logtivity_datetime(string $time): string
254 {
255 $format = get_option('date_format') . ' ' . get_option('time_format');
256
257 return get_date_from_gmt($time, $format);
258 }
259 }
260