$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'; ?>

Modo debug

Activa o desactiva estas constantes en wp-config.php. Encendido, WordPress muestra los errores de PHP y los guarda en wp-content/debug.log.

get_error_message()); ?>

El modo debug está activo. Los avisos de PHP se ven en el sitio público y en el administrador.

No se puede escribir wp-config.php. Revisa los permisos del archivo.

El interruptor escribe las tres líneas juntas. Apagado las deja en false.

'disabled']); ?>

Archivo: · Log: wp-content/debug.log (, ) · El log se crea en wp-content/debug.log cuando aparezca el primer error.