Created
October 9, 2025 05:04
-
-
Save mlbd/b1f89270595667b63b3bff3d2b7c0b67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Regenerate Elementor CSS for a specific post (3.31.x ↔ 3.32.4 compatible) | |
| */ | |
| 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(); | |
| // Get the document object safely (newer, supported path) | |
| $document = null; | |
| if ( isset( $plugin->documents ) && method_exists( $plugin->documents, 'get' ) ) { | |
| $document = $plugin->documents->get( $post_id ); | |
| } | |
| // If we have a document, prefer its API | |
| if ( $document && method_exists( $document, 'is_built_with_elementor' ) && $document->is_built_with_elementor() ) { | |
| // 1) Preferred in modern Elementor: ask the document for its CSS file | |
| if ( method_exists( $document, 'get_css_file' ) ) { | |
| $css_file = $document->get_css_file(); | |
| if ( $css_file && method_exists( $css_file, 'update' ) ) { | |
| $css_file->update(); // Rebuild per-post CSS for this document | |
| return; | |
| } | |
| } | |
| // 2) Fallback for older installs: use the legacy Post CSS class | |
| if ( class_exists( '\Elementor\Core\Files\CSS\Post' ) ) { | |
| $css = new \Elementor\Core\Files\CSS\Post( $post_id ); | |
| if ( method_exists( $css, 'update' ) ) { | |
| $css->update(); | |
| return; | |
| } | |
| } | |
| } | |
| // If we couldn’t confirm it’s an Elementor document (or APIs missing), do nothing | |
| }; | |
| did_action( 'elementor/loaded' ) ? $run() : add_action( 'elementor/loaded', $run, 99 ); | |
| } | |
| /** | |
| * 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(); | |
| // 3.31+ and 3.32.x: Files manager clear (safe no-op if missing) | |
| if ( isset( $plugin->files_manager ) && method_exists( $plugin->files_manager, 'clear_cache' ) ) { | |
| $plugin->files_manager->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 = (int) get_post_meta( $post_id, '_oasis_original', true ); | |
| if ( $original_post_id ) { | |
| ml_el_regenerate_post_css( $original_post_id ); | |
| } else { | |
| // If we don't know the original, fall back to a global clear so nothing is stale | |
| ml_el_clear_global_css_js_cache(); | |
| } | |
| }, 11, 1 ); | |
| /** Approach B: global cache clear on OWF complete (toggle if you prefer) */ | |
| // 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