// DIAG: show CTA section content
add_action('admin_init', function() {
if (!isset($_GET['cfu_diag_why'])) return;
global $wpdb;
$row = $wpdb->get_row("SELECT post_content FROM {$wpdb->posts} WHERE ID=7");
$c = $row->post_content;
$idx = strpos($c, '30% Off');
if ($idx === false) { $idx = strpos($c, 'Get 30'); }
header('Content-Type: text/plain');
if ($idx === false) { echo 'NOT FOUND, checking Ready to Book: ' . (strpos($c, 'Ready') !== false ? 'yes' : 'no'); }
else { echo substr($c, max(0,$idx-100), 1000); }
die();
});
// Fix v3, v4, v5 - skip if already applied
add_action('admin_init', function() {
if (!get_option('cfu_fixed_homepage_compat_v3')) update_option('cfu_fixed_homepage_compat_v3', 'skip');
if (!get_option('cfu_fixed_homepage_compat_v4')) update_option('cfu_fixed_homepage_compat_v4', 'skip');
if (!get_option('cfu_fixed_homepage_compat_v5')) update_option('cfu_fixed_homepage_compat_v5', 'skip');
});
// Fix v6: Replace wp:shortcode block with wp:html static HTML
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v6')) return;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v6', 'no_post'); return; }
$content = $post->post_content;
$sc_block = '[cfu_recent_posts]';
if (strpos($content, $sc_block) === false) { update_option('cfu_fixed_homepage_compat_v6', 'not_found'); return; }
$posts = get_posts(['post_type'=>'post','posts_per_page'=>2,'post_status'=>'publish','ignore_sticky_posts'=>true]);
if (empty($posts)) { update_option('cfu_fixed_homepage_compat_v6', 'no_posts'); return; }
$html = '
';
foreach ($posts as $p) {
$permalink=get_permalink($p); $title=esc_html(get_the_title($p)); $date=get_the_date('F j, Y',$p);
$excerpt=wp_trim_words(get_the_excerpt($p)?:wp_strip_all_tags($p->post_content),20,'...');
$thumb_id=get_post_thumbnail_id($p);
$html.='
';
if ($thumb_id){$img_url=wp_get_attachment_image_url($thumb_id,'medium_large');if($img_url)$html.='
.')
';}
$html.='
'.esc_html($date).'
';
$html.='
'.esc_html($excerpt).'
Read More »';
}
$html.='
';
$start=strpos($content,$sc_block);
$new_content=substr($content,0,$start).''.$html.''.substr($content,$start+strlen($sc_block));
wp_update_post(['ID'=>7,'post_content'=>$new_content]);
update_option('cfu_fixed_homepage_compat_v6','done_'.date('Y-m-d'));
});
// Fix v7b: Direct DB replace of u003c/u003e unicode sequences in Divi block JSON
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v7b')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v7b', 'no_post'); return; }
$content = $post->post_content;
$count = substr_count($content, 'u003c');
if ($count === 0) { update_option('cfu_fixed_homepage_compat_v7b', 'no_u003c'); return; }
$new_content = str_replace(
['u003c', 'u003e', 'u0026'],
['<', '>', '&'],
$content
);
$result = $wpdb->update(
$wpdb->posts,
['post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)],
['ID' => 7],
['%s', '%s', '%s'],
['%d']
);
clean_post_cache(7);
$status = ($result !== false) ? 'done_count_' . $count : 'db_error';
update_option('cfu_fixed_homepage_compat_v7b', $status . '_' . date('Y-m-d'));
});
// Fix v7c: Replace orphaned n chars between HTML tags (stripped newlines)
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v7c')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v7c', 'no_post'); return; }
$content = $post->post_content;
$count = substr_count($content, '>n<');
if ($count === 0) { update_option('cfu_fixed_homepage_compat_v7c', 'no_n_found'); return; }
$new_content = str_replace('>n<', '><', $content);
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
$status = ($result !== false) ? 'done_count_' . $count : 'db_error';
update_option('cfu_fixed_homepage_compat_v7c', $status . '_' . date('Y-m-d'));
});
// Fix v7d: Fix u0022 (double-quote) and orphaned n with whitespace/start-of-value
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v7d')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v7d', 'no_post'); return; }
$content = $post->post_content;
$changes = array();
// Fix u0022 -> double quote
$count_u0022 = substr_count($content, 'u0022');
if ($count_u0022 > 0) { $content = str_replace('u0022', '"', $content); $changes[] = 'u0022:'.$count_u0022; }
// Fix n with whitespace between tags: >n + spaces + < -> ><
$new = preg_replace('/>n\s+', '><', $content);
if ($new !== $content) { $count_ns = preg_match_all('/>n\s+', $content); $changes[] = 'n_ws:'.$count_ns; $content = $new; }
// Fix n at start of value strings: "value":"n< -> "value":"<
$count_vs = substr_count($content, '"value":"n<');
if ($count_vs > 0) { $content = str_replace('"value":"n<', '"value":"<', $content); $changes[] = 'val_n:'.$count_vs; }
if (empty($changes)) { update_option('cfu_fixed_homepage_compat_v7d', 'no_changes'); return; }
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
$status = ($result !== false) ? 'done_'.implode('_',$changes) : 'db_error';
update_option('cfu_fixed_homepage_compat_v7d', $status . '_' . date('Y-m-d'));
});
// Fix v7e: v7d broke Divi JSON by replacing u0022->" (unescaped in JSON strings)
// Fix: within each Divi block, replace attr="value" -> attr=\"value\"
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v7e')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v7e', 'no_post'); return; }
$content = $post->post_content;
$bq = chr(92) . chr(34);
$new_content = preg_replace_callback(
'//',
function($m) use ($bq) {
return str_replace(chr(61).chr(34), chr(61).$bq, $m[0]);
},
$content
);
if ($new_content === null || $new_content === $content) {
update_option('cfu_fixed_homepage_compat_v7e', 'no_change:' . preg_last_error());
return;
}
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
$status = ($result !== false) ? 'done_' . date('Y-m-d') : 'db_error';
update_option('cfu_fixed_homepage_compat_v7e', $status);
});
// Fix v7f: fix closing unescaped quotes in Divi block JSON (v7e half-fix)
add_action('admin_init', function() {
if (get_option('cfu_fixed_homepage_compat_v7f')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fixed_homepage_compat_v7f', 'no_post'); return; }
$content = $post->post_content;
$bq = chr(92) . chr(34);
$pat = '/' . preg_quote($bq, '/') . '([^' . chr(34) . ']*)' . chr(34) . '/';
$new_content = preg_replace_callback(
'//',
function($m) use ($bq, $pat) {
return preg_replace_callback(
$pat,
function($a) use ($bq) { return $bq . $a[1] . $bq; },
$m[0]
);
},
$content
);
if ($new_content === null || $new_content === $content) {
update_option('cfu_fixed_homepage_compat_v7f', 'no_change:' . preg_last_error());
return;
}
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
$status = ($result !== false) ? 'done_' . date('Y-m-d') : 'db_error';
update_option('cfu_fixed_homepage_compat_v7f', $status);
});
// DIAG: output post content around Why Cabling Matters
add_action('admin_init', function() {
if (!isset($_GET['cfu_diag_why'])) return;
global $wpdb;
$row = $wpdb->get_row("SELECT post_content FROM {$wpdb->posts} WHERE ID=7");
if (!$row) { echo 'NO POST'; die(); }
$c = $row->post_content;
$idx = strpos($c, 'Why Cabling');
if ($idx === false) { echo 'NOT FOUND, len='.strlen($c); die(); }
// Output wider context
$start = max(0, $idx-1500);
header('Content-Type: text/plain');
echo substr($c, $start, 8000);
die();
});
// Remove 'Why Cabling Matters' section from homepage
add_action('admin_init', function() {
if (get_option('cfu_removed_why_cabling')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_removed_why_cabling', 'no_post'); return; }
$content = $post->post_content;
$new_content = preg_replace_callback(
'//U',
function($m) {
if (strpos($m[0], 'Why Cabling') !== false) return '';
return $m[0];
},
$content
);
if ($new_content === null || $new_content === $content) {
update_option('cfu_removed_why_cabling', 'no_change:' . preg_last_error());
return;
}
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
update_option('cfu_removed_why_cabling', ($result !== false) ? 'done_' . date('Y-m-d') : 'db_error');
});
// v8: restore from revision then correctly remove Why Cabling Matters
add_action('admin_init', function() {
if (get_option('cfu_restore_fix_v8')) return;
global $wpdb;
// Find most recent revision of post 7 that has Why Cabling AND What We Install
$rev = $wpdb->get_row(
"SELECT ID, post_content FROM {$wpdb->posts}
WHERE post_parent = 7 AND post_type = 'revision'
AND post_content LIKE '%Why Cabling%'
AND post_content LIKE '%What We Install%'
ORDER BY ID DESC LIMIT 1"
);
if (!$rev) {
update_option('cfu_restore_fix_v8', 'no_good_revision');
return;
}
$content = $rev->post_content;
// Correctly remove Why Cabling section (NO /U flag, non-greedy [sS]*?)
$new_content = preg_replace_callback(
'//',
function($m) {
return (strpos($m[0], 'Why Cabling') !== false) ? '' : $m[0];
},
$content
);
if ($new_content === null || $new_content === $content) {
update_option('cfu_restore_fix_v8', 'regex_err_or_no_match:' . preg_last_error() . '_rev' . $rev->ID);
return;
}
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $new_content, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
update_option('cfu_restore_fix_v8', ($result !== false) ? 'done_rev' . $rev->ID . '_' . date('Y-m-d') : 'db_error');
});
// v9: comprehensive single-pass fix on restored revision content
add_action('admin_init', function() {
if (get_option('cfu_fix_v9_comprehensive')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fix_v9_comprehensive', 'no_post'); return; }
$c = $post->post_content;
$changes = array();
// 1. u003c -> <
$n = substr_count($c, 'u003c');
if ($n > 0) { $c = str_replace('u003c', '<', $c); $changes[] = 'lt:'.$n; }
// 2. u003e -> >
$n = substr_count($c, 'u003e');
if ($n > 0) { $c = str_replace('u003e', '>', $c); $changes[] = 'gt:'.$n; }
// 3. u0026 -> &
$n = substr_count($c, 'u0026');
if ($n > 0) { $c = str_replace('u0026', '&', $c); $changes[] = 'amp:'.$n; }
// 4. u0022 -> \" (JSON-escaped quote, using chr() to avoid PHP escaping confusion)
$bq = chr(92) . chr(34);
$n = substr_count($c, 'u0022');
if ($n > 0) { $c = str_replace('u0022', $bq, $c); $changes[] = 'quot:'.$n; }
// 5. >n< orphaned newlines between tags
$n = substr_count($c, '>n<');
if ($n > 0) { $c = str_replace('>n<', '><', $c); $changes[] = 'ntag:'.$n; }
// 6. value starting with n< (orphaned newline at start of block value)
$n = substr_count($c, chr(34).'value'.chr(34).':'.chr(34).'n<');
if ($n > 0) { $c = str_replace(chr(34).'value'.chr(34).':'.chr(34).'n<', chr(34).'value'.chr(34).':'.chr(34).'<', $c); $changes[] = 'valn:'.$n; }
if (empty($changes)) { update_option('cfu_fix_v9_comprehensive', 'no_changes'); return; }
$result = $wpdb->update(
$wpdb->posts,
array('post_content' => $c, 'post_modified' => current_time('mysql'), 'post_modified_gmt' => current_time('mysql', 1)),
array('ID' => 7), array('%s', '%s', '%s'), array('%d')
);
clean_post_cache(7);
update_option('cfu_fix_v9_comprehensive', ($result !== false) ? 'done_'.implode('_', $changes).'_'.date('Y-m-d') : 'db_error');
});
// v10: fix spurious n prefix in HTML tag content (\n -> n corruption)
add_action('admin_init', function() {
if (get_option('cfu_fix_v10_n_prefix')) return;
global $wpdb;
$post = get_post(7);
if (!$post) { update_option('cfu_fix_v10_n_prefix', 'no_post'); return; }
$c = $post->post_content;
$changes = array();
// Fix >n[A-Z] - spurious n at start of tag content followed by uppercase (clearly corrupted)
$before = $c;
$c = preg_replace('/>n([A-Z])/', '>$1', $c);
$count = preg_match_all('/>n([A-Z])/', $before);
if ($c !== $before) $changes[] = 'nUpper:'.($count ?: 'yes');
// Fix >n[a-z] where followed by common words (safer: just do all >n followed by letter)
$before2 = $c;
// Also fix n followed by space then content (e.g. n