| 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.Files.SideEffects.FoundWithSymbols |
| 26 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace |
| 27 |
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 28 |
|
| 29 |
class Logtivity_Clear_Debug_Log_Controller |
| 30 |
{ |
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
add_action('admin_post_logtivity_clear_debug_log', [$this, 'clear']); |
| 34 |
add_action('admin_post_logtivity_download_debug_log', [$this, 'download']); |
| 35 |
add_action('update_option_logtivity_enable_debug_mode', [$this, 'maybeClearOnDisable'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return self |
| 40 |
*/ |
| 41 |
public static function init(): self |
| 42 |
{ |
| 43 |
return new static(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function clear(): void |
| 50 |
{ |
| 51 |
$nonce = isset($_POST['logtivity_clear_debug_log']) |
| 52 |
? sanitize_text_field(wp_unslash($_POST['logtivity_clear_debug_log'])) |
| 53 |
: ''; |
| 54 |
|
| 55 |
if ( |
| 56 |
!wp_verify_nonce( |
| 57 |
$nonce, |
| 58 |
'logtivity_clear_debug_log' |
| 59 |
) |
| 60 |
) { |
| 61 |
wp_safe_redirect($this->settingsPageUrl()); |
| 62 |
exit(); |
| 63 |
} |
| 64 |
|
| 65 |
if (!current_user_can(Logtivity::ACCESS_SETTINGS)) { |
| 66 |
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'logtivity')); |
| 67 |
} |
| 68 |
|
| 69 |
(new Logtivity_Debug_Log())->clear(); |
| 70 |
|
| 71 |
wp_safe_redirect($this->settingsPageUrl()); |
| 72 |
exit(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Download the raw debug log file. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function download(): void |
| 81 |
{ |
| 82 |
$nonce = isset($_GET['logtivity_download_debug_log']) |
| 83 |
? sanitize_text_field(wp_unslash($_GET['logtivity_download_debug_log'])) |
| 84 |
: ''; |
| 85 |
|
| 86 |
if ( |
| 87 |
!wp_verify_nonce( |
| 88 |
$nonce, |
| 89 |
'logtivity_download_debug_log' |
| 90 |
) |
| 91 |
) { |
| 92 |
wp_safe_redirect($this->settingsPageUrl()); |
| 93 |
exit(); |
| 94 |
} |
| 95 |
|
| 96 |
if (!current_user_can(Logtivity::ACCESS_SETTINGS)) { |
| 97 |
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'logtivity')); |
| 98 |
} |
| 99 |
|
| 100 |
$contents = (new Logtivity_Debug_Log())->contents(); |
| 101 |
|
| 102 |
nocache_headers(); |
| 103 |
header('Content-Type: text/plain; charset=utf-8'); |
| 104 |
header('Content-Disposition: attachment; filename="' . Logtivity_Debug_Log::FILE_NAME . '"'); |
| 105 |
header('Content-Length: ' . strlen($contents)); |
| 106 |
|
| 107 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- raw file download, not HTML output. |
| 108 |
echo $contents; |
| 109 |
exit(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Remove the debug log file and its directory entirely whenever debug mode |
| 114 |
* is turned off, so nothing sits around with old request/response data once |
| 115 |
* the site owner has stopped needing it. |
| 116 |
* |
| 117 |
* @param mixed $oldValue |
| 118 |
* @param mixed $newValue |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function maybeClearOnDisable($oldValue, $newValue): void |
| 123 |
{ |
| 124 |
if ($newValue) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
(new Logtivity_Debug_Log())->delete(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
protected function settingsPageUrl(): string |
| 135 |
{ |
| 136 |
return admin_url('admin.php?page=logtivity-settings'); |
| 137 |
} |
| 138 |
} |
| 139 |
|