| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, hello@logtivity.io |
| 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_production_app_url() == logtivity_get_app_url() |
| 142 |
|| logtivity_get_production_api_url() == logtivity_get_api_url(); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
function logtivity_has_site_url_changed(): bool |
| 149 |
{ |
| 150 |
$hash = (new Logtivity_Options())->urlHash(); |
| 151 |
|
| 152 |
return $hash != md5(home_url()); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* @param array $array |
| 158 |
* |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
function logtivity_is_associative(array $array): bool |
| 162 |
{ |
| 163 |
foreach ($array as $key => $value) { |
| 164 |
if (is_string($key)) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get all known capabilities |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
function logtivity_get_capabilities(): array |
| 178 |
{ |
| 179 |
$capabilities = []; |
| 180 |
if ($roles = wp_roles()) { |
| 181 |
foreach ($roles->roles as $role) { |
| 182 |
$capabilities = array_merge($capabilities, $role['capabilities']); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return $capabilities; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @param string $label |
| 191 |
* |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
function logtivity_logger_label(string $label): string |
| 195 |
{ |
| 196 |
global $wpdb; |
| 197 |
|
| 198 |
return ucwords( |
| 199 |
str_replace( |
| 200 |
['_', '-'], ' ', |
| 201 |
str_replace($wpdb->get_blog_prefix(), '', $label) |
| 202 |
) |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @param mixed $value |
| 208 |
* |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
function logtivity_logger_value($value): string |
| 212 |
{ |
| 213 |
$value = is_string($value) ? maybe_unserialize($value) : $value; |
| 214 |
|
| 215 |
switch (gettype($value)) { |
| 216 |
case 'NULL': |
| 217 |
return 'NULL'; |
| 218 |
|
| 219 |
case 'object': |
| 220 |
$value = get_object_vars($value); |
| 221 |
// fall through to array type |
| 222 |
case 'array': |
| 223 |
return print_r($value, true); |
| 224 |
|
| 225 |
case 'boolean': |
| 226 |
return $value ? 'True' : 'False'; |
| 227 |
|
| 228 |
default: |
| 229 |
return empty($value) ? '[Empty]' : $value; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* @param string $time |
| 235 |
* |
| 236 |
* @return string |
| 237 |
*/ |
| 238 |
function logtivity_datetime(string $time): string |
| 239 |
{ |
| 240 |
$format = get_option('date_format') . ' ' . get_option('time_format'); |
| 241 |
|
| 242 |
return get_date_from_gmt($time, $format); |
| 243 |
} |
| 244 |
} |
| 245 |
|