-
Notifications
You must be signed in to change notification settings - Fork 0
/
odwp-debug_log.php
274 lines (240 loc) · 8.64 KB
/
odwp-debug_log.php
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* Plugin Name: Debug Log Viewer
* Plugin URI: https://github.com/ondrejd/odwp-debug_log
* Description: Small plugin aimed for developers that want easily see their <code>debug.log</code> file.
* Version: 1.0.0
* Author: Ondřej Doněk
* Author URI: https://ondrejd.com/
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
* Requires at least: 4.7
* Requires PHP: 7.0
* Tested up to: 4.9.8
* Tags: debug,log,development
* Donate link: https://www.paypal.me/ondrejd
* Text Domain: odwpdl
* Domain Path: /languages/
*
* WordPress plugin Debug Log Viewer shows contents of `debug.log` file.
*
* Copyright (C) 2018 Ondřej Doněk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Ondřej Doněk <[email protected]>
* @link https://github.com/ondrejd/odwp-debug_log for the canonical source repository
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License 3.0
* @package odwp-debug_log
* @since 1.0.0
*/
/**
* This file is just a bootstrap. It checks if requirements of plugins
* are met and accordingly either allow activating the plugin or stops
* the activation process.
*
* Requirements can be specified either for PHP interperter or for
* the WordPress self. In both cases you can specify minimal required
* version and required extensions/plugins.
*
* If you are using copy of original file in your plugin you should change
* prefix "odwpdl" and name "odwp-debug_log" to your own values.
*
* To set the requirements go down to line 200 and define array that
* is used as a parameter for `odwpdl_check_requirements` function.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Some constants
defined( 'DL_SLUG' ) || define( 'DL_SLUG', 'odwpdl' );
defined( 'DL_NAME' ) || define( 'DL_NAME', 'odwp-debug_log' );
defined( 'DL_PATH' ) || define( 'DL_PATH', dirname( __FILE__ ) . '/' );
defined( 'DL_FILE' ) || define( 'DL_FILE', __FILE__ );
defined( 'DL_LOG' ) || define( 'DL_LOG', WP_CONTENT_DIR . '/debug.log' );
defined( 'DL_DEBUG' ) || define( 'DL_DEBUG', true );
if ( ! function_exists( 'odwpdl_check_requirements' ) ) :
/**
* Check requirements of our plugin.
*
* @global string $wp_version
* @param array $requirements
* @return array
* @since 1.0.0
* @uses get_option()
* @uses load_plugin_textdomain()
*/
function odwpdl_check_requirements( array $requirements ) {
global $wp_version;
// Initialize locales
load_plugin_textdomain( DL_SLUG, false, DL_NAME . '/languages' );
/**
* @var array Hold requirement errors
*/
$errors = [];
// Check PHP version
if ( ! empty( $requirements['php']['version'] ) ) {
if ( version_compare( phpversion(), $requirements['php']['version'], '<' ) ) {
$errors[] = sprintf(
__( 'Used PHP interpreter doesn\'t meet requirements of this plugin (is required version <b>%1$s</b> at least)!'),
$requirements['php']['version']
);
}
}
// Check PHP extensions
if ( count( $requirements['php']['extensions'] ) > 0 ) {
foreach ( $requirements['php']['extensions'] as $req_ext ) {
if ( ! extension_loaded( $req_ext ) ) {
$errors[] = sprintf( __( 'PHP extension <b>%1$s</b> is required but not installed!', DL_SLUG ), $req_ext );
}
}
}
// Check WP version
if ( ! empty( $requirements['wp']['version'] ) ) {
if ( version_compare( $wp_version, $requirements['wp']['version'], '<' ) ) {
$errors[] = sprintf(
__( 'This plugin requires higher version of <b>WordPress</b> (at least version <b>%1$s</b>)!', DL_SLUG ),
$requirements['wp']['version']
);
}
}
// Check WP plugins
if ( count( $requirements['wp']['plugins'] ) > 0 ) {
$active_plugins = (array) get_option( 'active_plugins', [] );
foreach ( $requirements['wp']['plugins'] as $req_plugin ) {
if ( ! in_array( $req_plugin, $active_plugins ) ) {
$errors[] = sprintf( __( 'The plugin <b>%1$s</b> is required but not installed!', DL_SLUG ), $req_plugin );
}
}
}
return $errors;
}
endif;
if ( ! function_exists( 'odwpdl_deactivate_raw' ) ) :
/**
* Deactivate plugin by the raw way (it updates directly WP options).
*
* @return void
* @since 1.0.0
* @uses get_option()
* @uses update_option()
*/
function odwpdl_deactivate_raw() {
$active_plugins = get_option( 'active_plugins' );
$out = [];
foreach ( $active_plugins as $key => $val ) {
if ( $val != DL_NAME . '/' . DL_NAME . '.php' ) {
$out[$key] = $val;
}
}
update_option( 'active_plugins', $out );
}
endif;
if ( ! function_exists( 'odwpdl_write_log' ) ) :
/**
* Write record to the `wp-content/debug.log` file.
*
* @param mixed $log
* @return void
* @since 1.0.0
*/
function odwpdl_write_log( $log ) {
if ( ! file_exists( DL_LOG ) || ! is_writable( DL_LOG ) ) {
return;
}
if ( is_null( $log ) ) {
$message = 'NULL';
} elseif ( is_array( $log ) || is_object( $log ) ) {
$message = print_r( $log, true );
} else {
$message = $log;
}
$datetime = date( 'd-M-Y H:i:s', time() );
$record = '[' . $datetime . ' UTC] ' . trim( $message ) . PHP_EOL;
file_put_contents( DL_LOG, $record, FILE_APPEND );
}
endif;
if ( ! function_exists( 'odwpdl_error_log' ) ) :
/**
* Write message to the `wp-content/debug.log` file.
*
* @deprecated 1.0.0
* @param string $message
* @param int $message_type (Optional.)
* @param string $destination (Optional.)
* @param string $extra_headers (Optional.)
* @return void
* @since 1.0.0
* @uses _deprecated_function()
*/
function odwpdl_error_log( string $message, int $message_type = null, string $destination = null, string $extra_headers = null ) {
_deprecated_function( __FUNCTION__, '1.0.0', __( 'Use function `odwpdl_write_log` instead!', DL_SLUG ) );
odwpdl_write_log( $message );
}
endif;
if ( ! function_exists( 'readonly' ) ) :
/**
* Prints HTML readonly attribute. It's an addition to WP original functions {@see disabled()} and {@see checked()}.
*
* @param mixed $value
* @param mixed $current (Optional.) Defaultly TRUE.
* @return string
* @since 1.0.0
*/
function readonly( $current, $value = true ) {
if ( $current == $value ) {
echo ' readonly';
}
}
endif;
/**
* @var array $odwpdl_errs Errors from the requirements check.
*/
$odwpdl_errs = odwpdl_check_requirements( [
'php' => [
// Enter minimum PHP version you needs
'version' => '7.0',
// Enter extensions that your plugin needs
'extensions' => [
//'gd',
],
],
'wp' => [
// Enter minimum WP version you need
'version' => '4.7',
// Enter WP plugins that your plugin needs
'plugins' => [
//'woocommerce/woocommerce.php',
],
],
] );
// Check if requirements are met or not
if ( count( $odwpdl_errs ) > 0 ) {
// Requirements are not met
odwpdl_deactivate_raw();
// In administration print errors
if ( is_admin() ) {
add_action( 'admin_notices', function() use ( $odwpdl_errs ) {
$err_head = __( '<b>Debug Log Viewer</b>: ', DL_SLUG );
foreach ( $odwpdl_errs as $err ) {
printf( '<div class="error"><p>%1$s</p></div>', $err_head . $err );
}
} );
}
} else {
// Requirements are met so initialize the plugin.
include( DL_PATH . 'src/DL_Screen_Prototype.php' );
include( DL_PATH . 'src/DL_Plugin.php' );
DL_Plugin::initialize();
}