-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-trace-cleaner.php
More file actions
367 lines (312 loc) · 14.6 KB
/
Copy pathllm-trace-cleaner.php
File metadata and controls
367 lines (312 loc) · 14.6 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Plugin Name: LLM Trace Cleaner
* Plugin URI: https://github.com/YaggoSEO/llm-trace-cleaner
* Description: Elimina rastros de herramientas LLM del HTML y audita/sanea metadatos de imágenes (JPEG/PNG/WebP).
* Version: 1.8.2
* Author: Yago Vázquez Gómez (Yaggoseo)
* Author URI: https://yaggoseo.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: llm-trace-cleaner
* Requires at least: 5.0
* Requires PHP: 7.4
*/
// Prevenir acceso directo
defined('ABSPATH') || exit;
// Verificar si las constantes ya están definidas (evita conflictos en actualizaciones)
if (!defined('LLM_TRACE_CLEANER_VERSION')) {
// Definir constantes del plugin
define('LLM_TRACE_CLEANER_VERSION', '1.8.2');
define('LLM_TRACE_CLEANER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('LLM_TRACE_CLEANER_PLUGIN_URL', plugin_dir_url(__FILE__));
}
// ========================================
// SISTEMA DE ACTUALIZACIONES DESDE GITHUB
// ========================================
// Cargar variables de entorno desde .env
require_once plugin_dir_path(__FILE__) . 'includes/class-llm-trace-cleaner-env-loader.php';
LLM_Trace_Cleaner_Env_Loader::load(plugin_dir_path(__FILE__) . '.env');
// Configuración de GitHub (repositorio público)
if (!defined('LLM_TRACE_CLEANER_GITHUB_USER')) {
define('LLM_TRACE_CLEANER_GITHUB_USER', 'YaggoSEO');
}
if (!defined('LLM_TRACE_CLEANER_GITHUB_REPO')) {
define('LLM_TRACE_CLEANER_GITHUB_REPO', 'llm-trace-cleaner');
}
if (!defined('LLM_TRACE_CLEANER_GITHUB_BRANCH')) {
define('LLM_TRACE_CLEANER_GITHUB_BRANCH', 'main');
}
// El token se carga desde .env (solo necesario para repositorios privados)
/**
* Clase principal del plugin
*/
if (!class_exists('LLM_Trace_Cleaner')) {
final class LLM_Trace_Cleaner {
/**
* Instancia única del plugin (Singleton)
*/
private static $instance = null;
/**
* Obtener instancia única del plugin
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor privado (Singleton)
*/
private function __construct() {
$this->init();
}
/**
* Inicializar el plugin
*/
private function init() {
// Cargar archivos necesarios
$this->load_dependencies();
// Inicializar componentes
add_action('plugins_loaded', array($this, 'load_components'));
// Verificar y ejecutar actualizaciones si es necesario
add_action('admin_init', array($this, 'check_version'));
// Inicializar sistema de actualizaciones desde GitHub
$this->init_github_updater();
}
/**
* Inicializar sistema de actualizaciones desde GitHub
*/
private function init_github_updater() {
if (defined('LLM_TRACE_CLEANER_GITHUB_USER') && defined('LLM_TRACE_CLEANER_GITHUB_REPO')) {
$updater_file = LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-github-updater.php';
if (!file_exists($updater_file)) {
return;
}
require_once $updater_file;
if (!class_exists('LLM_Trace_Cleaner_GitHub_Updater')) {
return;
}
// Obtener token (opcional para repos públicos)
$token = defined('LLM_TRACE_CLEANER_GITHUB_TOKEN') ? LLM_TRACE_CLEANER_GITHUB_TOKEN : null;
// Limpiar el token si existe
if (!empty($token)) {
$token = trim($token);
$token = preg_replace('/\s+/', '', $token);
}
$branch = defined('LLM_TRACE_CLEANER_GITHUB_BRANCH') ? LLM_TRACE_CLEANER_GITHUB_BRANCH : 'main';
// Inicializar updater
new LLM_Trace_Cleaner_GitHub_Updater(
LLM_TRACE_CLEANER_PLUGIN_DIR . 'llm-trace-cleaner.php',
LLM_TRACE_CLEANER_GITHUB_USER,
LLM_TRACE_CLEANER_GITHUB_REPO,
$branch,
$token
);
}
}
/**
* Verificar versión y ejecutar actualizaciones si es necesario
*/
public function check_version() {
$installed_version = get_option('llm_trace_cleaner_version', '0.0.0');
if (version_compare($installed_version, LLM_TRACE_CLEANER_VERSION, '<')) {
// Ejecutar actualización
$this->upgrade($installed_version);
update_option('llm_trace_cleaner_version', LLM_TRACE_CLEANER_VERSION);
}
}
/**
* Ejecutar actualizaciones según la versión anterior
*/
private function upgrade($old_version) {
// Añadir nuevas opciones si no existen (para actualizaciones)
if (version_compare($old_version, '1.1.2', '<')) {
// Opciones añadidas en 1.1.2
if (get_option('llm_trace_cleaner_telemetry_opt_in') === false) {
add_option('llm_trace_cleaner_telemetry_opt_in', true);
}
if (get_option('llm_trace_cleaner_batch_size') === false) {
add_option('llm_trace_cleaner_batch_size', 10);
}
if (get_option('llm_trace_cleaner_error_logs') === false) {
add_option('llm_trace_cleaner_error_logs', array());
}
if (get_option('llm_trace_cleaner_debug_logs') === false) {
add_option('llm_trace_cleaner_debug_logs', array());
}
}
// Asegurar que la tabla de logs existe (por si acaso)
if (class_exists('LLM_Trace_Cleaner_Activator')) {
LLM_Trace_Cleaner_Activator::create_log_table();
LLM_Trace_Cleaner_Activator::migrate_image_module();
}
}
/**
* Cargar archivos de dependencias
*/
private function load_dependencies() {
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-activator.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-cleaner.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-logger.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-cache.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-admin.php';
// Módulo de metadatos de imágenes
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-capabilities.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/engines/interface-llm-trace-cleaner-image-engine.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/engines/class-llm-trace-cleaner-image-engine-imagick.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/engines/class-llm-trace-cleaner-image-engine-gd.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-logger.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-profile.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-inspector.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-sanitizer.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-tag-map.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-metadata-writer.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-backup.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-batch.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/class-llm-trace-cleaner-image-manager.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/engines/class-llm-trace-cleaner-image-engine-exiftool.php';
require_once LLM_TRACE_CLEANER_PLUGIN_DIR . 'includes/admin/class-llm-trace-cleaner-image-admin.php';
}
/**
* Cargar componentes del plugin
*/
public function load_components() {
// Inicializar administración
if (is_admin()) {
LLM_Trace_Cleaner_Admin::get_instance();
LLM_Trace_Cleaner_Image_Admin::get_instance();
}
// Inicializar control de caché para bots
LLM_Trace_Cleaner_Cache::get_instance();
// Módulo de imágenes: hooks solo si está activado en settings
$image_settings = get_option( 'llm_trace_cleaner_image_settings', array() );
if ( is_array( $image_settings ) && ! empty( $image_settings['enabled'] ) ) {
LLM_Trace_Cleaner_Image_Manager::get_instance();
}
// Inicializar limpieza automática si está activada
$auto_clean_enabled = get_option('llm_trace_cleaner_auto_clean', false);
if ($auto_clean_enabled) {
add_action('save_post', array($this, 'auto_clean_post'), 10, 2);
}
}
/**
* Actualizar post sin ejecutar hooks de save_post
* Esto evita bloqueos causados por plugins pesados como WPML, WooCommerce, RankMath, etc.
*
* @param int $post_id ID del post a actualizar
* @param string $post_content Contenido nuevo del post
* @return int|false ID del post si se actualizó correctamente, false en caso de error
*/
private function update_post_without_hooks($post_id, $post_content) {
global $wpdb;
// Actualizar directamente en la base de datos para evitar hooks
$result = $wpdb->update(
$wpdb->posts,
array(
'post_content' => $post_content,
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql', 1)
),
array('ID' => $post_id),
array('%s', '%s', '%s'),
array('%d')
);
if ($result !== false) {
// Limpiar caché de WordPress
clean_post_cache($post_id);
return $post_id;
}
return false;
}
/**
* Limpiar automáticamente el contenido al guardar
*/
public function auto_clean_post($post_id, $post) {
// Verificar que no sea una revisión o autosave
if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
return;
}
// Solo procesar posts y páginas publicados
if ($post->post_status !== 'publish') {
return;
}
// Solo procesar post types 'post' y 'page'
if (!in_array($post->post_type, array('post', 'page'))) {
return;
}
// Obtener el contenido actual
$content = $post->post_content;
// Obtener opciones de configuración
$clean_options = array(
'clean_attributes' => get_option('llm_trace_cleaner_clean_attributes', false),
'clean_unicode' => get_option('llm_trace_cleaner_clean_unicode', false),
'track_locations' => true
);
// Si ambas opciones están desactivadas, no hacer nada
if (!$clean_options['clean_attributes'] && !$clean_options['clean_unicode']) {
return;
}
// Limpiar el contenido
$cleaner = new LLM_Trace_Cleaner_Cleaner();
$cleaned_content = $cleaner->clean_html($content, $clean_options);
// Si hubo cambios, actualizar el post
if ($cleaned_content !== $content) {
// Desactivar caché durante la limpieza
LLM_Trace_Cleaner_Cache::disable_cache_for_cleaning();
// Remover el hook para evitar loop infinito
remove_action('save_post', array($this, 'auto_clean_post'), 10);
// Actualizar el post SIN hooks para evitar bloqueos de plugins pesados
// Esto evita ejecutar los 50+ hooks de save_post (WPML, WooCommerce, RankMath, etc.)
$update_result = $this->update_post_without_hooks($post_id, $cleaned_content);
if ($update_result === false) {
// Si falla la actualización directa, intentar con wp_update_post como fallback
wp_update_post(array(
'ID' => $post_id,
'post_content' => $cleaned_content
));
}
// Limpiar caché del post modificado
LLM_Trace_Cleaner_Cache::clear_post_cache($post_id);
// Volver a agregar el hook
add_action('save_post', array($this, 'auto_clean_post'), 10, 2);
// Registrar en el log - forzar registro si el contenido cambió (incluso sin stats)
$logger = new LLM_Trace_Cleaner_Logger();
$stats = $cleaner->get_last_stats();
$change_locations = $cleaner->get_change_locations();
$logger->log_action('auto', $post_id, $post->post_title, $stats, true, $content, $cleaned_content, $change_locations);
}
}
}
}
/**
* Inicializar el plugin
*/
if (!function_exists('llm_trace_cleaner_init')) {
function llm_trace_cleaner_init() {
if (!class_exists('LLM_Trace_Cleaner')) {
return false;
}
return LLM_Trace_Cleaner::get_instance();
}
}
// Registrar hooks de activación/desactivación (debe estar fuera de la clase)
if (!function_exists('llm_trace_cleaner_register_hooks')) {
function llm_trace_cleaner_register_hooks() {
// Cargar el activador primero
$activator_file = plugin_dir_path(__FILE__) . 'includes/class-llm-trace-cleaner-activator.php';
if (file_exists($activator_file)) {
require_once $activator_file;
}
// Registrar hooks
register_activation_hook(__FILE__, array('LLM_Trace_Cleaner_Activator', 'activate'));
register_deactivation_hook(__FILE__, array('LLM_Trace_Cleaner_Activator', 'deactivate'));
}
llm_trace_cleaner_register_hooks();
}
// Iniciar el plugin solo si no se ha inicializado ya
if (!did_action('llm_trace_cleaner_loaded')) {
llm_trace_cleaner_init();
do_action('llm_trace_cleaner_loaded');
}