modo debug WP
This commit is contained in:
@@ -0,0 +1,585 @@
|
||||
<?php
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Modo debug en wp-config.php
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
function alcastromol_debug_constants() {
|
||||
|
||||
return ['WP_DEBUG', 'WP_DEBUG_LOG', 'WP_DEBUG_DISPLAY'];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_config_path() {
|
||||
|
||||
$candidates = [];
|
||||
|
||||
if (defined('ABSPATH')) {
|
||||
$candidates[] = ABSPATH . 'wp-config.php';
|
||||
$candidates[] = dirname(ABSPATH) . '/wp-config.php';
|
||||
}
|
||||
|
||||
foreach ($candidates as $path) {
|
||||
if (is_file($path)) {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_define_pattern() {
|
||||
|
||||
return '/^([ \t]*)define\s*\(\s*[\'"](WP_DEBUG|WP_DEBUG_LOG|WP_DEBUG_DISPLAY)[\'"]\s*,\s*([^)]*)\)\s*;[ \t]*(?:(?:\/\/|#).*)?$/';
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_read() {
|
||||
|
||||
$path = alcastromol_debug_config_path();
|
||||
|
||||
if ($path === '' || !is_readable($path)) {
|
||||
return new WP_Error('alcastromol_debug_read', 'No se pudo leer wp-config.php.');
|
||||
}
|
||||
|
||||
$contents = file_get_contents($path);
|
||||
|
||||
if (!is_string($contents)) {
|
||||
return new WP_Error('alcastromol_debug_read', 'No se pudo leer wp-config.php.');
|
||||
}
|
||||
|
||||
return [
|
||||
'path' => $path,
|
||||
'contents' => $contents,
|
||||
'writable' => is_writable($path),
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_scan($contents) {
|
||||
|
||||
$newline = str_contains($contents, "\r\n") ? "\r\n" : "\n";
|
||||
$lines = preg_split("/\r\n|\n|\r/", $contents);
|
||||
$state = [
|
||||
'WP_DEBUG' => null,
|
||||
'WP_DEBUG_LOG' => null,
|
||||
'WP_DEBUG_DISPLAY' => null,
|
||||
];
|
||||
$indexes = [];
|
||||
|
||||
if (!is_array($lines)) {
|
||||
return new WP_Error('alcastromol_debug_read', 'No se pudo leer wp-config.php.');
|
||||
}
|
||||
|
||||
foreach ($lines as $index => $line) {
|
||||
if (!preg_match(alcastromol_debug_define_pattern(), $line, $match)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $match[2];
|
||||
|
||||
if (isset($indexes[$name])) {
|
||||
return new WP_Error('alcastromol_debug_duplicate', 'wp-config.php define ' . $name . ' más de una vez.');
|
||||
}
|
||||
|
||||
$indexes[$name] = $index;
|
||||
$raw = trim($match[3]);
|
||||
$normalized = strtolower(trim($raw, " \t'\""));
|
||||
$state[$name] = [
|
||||
'on' => in_array($normalized, ['true', '1'], true),
|
||||
'raw' => $raw,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'newline' => $newline,
|
||||
'lines' => $lines,
|
||||
'state' => $state,
|
||||
'indexes' => $indexes,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_status() {
|
||||
|
||||
$file = alcastromol_debug_read();
|
||||
|
||||
if (is_wp_error($file)) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
$scan = alcastromol_debug_scan($file['contents']);
|
||||
|
||||
if (is_wp_error($scan)) {
|
||||
return $scan;
|
||||
}
|
||||
|
||||
$enabled = true;
|
||||
|
||||
foreach (alcastromol_debug_constants() as $name) {
|
||||
$item = $scan['state'][$name];
|
||||
|
||||
if (!is_array($item) || empty($item['on'])) {
|
||||
$enabled = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'path' => $file['path'],
|
||||
'writable' => $file['writable'],
|
||||
'state' => $scan['state'],
|
||||
'enabled' => $enabled,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_is_enabled() {
|
||||
|
||||
$status = alcastromol_debug_status();
|
||||
|
||||
if (is_wp_error($status)) {
|
||||
return $status;
|
||||
}
|
||||
|
||||
return $status['enabled'];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_transform($contents, $enabled) {
|
||||
|
||||
$scan = alcastromol_debug_scan($contents);
|
||||
|
||||
if (is_wp_error($scan)) {
|
||||
return $scan;
|
||||
}
|
||||
|
||||
$value = $enabled ? 'true' : 'false';
|
||||
$lines = $scan['lines'];
|
||||
$indexes = array_values($scan['indexes']);
|
||||
sort($indexes);
|
||||
$indent = '';
|
||||
$anchor = null;
|
||||
|
||||
if ($indexes) {
|
||||
$anchor = $indexes[0];
|
||||
|
||||
if (preg_match('/^([ \t]*)/', $lines[$anchor], $match)) {
|
||||
$indent = $match[1];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_reverse($indexes) as $index) {
|
||||
array_splice($lines, $index, 1);
|
||||
}
|
||||
|
||||
if ($anchor === null) {
|
||||
foreach ($lines as $index => $line) {
|
||||
if (str_contains($line, 'stop editing')) {
|
||||
$anchor = $index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($anchor === null) {
|
||||
return new WP_Error('alcastromol_debug_anchor', 'No se encontró dónde escribir el modo debug en wp-config.php.');
|
||||
}
|
||||
}
|
||||
|
||||
$block = [];
|
||||
|
||||
foreach (alcastromol_debug_constants() as $name) {
|
||||
$block[] = $indent . "define( '{$name}', {$value} );";
|
||||
}
|
||||
|
||||
array_splice($lines, $anchor, 0, $block);
|
||||
|
||||
return implode($scan['newline'], $lines);
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_unmanaged_lines($contents) {
|
||||
|
||||
$lines = preg_split("/\r\n|\n|\r/", $contents);
|
||||
$kept = [];
|
||||
|
||||
if (!is_array($lines)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match(alcastromol_debug_define_pattern(), $line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$kept[] = $line;
|
||||
}
|
||||
|
||||
return $kept;
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_lint($contents) {
|
||||
|
||||
if (!is_string($contents) || $contents === '') {
|
||||
return new WP_Error('alcastromol_debug_lint', 'El resultado estaba vacío y no se guardó.');
|
||||
}
|
||||
|
||||
if (!defined('TOKEN_PARSE')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
token_get_all($contents, TOKEN_PARSE);
|
||||
} catch (ParseError $exception) {
|
||||
return new WP_Error('alcastromol_debug_lint', 'No se guardó wp-config.php porque el resultado no es PHP válido. ' . $exception->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_sync_module($enabled) {
|
||||
|
||||
if (!function_exists('alcastromol_get_modules')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$modules = alcastromol_get_modules();
|
||||
$modules['debug'] = $enabled ? '1' : '0';
|
||||
update_option('alcastromol_modules', $modules);
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_set($enabled) {
|
||||
|
||||
$enabled = (bool) $enabled;
|
||||
$file = alcastromol_debug_read();
|
||||
|
||||
if (is_wp_error($file)) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
if (!$file['writable']) {
|
||||
return new WP_Error('alcastromol_debug_write', 'wp-config.php no tiene permisos de escritura.');
|
||||
}
|
||||
|
||||
$next = alcastromol_debug_transform($file['contents'], $enabled);
|
||||
|
||||
if (is_wp_error($next)) {
|
||||
return $next;
|
||||
}
|
||||
|
||||
if ($next !== $file['contents']) {
|
||||
if (alcastromol_debug_unmanaged_lines($file['contents']) !== alcastromol_debug_unmanaged_lines($next)) {
|
||||
return new WP_Error('alcastromol_debug_guard', 'El cambio afectaba otras líneas de wp-config.php y no se guardó.');
|
||||
}
|
||||
|
||||
$lint = alcastromol_debug_lint($next);
|
||||
|
||||
if (is_wp_error($lint)) {
|
||||
return $lint;
|
||||
}
|
||||
|
||||
if (file_put_contents($file['path'], $next, LOCK_EX) === false) {
|
||||
return new WP_Error('alcastromol_debug_write', 'No se pudo guardar wp-config.php.');
|
||||
}
|
||||
}
|
||||
|
||||
alcastromol_debug_sync_module($enabled);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_lines($enabled) {
|
||||
|
||||
$value = $enabled ? 'true' : 'false';
|
||||
$lines = [];
|
||||
|
||||
foreach (alcastromol_debug_constants() as $name) {
|
||||
$lines[] = "define( '{$name}', {$value} );";
|
||||
}
|
||||
|
||||
return $lines;
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_notice($type, $message) {
|
||||
|
||||
set_transient('alcastromol_debug_notice_' . get_current_user_id(), [
|
||||
'type' => $type,
|
||||
'message' => $message,
|
||||
], MINUTE_IN_SECONDS);
|
||||
|
||||
}
|
||||
|
||||
if (function_exists('add_action')) {
|
||||
|
||||
add_action('admin_menu', function () {
|
||||
|
||||
add_submenu_page(
|
||||
'alcastromol-functions',
|
||||
'Modo debug',
|
||||
'Modo debug',
|
||||
'manage_options',
|
||||
'alcastromol-debug',
|
||||
'alcastromol_debug_admin_page'
|
||||
);
|
||||
|
||||
}, 22);
|
||||
|
||||
add_action('admin_bar_menu', function ($bar) {
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$bar->add_node([
|
||||
'id' => 'alcastromol-debug',
|
||||
'parent' => 'alcastromol-tailwind',
|
||||
'title' => 'Modo debug',
|
||||
'href' => admin_url('admin.php?page=alcastromol-debug'),
|
||||
]);
|
||||
|
||||
}, 92);
|
||||
|
||||
add_action('admin_post_alcastromol_debug_save', function () {
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_die('No tienes permisos.');
|
||||
}
|
||||
|
||||
check_admin_referer('alcastromol_debug_save');
|
||||
|
||||
$enabled = !empty($_POST['debug_mode']);
|
||||
$result = alcastromol_debug_set($enabled);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
alcastromol_debug_notice('error', $result->get_error_message());
|
||||
} elseif ($enabled) {
|
||||
alcastromol_debug_notice('success', 'Modo debug activado en wp-config.php.');
|
||||
} else {
|
||||
alcastromol_debug_notice('success', 'Modo debug desactivado en wp-config.php.');
|
||||
}
|
||||
|
||||
wp_safe_redirect(admin_url('admin.php?page=alcastromol-debug'));
|
||||
exit;
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_state_label($value) {
|
||||
|
||||
if (!is_array($value) || !isset($value['raw'])) {
|
||||
return 'no está';
|
||||
}
|
||||
|
||||
return $value['raw'];
|
||||
|
||||
}
|
||||
|
||||
function alcastromol_debug_admin_page() {
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$status = alcastromol_debug_status();
|
||||
$notice = get_transient('alcastromol_debug_notice_' . get_current_user_id());
|
||||
|
||||
if ($notice) {
|
||||
delete_transient('alcastromol_debug_notice_' . get_current_user_id());
|
||||
}
|
||||
|
||||
$enabled = !is_wp_error($status) && !empty($status['enabled']);
|
||||
$writable = !is_wp_error($status) && !empty($status['writable']);
|
||||
$log = WP_CONTENT_DIR . '/debug.log';
|
||||
|
||||
?>
|
||||
<div class="wrap alcastromol-debug">
|
||||
<h1>Modo debug</h1>
|
||||
<p class="alcastromol-debug-intro">
|
||||
Activa o desactiva estas constantes en <code>wp-config.php</code>.
|
||||
Encendido, WordPress muestra los errores de PHP y los guarda en <code>wp-content/debug.log</code>.
|
||||
</p>
|
||||
|
||||
<?php if (is_array($notice) && !empty($notice['message'])) : ?>
|
||||
<div class="notice notice-<?php echo $notice['type'] === 'error' ? 'error' : 'success'; ?> is-dismissible">
|
||||
<p><?php echo esc_html($notice['message']); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (is_wp_error($status)) : ?>
|
||||
<div class="notice notice-error">
|
||||
<p><?php echo esc_html($status->get_error_message()); ?></p>
|
||||
</div>
|
||||
<?php else : ?>
|
||||
|
||||
<?php if ($enabled) : ?>
|
||||
<div class="notice notice-warning">
|
||||
<p>El modo debug está activo. Los avisos de PHP se ven en el sitio público y en el administrador.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!$writable) : ?>
|
||||
<div class="notice notice-error">
|
||||
<p>No se puede escribir <code>wp-config.php</code>. Revisa los permisos del archivo.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<section class="alcastromol-debug-box">
|
||||
<h2><?php echo $enabled ? 'Activo' : 'Inactivo'; ?></h2>
|
||||
<p>El interruptor escribe las tres líneas juntas. Apagado las deja en <code>false</code>.</p>
|
||||
|
||||
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
|
||||
<?php wp_nonce_field('alcastromol_debug_save'); ?>
|
||||
<input type="hidden" name="action" value="alcastromol_debug_save">
|
||||
<label class="alcastromol-debug-toggle">
|
||||
<span class="alcastromol-switch">
|
||||
<input type="checkbox" name="debug_mode" value="1" <?php checked($enabled); ?> <?php disabled(!$writable); ?>>
|
||||
<span class="track"></span>
|
||||
</span>
|
||||
Modo debug
|
||||
</label>
|
||||
<pre class="alcastromol-debug-rules"><?php echo esc_html(implode("\n", alcastromol_debug_lines($enabled))); ?></pre>
|
||||
<ul class="alcastromol-debug-state">
|
||||
<?php foreach (alcastromol_debug_constants() as $name) : ?>
|
||||
<li><code><?php echo esc_html($name); ?></code> <?php echo esc_html(alcastromol_debug_state_label($status['state'][$name])); ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php submit_button('Guardar', 'primary', 'submit', false, $writable ? [] : ['disabled' => 'disabled']); ?>
|
||||
</form>
|
||||
|
||||
<p class="alcastromol-debug-meta">
|
||||
Archivo: <code><?php echo esc_html($status['path']); ?></code>
|
||||
<?php if (is_file($log)) : ?>
|
||||
· Log: <code>wp-content/debug.log</code>
|
||||
(<?php echo esc_html(size_format(filesize($log))); ?>,
|
||||
<?php echo esc_html(date_i18n('d/m/Y g:i a', filemtime($log))); ?>)
|
||||
<?php else : ?>
|
||||
· El log se crea en <code>wp-content/debug.log</code> cuando aparezca el primer error.
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.alcastromol-debug {
|
||||
max-width: 860px;
|
||||
}
|
||||
.alcastromol-debug-intro,
|
||||
.alcastromol-debug-meta,
|
||||
.alcastromol-debug-box p {
|
||||
color: #646970;
|
||||
}
|
||||
.alcastromol-debug-box {
|
||||
margin: 18px 0;
|
||||
padding: 18px 20px 8px;
|
||||
background: #fff;
|
||||
border: 1px solid #e7eaee;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.alcastromol-debug-box h2 {
|
||||
margin: 0 0 8px;
|
||||
}
|
||||
.alcastromol-debug-toggle {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
font-weight: 600;
|
||||
color: #1d2327;
|
||||
}
|
||||
.alcastromol-debug-rules {
|
||||
margin: 14px 0;
|
||||
padding: 14px 16px;
|
||||
overflow: auto;
|
||||
background: #f8f9fb;
|
||||
border: 1px solid #e7eaee;
|
||||
border-radius: 8px;
|
||||
color: #1d2327;
|
||||
font-size: 12px;
|
||||
line-height: 1.55;
|
||||
}
|
||||
.alcastromol-debug-state {
|
||||
margin: 0 0 14px;
|
||||
padding-left: 18px;
|
||||
color: #1d2327;
|
||||
}
|
||||
.alcastromol-switch {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
width: 38px;
|
||||
height: 22px;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
.alcastromol-switch input {
|
||||
position: absolute;
|
||||
opacity: 0;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
.alcastromol-switch .track {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
background: #c3c4c7;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
transition: background .15s ease;
|
||||
}
|
||||
.alcastromol-switch .track:before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
left: 3px;
|
||||
top: 3px;
|
||||
background: #fff;
|
||||
border-radius: 50%;
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, .18);
|
||||
transition: transform .15s ease;
|
||||
}
|
||||
.alcastromol-switch input:checked + .track {
|
||||
background: #d97706;
|
||||
}
|
||||
.alcastromol-switch input:checked + .track:before {
|
||||
transform: translateX(16px);
|
||||
}
|
||||
.alcastromol-switch input:focus-visible + .track {
|
||||
outline: 2px solid #2271b1;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
.alcastromol-switch input:disabled + .track {
|
||||
opacity: .5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var input = document.querySelector('.alcastromol-debug-toggle input');
|
||||
var preview = document.querySelector('.alcastromol-debug-rules');
|
||||
if (!input || !preview) {
|
||||
return;
|
||||
}
|
||||
var blocks = {
|
||||
on: <?php echo wp_json_encode(implode("\n", alcastromol_debug_lines(true))); ?>,
|
||||
off: <?php echo wp_json_encode(implode("\n", alcastromol_debug_lines(false))); ?>
|
||||
};
|
||||
input.addEventListener('change', function () {
|
||||
preview.textContent = input.checked ? blocks.on : blocks.off;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user