| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, hello@logtivity.io |
| 6 |
* @copyright 2024-2025 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.Files.SideEffects.FoundWithSymbols |
| 26 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace |
| 27 |
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 28 |
|
| 29 |
class Logtivity_Admin |
| 30 |
{ |
| 31 |
/** |
| 32 |
* @var Logtivity_Options |
| 33 |
*/ |
| 34 |
protected Logtivity_Options $options; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var bool |
| 38 |
*/ |
| 39 |
protected static bool $shouldHidePluginFromUI = false; |
| 40 |
|
| 41 |
public function __construct() |
| 42 |
{ |
| 43 |
add_action('admin_menu', [$this, 'registerOptionsPage']); |
| 44 |
|
| 45 |
add_action('wp_ajax_logtivity_update_settings', [$this, 'update']); |
| 46 |
add_action('wp_ajax_nopriv_logtivity_update_settings', [$this, 'update']); |
| 47 |
|
| 48 |
add_action('wp_ajax_logtivity_register_site', [$this, 'registerSite']); |
| 49 |
|
| 50 |
add_filter('logtivity_hide_from_menu', [$this, 'shouldHidePluginFromUI']); |
| 51 |
add_filter('all_plugins', [$this, 'maybeHideFromMenu']); |
| 52 |
|
| 53 |
$this->options = new Logtivity_Options(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return self |
| 58 |
*/ |
| 59 |
public static function init(): self |
| 60 |
{ |
| 61 |
return new static(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param array $plugins |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function maybeHideFromMenu(array $plugins): array |
| 70 |
{ |
| 71 |
if ($name = (new Logtivity_Options())->customPluginName()) { |
| 72 |
if (isset($plugins['logtivity/logtivity.php'])) { |
| 73 |
$plugins['logtivity/logtivity.php']['Name'] = $name; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (!$this->shouldHidePluginFromUI()) { |
| 78 |
return $plugins; |
| 79 |
} |
| 80 |
|
| 81 |
$shouldHide = !array_key_exists('show_all', $_GET); |
| 82 |
|
| 83 |
if ($shouldHide) { |
| 84 |
$hiddenPlugins = [ |
| 85 |
'logtivity/logtivity.php', |
| 86 |
]; |
| 87 |
|
| 88 |
foreach ($hiddenPlugins as $hiddenPlugin) { |
| 89 |
unset($plugins[$hiddenPlugin]); |
| 90 |
} |
| 91 |
} |
| 92 |
return $plugins; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param bool $value |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function shouldHidePluginFromUI(bool $value = false): bool |
| 101 |
{ |
| 102 |
if (static::$shouldHidePluginFromUI = (new Logtivity_Options())->isPluginHiddenFromUI()) { |
| 103 |
return static::$shouldHidePluginFromUI; |
| 104 |
} |
| 105 |
|
| 106 |
return $value; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create the admin menus |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function registerOptionsPage(): void |
| 115 |
{ |
| 116 |
if (!apply_filters('logtivity_hide_from_menu', false)) { |
| 117 |
add_menu_page( |
| 118 |
($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'), |
| 119 |
($this->options->isWhiteLabelMode() ? 'Logs' : 'Logtivity'), |
| 120 |
Logtivity::ACCESS_LOGS, |
| 121 |
($this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity'), |
| 122 |
[$this, 'showLogIndexPage'], |
| 123 |
'dashicons-chart-area', |
| 124 |
26 |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
if (!apply_filters('logtivity_hide_settings_page', false)) { |
| 129 |
add_submenu_page( |
| 130 |
$this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity', |
| 131 |
'Logtivity Settings', |
| 132 |
'Settings', |
| 133 |
Logtivity::ACCESS_SETTINGS, |
| 134 |
'logtivity-settings', |
| 135 |
[$this, 'showSettingsPage'] |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
if ($this->options->getApiKey() == false) { |
| 140 |
add_submenu_page( |
| 141 |
$this->options->isWhiteLabelMode() ? 'lgtvy-logs' : 'logtivity', |
| 142 |
'Register Site', |
| 143 |
'Register Site', |
| 144 |
Logtivity::ACCESS_SETTINGS, |
| 145 |
'logtivity-register-site', |
| 146 |
[$this, 'showRegisterSitePage'] |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Show the admin log index |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function showLogIndexPage(): void |
| 157 |
{ |
| 158 |
if (!current_user_can(Logtivity::ACCESS_LOGS)) { |
| 159 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 160 |
} |
| 161 |
|
| 162 |
$options = $this->options->getOptions(); |
| 163 |
|
| 164 |
echo logtivity_view('log-index', compact('options')); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Show the admin settings template |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function showSettingsPage(): void |
| 173 |
{ |
| 174 |
if (!current_user_can(Logtivity::ACCESS_SETTINGS)) { |
| 175 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 176 |
} |
| 177 |
|
| 178 |
$options = $this->options->getOptions(); |
| 179 |
|
| 180 |
echo logtivity_view('settings', compact('options')); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Show the register by team API page |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
public function showRegisterSitePage(): void |
| 189 |
{ |
| 190 |
if (!current_user_can(Logtivity::ACCESS_SETTINGS)) { |
| 191 |
wp_die(__('You do not have sufficient permissions to access this page.')); |
| 192 |
} |
| 193 |
|
| 194 |
$options = $this->options->getOptions(); |
| 195 |
|
| 196 |
echo logtivity_view('register', compact('options')); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function update(): void |
| 203 |
{ |
| 204 |
if (!wp_verify_nonce($_POST['logtivity_update_settings'] ?? null, 'logtivity_update_settings')) { |
| 205 |
wp_safe_redirect($this->settingsPageUrl()); |
| 206 |
exit(); |
| 207 |
} |
| 208 |
|
| 209 |
$user = new Logtivity_Wp_User(); |
| 210 |
|
| 211 |
if (!$user->hasRole('administrator')) { |
| 212 |
wp_safe_redirect($this->settingsPageUrl()); |
| 213 |
exit(); |
| 214 |
} |
| 215 |
|
| 216 |
$this->options->update( |
| 217 |
[ |
| 218 |
'logtivity_url_hash' => md5(home_url()), |
| 219 |
], |
| 220 |
false |
| 221 |
); |
| 222 |
|
| 223 |
delete_transient('dismissed-logtivity-site-url-has-changed-notice'); |
| 224 |
|
| 225 |
$this->options->update(); |
| 226 |
|
| 227 |
(new Logtivity_Check_For_New_Settings())->checkForNewSettings(); |
| 228 |
|
| 229 |
wp_safe_redirect($this->settingsPageUrl()); |
| 230 |
exit(); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* ajax endpoint for registering with a team API Key |
| 235 |
* |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
public function registerSite(): void |
| 239 |
{ |
| 240 |
try { |
| 241 |
if (wp_verify_nonce($_POST['logtivity_register_site'] ?? null, 'logtivity_register_site')) { |
| 242 |
$teamApi = sanitize_text_field($_POST['logtivity_team_api_key'] ?? null); |
| 243 |
|
| 244 |
$response = Logtivity::registerSite($teamApi); |
| 245 |
|
| 246 |
|
| 247 |
if ($response instanceof WP_Error) { |
| 248 |
wp_send_json_error($response); |
| 249 |
} else { |
| 250 |
wp_send_json_success($response); |
| 251 |
} |
| 252 |
|
| 253 |
} else { |
| 254 |
wp_send_json_error('Invalid Request'); |
| 255 |
} |
| 256 |
|
| 257 |
} catch (Throwable $error) { |
| 258 |
wp_send_json_error($error->getMessage(), $error->getCode()); |
| 259 |
} |
| 260 |
|
| 261 |
wp_die(); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @return string |
| 266 |
*/ |
| 267 |
public function settingsPageUrl(): string |
| 268 |
{ |
| 269 |
return admin_url('admin.php?page=logtivity-settings'); |
| 270 |
} |
| 271 |
} |
| 272 |
|