2026-10-04 GLOBAL
This commit is contained in:
parent
f5d030a6d2
commit
c8a3e68484
@ -4624,6 +4624,7 @@ function dekart_flush_cache_page()
|
||||
*/
|
||||
require_once __DIR__ . '/inc/schema.php';
|
||||
require_once __DIR__ . '/inc/webmaster-recrawl.php';
|
||||
require_once __DIR__ . '/inc/js-snippet-comments.php';
|
||||
|
||||
/**
|
||||
* Фильтр: замена H1 для страницы "Подготовка к школе" (ID 2)
|
||||
|
||||
109
wp-content/themes/dekart/inc/js-snippet-comments.php
Normal file
109
wp-content/themes/dekart/inc/js-snippet-comments.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
// Чистка построчных //-комментариев в inline-JS сниппетах плагина custom-css-js
|
||||
|
||||
if (! defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Версия чистки — меняется, если правила обновятся
|
||||
const DEKART_JS_SNIPPET_MIGRATION = '006';
|
||||
|
||||
// Позиция //-комментария вне строковых литералов или false
|
||||
function dekart_js_comment_pos($line)
|
||||
{
|
||||
$len = strlen($line);
|
||||
$q = '';
|
||||
$esc = false;
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
$ch = $line[$i];
|
||||
if ($q !== '') {
|
||||
if ($esc) {
|
||||
$esc = false;
|
||||
} elseif ($ch === '\\') {
|
||||
$esc = true;
|
||||
} elseif ($ch === $q) {
|
||||
$q = '';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ($ch === '"' || $ch === "'" || $ch === '`') {
|
||||
$q = $ch;
|
||||
continue;
|
||||
}
|
||||
if ($ch === '/' && $i + 1 < $len && $line[$i + 1] === '/') {
|
||||
return $i;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Удаляет //-комментарии (строки и хвосты), переносы строк сохраняет
|
||||
function dekart_js_strip_comments($code)
|
||||
{
|
||||
$parts = preg_split('/(\r\n|\n|\r)/', $code, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$out = '';
|
||||
$n = count($parts);
|
||||
for ($p = 0; $p < $n; $p += 2) {
|
||||
$text = $parts[$p];
|
||||
$eol = isset($parts[$p + 1]) ? $parts[$p + 1] : '';
|
||||
$idx = dekart_js_comment_pos($text);
|
||||
$had = false;
|
||||
if ($idx !== false) {
|
||||
$text = rtrim(substr($text, 0, $idx));
|
||||
$had = true;
|
||||
}
|
||||
if ($had && trim($text) === '') {
|
||||
continue; // строка-комментарий целиком
|
||||
}
|
||||
$out .= $text . $eol;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
// Разовая идемпотентная чистка (флаг в опции) — применяется при деплое темы
|
||||
add_action('init', 'dekart_js_snippet_cleanup', 20);
|
||||
function dekart_js_snippet_cleanup()
|
||||
{
|
||||
if (get_option('dekart_js_snippet_migration') === DEKART_JS_SNIPPET_MIGRATION) {
|
||||
return;
|
||||
}
|
||||
|
||||
$posts = get_posts(array(
|
||||
'post_type' => 'custom-css-js',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'fields' => 'ids',
|
||||
));
|
||||
if (empty($posts)) {
|
||||
return; // сниппетов нет — повторим позже
|
||||
}
|
||||
|
||||
$upload = wp_upload_dir();
|
||||
$dir = trailingslashit($upload['basedir']) . 'custom-css-js';
|
||||
|
||||
foreach ($posts as $pid) {
|
||||
$content = (string) get_post_field('post_content', $pid);
|
||||
if (false === strpos($content, 'open-bitrix-form')) {
|
||||
continue; // чистим только сниппет формы B24
|
||||
}
|
||||
|
||||
$clean = dekart_js_strip_comments($content);
|
||||
if ($clean !== $content) {
|
||||
wp_update_post(array( 'ID' => $pid, 'post_content' => $clean ));
|
||||
}
|
||||
|
||||
foreach (array( $pid . '.js', $pid . '.min.js' ) as $fname) {
|
||||
$file = $dir . '/' . $fname;
|
||||
if (! is_file($file)) {
|
||||
continue;
|
||||
}
|
||||
$raw = (string) file_get_contents($file);
|
||||
$fixed = dekart_js_strip_comments($raw);
|
||||
if ($fixed !== $raw) {
|
||||
file_put_contents($file, $fixed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_option('dekart_js_snippet_migration', DEKART_JS_SNIPPET_MIGRATION, false);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user