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

Don't cache not valid taxonomy frontend pages and stop applying optimizations #6984

Merged
2 changes: 2 additions & 0 deletions inc/Engine/Cache/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ServiceProvider extends AbstractServiceProvider {
'expired_cache_purge_subscriber',
'preload_caches_query',
'cache_config',
'taxonomy_subscriber',
];

/**
Expand Down Expand Up @@ -75,5 +76,6 @@ public function register(): void {
$this->getContainer()->add( 'cache_config', ConfigSubscriber::class )
->addArgument( $this->getContainer()->get( 'options' ) )
->addArgument( $this->getContainer()->get( 'options_api' ) );
$this->getContainer()->addShared( 'taxonomy_subscriber', TaxonomySubscriber::class );
}
}
100 changes: 100 additions & 0 deletions inc/Engine/Cache/TaxonomySubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace WP_Rocket\Engine\Cache;

use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Subscriber for the taxonomy frontend pages.
*/
class TaxonomySubscriber implements Subscriber_Interface {
/**
* {@inheritdoc}
*/
public static function get_subscribed_events() {
return [
'do_rocket_generate_caching_files' => 'disable_cache_on_not_valid_taxonomy_pages',
'rocket_buffer' => [ 'stop_optimizations_for_not_valid_taxonomy_pages', 1 ],
];
}

/**
* Disable caching invalid taxonomy frontend pages.
*
* @param bool $can_cache Filter callback passed value.
* @return bool
*/
public function disable_cache_on_not_valid_taxonomy_pages( $can_cache ) {
if ( $this->is_not_valid_taxonomies_page() ) {
return false;
}

return $can_cache;
}

/**
* Stop optimizing those invalid taxonomy 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_taxonomy_pages( $html ) {
return $this->is_not_valid_taxonomies_page() ? '' : $html;
}

/**
* Get all public taxonomy query vars.
*
* @return array
*/
private function get_all_taxonomies_query_var() {
$atts = [
'public' => true,
'show_ui' => true,
];

$taxonomies = get_taxonomies( $atts, 'objects' );

if ( empty( $taxonomies ) ) {
return [];
}

$output = [];
foreach ( $taxonomies as $taxonomy ) {
$output[] = $taxonomy->query_var;
}
return $output;
}

/**
* Check if we are on any taxonomy frontend page, but the url query is not valid.
*
* @return bool (True when not valid page, False if it's a valid taxonomy page)
*/
private function is_not_valid_taxonomies_page() {
if ( ! is_category() && ! is_tag() && ! is_tax() ) {
return false;
}
$taxonomies_query_vars = wpm_apply_filters_typed( 'string[]', 'rocket_cache_taxonomy_query_vars', $this->get_all_taxonomies_query_var() );

foreach ( $taxonomies_query_vars as $taxonomy_query_var ) {
if ( $this->is_not_valid_taxonomy_page( $taxonomy_query_var ) ) {
return true;
}
}
return false;
}

/**
* Check if we are on the taxonomy frontend page, but it's not valid url query.
*
* @param string $taxonomy_query_var Current taxonomy query var to check.
* @return bool (True when not valid taxonomy page, False if it's a valid one)
*/
private function is_not_valid_taxonomy_page( $taxonomy_query_var ) {
global $wp_query;
return isset( $wp_query->query_vars[ $taxonomy_query_var ], $wp_query->query[ $taxonomy_query_var ] )
&&
$wp_query->query_vars[ $taxonomy_query_var ] !== $wp_query->query[ $taxonomy_query_var ];
}
}
10 changes: 8 additions & 2 deletions inc/Engine/Optimization/Buffer/Optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ public function maybe_process_buffer( $buffer ) {
*
* @param string $buffer The page content.
*/
$buffer = (string) apply_filters( 'rocket_buffer', $buffer );
$filtered_buffer = (string) apply_filters( 'rocket_buffer', $buffer );

if ( empty( $filtered_buffer ) ) {
$this->log_last_test_error();
$this->log( 'Empty buffer.', [], 'error' );
return $buffer;
}

$this->log( 'Page optimized.', [], 'info' );

Expand All @@ -108,7 +114,7 @@ public function maybe_process_buffer( $buffer ) {
*/
do_action( 'rocket_after_process_buffer' );

return $buffer;
return $filtered_buffer;
}

/**
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private function init_common_subscribers() {
'performance_hints_warmup_subscriber',
'performance_hints_admin_subscriber',
'lrc_frontend_subscriber',
'taxonomy_subscriber',
];

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

return [
'test_data' => [
'testIsNotInCategoryPage' => [
'config' => [
'is_category' => false,
],
'can_cache' => true,
],
'testIsNotInTagPage' => [
'config' => [
'is_category' => false,
'is_tag' => false,
],
'can_cache' => true,
],
'testIsNotInTaxPage' => [
'config' => [
'is_category' => false,
'is_tag' => false,
'is_tax' => false,
],
'can_cache' => true,
],
'testEmptyTaxonomies' => [
'config' => [
'is_category' => true,
'is_tag' => false,
'is_tax' => false,
'taxonomies' => [],
],
'can_cache' => true,
],
'testValidTaxonomyPage' => [
'config' => [
'is_category' => true,
'is_tag' => false,
'is_tax' => false,
'taxonomies' => [
(object) [
'query_var' => 'category_name',
],
],
'current_query' => [
'category_name' => 'category',
],
'current_query_var' => [
'category_name' => 'category',
],
],
'can_cache' => true,
],
'testNotValidTaxonomyPage' => [
'config' => [
'is_category' => true,
'is_tag' => false,
'is_tax' => false,
'taxonomies' => [
(object) [
'query_var' => 'category_name',
],
],
'current_query' => [
'category_name' => 'category1',
],
'current_query_var' => [
'category_name' => 'nothing',
],
],
'can_cache' => false,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

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

/**
* Test class covering \WP_Rocket\Engine\Cache\TaxonomySubscriber::disable_cache_on_not_valid_taxonomy_pages
*
* @uses \WP_Rocket\Engine\Cache\TaxonomySubscriber::is_not_valid_taxonomies_page
*
* @group Cache
*/
class Test_DisableCacheOnNotValidTaxonomyPages extends TestCase {
private $subscriber;

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

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


/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $can_cache ) {
Functions\expect( 'is_category' )->once()->andReturn( ! empty( $config['is_category'] ) );
Functions\when( 'is_tag' )->justReturn( ! empty( $config['is_tag'] ) );
Functions\when( 'is_tax' )->justReturn( ! empty( $config['is_tax'] ) );
Functions\when( 'get_taxonomies' )->justReturn( $config['taxonomies'] ?? [] );

if ( ! empty( $config['current_query'] ) && ! empty( $config['current_query_var'] ) ) {
global $wp_query;
$wp_query = (object) [
'query_vars' => $config['current_query_var'],
'query' => $config['current_query'],
];
}

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