Hi @charlie,
Thanks to your help yesterday, I think I have now achieved what I was looking for, having altered your code with AI help. I consequently now have clean pure HTML, stripped of classes and styles. Here is the amended code:
add_filter("cs_save_document", function($doc) {
try {
ob_start();
// Needed For styling
do_action("wp_enqueue_scripts");
// Find document and renderContentFromDocument
$resolver = cornerstone("Resolver");
$docDB = $resolver->getDocument( $doc->id() );
$html = cornerstone("Resolver")->renderContentFromDocument($docDB);
$html .= ob_get_clean();
// Attributes to remove
$attributes = [
'class', 'style', 'data-x-effect', 'data-x-slide-container', 'loading', 'width', 'height',
'aria-hidden', 'tabindex', 'data-x-effect-provider', 'data-x-slide-context', 'data-x-slide',
'aria-expanded', 'id', 'role', 'aria-selected', 'aria-controls', 'data-x-toggle', 'data-x-toggleable',
'data-x-toggle-collapse', 'aria-labelledby'
];
// Remove attributes
foreach ($attributes as $attribute) {
$html = preg_replace('/\s*' . $attribute . '\s*=\s*".*?"/i', '', $html);
}
// Remove content within certain tags
$tags = ['style', 'iframe', 'script', 'i', 'blockquote'];
foreach ($tags as $tag) {
$html = preg_replace('/<' . $tag . '\b[^>]*>.*?<\/' . $tag . '>/is', '', $html);
}
// Remove span tags
$html = preg_replace('~</?span[^>]*>~i', '', $html);
// Convert special characters to ASCII
$html = iconv('UTF-8', 'ASCII//IGNORE', $html);
// Remove all div tags
$html = preg_replace('~</?div[^>]*>~i', '', $html);
// Update post meta with HTML
update_post_meta($doc->id(), '_cs_built_html', $html);
} catch(\Throwable $e) {
trigger_error("Error with document HTML post meta saving : " . $e->getMessage());
}
});
In addition, I have a small plugin to bulk generate the HTML into the meta data. It is a bit rough and ready, but does the trick though! The plugin code is uploaded to a new folder with the same name as the plugin file in wp-content/plugins and is then activated as a normal plugin. Below is the plugin code:
<?PHP
/**
* Plugin Name: Cornerstone Bulk HTML Generator
* Description: This is a custom plugin to update all posts and generate HTML for Cornerstone.
* Version: 1.0
* Author: White Media
* Author URI: https://www.whitemedia.uk/
*/
add_action('admin_menu', 'cornerstone_bulk_html_generator_menu');
function cornerstone_bulk_html_generator_menu() {
add_menu_page(
'Cornerstone Bulk HTML Generator',
'Bulk HTML Generator',
'manage_options',
'cornerstone-bulk-html-generator',
'cornerstone_bulk_html_generator_page',
'',
6
);
}
function cornerstone_bulk_html_generator_page() {
// Get all public post types
$post_types = get_post_types(array('public' => true));
$continue = get_option('cornerstone_bulk_html_generator_continue', false);
$button_color = $continue ? '#0073aa' : '#ddd';
$button_disabled = $continue ? '' : 'disabled';
?>
<div class="wrap">
<h1>Cornerstone Bulk HTML Generator</h1>
<form method="post" action="">
<select name="post_type">
<option value="">All Post Types</option>
<?PHP
foreach ($post_types as $post_type) {
echo '<option value="' . $post_type . '">' . $post_type . '</option>';
}
?>
</select>
<input type="submit" name="update_posts" value="Update Posts" class="button button-primary" />
</form>
<form method="post" action="" style="margin-top: 20px;">
<input type="submit" name="continue_posts" value="Continue" class="button button-primary" style="background-color: <?php echo $button_color; ?>" <?php echo $button_disabled; ?> />
</form>
</div>
<?PHP
if (isset($_POST['update_posts'])) {
update_option('cornerstone_bulk_html_generator_post_type', $_POST['post_type']);
cornerstone_bulk_html_generator_update_all_posts($_POST['post_type']);
}
if (isset($_POST['continue_posts']) && $continue) { cornerstone_bulk_html_generator_update_all_posts(get_option('cornerstone_bulk_html_generator_post_type', ''));
}
}
function cornerstone_bulk_html_generator_update_all_posts($post_type = '') {
$batch_size = 50;
$offset = (int) get_option('cornerstone_bulk_html_generator_offset', 0);
$args = array(
'numberposts' => -1,
'offset' => $offset,
'post_type' => $post_type ? $post_type : get_post_types(array('public' => true)),
);
$total_posts = count(get_posts($args));
$args['numberposts'] = $batch_size;
$posts = get_posts($args);
if (empty($posts)) {
echo '<div class="updated"><p>All posts updated successfully.</p></div>';
update_option('cornerstone_bulk_html_generator_offset', 0);
update_option('cornerstone_bulk_html_generator_continue', false);
return;
}
foreach($posts as $post) {
try {
ob_start();
do_action("wp_enqueue_scripts");
$resolver = cornerstone("Resolver");
$docDB = $resolver->getDocument($post->ID);
$html = cornerstone("Resolver")->renderContentFromDocument($docDB);
$html .= ob_get_clean();
// Attributes to remove
$attributes = [
'class', 'style', 'data-x-effect', 'data-x-slide-container', 'loading', 'width', 'height',
'aria-hidden', 'tabindex', 'data-x-effect-provider', 'data-x-slide-context', 'data-x-slide',
'aria-expanded', 'id', 'role', 'aria-selected', 'aria-controls', 'data-x-toggle', 'data-x-toggleable',
'data-x-toggle-collapse', 'aria-labelledby'
];
// Remove attributes
foreach ($attributes as $attribute) {
$html = preg_replace('/\s*' . $attribute . '\s*=\s*".*?"/i', '', $html);
}
// Remove content within certain tags
$tags = ['style', 'iframe', 'script', 'i', 'blockquote'];
foreach ($tags as $tag) {
$html = preg_replace('/<' . $tag . '\b[^>]*>.*?<\/' . $tag . '>/is', '', $html);
}
// Remove span tags
$html = preg_replace('~</?span[^>]*>~i', '', $html);
// Convert special characters to ASCII
$html = iconv('UTF-8', 'ASCII//IGNORE', $html);
// Remove all div tags
$html = preg_replace('~</?div[^>]*>~i', '', $html);
update_post_meta($post->ID, '_cs_built_html', $html);
} catch(\Throwable $e) {
trigger_error("Error with document HTML post meta saving : " . $e->getMessage());
}
}
update_option('cornerstone_bulk_html_generator_offset', $offset + $batch_size);
update_option('cornerstone_bulk_html_generator_continue', true);
$progress = min(100, (($offset + $batch_size) / $total_posts) * 100);
echo '<div class="updated"><p>Updated ' . count($posts) . ' posts. </p></div>';
echo '<div style="width: 100%; background: #f5f5f5; border: 1px solid #ddd; margin: 20px 0; height: 20px; display: flex; align-items: center;"><div style="height: 100%; background: #0073aa; width:' . $progress . '%;"></div></div>';
}
?>
Please let me know if this works for you as well.
Many thanks again,
Christopher