| 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 |
class Logtivity_Core extends Logtivity_Abstract_Logger |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var string[] |
| 29 |
*/ |
| 30 |
protected array $ignoredOptions = [ |
| 31 |
'_edd_table_check', |
| 32 |
'_wp_suggested_policy_text_has_changed', |
| 33 |
'action_scheduler_lock_async-request-runner', |
| 34 |
'active_plugins', |
| 35 |
'admin_email_lifespan', |
| 36 |
'adminhash', |
| 37 |
'akismet_spam_count', |
| 38 |
'allowedthemes', |
| 39 |
'auto_plugin_theme_update_emails', |
| 40 |
'cron', |
| 41 |
'db_upgraded', |
| 42 |
'delete_blog_hash', |
| 43 |
'fresh_site', |
| 44 |
'ftp_credentials', |
| 45 |
'gmt_offset', |
| 46 |
'jetpack_next_sync_time_sync', |
| 47 |
'jetpack_plugin_api_action_links', |
| 48 |
'jetpack_protect_blocked_attempts', |
| 49 |
'jetpack_sync_settings_dedicated_sync_enabled', |
| 50 |
'jetpack_updates_sync_checksum', |
| 51 |
'jp_sync_last_success_sync', |
| 52 |
'jp_sync_retry_after_sync', |
| 53 |
'limit_login_retries', |
| 54 |
'mepr_coupons_expire_last_run', |
| 55 |
'mepr_groups_db_cleanup_last_run', |
| 56 |
'mepr_products_db_cleanup_last_run', |
| 57 |
'mepr_rules_db_cleanup_last_run', |
| 58 |
'post_views_count', |
| 59 |
'postman_state', |
| 60 |
'recently_activated', |
| 61 |
'recently_edited', |
| 62 |
'rewrite_rules', |
| 63 |
'rxpp_blocked_methods_count', |
| 64 |
'stats_cache', |
| 65 |
'uninstall_plugins', |
| 66 |
'woocommerce_marketplace_suggestions', |
| 67 |
'wordfence_syncAttackDataAttempts', |
| 68 |
'wp_all_export_pro_addons_not_included', |
| 69 |
'wp_force_deactivated_plugins', |
| 70 |
'wpcf7', |
| 71 |
'ws_form_css', |
| 72 |
]; |
| 73 |
|
| 74 |
/** |
| 75 |
* @var string[] |
| 76 |
*/ |
| 77 |
protected array $ignoredWildcardOptions = [ |
| 78 |
'auto_updater', |
| 79 |
'cache', |
| 80 |
'cron', |
| 81 |
'edd_api', |
| 82 |
'edd_sl', |
| 83 |
'frm_', |
| 84 |
'last_run', |
| 85 |
'logtivity_api', |
| 86 |
'logtivity_app', |
| 87 |
'logtivity_custom_plugin_name', |
| 88 |
'logtivity_disable', |
| 89 |
'logtivity_enable', |
| 90 |
'logtivity_global', |
| 91 |
'logtivity_hide_plugin_from_ui', |
| 92 |
'logtivity_last_settings_check_in_at', |
| 93 |
'logtivity_latest_response', |
| 94 |
'logtivity_should', |
| 95 |
'logtivity_site_api_key', |
| 96 |
'logtivity_url_hash', |
| 97 |
'logtivity_version', |
| 98 |
'queue', |
| 99 |
'sync', |
| 100 |
'transient', |
| 101 |
'wpe', |
| 102 |
]; |
| 103 |
|
| 104 |
/** |
| 105 |
* @inheritDoc |
| 106 |
*/ |
| 107 |
protected function registerHooks(): void |
| 108 |
{ |
| 109 |
add_action('upgrader_process_complete', [$this, 'upgradeProcessComplete'], 10, 2); |
| 110 |
add_action('wp_update_nav_menu', [$this, 'menuUpdated'], 10, 2); |
| 111 |
add_action('init', [$this, 'maybeSettingsUpdated'], 10, 1); |
| 112 |
add_action('permalink_structure_changed', [$this, 'permalinksUpdated'], 10, 2); |
| 113 |
|
| 114 |
if (get_option('logtivity_enable_options_table_logging')) { |
| 115 |
add_action('update_option', [$this, 'optionUpdated'], 10, 3); |
| 116 |
} |
| 117 |
|
| 118 |
add_filter('widget_update_callback', [$this, 'widgetUpdated'], 10, 4); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param WP_Upgrader $upgrader |
| 123 |
* @param array $options |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function upgradeProcessComplete(WP_Upgrader $upgrader, array $options): void |
| 128 |
{ |
| 129 |
$type = $options['type'] ?? null; |
| 130 |
if ($type == 'core') { |
| 131 |
$action = $options['action'] ?? null; |
| 132 |
|
| 133 |
switch ($action) { |
| 134 |
case 'update': |
| 135 |
Logtivity::log('WP Core Updated')->send(); |
| 136 |
break; |
| 137 |
|
| 138 |
case 'install': |
| 139 |
Logtivity::log('WP Core Installed')->send(); |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @param int $menuId |
| 147 |
* @param ?array $menuData |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function menuUpdated(int $menuId, ?array $menuData = null): void |
| 152 |
{ |
| 153 |
$menuName = $menuData['menu-name'] ?? null; |
| 154 |
|
| 155 |
if ($menuName) { |
| 156 |
Logtivity::log() |
| 157 |
->setAction('Menu Updated') |
| 158 |
->setContext($menuName) |
| 159 |
->addMeta('Menu ID', $menuId) |
| 160 |
->send(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @param array $currentSettings |
| 166 |
* @param array $newSettings |
| 167 |
* @param array $oldSettings |
| 168 |
* @param WP_Widget $widget |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function widgetUpdated( |
| 173 |
array $currentSettings, |
| 174 |
array $newSettings, |
| 175 |
array $oldSettings, |
| 176 |
WP_Widget $widget |
| 177 |
): array { |
| 178 |
Logtivity::log() |
| 179 |
->setAction('Widget Updated') |
| 180 |
->setContext($widget->name) |
| 181 |
->addMeta('New Content', $newSettings) |
| 182 |
->addMeta('Old Content', $oldSettings) |
| 183 |
->send(); |
| 184 |
|
| 185 |
return $currentSettings; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @TODO: When does this get called? |
| 190 |
* |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function maybeSettingsUpdated(): void |
| 194 |
{ |
| 195 |
$optionPage = sanitize_text_field($_POST['option_page'] ?? ''); |
| 196 |
$action = sanitize_text_field($_POST['action'] ?? ''); |
| 197 |
|
| 198 |
if ($optionPage && $action == 'update') { |
| 199 |
Logtivity::log() |
| 200 |
->setAction('Settings Updated') |
| 201 |
->setContext('Core:' . $optionPage) |
| 202 |
->send(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* @param string $option |
| 208 |
* |
| 209 |
* @return bool |
| 210 |
*/ |
| 211 |
protected function ignoreOption(string $option): bool |
| 212 |
{ |
| 213 |
$ignore = in_array($option, $this->ignoredOptions) !== false; |
| 214 |
if ($ignore == false) { |
| 215 |
foreach ($this->ignoredWildcardOptions as $wildcard) { |
| 216 |
if (strpos($option, $wildcard) !== false) { |
| 217 |
return true; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $ignore; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* @param string $option |
| 227 |
* @param mixed $oldValue |
| 228 |
* @param mixed $newValue |
| 229 |
* |
| 230 |
* @return void |
| 231 |
*/ |
| 232 |
public function optionUpdated(string $option, $oldValue, $newValue): void |
| 233 |
{ |
| 234 |
if ( |
| 235 |
$this->getRequestMethod() != 'GET' |
| 236 |
&& is_admin() |
| 237 |
&& $oldValue !== $newValue |
| 238 |
&& $this->ignoreOption($option) == false |
| 239 |
&& get_option('logtivity_enable_options_table_logging') |
| 240 |
) { |
| 241 |
Logtivity::log() |
| 242 |
->setAction('Option Updated') |
| 243 |
->setContext($option) |
| 244 |
->addMetaIf(is_scalar($oldValue), 'Old Value', $oldValue) |
| 245 |
->addMetaIf(is_scalar($newValue), 'New Value', $newValue) |
| 246 |
->send(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* @return string |
| 252 |
*/ |
| 253 |
private function getRequestMethod(): string |
| 254 |
{ |
| 255 |
return sanitize_text_field($_SERVER['REQUEST_METHOD'] ?? ''); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* @param string $oldStructure |
| 260 |
* @param string $newStructure |
| 261 |
* |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
public function permalinksUpdated(string $oldStructure, string $newStructure): void |
| 265 |
{ |
| 266 |
Logtivity::log() |
| 267 |
->setAction('Permalinks Updated') |
| 268 |
->setContext($this->getPermalinkStructure($newStructure)) |
| 269 |
->addMeta('Old Structure', $this->getPermalinkStructure($oldStructure)) |
| 270 |
->send(); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* @param string $structure |
| 275 |
* |
| 276 |
* @return string |
| 277 |
*/ |
| 278 |
private function getPermalinkStructure(string $structure): string |
| 279 |
{ |
| 280 |
return $structure ?: 'Plain'; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
new Logtivity_Core(); |
| 285 |
|