Skip to content

Commit

Permalink
PR modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Aug 16, 2024
1 parent e88d8df commit 9f06e64
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 113 deletions.
35 changes: 10 additions & 25 deletions inc/Engine/Common/PerformanceHints/Admin/AdminBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,28 @@

use WP_Admin_Bar;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Settings\AdminBarMenuTrait;
use WP_Rocket\Engine\Common\Context\ContextInterface;

class AdminBar extends Abstract_Render {
use AdminBarMenuTrait;

/**
* ATF context.
* Array of factories
*
* @var ContextInterface
* @var array
*/
private $atf_context;

/**
* LRC context.
*
* @var ContextInterface
*/
private $lrc_context;

private $factories;

/**
* Constructor
*
* @param ContextInterface $atf_context ATF context.
* @param ContextInterface $lrc_context LRC context.
* @param string $template_path Template path.
* @param array $factories Array of factories.
* @param string $template_path Template path.
*/
public function __construct( ContextInterface $atf_context, ContextInterface $lrc_context, $template_path ) {
public function __construct( array $factories, $template_path ) {
parent::__construct( $template_path );

$this->atf_context = $atf_context;
$this->lrc_context = $lrc_context;
$this->factories = $factories;
}

/**
Expand All @@ -49,10 +37,7 @@ public function __construct( ContextInterface $atf_context, ContextInterface $lr
* @return void
*/
public function add_clear_performance_menu_item( WP_Admin_Bar $wp_admin_bar ): void {
if (
! $this->atf_context->is_allowed()
&& ! $this->lrc_context->is_allowed()
) {
if ( empty( $this->factories ) ) {
return;
}

Expand Down Expand Up @@ -98,7 +83,7 @@ public function add_clear_url_performance_hints_menu_item( WP_Admin_Bar $wp_admi
'clear-performance-hints-data-url',
$title,
$action,
$this->atf_context->is_allowed() || $this->lrc_context->is_allowed()
! empty( $this->factories )
);
}

Expand All @@ -108,7 +93,7 @@ public function add_clear_url_performance_hints_menu_item( WP_Admin_Bar $wp_admi
* @return void
*/
public function display_dashboard_button() {
$context = $this->lrc_context->is_allowed() || $this->atf_context->is_allowed();
$context = ! empty( $this->factories );

$this->dashboard_button(
$context,
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Common/PerformanceHints/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function truncate_from_admin( $clean ) {
'status' => 'success',
'message' => sprintf(
// translators: %1$s = plugin name.
__( '%1$s: WP Rocket: Critical images and Lazy Render data was cleared!', 'rocket' ),
__( '%1$s: Critical images and Lazy Render data was cleared!', 'rocket' ),
'<strong>WP Rocket</strong>'
),
];
Expand Down
23 changes: 7 additions & 16 deletions inc/Engine/Common/PerformanceHints/Admin/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@

class Notices {
/**
* Context instance
* Array of factories
*
* @var ContextInterface
* @var array
*/
private $atf_context;

/**
* Context instance
*
* @var ContextInterface
*/
private $lrc_context;
private $factories;

/**
* Constructor
*
* @param ContextInterface $atf_context ATF context instance.
* @param ContextInterface $lrc_context LRC context instance.
* @param array $factories Array of factories.
*/
public function __construct( ContextInterface $atf_context, ContextInterface $lrc_context ) {
$this->atf_context = $atf_context;
$this->lrc_context = $lrc_context;
public function __construct( array $factories ) {
$this->factories = $factories;
}

/**
Expand All @@ -43,7 +34,7 @@ public function clean_performance_hint_result() {
return;
}

if ( ! $this->atf_context->is_allowed() && ! $this->lrc_context->is_allowed() ) {
if ( empty( $this->factories ) ) {
return;
}

Expand Down
14 changes: 10 additions & 4 deletions inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ public function register(): void {
);

$this->getContainer()->add( 'performance_hints_notices', Notices::class )
->addArgument( $this->getContainer()->get( 'atf_context' ) )
->addArgument( $this->getContainer()->get( 'lrc_context' ) );
->addArgument(
[
$factories,
]
);

$this->getContainer()->add( 'performance_hints_admin_bar', Adminbar::class )
->addArgument( $this->getContainer()->get( 'atf_context' ) )
->addArgument( $this->getContainer()->get( 'lrc_context' ) )
->addArgument(
[
$factories,
]
)
->addArgument( $this->getContainer()->get( 'template_path' ) . '/settings' );

$this->getContainer()->add( 'performance_hints_clean', Clean::class );
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Saas/Admin/Clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function clean_saas() {
*
* @param array $clean An array containing the status and message.
*/
$clean = apply_filters( 'rocket_saas_clean_all', [] );
$clean = wpm_apply_filters_typed( 'array', 'rocket_saas_clean_all', [] );

$this->clean_data( $clean, 'rocket_saas_clean_message' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
'post_status' => 'publish',
],
'can_display_options' => true,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
],
'expected' => null,
],
Expand All @@ -24,8 +23,7 @@
'post_status' => 'publish',
],
'can_display_options' => true,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
],
'expected' => null,
],
Expand All @@ -38,12 +36,11 @@
'post_status' => 'draft',
],
'can_display_options' => false,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
],
'expected' => null,
],
'testShouldReturnNullWhenNotAllowed' => [
'testShouldReturnNullWhenFactoriesIsEmpty' => [
'config' => [
'environment' => 'production',
'is_admin' => false,
Expand All @@ -52,8 +49,7 @@
'post_status' => 'publish',
],
'can_display_options' => true,
'atf_context' => false,
'lrc_context' => false,
'factories' => false,
],
'expected' => null,
],
Expand All @@ -66,8 +62,7 @@
'post_status' => 'publish',
],
'can_display_options' => true,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
],
'expected' => [
'id' => 'clear-performance-hints-data-url',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,25 @@
'shouldDoNothingWhenNoCapability' => [
'config' => [
'capability' => false,
'atf_context' => 1,
'lrc_context' => 1,
'factories' => true,
'transient' => false,
'performance_hints_transient' => false,
],
'expected' => false,
],
'shouldDoNothingWhenATFDisabled' => [
'shouldDoNothingWhenFactoriesIsEmpty' => [
'config' => [
'capability' => true,
'atf_context' => false,
'lrc_context' => false,
'factories' => false,
'transient' => false,
'performance_hints_transient' => false,
],
'expected' => false,
],
'shouldDoNothingWhenTransientTimeLessThanCurrentTime' => [
'config' => [
'capability' => true,
'atf_context' => false,
'lrc_context' => false,
'transient' => time() - 30,
'performance_hints_transient' => false,
],
'expected' => false,
],
'shouldDoNothingWhenNoTransient' => [
'config' => [
'capability' => true,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
'transient' => false,
'performance_hints_transient' => false,
],
Expand All @@ -45,8 +32,7 @@
'shouldShowNoticeWhenTransient' => [
'config' => [
'capability' => true,
'atf_context' => true,
'lrc_context' => true,
'factories' => true,
'transient' => time() + 3600,
'performance_hints_transient' => false,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Mockery;
use WP_Admin_Bar;
use Brain\Monkey\Functions;
use WP_Rocket\Engine\Media\AboveTheFold\Factory as ATFFactory;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Factory;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Admin\AdminBar;
Expand All @@ -19,6 +21,7 @@ class Test_AddCleanPerformanceHintsItem extends TestCase {
private $lrc_context;

private $wp_admin_bar;
private $factories;

public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
Expand All @@ -29,11 +32,16 @@ public static function setUpBeforeClass(): void {
protected function setUp(): void {
parent::setUp();

$this->atf_context = Mockery::mock( ContextInterface::class );
$this->lrc_context = Mockery::mock( ContextInterface::class );
$this->admin_bar = new AdminBar( $this->atf_context, $this->lrc_context,'' );
$atf_factory = $this->createMock(ATFFactory::class);
$lrc_factory = $this->createMock(Factory::class);
$this->wp_admin_bar = new WP_Admin_Bar();

$this->factories = [
$atf_factory,
$lrc_factory
];
$this->admin_bar = new AdminBar( $this->factories,'' );

$this->stubTranslationFunctions();
}

Expand All @@ -57,12 +65,6 @@ function ( $url ) {
}
);

$this->atf_context->shouldReceive( 'is_allowed' )
->andReturn( $config['atf_context'] );

$this->lrc_context->shouldReceive( 'is_allowed' )
->andReturn( $config['lrc_context'] );

Functions\when( 'admin_url' )->alias(
function ( $path ) {
return "http://example.org/wp-admin/{$path}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Brain\Monkey\Functions;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Common\PerformanceHints\Admin\AdminBar;
use WP_Rocket\Engine\Media\AboveTheFold\Factory as ATFFactory;
use WP_Rocket\Engine\Optimization\LazyRenderContent\Factory;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\Engine\Common\Context\ContextInterface;

Expand All @@ -15,9 +17,7 @@
* @group PerformanceHints
*/
class Test_AddPerformanceHintsClearUrlMenuItem extends TestCase {
private $admin_bar;
private $atf_context;
private $lrc_context;
private $factories;
private $wp_admin_bar;

public static function setUpBeforeClass(): void {
Expand All @@ -29,10 +29,14 @@ public static function setUpBeforeClass(): void {
protected function setUp(): void {
parent::setUp();

$this->atf_context = Mockery::mock( ContextInterface::class );
$this->lrc_context = Mockery::mock( ContextInterface::class );
$this->admin_bar = new AdminBar( $this->atf_context, $this->lrc_context, '' );
$this->wp_admin_bar = new WP_Admin_Bar();
$atf_factory = $this->createMock(ATFFactory::class);
$lrc_factory = $this->createMock(Factory::class);
$this->wp_admin_bar = new WP_Admin_Bar();

$this->factories = [
$atf_factory,
$lrc_factory
];

$this->stubTranslationFunctions();
}
Expand All @@ -47,6 +51,8 @@ protected function tearDown(): void {
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
$admin_bar = new AdminBar( $config['factories'] ? $this->factories : [], '' );

Functions\when( 'wp_get_environment_type' )
->justReturn( $config['environment'] );
Functions\when( 'is_admin' )
Expand All @@ -57,12 +63,6 @@ public function testShouldDoExpected( $config, $expected ) {
Functions\when( 'rocket_can_display_options' )
->justReturn( $config['can_display_options'] );

$this->atf_context->shouldReceive( 'is_allowed' )
->andReturn( $config['atf_context'] );

$this->lrc_context->shouldReceive( 'is_allowed' )
->andReturn( $config['lrc_context'] );

Functions\when( 'wp_nonce_url' )->alias(
function ( $url ) {
return str_replace( '&', '&amp;', "{$url}&_wpnonce=123456" );
Expand All @@ -75,7 +75,7 @@ function ( $path ) {
}
);

$this->admin_bar->add_clear_url_performance_hints_menu_item( $this->wp_admin_bar );
$admin_bar->add_clear_url_performance_hints_menu_item( $this->wp_admin_bar );

$node = $this->wp_admin_bar->get_node( 'clear-performance-hints-data-url' );

Expand Down
Loading

0 comments on commit 9f06e64

Please sign in to comment.