Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that we cache only valid posts/pages urls #7236

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
74 changes: 74 additions & 0 deletions inc/Engine/Cache/PostSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace WP_Rocket\Engine\Cache;

use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Subscriber for the post/page frontend pages.
*/
class PostSubscriber implements Subscriber_Interface {
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'do_rocket_generate_caching_files' => 'disable_cache_on_not_valid_pages',
'rocket_buffer' => [ 'stop_optimizations_for_not_valid_pages', 1 ],
];
}

/**
* Disable caching invalid page urls.
*
* @param bool $can_cache Filter callback passed value.
* @return bool
*/
public function disable_cache_on_not_valid_pages( $can_cache ) {
if ( $this->is_not_valid_page() ) {
return false;
}

return $can_cache;
}

/**
* Stop optimizing those invalid pages by returning empty html string,
* So it fall back to the normal page's HTML.
*
* @param string $html Page's buffer HTML.
* @return string
*/
public function stop_optimizations_for_not_valid_pages( $html ) {
return $this->is_not_valid_page() ? '' : $html;
}

/**
* Check if we are on the post frontend page, but it's not valid url query.
*
* @return bool (True when not valid post url, False if it's a valid one)
*/
private function is_not_valid_page() {
if ( ! is_singular() ) {
return false;
}

$post_id = get_queried_object_id();
if ( empty( $post_id ) ) {
return false;
}

global $wp;

$post_link = get_permalink( $post_id );
if ( ! $post_link ) {
return false;
}

$current_link = home_url( add_query_arg( [], $wp->request ?? '' ) );
if ( is_paged() ) {
$post_link = trailingslashit( $post_link ) . 'page/' . get_query_var( 'paged' );
}

return urldecode( untrailingslashit( $post_link ) ) !== urldecode( untrailingslashit( $current_link ) );
}
}
2 changes: 2 additions & 0 deletions inc/Engine/Cache/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ServiceProvider extends AbstractServiceProvider {
'preload_caches_query',
'cache_config',
'taxonomy_subscriber',
'post_subscriber',
];

/**
Expand Down Expand Up @@ -77,5 +78,6 @@ public function register(): void {
->addArgument( $this->getContainer()->get( 'options' ) )
->addArgument( $this->getContainer()->get( 'options_api' ) );
$this->getContainer()->addShared( 'taxonomy_subscriber', TaxonomySubscriber::class );
$this->getContainer()->addShared( 'post_subscriber', PostSubscriber::class );
}
}
4 changes: 4 additions & 0 deletions inc/Engine/Common/PerformanceHints/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static function get_subscribed_events(): array {
* @return string
*/
public function maybe_apply_optimizations( $html ): string {
if ( empty( $html ) ) {
return $html;
}

if ( ! isset( $_GET['wpr_imagedimensions'] ) && isset( $_GET['wpr_lazyrendercontent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $html;
}
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ private function init_common_subscribers() {
'media_fonts_frontend_subscriber',
'media_fonts_admin_subscriber',
'media_fonts_clean_subscriber',
'post_subscriber',
];

$host_type = HostResolver::get_host_service();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

return [
'test_data' => [
'testIsNotInPostPage' => [
'config' => [
'is_singular' => false,
],
'can_cache' => true,
],
'testValidPostPage' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/test1',
],
'can_cache' => true,
],
'testValidPostPageWithPagination' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/test1/page/2',
'page' => 2,
],
'can_cache' => true,
],
'testEmptyPostId' => [
'config' => [
'is_singular' => true,
'current_post_id' => 0,
'current_post_link' => '',
'current_post_url' => 'http://example.com/test1',
],
'can_cache' => true,
],
'testNotValidPostPage' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_post_url' => 'http://example.com/additional-query/test1',
],
'can_cache' => false,
],
'testNotValidPostPageWithPagination' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/additional-query/test1/page/2',
'page' => 2,
],
'can_cache' => false,
],
'testValidPostPageWithNonLatinCharactersInUrl' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/%D0%BF%D1%80%D0%BE%D0%B4%D1%83%D0%BA%D1%82%D0%BE%D0%B2%D0%B0-%D0%BA%D0%B0%D1%82%D0%B5%D0%B3%D0%BE%D1%80%D0%B8%D1%8F/',
'current_page_url' => 'http://example.com/продуктова-категория/',
],
'can_cache' => true,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\Cache\PostSubscriber;

use Brain\Monkey\Functions;
use WP_Rocket\Engine\Cache\PostSubscriber;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering PostSubscriber::disable_cache_on_not_valid_pages
* @covers \WP_Rocket\Engine\Cache\PostSubscriber::disable_cache_on_not_valid_pages
*
* @uses PostSubscriber::is_not_valid_page
*
* @group Cache
*/
class Test_DisableCacheOnNotValidPages extends TestCase {
private $subscriber;

protected function setUp(): void {
parent::setUp();

$this->subscriber = new PostSubscriber();
}


/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $can_cache ) {
Functions\expect( 'is_singular' )->once()->andReturn( ! empty( $config['is_singular'] ) );
Functions\when( 'get_queried_object_id' )->justReturn( $config['current_post_id'] ?? 0 );

Functions\when( 'get_permalink' )->justReturn( $config['current_post_link'] ?? '' );
Functions\when( 'add_query_arg' )->justReturn( '' );
Functions\when( 'home_url' )->justReturn( $config['current_page_url'] ?? '' );
Functions\when( 'is_paged' )->justReturn( ! empty( $config['page'] ) );
Functions\when( 'get_query_var' )->justReturn( $config['page'] ?? 0 );

$this->assertSame( $can_cache, $this->subscriber->disable_cache_on_not_valid_pages( true ) );
}
}
Loading