Files
roi-theme/wp-content/mu-plugins/allow-unfiltered-html.php.backup
root a22573bf0b Commit inicial - WordPress Análisis de Precios Unitarios
- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 21:04:30 -06:00

333 lines
11 KiB
Plaintext
Executable File

<?php
/**
* Plugin Name: HTML Raw Mode (Thrive Compatible)
* Description: Renderiza HTML/CSS/JS para TODOS los visitantes. Solo Admins pueden GUARDAR código. Compatible con Thrive Architect.
* Version: 6.0.0
* Author: PODC4
* License: GPL-2.0-or-later
*/
if (!defined('ABSPATH')) exit;
// ============================================
// FUNCIÓN: DETECTAR PÁGINAS DE THRIVE ARCHITECT
// ============================================
function is_thrive_architect_page($post_id = null) {
if (!$post_id) {
global $post;
if (!$post) return false;
$post_id = $post->ID;
}
$thrive_meta_keys = [
'tcb_editor_enabled',
'tve_updated_post',
'tve_landing_page',
'thrive_tcb_post_fonts',
'tve_globals',
'tcb2_ready',
];
foreach ($thrive_meta_keys as $key) {
$value = get_post_meta($post_id, $key, true);
if (!empty($value)) {
return true;
}
}
return false;
}
// ============================================
// CAPACIDADES PARA ADMINISTRADORES
// ============================================
add_action('init', function () {
if (is_multisite()) return;
$role = get_role('administrator');
if ($role && !$role->has_cap('unfiltered_html')) {
$role->add_cap('unfiltered_html');
}
}, 1);
// ============================================
// DESACTIVAR KSES AL GUARDAR (SOLO ADMINS)
// ============================================
add_action('init', function () {
if (defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML) return;
if (!current_user_can('unfiltered_html')) return;
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
remove_filter('content_save_pre', 'wp_filter_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_kses');
remove_filter('excerpt_save_pre', 'wp_filter_kses');
add_action('wp_loaded', function () {
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
remove_filter('content_save_pre', 'wp_filter_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_kses');
remove_filter('excerpt_save_pre', 'wp_filter_kses');
}, 9999);
}, 20);
// ============================================
// FRONTEND: RENDERIZAR HTML CRUDO PARA TODOS (EXCEPTO THRIVE)
// ============================================
add_action('init', function () {
if (is_admin()) return;
// Quita filtros KSES típicos en frontend para TODOS
foreach (['wp_kses_post', 'wp_kses_data', 'wp_filter_kses'] as $cb) {
if (has_filter('the_content', $cb)) {
remove_filter('the_content', $cb);
}
}
// Red de seguridad: elimina filtros KSES globales (TODOS los usuarios)
if (function_exists('kses_remove_filters')) {
kses_remove_filters();
}
// Acepta <script> y <style> si algún filtro externo aplica wp_kses
add_filter('wp_kses_allowed_html', function ($allowed, $context) {
if (!is_array($allowed)) $allowed = [];
if ($context === 'post' || $context === null) {
$allowed['script'] = [
'src' => true,
'type' => true,
'async' => true,
'defer' => true,
'referrerpolicy' => true,
'crossorigin' => true,
];
$allowed['style'] = [
'type' => true,
'media' => true,
];
$allowed['iframe'] = [
'src' => true,
'width' => true,
'height' => true,
'frameborder' => true,
'allowfullscreen' => true,
'allow' => true,
];
}
return $allowed;
}, 10, 2);
// FUERZA contenido crudo desde BD para TODOS los visitantes (máxima prioridad)
add_filter('the_content', function ($content) {
if (is_admin()) return $content;
global $post;
if (!$post) return $content;
// NO TOCAR páginas de Thrive Architect
if (is_thrive_architect_page($post->ID)) {
return $content;
}
// Obtén contenido crudo desde la base de datos (sin filtros)
$raw = get_post_field('post_content', $post->ID, 'raw');
if ($raw === '' || $raw === null) return $content;
// Aplica shortcodes si los usas
if (function_exists('do_shortcode')) {
$raw = do_shortcode($raw);
}
// NO aplicar wp_kses ni filtros. Retorna HTML/CSS/JS crudo para TODOS
return $raw;
}, PHP_INT_MAX);
}, 30);
// ============================================
// CONFIGURACIÓN TINYMCE (SOLO ADMINS, EXCEPTO THRIVE)
// ============================================
add_filter('tiny_mce_before_init', function ($init) {
if (!current_user_can('unfiltered_html')) return $init;
global $post;
if ($post && is_thrive_architect_page($post->ID)) {
return $init;
}
$init['valid_elements'] = '*[*]';
$init['extended_valid_elements'] = 'script[src|type|async|defer|referrerpolicy|crossorigin],style[type|media],iframe[src|width|height|frameborder|allowfullscreen|allow]';
$init['paste_remove_styles'] = false;
$init['paste_as_text'] = false;
$init['verify_html'] = false;
$init['cleanup'] = false;
$init['forced_root_block'] = false;
return $init;
});
// ============================================
// DESACTIVAR WPAUTOP PARA TODOS EN FRONTEND (CRÍTICO)
// ============================================
/**
* IMPORTANTE: Esta función NO verifica capacidades porque debe aplicar
* a TODOS los visitantes (logueados o no) en el frontend.
* Solo afecta la visualización, no el guardado.
*/
add_action('init', function () {
if (is_admin()) return; // Solo frontend
// Desactiva wpautop para TODOS los usuarios
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
}, 20);
// Hook adicional con prioridad 0 para asegurar que se ejecute antes que todo
add_filter('the_content', function($content) {
if (is_admin()) return $content;
global $post;
if ($post && is_thrive_architect_page($post->ID)) {
return $content; // Mantener wpautop para Thrive
}
// Para páginas normales: desactiva wpautop (TODOS los usuarios)
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
return $content;
}, 0);
// ============================================
// HOOK ADICIONAL: wp_insert_post_data (REFUERZO)
// ============================================
add_filter('wp_insert_post_data', function($data, $postarr) {
// Solo para admins y si NO es Thrive
if (!current_user_can('unfiltered_html')) return $data;
if (isset($postarr['ID']) && is_thrive_architect_page($postarr['ID'])) return $data;
// Guarda el contenido exactamente como viene
if (isset($postarr['content']) && !empty($postarr['content'])) {
$data['post_content'] = $postarr['content'];
$data['post_content_filtered'] = '';
}
return $data;
}, 99, 2);
// ============================================
// OPTIMIZACIÓN: REMOVER CSS DE BLOQUES
// ============================================
add_action('wp_enqueue_scripts', function() {
global $post;
if ($post && is_thrive_architect_page($post->ID)) {
return;
}
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('wc-blocks-style');
wp_dequeue_style('global-styles');
wp_dequeue_style('classic-theme-styles');
}, 100);
// ============================================
// COLUMNA EN LISTADO DE PÁGINAS
// ============================================
add_filter('manage_pages_columns', function($columns) {
$new_columns = [];
foreach ($columns as $key => $value) {
$new_columns[$key] = $value;
if ($key === 'title') {
$new_columns['page_editor_type'] = '🔧 Editor';
}
}
return $new_columns;
});
add_action('manage_pages_custom_column', function($column, $post_id) {
if ($column === 'page_editor_type') {
if (is_thrive_architect_page($post_id)) {
echo '<span style="color: #00a32a; font-weight: bold;">🎨 Thrive</span>';
} else {
echo '<span style="color: #2271b1;">📝 HTML Raw</span>';
}
}
}, 10, 2);
// ============================================
// NOTIFICACIONES EN ADMIN
// ============================================
add_action('admin_notices', function() {
global $post, $pagenow;
if (!in_array($pagenow, ['post.php', 'post-new.php'])) return;
if (!$post) return;
if (is_thrive_architect_page($post->ID)) {
echo '<div class="notice notice-success">';
echo '<p><strong>🎨 Thrive Architect</strong> - Esta página no será afectada por HTML Raw Mode.</p>';
echo '</div>';
} else {
$screen = get_current_screen();
if ($screen && !$screen->is_block_editor()) {
echo '<div class="notice notice-info">';
echo '<p><strong>📝 Modo HTML Raw</strong> - Usa la pestaña <strong>"Texto"</strong> para pegar HTML/CSS/JS sin restricciones. El contenido se verá igual para TODOS los visitantes.</p>';
echo '</div>';
}
}
});
add_action('admin_notices', function() {
$screen = get_current_screen();
if ($screen->id !== 'dashboard') return;
if (!is_plugin_active('classic-editor/classic-editor.php')) {
echo '<div class="notice notice-warning is-dismissible">';
echo '<p><strong>⚠️ HTML Raw Mode:</strong> Se recomienda instalar <strong>"Classic Editor"</strong> para deshabilitar bloques.</p>';
echo '<p><a href="' . admin_url('plugin-install.php?s=classic+editor&tab=search&type=term') . '" class="button button-primary">Instalar Classic Editor</a></p>';
echo '</div>';
}
});
// ============================================
// SCRIPT ADMIN: Destacar pestaña TEXTO
// ============================================
add_action('admin_head', function() {
global $post;
if (!$post || !current_user_can('unfiltered_html')) return;
if (is_thrive_architect_page($post->ID)) return;
?>
<style>
#content-html {
background: #2271b1 !important;
color: white !important;
font-weight: bold !important;
}
</style>
<?php
});
// ============================================
// PROTECCIÓN EXTRA PARA THRIVE
// ============================================
add_action('admin_init', function() {
if (isset($_GET['tve']) || isset($_GET['tcbf']) || isset($_GET['tar_edit_mode'])) {
// Estamos en el editor de Thrive, no modificar nada
return;
}
});