-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmu-cc-console-mute.php
More file actions
176 lines (160 loc) · 5.87 KB
/
mu-cc-console-mute.php
File metadata and controls
176 lines (160 loc) · 5.87 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
<?php
/**
* Plugin Name: WP Console Mute (MU)
* Plugin URI: https://github.com/codecorntech/wp-console-mute
* Description: Disattiva in modo selettivo o totale i messaggi della console JavaScript in WordPress (anche negli iframe),
* e previene errori noti di librerie in editori visuali (es. Elementor Checklist bug).
* Version: 1.1.2
* Author: CodeCorn™ Technology
* Author URI: https://www.codecorn.it
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: wp-console-mute
* Domain Path: /languages
*
* ============================================================
* MU-PLUGIN NOTICE:
* Questo file può essere copiato in:
* → /wp-content/mu-plugins/mu-cc-console-mute.php
* Verrà caricato automaticamente da WordPress, senza attivazione manuale.
* ============================================================
*/
/**
* ============================================================
* 1 FIX SPECIFICO PER EDITOR ELEMENTOR
* Evita crash "Cannot read properties of null (reading 'parentElement')"
* causato da checklist.min.js → ToggleIcon.apply()
* ============================================================
*/
// add_action('elementor/editor/after_enqueue_scripts', function () {
// // Silenzia solo in editor (non nel frontend)
// $js = <<<'JS'
// (function(){
// try {
// // Intercetta l'errore specifico in runtime
// window.addEventListener('error', function(ev){
// if(
// ev?.message?.includes("Cannot read properties of null (reading 'parentElement')") &&
// String(ev?.filename || '').includes('checklist.min.js')
// ){
// ev.preventDefault();
// ev.stopImmediatePropagation();
// return false;
// }
// }, true);
// // Patcha ToggleIcon.apply se presente
// function patchToggleIcon(){
// try{
// var ToggleIcon =
// window.ToggleIcon ||
// window.elementor?.modules?.checklist?.commands?.ToggleIcon ||
// window.elementor?.common?.commands?.ToggleIcon;
// if(!ToggleIcon || ToggleIcon.__ccPatched) return;
// var proto = ToggleIcon.prototype || ToggleIcon;
// var orig = proto.apply;
// if(typeof orig !== 'function') return;
// proto.apply = function(){
// try{
// var t = this?.target || this?.$target || this?.el || null;
// if(t && !t.parentElement) return;
// return orig.apply(this, arguments);
// }catch(e){
// if(String(e).includes("parentElement")) return;
// throw e;
// }
// };
// ToggleIcon.__ccPatched = true;
// }catch(e){}
// }
// document.addEventListener('DOMContentLoaded', patchToggleIcon);
// (window.elementor ? elementor.on('preview:loaded', patchToggleIcon) : null);
// setTimeout(patchToggleIcon, 1500);
// }catch(e){}
// })();
// JS;
// echo "<script>{$js}</script>";
// }, PHP_INT_MAX);
/**
* ============================================================
* 2 SOLO GMaps warnings → Silenziamento selettivo (default)
* ============================================================
*/
function cc_console_mute_gmaps_warnings()
{
$disable_in_dev = false; // setta true per non silenziare in WP_DEBUG
if (defined('WP_DEBUG') && WP_DEBUG && $disable_in_dev) {
error_log('[WP Console Mute] Debug attivo, skip del silenziamento.');
return;
}
$patterns = apply_filters(
'cc_console_mute_patterns',
[
'Google Maps JavaScript API',
'Google Maps JavaScript API has been loaded',
]
);
$json_patterns = wp_json_encode($patterns ?: []);
$js = <<<JS
(function(){
try {
var patterns = {$json_patterns};
var originalWarn = console.warn ? console.warn.bind(console) : function(){};
console.warn = function(){
try {
var first = arguments[0];
if(typeof first==='string' && patterns.some(p => first.toLowerCase().includes(String(p).toLowerCase()))){
return;
}
}catch(e){}
return originalWarn.apply(console, arguments);
};
} catch(e) {}
})();
JS;
echo "<script>{$js}</script>";
}
/**
* ============================================================
* 3 MUTO TOTALE → tutti i metodi console (opzionale)
* ============================================================
*/
function cc_console_mute_all()
{
if (defined('WP_DEBUG') && WP_DEBUG)
return;
$methods = ['log', 'warn', 'error', 'debug', 'info', 'trace', 'table', 'group', 'groupCollapsed', 'groupEnd', 'dir'];
$json_methods = wp_json_encode($methods ?: []);
$js = <<<JS
(function(){
try{
var noop=function(){};
var methods={$json_methods};
function patch(win){
if(!win?.console) return;
methods.forEach(m=>{try{win.console[m]=noop;}catch(e){}});
}
patch(window);
document.querySelectorAll('iframe').forEach(f=>{
try{patch(f.contentWindow);}catch(e){}
f.addEventListener('load',ev=>{try{patch(ev.target.contentWindow);}catch(e){}});
});
new MutationObserver(m=>m.forEach(rec=>rec.addedNodes.forEach(n=>{
if(n.tagName?.toLowerCase()==='iframe'){try{patch(n.contentWindow);}catch(e){}}
n.querySelectorAll?.('iframe').forEach(f=>{try{patch(f.contentWindow);}catch(e){}});
}))).observe(document.documentElement,{childList:true,subtree:true});
}catch(e){}
})();
JS;
echo "<script>{$js}</script>";
}
/**
* ============================================================
* 4 HOOKS ATTIVI
* ============================================================
*/
// Default: silenzia solo Google Maps
add_action('wp_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX);
add_action('admin_footer', 'cc_console_mute_gmaps_warnings', PHP_INT_MAX);
// Per silenziare TUTTO, decommenta:
// add_action('wp_footer', 'cc_console_mute_all', PHP_INT_MAX);
// add_action('admin_footer', 'cc_console_mute_all', PHP_INT_MAX);