From 7a29c40ad4e708f3cda63f4088ba8114a77c04bb Mon Sep 17 00:00:00 2001 From: Girish Panchal <79647963+girishpanchal30@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:31:50 +0530 Subject: [PATCH] Ensured dynamic rendering of forms (#1023) * fix: ensure forms are rendered dynamically * fix: respect per-widget form cache settings * fix: disable caching for paginated post grids --- .../elementor/elementor_widget_base.php | 17 +++++++--- .../widgets/elementor/posts-grid.php | 14 +++++++++ tests/stubs/elementor.php | 29 +++++++++++++++++ tests/test-content-forms-markup.php | 31 +++++++++++++++++++ tests/test-elementor-posts-grid.php | 30 ++++++++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) diff --git a/obfx_modules/content-forms/includes/widgets-admin/elementor/elementor_widget_base.php b/obfx_modules/content-forms/includes/widgets-admin/elementor/elementor_widget_base.php index e3232f66..c3242437 100644 --- a/obfx_modules/content-forms/includes/widgets-admin/elementor/elementor_widget_base.php +++ b/obfx_modules/content-forms/includes/widgets-admin/elementor/elementor_widget_base.php @@ -44,13 +44,22 @@ public function get_icon() { return 'eicon-text-align-left'; } - /** - * Is dynamic content. + /** + * Keep forms out of Elementor's element cache: the markup carries a per-visitor nonce. * - * @return bool + * @return true */ protected function is_dynamic_content():bool { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.boolFound - return false; + return true; + } + + /** + * Ignore the per-widget "Cache Settings", which Elementor checks before is_dynamic_content(). + * + * @return bool True while Elementor builds its element cache. + */ + protected function should_render_shortcode() { + return (bool) apply_filters( 'elementor/element/should_render_shortcode', false ); } /** diff --git a/obfx_modules/elementor-extra-widgets/widgets/elementor/posts-grid.php b/obfx_modules/elementor-extra-widgets/widgets/elementor/posts-grid.php index a306e8e0..892bc1cb 100644 --- a/obfx_modules/elementor-extra-widgets/widgets/elementor/posts-grid.php +++ b/obfx_modules/elementor-extra-widgets/widgets/elementor/posts-grid.php @@ -86,6 +86,20 @@ public function get_categories() { return array( $slug ); } + /** + * A paginated grid depends on the requested page, so it ignores the per-widget "Cache Settings". + * + * @return bool + */ + protected function should_render_shortcode() { + $settings = $this->get_settings(); + if ( empty( $settings['grid_pagination'] ) ) { + return parent::should_render_shortcode(); + } + + return (bool) apply_filters( 'elementor/element/should_render_shortcode', false ); + } + /** * Get post types. */ diff --git a/tests/stubs/elementor.php b/tests/stubs/elementor.php index d1a3055e..59561d0a 100644 --- a/tests/stubs/elementor.php +++ b/tests/stubs/elementor.php @@ -247,6 +247,35 @@ public function get_settings(): array { public function get_settings_for_display(): array { return $this->settings; } + + /** + * @return array{settings: array} + */ + public function get_raw_data(): array { + return array( 'settings' => $this->settings ); + } + + protected function is_dynamic_content(): bool { + return true; + } + + /** + * Mirrors Elementor 4.3's element cache decision, without the dynamic tag check. + * + * @return bool + */ + protected function should_render_shortcode() { + if ( ! apply_filters( 'elementor/element/should_render_shortcode', false ) ) { + return false; + } + + $raw_data = $this->get_raw_data(); + if ( ! empty( $raw_data['settings']['_element_cache'] ) ) { + return 'yes' === $raw_data['settings']['_element_cache']; + } + + return $this->is_dynamic_content() || (bool) apply_filters( 'elementor/element/is_dynamic_content', false, $raw_data, $this ); + } } } } diff --git a/tests/test-content-forms-markup.php b/tests/test-content-forms-markup.php index 3b7f4320..9fba30f0 100644 --- a/tests/test-content-forms-markup.php +++ b/tests/test-content-forms-markup.php @@ -345,4 +345,35 @@ public function test_beaver_submit_fieldset_keeps_its_classes(): void { $this->render_beaver_template( array( 'submit_display' => 'block' ) ) ); } + + /** + * The form nonce is per visitor, so Elementor must render the form on every request. + */ + public function test_elementor_form_is_rendered_per_request(): void { + $this->require_elementor_double(); + + $is_dynamic = new ReflectionMethod( Orbit_Fox_Elementor_Form_Double::class, 'is_dynamic_content' ); + $is_dynamic->setAccessible( true ); + + $this->assertTrue( $is_dynamic->invoke( $this->elementor_form( array() ) ) ); + } + + /** + * A widget's own "Cache Settings: Active" must not cache the form either. + */ + public function test_elementor_form_ignores_per_widget_cache_setting(): void { + $this->require_elementor_double(); + + $should_render_shortcode = new ReflectionMethod( Orbit_Fox_Elementor_Form_Double::class, 'should_render_shortcode' ); + $should_render_shortcode->setAccessible( true ); + $form = $this->elementor_form( array( '_element_cache' => 'no' ) ); + + $this->assertFalse( $should_render_shortcode->invoke( $form ) ); + + add_filter( 'elementor/element/should_render_shortcode', '__return_true' ); + $while_caching = $should_render_shortcode->invoke( $form ); + remove_filter( 'elementor/element/should_render_shortcode', '__return_true' ); + + $this->assertTrue( $while_caching ); + } } diff --git a/tests/test-elementor-posts-grid.php b/tests/test-elementor-posts-grid.php index ae1e2c5a..0a3f1adc 100644 --- a/tests/test-elementor-posts-grid.php +++ b/tests/test-elementor-posts-grid.php @@ -82,4 +82,34 @@ public function test_issue_2104_title_cannot_break_out_of_link_title_attribute() $this->assertFalse( $link->hasAttribute( 'autofocus' ) ); $this->assertSame( '" onfocus="alert(1)" autofocus="', $link->getAttribute( 'title' ) ); } + + /** + * @param array $settings Widget settings. + */ + protected function renders_per_request_while_caching( array $settings ): bool { + $should_render_shortcode = new ReflectionMethod( Posts_Grid::class, 'should_render_shortcode' ); + $should_render_shortcode->setAccessible( true ); + + add_filter( 'elementor/element/should_render_shortcode', '__return_true' ); + $per_request = $should_render_shortcode->invoke( new Posts_Grid( $settings ) ); + remove_filter( 'elementor/element/should_render_shortcode', '__return_true' ); + + return $per_request; + } + + public function test_paginated_grid_ignores_per_widget_cache_setting(): void { + if ( ! ORBIT_FOX_ELEMENTOR_DOUBLE_ACTIVE ) { + $this->markTestSkipped( 'The real Elementor plugin is loaded; the test double is not in use.' ); + } + + $this->assertTrue( + $this->renders_per_request_while_caching( + array( + 'grid_pagination' => 'yes', + '_element_cache' => 'no', + ) + ) + ); + $this->assertFalse( $this->renders_per_request_while_caching( array( '_element_cache' => 'no' ) ) ); + } }