class-logs.php
5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace WP_Rocket\Admin;
use WP_Rocket\Logger\Logger;
use WP_Rocket\Event_Management\Subscriber_Interface;
defined( 'ABSPATH' ) || exit;
/**
* Class that handles few things about the logs.
*
* @since 3.1.4
* @author Grégory Viguier
*/
class Logs implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @since 3.1.4
* @access public
* @author Grégory Viguier
*
* @return array
*/
public static function get_subscribed_events() {
return [
'pre_update_option_' . WP_ROCKET_SLUG => [ 'enable_debug', 10, 2 ],
'admin_post_rocket_download_debug_file' => 'download_debug_file',
'admin_post_rocket_delete_debug_file' => 'delete_debug_file',
];
}
/** ----------------------------------------------------------------------------------------- */
/** DEBUG ACTIVATION ======================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Enable or disable the debug mode when settings are saved.
*
* @since 3.1.4
* @access public
* @author Grégory Viguier
*
* @param array $newvalue An array of submitted options values.
* @param array $oldvalue An array of previous options values.
* @return array Updated submitted options values.
*/
public function enable_debug( $newvalue, $oldvalue ) {
if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return $newvalue;
}
if ( ! empty( $newvalue['debug_enabled'] ) ) {
Logger::enable_debug();
} else {
Logger::disable_debug();
}
unset( $newvalue['debug_enabled'] );
return $newvalue;
}
/** ----------------------------------------------------------------------------------------- */
/** ADMIN POST CALLBACKS ==================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Download the log file.
*
* @since 3.1.4
* @access public
* @author Grégory Viguier
*/
public function download_debug_file() {
if ( ! $this->verify_nonce( 'download_debug_file' ) ) {
wp_nonce_ays( '' );
}
if ( ! $this->current_user_can() ) {
$this->redirect();
}
$contents = Logger::get_log_file_contents();
if ( is_wp_error( $contents ) ) {
add_settings_error( 'general', $contents->get_error_code(), $contents->get_error_message(), 'error' );
set_transient( 'settings_errors', get_settings_errors(), 30 );
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
}
$file_name = Logger::get_log_file_path();
$file_name = basename( $file_name, '.log' ) . Logger::get_log_file_extension();
nocache_headers();
@header( 'Content-Type: text/x-log' );
@header( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
@header( 'Content-Transfer-Encoding: binary' );
@header( 'Content-Length: ' . strlen( $contents ) );
@header( 'Connection: close' );
echo $contents; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit();
}
/**
* Delete the log file.
*
* @since 3.1.4
* @access public
* @author Grégory Viguier
*/
public function delete_debug_file() {
if ( ! $this->verify_nonce( 'delete_debug_file' ) ) {
wp_nonce_ays( '' );
}
if ( ! $this->current_user_can() ) {
$this->redirect();
}
if ( ! Logger::delete_log_file() ) {
add_settings_error( 'general', 'debug_file_not_deleted', __( 'The debug file could not be deleted.', 'rocket' ), 'error' );
set_transient( 'settings_errors', get_settings_errors(), 30 );
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
}
// Done.
$this->redirect();
}
/** ----------------------------------------------------------------------------------------- */
/** TOOLS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Verify the nonce sent in $_GET['_wpnonce'].
*
* @since 3.1.4
* @access protected
* @author Grégory Viguier
*
* @param string $nonce_name The nonce name.
* @return bool
*/
protected function verify_nonce( $nonce_name ) {
return isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $nonce_name ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
}
/**
* Tell if the current user can operate.
*
* @since 3.1.4
* @access protected
* @author Grégory Viguier
*
* @return bool
*/
protected function current_user_can() {
return current_user_can( 'rocket_manage_options' );
}
/**
* Redirect the user.
*
* @since 3.1.4
* @access protected
* @author Grégory Viguier
*
* @param string $redirect URL to redirect the user to.
*/
protected function redirect( $redirect = null ) {
if ( empty( $redirect ) ) {
$redirect = wp_get_referer();
}
wp_safe_redirect( esc_url_raw( $redirect ) );
die();
}
}