editor ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) { return true; } if ( isset( \Elementor\Plugin::$instance->preview ) && \Elementor\Plugin::$instance->preview->is_preview_mode() ) { return true; } } return false; } /** * Filter whether the request should be optimized. * * @param bool $should_optimize Initial value. * @return bool */ public function filter_should_optimize( $should_optimize ) { if ( self::is_elementor_editor_or_preview() ) { return false; } return $should_optimize; } /** * Filter whether page should be cached. * * @param bool $should_cache Initial value. * @return bool */ public function filter_should_cache( $should_cache ) { if ( self::is_elementor_editor_or_preview() ) { return false; } return $should_cache; } /** * When Elementor regenerates CSS, purge plugin CSS and page caches. */ public function on_elementor_clear_cache() { wpsco_increment_cache_version(); $plugin = WPSCO_Plugin::get_instance(); $purger = $plugin ? $plugin->get_module( 'cache_purger' ) : null; if ( $purger ) { $purger->purge_css_cache(); $purger->purge_html_cache(); } wpsco_log( 'Elementor CSS cleared: invalidated plugin CSS & HTML cache.' ); } /** * When a post or template is saved in Elementor editor, purge cache across the site. * * @param int|object $post_or_doc Post ID or Elementor Document object. */ public function on_elementor_save( $post_or_doc ) { $post_id = 0; if ( is_numeric( $post_or_doc ) ) { $post_id = (int) $post_or_doc; } elseif ( is_object( $post_or_doc ) ) { if ( method_exists( $post_or_doc, 'get_main_id' ) ) { $post_id = (int) $post_or_doc->get_main_id(); } elseif ( isset( $post_or_doc->ID ) ) { $post_id = (int) $post_or_doc->ID; } } $plugin = WPSCO_Plugin::get_instance(); $purger = $plugin ? $plugin->get_module( 'cache_purger' ) : null; if ( $purger ) { // In Elementor, templates (headers, footers, popups, loops) and site settings affect all pages. // Purging all HTML & CSS caches ensures edits appear instantly on the live site. $purger->purge_html_cache(); $purger->purge_css_cache(); // Always regenerate Elementor CSS and bust browser cache version self::regenerate_elementor_css(); wpsco_increment_cache_version(); } wpsco_log( sprintf( 'Elementor saved for ID %d: purged all HTML & CSS cache.', $post_id ) ); } /** * Trigger Elementor CSS regeneration officially. * * @return array */ public static function regenerate_elementor_css() { if ( ! wpsco_is_elementor_active() || ! class_exists( '\Elementor\Plugin' ) ) { return array( 'status' => 'not_active', 'message' => __( 'Elementor is not active.', 'speednest' ), ); } try { if ( isset( \Elementor\Plugin::$instance->files_manager ) && method_exists( \Elementor\Plugin::$instance->files_manager, 'clear_cache' ) ) { \Elementor\Plugin::$instance->files_manager->clear_cache(); return array( 'status' => 'success', 'message' => __( 'Elementor CSS files regenerated successfully.', 'speednest' ), ); } } catch ( Exception $e ) { return array( 'status' => 'error', 'message' => $e->getMessage(), ); } return array( 'status' => 'error', 'message' => __( 'Could not access Elementor files manager.', 'speednest' ), ); } }