image_renderer = $image_renderer; } public function register_hooks() { add_filter( 'elementor/widget/render_content', array( $this, 'filter_widget_content' ), 20, 2 ); add_action( 'aiparently_classification_changed', array( $this, 'classification_changed' ), 20, 2 ); add_action( 'update_option_aiparently_settings', array( $this, 'clear_document_cache' ), 20, 2 ); add_action( 'init', array( $this, 'maybe_clear_cache_after_upgrade' ), 20 ); } public function filter_widget_content( $widget_content, $widget ) { if ( ! is_object( $widget ) || ! method_exists( $widget, 'get_settings_for_display' ) ) { return $widget_content; } $settings = $widget->get_settings_for_display(); $attachment_id = isset( $settings['image']['id'] ) ? absint( $settings['image']['id'] ) : 0; return $this->image_renderer->render_detected_images( $widget_content, $attachment_id ); } public function classification_changed( $post_id, $status ) { if ( 'attachment' === get_post_type( $post_id ) ) { $this->clear_document_cache(); } } /** * Elementor stores rendered widget HTML in a document cache. A media-meta * change does not invalidate that cache, so remove only the rendered HTML * cache and leave generated CSS/assets untouched. */ public function clear_document_cache( $old_value = null, $new_value = null ) { if ( class_exists( '\\Elementor\\Core\\Base\\Document' ) ) { delete_post_meta_by_key( \Elementor\Core\Base\Document::CACHE_META_KEY ); } } public function maybe_clear_cache_after_upgrade() { if ( AIPARENTLY_VERSION === get_option( 'aiparently_version' ) ) { return; } $this->clear_document_cache(); update_option( 'aiparently_version', AIPARENTLY_VERSION, false ); } }