Skip to content
Open

Release #1024

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/stubs/elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,35 @@ public function get_settings(): array {
public function get_settings_for_display(): array {
return $this->settings;
}

/**
* @return array{settings: array<string, mixed>}
*/
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 );
}
}
}
}
31 changes: 31 additions & 0 deletions tests/test-content-forms-markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
30 changes: 30 additions & 0 deletions tests/test-elementor-posts-grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> $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' ) ) );
}
}
Loading