Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/37206c26d322901dfd6270e35a9dd352 to your computer and use it in GitHub Desktop.
Save mlbd/37206c26d322901dfd6270e35a9dd352 to your computer and use it in GitHub Desktop.
/**
* A) Regenerate Elementor CSS for a specific post.
*
* Safe on Elementor 3.31.x. Only runs if the post is built with Elementor.
*/
function ml_el_regenerate_post_css( $post_id ) {
if ( empty( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( ! class_exists( '\Elementor\Plugin' ) ) return;
$run = function() use ( $post_id ) {
$plugin = \Elementor\Plugin::instance();
// Only regenerate if the document is built with Elementor
if ( method_exists( $plugin->documents, 'is_built_with_elementor' )
&& $plugin->documents->is_built_with_elementor( $post_id )
&& class_exists( '\Elementor\Core\Files\CSS\Post' ) ) {
$css = new \Elementor\Core\Files\CSS\Post( $post_id );
if ( method_exists( $css, 'update' ) ) {
$css->update(); // Rebuilds post-{ID}.css
}
}
};
did_action( 'elementor/loaded' ) ? $run() : add_action( 'elementor/loaded', $run, 99 );
}
/**
* B) Clear Elementor's global cached CSS/JS (like Tools → Regenerate Files & Data).
*/
function ml_el_clear_global_css_js_cache() {
if ( ! class_exists( '\Elementor\Plugin' ) ) return;
$run = function() {
$plugin = \Elementor\Plugin::instance();
if ( isset( $plugin->files_manager )
&& method_exists( $plugin->files_manager, 'clear_cache' ) ) {
$plugin->files_manager->clear_cache(); // Triggers elementor/core/files/clear_cache
}
};
did_action( 'elementor/loaded' ) ? $run() : add_action( 'elementor/loaded', $run, 99 );
}
/** Approach A: per-post CSS regenerate on OWF complete */
add_action( 'owf_revision_workflow_complete', function( $post_id ) {
$original_post_id = get_post_meta( $post_id, '_oasis_original', true );
if( ! empty( $original_post_id ) ) {
ml_el_regenerate_post_css( $original_post_id );
} else {
ml_el_clear_global_css_js_cache();
}
}, 11, 1 );
/** Approach B: global cache clear on OWF complete (enable if you prefer global purge) */
// add_action( 'owf_revision_workflow_complete', function( $post_id ) {
// ml_el_clear_global_css_js_cache();
// }, 11, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment