You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB
PHP

#!/usr/bin/env php
<?php
/**
* Strip WPBakery shortcodes from content while preserving inner text
*
* Usage: php strip-wpbakery-shortcodes.php < input.sql > output.sql
* Or: php strip-wpbakery-shortcodes.php --test "content with [vc_row]shortcodes[/vc_row]"
*/
function strip_wpbakery_shortcodes(string $content): string {
// List of WPBakery shortcode prefixes to remove
$prefixes = [
'vc_', // Visual Composer / WPBakery
'rev_', // Slider Revolution
'ozy_', // Theme-specific (ozy-diwine-essentials)
];
// Build regex pattern for opening tags: [vc_anything attr="value"]
$pattern_open = '/\[(' . implode('|', $prefixes) . ')[^\]]*\]/i';
// Build regex pattern for closing tags: [/vc_anything]
$pattern_close = '/\[\/(' . implode('|', $prefixes) . ')[^\]]*\]/i';
// Remove opening tags
$content = preg_replace($pattern_open, '', $content);
// Remove closing tags
$content = preg_replace($pattern_close, '', $content);
// Clean up excessive newlines (more than 2 in a row)
$content = preg_replace('/\n{3,}/', "\n\n", $content);
// Clean up excessive spaces
$content = preg_replace('/[ \t]{2,}/', ' ', $content);
// Trim leading/trailing whitespace
$content = trim($content);
return $content;
}
/**
* Process SQL dump, stripping shortcodes from post_content
*/
function process_sql_dump(string $sql): string {
// Match INSERT INTO ... post_content patterns
// This is a simplified approach - for production, consider using proper SQL parsing
return preg_replace_callback(
"/'((?:[^'\\\\]|\\\\.)*?)'/",
function($matches) {
$content = $matches[1];
// Only process if it looks like it contains WPBakery shortcodes
if (preg_match('/\[(vc_|rev_|ozy_)/', $content)) {
$cleaned = strip_wpbakery_shortcodes(stripslashes($content));
return "'" . addslashes($cleaned) . "'";
}
return $matches[0];
},
$sql
);
}
// CLI handling
if (php_sapi_name() === 'cli') {
$args = getopt('', ['test:', 'help', 'sql']);
if (isset($args['help'])) {
echo "Usage:\n";
echo " php strip-wpbakery-shortcodes.php --test \"content\" Test stripping on a string\n";
echo " php strip-wpbakery-shortcodes.php --sql < dump.sql Process SQL dump from stdin\n";
echo " php strip-wpbakery-shortcodes.php < input.txt Process plain text from stdin\n";
exit(0);
}
if (isset($args['test'])) {
echo "Input:\n" . $args['test'] . "\n\n";
echo "Output:\n" . strip_wpbakery_shortcodes($args['test']) . "\n";
exit(0);
}
// Read from stdin
$input = file_get_contents('php://stdin');
if (isset($args['sql'])) {
echo process_sql_dump($input);
} else {
echo strip_wpbakery_shortcodes($input);
}
}