| 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_Theme extends Logtivity_Abstract_Logger |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @inheritDoc |
| 29 |
*/ |
| 30 |
protected function registerHooks(): void |
| 31 |
{ |
| 32 |
add_action('switch_theme', [$this, 'themeSwitched'], 10, 3); |
| 33 |
add_action('upgrader_process_complete', [$this, 'upgradeProcessComplete'], 10, 2); |
| 34 |
add_filter('wp_theme_editor_filetypes', [$this, 'themeFileModified'], 10, 2); |
| 35 |
add_action('customize_save', [$this, 'themeCustomizerModified'], 10, 2); |
| 36 |
add_action('delete_site_transient_update_themes', [$this, 'themeMaybeDeleted'], 10, 1); |
| 37 |
} |
| 38 |
|
| 39 |
public function themeSwitched($new_name, $new_theme, $old_theme) |
| 40 |
{ |
| 41 |
Logtivity::log() |
| 42 |
->setAction('Theme Activated') |
| 43 |
->setContext((is_object($new_theme) ? $new_theme->name : $new_theme)) |
| 44 |
->addMeta('Version', (is_object($new_theme) ? $new_theme->version : $new_theme)) |
| 45 |
->addMeta('Old Theme', (is_object($old_theme) ? $old_theme->name : $old_theme)) |
| 46 |
->send(); |
| 47 |
} |
| 48 |
|
| 49 |
public function themeMaybeDeleted() |
| 50 |
{ |
| 51 |
$delete_theme_call = $this->getDeleteThemeCall(); |
| 52 |
|
| 53 |
if (empty($delete_theme_call)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$slug = $delete_theme_call['args'][0]; |
| 58 |
$theme = wp_get_theme($slug); |
| 59 |
|
| 60 |
Logtivity::log() |
| 61 |
->setAction('Theme Deleted') |
| 62 |
->setContext($theme->get('Name')) |
| 63 |
->addMeta('Version', $theme->get('Version')) |
| 64 |
->addMeta('URI', $theme->get('ThemeURI')) |
| 65 |
->send(); |
| 66 |
} |
| 67 |
|
| 68 |
public function upgradeProcessComplete($upgrader_object, $options) |
| 69 |
{ |
| 70 |
if ($options['type'] != 'theme') { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if ($options['action'] == 'update') { |
| 75 |
$this->themeUpdated($upgrader_object, $options); |
| 76 |
} |
| 77 |
|
| 78 |
if ($options['action'] == 'install') { |
| 79 |
$this->themeInstalled($upgrader_object, $options); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function themeUpdated($upgrader, $options) |
| 84 |
{ |
| 85 |
$bulk = $options['bulk'] ?? false; |
| 86 |
if ($bulk) { |
| 87 |
$slugs = $options['themes']; |
| 88 |
} else { |
| 89 |
$slugs = [$upgrader->skin->theme]; |
| 90 |
} |
| 91 |
|
| 92 |
foreach ($slugs as $slug) { |
| 93 |
$theme = wp_get_theme($slug); |
| 94 |
|
| 95 |
Logtivity::log() |
| 96 |
->setAction('Theme Updated') |
| 97 |
->setContext($theme->name) |
| 98 |
->addMeta('Version', $theme->version) |
| 99 |
->addMeta('Bulk Update', $bulk ? 'Yes' : 'No') |
| 100 |
->send(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function themeInstalled($upgrader_object, $options) |
| 105 |
{ |
| 106 |
$slug = $upgrader_object->theme_info(); |
| 107 |
|
| 108 |
if (!$slug) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
wp_clean_themes_cache(); |
| 113 |
|
| 114 |
$theme = wp_get_theme($slug); |
| 115 |
|
| 116 |
Logtivity::log() |
| 117 |
->setAction('Theme Installed') |
| 118 |
->setContext($theme->name) |
| 119 |
->addMeta('Version', $theme->version) |
| 120 |
->send(); |
| 121 |
} |
| 122 |
|
| 123 |
public function themeFileModified($default_types, $theme) |
| 124 |
{ |
| 125 |
if (!isset($_POST['action']) || $_POST['action'] != 'edit-theme-plugin-file') { |
| 126 |
return $default_types; |
| 127 |
} |
| 128 |
|
| 129 |
if (!isset($_POST['theme'])) { |
| 130 |
return $default_types; |
| 131 |
} |
| 132 |
|
| 133 |
$log = Logtivity::log('Theme File Edited'); |
| 134 |
|
| 135 |
if (!empty($_POST['file']) && is_string($_POST['file'])) { |
| 136 |
$log->addMeta('File', sanitize_text_field($_POST['file'])); |
| 137 |
} |
| 138 |
|
| 139 |
if (!empty($_POST['theme'])) { |
| 140 |
$log->setContext($theme->display('Name')); |
| 141 |
} |
| 142 |
|
| 143 |
$log->send(); |
| 144 |
|
| 145 |
return $default_types; |
| 146 |
} |
| 147 |
|
| 148 |
public function themeCustomizerModified(WP_Customize_Manager $obj) |
| 149 |
{ |
| 150 |
Logtivity::log() |
| 151 |
->setAction('Theme Customizer Updated') |
| 152 |
->setContext($obj->theme()->display('Name')) |
| 153 |
->send(); |
| 154 |
} |
| 155 |
|
| 156 |
private function getDeleteThemeCall() |
| 157 |
{ |
| 158 |
$backtrace = debug_backtrace(); |
| 159 |
|
| 160 |
$delete_theme_call = null; |
| 161 |
|
| 162 |
foreach ($backtrace as $call) { |
| 163 |
if (isset($call['function']) && 'delete_theme' === $call['function']) { |
| 164 |
$delete_theme_call = $call; |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $delete_theme_call; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
new Logtivity_Theme(); |
| 174 |
|