PluginProbe
WP Console – WordPress PHP Console powered by PsySH / trunk
WP Console – WordPress PHP Console powered by PsySH vtrunk
trunk 2.4.0 2.4.1 2.5.0 2.5.1 2.6.0
wp-console / includes / Core / DebugLog / RestController.php

RestController.php in WP Console – WordPress PHP Console powered by PsySH trunk, at includes/Core/DebugLog/RestController.php

196 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPConsole\Core\DebugLog;
4
5 use Exception;
6 use WPConsole\Exceptions\WPConsoleException;
7 use WPConsole\Traits\RESTResponseError;
8 use WP_REST_Controller;
9 use WP_REST_Server;
10
11 class RestController extends WP_REST_Controller {
12
13 use RESTResponseError;
14
15 /**
16 * Endpoint namespace
17 *
18 * @since 1.3.0
19 *
20 * @var string
21 */
22 protected $namespace = 'wp-console/v1';
23
24 /**
25 * Route name
26 *
27 * @since 1.3.0
28 *
29 * @var string
30 */
31 protected $base = 'debug-log';
32
33 /**
34 * Class constructor
35 *
36 * @since 1.3.0
37 *
38 * @return void
39 */
40 public function __construct() {
41 $this->register_routes();
42 }
43
44 /**
45 * Register REST routes
46 *
47 * @since 1.3.0
48 *
49 * @return void
50 */
51 public function register_routes() {
52 register_rest_route( $this->namespace, '/' . $this->base, [
53 [
54 'methods' => WP_REST_Server::READABLE,
55 'callback' => [ $this, 'get_item' ],
56 'permission_callback' => [ $this, 'can_manage_options' ],
57 ],
58 [
59 'methods' => WP_REST_Server::DELETABLE,
60 'callback' => [ $this, 'delete_item' ],
61 'permission_callback' => [ $this, 'can_manage_options' ],
62 ],
63 ] );
64 }
65
66 /**
67 * Check if current user has manage_options capability
68 *
69 * @since 2.0.0
70 *
71 * @param \WP_REST_Request $request
72 *
73 * @return bool
74 */
75 public function can_manage_options( $request ) {
76 return current_user_can( 'manage_options' );
77 }
78
79 protected function get_log_file() {
80 if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
81 throw new WPConsoleException( 'wp_console_rest_error', __( '`WP_DEBUG` is required to be set true in wp-config.php', 'wp-console' ) );
82 }
83
84 if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) {
85 throw new WPConsoleException( 'wp_console_rest_error', __( '`WP_DEBUG_LOG` is required to be set true in wp-config.php', 'wp-console' ) );
86 }
87
88 $log_file = ini_get( 'error_log' );
89
90 if ( ! $log_file ) {
91 throw new WPConsoleException( 'wp_console_rest_error', __( 'Error log file not found in ini_get( "error_log" )', 'wp-console' ) );
92 }
93
94 if ( ! file_exists( $log_file ) ) {
95 throw new WPConsoleException(
96 'wp_console_rest_error',
97 /* translators: %s: log file */
98 sprintf( __( 'Log file: `%s` not found in your system.', 'wp-console' ), $log_file )
99 );
100 }
101
102 return $log_file;
103 }
104
105 /**
106 * Get debug log
107 *
108 * @since 1.3.0
109 *
110 * @param \WP_REST_Request $request
111 *
112 * @return \WP_REST_Response|\WP_Error
113 */
114 public function get_item( $request ) {
115 try {
116 $log_file = $this->get_log_file();
117
118 $debug_log = file_get_contents( $log_file );
119
120 $response = rest_ensure_response( $debug_log );
121
122 $extra_info = json_encode( [
123 'php_version' => PHP_VERSION,
124 'current_time' => current_time( 'mysql' ),
125 'timezone' => $this->wp_timezone_string(),
126 ] );
127
128 $response->header( 'X-WP-Console-Debug-Log-Extra-Info', $extra_info );
129
130 return $response;
131
132 } catch ( Exception $e ) {
133 return $this->send_response_error( $e, __( 'Something went wrong', 'wp-console' ) );
134 }
135 }
136
137 /**
138 * Erase/Clear debug.log
139 *
140 * @since 2.0.0
141 *
142 * @param \WP_REST_Request $request
143 *
144 * @return \WP_REST_Response|\WP_Error
145 */
146 public function delete_item( $request ) {
147 try {
148 $log_file = $this->get_log_file();
149
150 file_put_contents( $log_file, '' );
151
152 $debug_log = file_get_contents( $log_file );
153
154 return rest_ensure_response( $debug_log );
155
156 } catch ( Exception $e ) {
157 return $this->send_response_error( $e, __( 'Something went wrong', 'wp-console' ) );
158 }
159 }
160
161 /**
162 * Retrieves the timezone from site settings as a string.
163 *
164 * Copied from `wp-includes/functions.php` to give support
165 * WordPress older than Version 5.3.
166 *
167 * @since 2.0.0
168 *
169 * @return string PHP timezone string or a ±HH:MM offset.
170 */
171 protected function wp_timezone_string() {
172 global $wp_version;
173
174 if ( function_exists( 'wp_timezone_string' ) ) {
175 return wp_timezone_string();
176 }
177
178 $timezone_string = get_option( 'timezone_string' );
179
180 if ( $timezone_string ) {
181 return $timezone_string;
182 }
183
184 $offset = (float) get_option( 'gmt_offset' );
185 $hours = (int) $offset;
186 $minutes = ( $offset - $hours );
187
188 $sign = ( $offset < 0 ) ? '-' : '+';
189 $abs_hour = abs( $hours );
190 $abs_mins = abs( $minutes * 60 );
191 $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
192
193 return $tz_offset;
194 }
195 }
196