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 / Loggers / Core / Logtivity_Theme.php

Logtivity_Theme.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Loggers/Core/Logtivity_Theme.php

184 lines 5.4 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 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 ->addInventory()
47 ->send();
48 }
49
50 public function themeMaybeDeleted()
51 {
52 $slug = $this->getDeletedThemeSlug();
53
54 if (empty($slug)) {
55 return;
56 }
57
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 ->addInventory()
66 ->send();
67 }
68
69 public function upgradeProcessComplete($upgrader_object, $options)
70 {
71 if ($options['type'] != 'theme') {
72 return;
73 }
74
75 if ($options['action'] == 'update') {
76 $this->themeUpdated($upgrader_object, $options);
77 }
78
79 if ($options['action'] == 'install') {
80 $this->themeInstalled($upgrader_object, $options);
81 }
82 }
83
84 public function themeUpdated($upgrader, $options)
85 {
86 $bulk = $options['bulk'] ?? false;
87 if ($bulk) {
88 $slugs = $options['themes'];
89 } else {
90 $slugs = [$upgrader->skin->theme];
91 }
92
93 foreach ($slugs as $slug) {
94 $theme = wp_get_theme($slug);
95
96 Logtivity::log()
97 ->setAction('Theme Updated')
98 ->setContext($theme->name)
99 ->addMeta('Version', $theme->version)
100 ->addMeta('Bulk Update', $bulk ? 'Yes' : 'No')
101 ->addInventory()
102 ->send();
103 }
104 }
105
106 public function themeInstalled($upgrader_object, $options)
107 {
108 $slug = $upgrader_object->theme_info();
109
110 if (!$slug) {
111 return;
112 }
113
114 wp_clean_themes_cache();
115
116 $theme = wp_get_theme($slug);
117
118 Logtivity::log()
119 ->setAction('Theme Installed')
120 ->setContext($theme->name)
121 ->addMeta('Version', $theme->version)
122 ->addInventory()
123 ->send();
124 }
125
126 public function themeFileModified($default_types, $theme)
127 {
128 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Passive logger reads core theme-editor request after WordPress validates it.
129 $action = isset($_POST['action'])
130 ? sanitize_text_field(wp_unslash($_POST['action']))
131 : null;
132 $themeSlug = isset($_POST['theme'])
133 ? sanitize_text_field(wp_unslash($_POST['theme']))
134 : null;
135 $file = isset($_POST['file'])
136 ? sanitize_text_field(wp_unslash($_POST['file']))
137 : null;
138 // phpcs:enable WordPress.Security.NonceVerification.Missing
139
140 if ($action != 'edit-theme-plugin-file') {
141 return $default_types;
142 }
143
144 if (!$themeSlug) {
145 return $default_types;
146 }
147
148 $log = Logtivity::log('Theme File Edited');
149
150 if ($file) {
151 $log->addMeta('File', $file);
152 }
153
154 if ($themeSlug) {
155 $log->setContext($theme->display('Name'));
156 }
157
158 $log->send();
159
160 return $default_types;
161 }
162
163 public function themeCustomizerModified(WP_Customize_Manager $obj)
164 {
165 Logtivity::log()
166 ->setAction('Theme Customizer Updated')
167 ->setContext($obj->theme()->display('Name'))
168 ->send();
169 }
170
171 private function getDeletedThemeSlug(): ?string
172 {
173 foreach ((new Exception())->getTrace() as $call) {
174 if (isset($call['function']) && 'delete_theme' === $call['function']) {
175 return $call['args'][0] ?? null;
176 }
177 }
178
179 return null;
180 }
181 }
182
183 new Logtivity_Theme();
184