Skip to content

Instantly share code, notes, and snippets.

@swinggraphics
Last active June 15, 2022 21:56
Show Gist options
  • Save swinggraphics/246564022eb66ecf12da4ab8ef1731c1 to your computer and use it in GitHub Desktop.
Save swinggraphics/246564022eb66ecf12da4ab8ef1731c1 to your computer and use it in GitHub Desktop.
Copy compiled Oxygen page content to post_content for search
/* Copy compiled Oxygen page content to post_content for search */
function sg_save_rendered_page_content( $meta_id, $object_id, $meta_key, $meta_value ) {
// Debugging
// $debug .= preg_replace( '#(\[/[^\]]+?\])#', "$1\n", $meta_value );
// file_put_contents( plugin_dir_path( __FILE__ ) . 'debug.html', $debug );
if ( 'ct_builder_shortcodes' != $meta_key ) return;
// Don't try to render anything with inner content
if ( false !== strpos( $meta_value, '[ct_inner_content' ) ) return;
if ( preg_match( '#\[oxygen [^\]]+ data=[\'"]content[\'"]#', $meta_value ) ) return $return;
// Only run on approved post types
$post = get_post( $object_id );
$allowed = apply_filters( 'sg_copy_oxy_content_post_types', [ 'page' ] );
if ( ! in_array( $post->post_type, (array) $allowed ) ) return;
// Remove reusable parts, easy posts, etc
$content = $meta_value;
$remove_tags = [ 'ct_link_button', 'ct_image', 'ct_video', 'ct_fancy_icon', 'ct_code_block', 'oxy_header', 'oxy_social_icons', 'oxy_progress_bar', 'oxy_posts_grid', 'oxy_gallery', 'oxy_dynamic_list', 'oxy_superbox', 'ct_modal', 'oxy_map', 'oxy_soundcloud', 'oxy-pro-menu', 'oxy_nav_menu', 'ct_shortcode', 'ct_nestable_shortcode', 'oxy_comments', 'oxy_comment_form', 'oxy_login_form', 'oxy_search_form', 'ct_widget', 'ct_sidebar', 'ct_reusable' ];
foreach ( $remove_tags as $tag ) {
$content = preg_replace( '#\[' . $tag . ' .+?\[/' . $tag. '\]#', '', $content );
}
// Render content, remove CSS and JS
$content = do_shortcode( $content );
$content = preg_replace( '#<link.+?>#s', '', $content );
$content = preg_replace( '#<script.+?</script>#s', '', $content );
// Wrap in html block with notice
$_post_content = "<!-- wp:html -->\n<!-- Edit with Oxygen -->\n<!-- /wp:html -->\n\n";
$_post_content .= "<!-- wp:html -->\n" . $content . "\n<!-- /wp:html -->";
// Save it to post_content
$_post = [
'ID' => $post->ID,
'post_content' => $_post_content
];
wp_update_post( $_post );
}
add_action( 'updated_postmeta', 'sg_save_rendered_page_content', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment