Skip to content

Commit

Permalink
Closes #6878 Activation and deactivation of Performance hints data fi…
Browse files Browse the repository at this point in the history
…lters (#6928)
  • Loading branch information
Khadreal authored Sep 2, 2024
1 parent 88bfea5 commit 4d7b54b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
36 changes: 23 additions & 13 deletions inc/Engine/Common/PerformanceHints/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public function __construct( array $factories ) {
* @return void
*/
public function truncate_tables() {
if ( empty( $this->factories ) ) {
return;
}

$this->delete_rows();
}

Expand Down Expand Up @@ -68,10 +64,6 @@ private function delete_rows() {
* @return void
*/
public function delete_post( $post_id ) {
if ( empty( $this->factories ) ) {
return;
}

$url = get_permalink( $post_id );

if ( false === $url ) {
Expand All @@ -89,10 +81,6 @@ public function delete_post( $post_id ) {
* @return void
*/
public function delete_term( $term_id ) {
if ( empty( $this->factories ) ) {
return;
}

$url = get_term_link( (int) $term_id );

if ( is_wp_error( $url ) ) {
Expand All @@ -102,6 +90,24 @@ public function delete_term( $term_id ) {
$this->delete_by_url( $url );
}

/**
* Should allow early if true.
*
* @return bool
*/
private function is_allowed(): bool {
$allowed = false;

foreach ( $this->factories as $factory ) {
if ( $factory->get_context()->is_allowed() ) {
$allowed = true;
break;
}
}

return $allowed;
}

/**
* Deletes rows when triggering clean from admin
*
Expand All @@ -110,7 +116,7 @@ public function delete_term( $term_id ) {
* @return array
*/
public function truncate_from_admin( $clean ) {
if ( empty( $this->factories ) ) {
if ( ! $this->is_allowed() ) {
return $clean;
}

Expand Down Expand Up @@ -172,6 +178,10 @@ public function truncate_on_update( $new_version, $old_version ) {
return;
}

if ( ! $this->is_allowed() ) {
return;
}

$this->truncate_tables();
}

Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function register(): void {
$this->getContainer()->add( 'performance_hints_admin_controller', AdminController::class )
->addArguments(
[
$factories,
$factory_array,
]
);

Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ function () {
}
);

tests_add_filter(
'wp_loaded',
function() {

if ( BootstrapManager::isGroup( 'PerformanceHints' ) ) {
return;
}
$container = apply_filters( 'rocket_container', null );
$atf_table = $container->get( 'atf_table' );
$atf_table->uninstall();

$lrc_table = $container->get( 'lrc_table' );
$lrc_table->uninstall();
}
);

// install WC.
tests_add_filter(
'setup_theme',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace WP_Rocket\tests\Unit\inc\Engine\Common\PerformanceHints\Admin\Controller;

use Mockery;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\Engine\Common\PerformanceHints\Admin\Controller;
use WP_Rocket\Engine\Media\AboveTheFold\Factory as ATFFactory;
Expand All @@ -15,19 +16,22 @@
*
* @group PerformanceHints
*/
class TestTruncateOnUpdate extends TestCase {
class Test_TruncateOnUpdate extends TestCase {
private $factories;
private $queries;
private $table;
private $context;

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

$this->queries = $this->createMock(AboveTheFold::class);
$this->table = $this->createMock(ATFTable::class);
$atf_factory = $this->createMock(ATFFactory::class);
$this->context = $this->createMock(ContextInterface::class);
$atf_factory->method('queries')->willReturn($this->queries);
$atf_factory->method('table')->willReturn($this->table);
$atf_factory->method('get_context')->willReturn($this->context);

$this->factories = [
$atf_factory,
Expand All @@ -44,6 +48,10 @@ public function testShouldDoExpected( $config, $expected ) {
$this->queries->expects( $this->never() )
->method( 'get_not_completed_count' );
} else {
$this->context->expects( $this->once() )
->method('is_allowed')
->willReturn(true);

$this->queries->expects( $this->once() )
->method( 'get_not_completed_count' )
->willReturn( $config['not_completed'] );
Expand Down

0 comments on commit 4d7b54b

Please sign in to comment.