| 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 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace |
| 26 |
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 27 |
|
| 28 |
class Logtivity_Site_Inventory |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Snapshot of installed plugins and themes, as sent to the API for |
| 32 |
* comparison against its stored copy. |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public function get(): array |
| 37 |
{ |
| 38 |
if (function_exists('get_plugins') == false) { |
| 39 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 40 |
} |
| 41 |
|
| 42 |
/* |
| 43 |
* The plugin/theme lists are cached per request and WordPress does not |
| 44 |
* always clear them before its post-change hooks fire (e.g. background |
| 45 |
* auto-updates and plugin deletion), so force a fresh read. Never clear |
| 46 |
* the update transients here: deleting update_themes fires the hook used |
| 47 |
* to detect theme deletions. |
| 48 |
*/ |
| 49 |
wp_clean_plugins_cache(false); |
| 50 |
wp_clean_themes_cache(false); |
| 51 |
|
| 52 |
$theme = wp_get_theme(); |
| 53 |
|
| 54 |
return [ |
| 55 |
'plugins' => $this->getPlugins(), |
| 56 |
'theme_name' => $theme ? $theme->name : null, |
| 57 |
'theme_version' => $theme ? $theme->version : null, |
| 58 |
'themes' => $this->getThemes(), |
| 59 |
'inventory_timestamp' => microtime(true), |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return array[] |
| 65 |
*/ |
| 66 |
public function getPlugins(): array |
| 67 |
{ |
| 68 |
$allPlugins = get_plugins(); |
| 69 |
$activePlugins = get_option('active_plugins'); |
| 70 |
$pluginsWithUpdate = get_site_transient('update_plugins'); |
| 71 |
|
| 72 |
$pluginData = []; |
| 73 |
|
| 74 |
foreach ($allPlugins as $filePath => $data) { |
| 75 |
$pluginData[$filePath]['Name'] = $data['Name']; |
| 76 |
$pluginData[$filePath]['Version'] = $data['Version']; |
| 77 |
$pluginData[$filePath]['Author'] = $data['Author']; |
| 78 |
$pluginData[$filePath]['AuthorURI'] = $data['AuthorURI']; |
| 79 |
$pluginData[$filePath]['slug'] = $filePath; |
| 80 |
|
| 81 |
if (in_array($filePath, $activePlugins)) { |
| 82 |
$pluginData[$filePath]['is_active'] = true; |
| 83 |
} else { |
| 84 |
$pluginData[$filePath]['is_active'] = false; |
| 85 |
} |
| 86 |
|
| 87 |
if (!empty($pluginsWithUpdate->response)) { |
| 88 |
if (array_key_exists($filePath, $pluginsWithUpdate->response)) { |
| 89 |
$pluginData[$filePath]['update_available'] = true; |
| 90 |
$pluginData[$filePath]['new_version'] = $this->getPropertyIfExists( |
| 91 |
$pluginsWithUpdate->response[$filePath], |
| 92 |
'new_version' |
| 93 |
); |
| 94 |
|
| 95 |
$pluginData[$filePath]['new_version_requires_wp'] = $this->getPropertyIfExists( |
| 96 |
$pluginsWithUpdate->response[$filePath], |
| 97 |
'requires' |
| 98 |
); |
| 99 |
$pluginData[$filePath]['tested'] = $this->getPropertyIfExists( |
| 100 |
$pluginsWithUpdate->response[$filePath], |
| 101 |
'tested' |
| 102 |
); |
| 103 |
$pluginData[$filePath]['new_version_requires_php'] = $this->getPropertyIfExists( |
| 104 |
$pluginsWithUpdate->response[$filePath], |
| 105 |
'requires_php' |
| 106 |
); |
| 107 |
|
| 108 |
} else { |
| 109 |
$pluginData[$filePath]['update_available'] = false; |
| 110 |
$pluginData[$filePath]['new_version'] = null; |
| 111 |
$pluginData[$filePath]['new_version_requires_wp'] = null; |
| 112 |
$pluginData[$filePath]['tested'] = null; |
| 113 |
$pluginData[$filePath]['new_version_requires_php'] = null; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return $pluginData; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @return array[] |
| 123 |
*/ |
| 124 |
public function getThemes(): array |
| 125 |
{ |
| 126 |
$themesDetails = []; |
| 127 |
|
| 128 |
foreach (wp_get_themes() as $theme) { |
| 129 |
$themesDetails[] = [ |
| 130 |
'name' => $theme->name, |
| 131 |
'version' => $theme->version, |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
return $themesDetails; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param object $object |
| 140 |
* @param string $property |
| 141 |
* |
| 142 |
* @return ?mixed |
| 143 |
*/ |
| 144 |
protected function getPropertyIfExists(object $object, string $property) |
| 145 |
{ |
| 146 |
return $object->{$property} ?? null; |
| 147 |
} |
| 148 |
} |
| 149 |
|