| 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_Plugin extends Logtivity_Abstract_Logger |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected array $savedPluginData = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritDoc |
| 34 |
*/ |
| 35 |
protected function registerHooks(): void |
| 36 |
{ |
| 37 |
add_action('activated_plugin', [$this, 'pluginActivated'], 10, 2); |
| 38 |
add_action('deactivated_plugin', [$this, 'pluginDeactivated'], 10, 2); |
| 39 |
add_action('delete_plugin', [$this, 'savePluginData'], 10, 1); |
| 40 |
add_action('deleted_plugin', [$this, 'pluginDeleted'], 10, 2); |
| 41 |
|
| 42 |
add_action('upgrader_process_complete', [$this, 'upgradeProcessComplete'], 10, 2); |
| 43 |
|
| 44 |
add_filter('editable_extensions', [$this, 'pluginFileModified'], 10, 2); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string|Plugin_Upgrader $plugin |
| 49 |
* |
| 50 |
* @return ?array |
| 51 |
*/ |
| 52 |
protected function getPluginData($plugin): ?array |
| 53 |
{ |
| 54 |
if (is_string($plugin)) { |
| 55 |
$slug = $plugin; |
| 56 |
$pluginData = $this->savedPluginData[$plugin] |
| 57 |
?? get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin, false, false); |
| 58 |
|
| 59 |
} elseif ($plugin instanceof Plugin_Upgrader) { |
| 60 |
if ($slug = $plugin->plugin_info()) { |
| 61 |
$pluginData = get_plugin_data($plugin->result['local_destination'] . '/' . $slug, false, false); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (isset($pluginData)) { |
| 66 |
$pluginData = array_merge( |
| 67 |
[ |
| 68 |
'Name' => 'Not Set', |
| 69 |
'Slug' => $slug ?? 'Not Set', |
| 70 |
'Version' => 'Not Set', |
| 71 |
], |
| 72 |
$pluginData |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return $pluginData ?? null; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param string $state |
| 81 |
* @param string $slug |
| 82 |
* @param bool $networkWide |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function pluginStateChanged(string $state, string $slug, bool $networkWide): void |
| 87 |
{ |
| 88 |
if ($plugin = $this->getPluginData($slug)) { |
| 89 |
Logtivity::log() |
| 90 |
->setAction('Plugin ' . $state) |
| 91 |
->setContext($plugin['Name']) |
| 92 |
->addMeta('Version', $plugin['Version']) |
| 93 |
->addMeta('Slug', $plugin['Slug']) |
| 94 |
->addMeta('Network Wide', $networkWide ? 'Yes' : 'No') |
| 95 |
->send(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param string $slug |
| 101 |
* @param bool $networkWide |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function pluginActivated(string $slug, bool $networkWide): void |
| 106 |
{ |
| 107 |
$this->pluginStateChanged('Activated', $slug, $networkWide); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param string $slug |
| 112 |
* @param bool $networkDeactivating |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function pluginDeactivated(string $slug, bool $networkDeactivating): void |
| 117 |
{ |
| 118 |
$this->pluginStateChanged('Deactivated', $slug, $networkDeactivating); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param string $plugin |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function savePluginData(string $plugin): void |
| 127 |
{ |
| 128 |
if (empty($this->savedPluginData)) { |
| 129 |
if ($pluginData = $this->getPluginData($plugin)) { |
| 130 |
$this->savedPluginData[$plugin] = $pluginData; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param string $pluginFile |
| 137 |
* @param bool $deleted |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
public function pluginDeleted(string $pluginFile, bool $deleted): void |
| 142 |
{ |
| 143 |
if ($plugin = $this->getPluginData($pluginFile)) { |
| 144 |
Logtivity::log() |
| 145 |
->setAction('Plugin Deleted') |
| 146 |
->setContext($plugin['Name']) |
| 147 |
->addMeta('Slug', $plugin['Slug']) |
| 148 |
->addMeta('Version', $plugin['Version']) |
| 149 |
->addMeta('Deletion Successful', $deleted ? 'Yes' : 'No') |
| 150 |
->send(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param WP_Upgrader $upgrader |
| 156 |
* @param array $options |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function upgradeProcessComplete(WP_Upgrader $upgrader, array $options): void |
| 161 |
{ |
| 162 |
$action = $options['action'] ?? null; |
| 163 |
|
| 164 |
if ($upgrader instanceof Plugin_Upgrader) { |
| 165 |
switch ($action) { |
| 166 |
case 'install': |
| 167 |
$this->pluginInstalled('Installed', $upgrader, $options); |
| 168 |
break; |
| 169 |
|
| 170 |
case 'update': |
| 171 |
$this->pluginInstalled('Updated', $upgrader, $options); |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @param Plugin_Upgrader $upgrader |
| 179 |
* @param bool $bulk |
| 180 |
* |
| 181 |
* @return string[]|Plugin_Upgrader[] |
| 182 |
*/ |
| 183 |
protected function getPluginList(Plugin_Upgrader $upgrader, array $options): array |
| 184 |
{ |
| 185 |
if ($options['bulk'] ?? false) { |
| 186 |
return (array)$options['plugins'] ?? []; |
| 187 |
} |
| 188 |
|
| 189 |
return [$upgrader]; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @param Plugin_Upgrader $upgrader |
| 194 |
* @param array $options |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function pluginInstalled(string $action, Plugin_Upgrader $upgrader, array $options): void |
| 199 |
{ |
| 200 |
$bulk = $options['bulk'] ?? false; |
| 201 |
$plugins = $this->getPluginList($upgrader, $options); |
| 202 |
|
| 203 |
foreach ($plugins as $plugin) { |
| 204 |
if ($pluginData = $this->getPluginData($plugin)) { |
| 205 |
Logtivity::log() |
| 206 |
->setAction('Plugin ' . $action) |
| 207 |
->setContext($pluginData['Name']) |
| 208 |
->addMeta('Slug', $pluginData['Slug']) |
| 209 |
->addMeta('Version', $pluginData['Version']) |
| 210 |
->addMeta('Bulk', $bulk ? 'Yes' : 'No') |
| 211 |
->send(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @param string[] $defaultTypes |
| 218 |
* |
| 219 |
* @return array |
| 220 |
*/ |
| 221 |
public function pluginFileModified(array $defaultTypes): array |
| 222 |
{ |
| 223 |
$action = sanitize_text_field($_POST['action'] ?? null); |
| 224 |
$plugin = sanitize_text_field($_POST['plugin'] ?? null); |
| 225 |
$file = sanitize_text_field($_REQUEST['file'] ?? null); |
| 226 |
|
| 227 |
if ($file && $plugin && $action == 'edit-theme-plugin-file') { |
| 228 |
$pluginData = $this->getPluginData($plugin); |
| 229 |
|
| 230 |
Logtivity::log('Plugin File Edited') |
| 231 |
->setContext($pluginData['Name']) |
| 232 |
->addMeta('File', $file) |
| 233 |
->addMeta('Slug', $pluginData['Slug']) |
| 234 |
->addMeta('Version', $pluginData['Version']) |
| 235 |
->send(); |
| 236 |
} |
| 237 |
|
| 238 |
return $defaultTypes; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
new Logtivity_Plugin(); |
| 243 |
|