From 8609ad27d9ce81ec498da569b45cdc2c1b243f9c Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 12 May 2023 17:20:49 +0100 Subject: [PATCH 01/31] Updated to not cache private pages --- .../Preload/Controller/CheckExcludedTrait.php | 13 +++++++++++++ inc/Engine/Preload/Controller/ClearCache.php | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 815100d154..da3eb8a54f 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -64,4 +64,17 @@ protected function is_excluded_by_filter( string $url ): bool { } return false; } + + /** + * Checks that a page is private. + * + * @param string $url Page url. + * + * @return bool Return true if page is private; false otherwise. + */ + protected function is_private( string $url ) : bool { + $post_id = url_to_postid( $url ); + + return 'private' === get_post_status( $post_id ); + } } diff --git a/inc/Engine/Preload/Controller/ClearCache.php b/inc/Engine/Preload/Controller/ClearCache.php index f900115c84..856421d2ed 100644 --- a/inc/Engine/Preload/Controller/ClearCache.php +++ b/inc/Engine/Preload/Controller/ClearCache.php @@ -31,6 +31,10 @@ public function __construct( Cache $query ) { public function partial_clean( array $urls ) { foreach ( $urls as $url ) { + if ( $this->is_private( $url ) ) { + continue; + } + if ( ! $this->is_excluded_by_filter( $url ) ) { $this->query->create_or_update( [ From 549660fceb9256861cf0c37b9696cde042c769f8 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 12 May 2023 17:22:32 +0100 Subject: [PATCH 02/31] Updated tests --- .../Controller/ClearCache/partialClean.php | 24 ++++++++++++++ .../Controller/ClearCache/partialClean.php | 31 ++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php index 83c3464a3a..f0966e3e80 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -8,6 +8,7 @@ ], 'is_excluded' => false, 'is_excluded_by_filter' => false, + 'is_private' => false, ], 'expected' => [ 'urls' => [ @@ -34,6 +35,7 @@ ], 'is_excluded' => true, 'is_excluded_by_filter' => false, + 'is_private' => false, ], 'expected' => [ 'urls' => [ @@ -60,6 +62,28 @@ ], 'is_excluded' => true, 'is_excluded_by_filter' => true, + 'is_private' => false, + ], + 'expected' => [ + 'urls' => [ + [ + 'url', + ], + [ + 'url1', + ] + ] + ] + ], + 'continueWithPostStatusAsPrivate' => [ + 'config' => [ + 'urls' => [ + 'url', + 'url1', + ], + 'is_excluded' => true, + 'is_excluded_by_filter' => true, + 'is_private' => true, ], 'expected' => [ 'urls' => [ diff --git a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php index 839a8ba478..d2ee1d9c7d 100644 --- a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -20,24 +20,39 @@ protected function setUp(): void { parent::setUp(); $this->query = $this->createMock(Cache::class); - $this->controller = Mockery::mock(ClearCache::class . '[is_excluded,is_excluded_by_filter]', [$this->query]) + $this->controller = Mockery::mock(ClearCache::class . '[is_excluded,is_excluded_by_filter,is_private]', [$this->query]) ->shouldAllowMockingProtectedMethods(); } + /** * @dataProvider configTestData */ public function testShouldDoAsExpected($config, $expected) { foreach ($config['urls'] as $url) { - $this->controller->expects()->is_excluded_by_filter($url)->andReturn($config['is_excluded']); - $this->controller->shouldReceive('is_excluded_by_filter')->with($url)->andReturn($config['is_excluded_by_filter']); + $this->controller->expects()->is_private($url)->andReturn($config['is_private']); + if ( ! $config['is_private'] ) { + $this->controller->expects()->is_excluded_by_filter($url)->andReturn($config['is_excluded']); + $this->controller->shouldReceive('is_excluded_by_filter')->with($url)->andReturn($config['is_excluded_by_filter']); + } + else{ + $this->controller->expects()->is_excluded_by_filter($url)->never(); + $this->controller->shouldReceive('is_excluded_by_filter')->never(); + } + } + + if ( $config['is_private'] ) { + $this->query->expects($this->never())->method('create_or_update'); + $this->query->expects($this->never())->method('delete_by_url'); } - if(! $config['is_excluded']) { - if(! $config['is_excluded_by_filter']) { - $this->query->expects(self::atLeastOnce())->method('create_or_update')->withConsecutive(...$expected['urls']); - } else { - $this->query->expects(self::atLeastOnce())->method('delete_by_url')->withConsecutive(...$expected['urls']); + else { + if(! $config['is_excluded']) { + if(! $config['is_excluded_by_filter']) { + $this->query->expects(self::atLeastOnce())->method('create_or_update')->withConsecutive(...$expected['urls']); + } else { + $this->query->expects(self::atLeastOnce())->method('delete_by_url')->withConsecutive(...$expected['urls']); + } } } From b74ea7456bf5da8b1ccd386a7e2f6aed5fa47e79 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 12 May 2023 17:28:42 +0100 Subject: [PATCH 03/31] Update test case --- .../inc/Engine/Preload/Controller/ClearCache/partialClean.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php index f0966e3e80..0a51c98048 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -75,7 +75,7 @@ ] ] ], - 'continueWithPostStatusAsPrivate' => [ + 'excludePostStatusWithPrivateStatus' => [ 'config' => [ 'urls' => [ 'url', From c6574d71745237cb3e953611a9f87f08b8362277 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 16 May 2023 13:08:55 +0100 Subject: [PATCH 04/31] Return early when not a post --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index da3eb8a54f..6aa5dfbaf1 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -75,6 +75,10 @@ protected function is_excluded_by_filter( string $url ): bool { protected function is_private( string $url ) : bool { $post_id = url_to_postid( $url ); + if ( 0 === $post_id ) { + return false; + } + return 'private' === get_post_status( $post_id ); } } From 46e18bf211becab05e38b062b2f954edc14179c7 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 16 May 2023 16:09:56 +0100 Subject: [PATCH 05/31] Enhance code readability --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 6aa5dfbaf1..966c9a63cd 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -75,7 +75,7 @@ protected function is_excluded_by_filter( string $url ): bool { protected function is_private( string $url ) : bool { $post_id = url_to_postid( $url ); - if ( 0 === $post_id ) { + if ( empty( $post_id ) ) { return false; } From 9a0f29dc608a39322fb5e22870f66a79a77bb6ac Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 18 May 2023 10:33:04 +0100 Subject: [PATCH 06/31] Remove post changed to private from cache table --- inc/Engine/Preload/Subscriber.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index eb91d3de91..246282a392 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -122,6 +122,7 @@ public static function get_subscribed_events() { [ 'add_cache_reject_uri_to_excluded' ], ], 'rocket_rucss_after_clearing_failed_url' => [ 'clean_urls', 20 ], + 'transition_post_status' => [ 'remove_private_post', 10, 3 ], ]; } @@ -451,4 +452,25 @@ public function add_preload_excluded_uri( $regexes ): array { return array_merge( $regexes, $preload_excluded_uri ); } + + /** + * Remove private post from cache. + * + * @param string $new_status New post status. + * @param string $old_status Old post status. + * @param WP_Post $post Wp post object. + * @return void + */ + public function remove_private_post( string $new_status, string $old_status, WP_Post $post ) { + + if ( $new_status === $old_status ) { + return; + } + + if ( 'private' !== $new_status ) { + return; + } + + $this->delete_post_preload_cache( $post->ID ); + } } From 72738247d80db818add2f7893556f4509339112e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 18 May 2023 12:13:57 +0100 Subject: [PATCH 07/31] Updated method --- inc/Engine/Preload/Subscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index 246282a392..ce76b7ad32 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -461,7 +461,7 @@ public function add_preload_excluded_uri( $regexes ): array { * @param WP_Post $post Wp post object. * @return void */ - public function remove_private_post( string $new_status, string $old_status, WP_Post $post ) { + public function remove_private_post( string $new_status, string $old_status, $post ) { if ( $new_status === $old_status ) { return; From 09ff7ac1f7d14b9087be81bcaf64c4d1b42268a2 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 1 Jun 2023 13:59:36 +0100 Subject: [PATCH 08/31] Check for private post --- inc/Engine/Preload/Frontend/FetchSitemap.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/Engine/Preload/Frontend/FetchSitemap.php b/inc/Engine/Preload/Frontend/FetchSitemap.php index be960a1750..b9d8c49aa4 100644 --- a/inc/Engine/Preload/Frontend/FetchSitemap.php +++ b/inc/Engine/Preload/Frontend/FetchSitemap.php @@ -64,6 +64,10 @@ public function parse_sitemap( string $url ) { $links = $this->sitemap_parser->get_links(); foreach ( $links as $link ) { + if ( $this->is_private( $link ) ) { + continue; + } + if ( ! $this->is_excluded_by_filter( $link ) ) { $this->query->create_or_nothing( [ From 1006e7269940a3ef8c5d028300dd3bed3a5f261d Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 1 Jun 2023 14:00:24 +0100 Subject: [PATCH 09/31] Updated tests --- .../Frontend/FetchSitemap/parseSitemap.php | 23 +++++++++++++++++++ .../Frontend/FetchSitemap/parseSitemap.php | 17 ++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 6442b49aa6..07ddd4fb66 100644 --- a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -3,6 +3,7 @@ 'shouldNoParseOnRequestFailure' => [ 'config' => [ 'is_excluded' => false, + 'is_private' => false, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => false, @@ -15,6 +16,7 @@ 'shouldParseOnRequestSucceed' => [ 'config' => [ 'is_excluded' => false, + 'is_private' => false, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => true, @@ -41,6 +43,27 @@ 'shouldNotAddOnExcluded' => [ 'config' => [ 'is_excluded' => true, + 'is_private' => false, + 'url' => 'http://example.com', + 'response' => 'response', + 'request_succeed' => true, + 'status' => 200, + 'content' => '', + 'links' => [ + 'url1', + 'url2', + ], + 'jobs' => [], + 'children' => [ + 'children1', + 'children2', + ] + ], + ], + 'shouldNotAddIfPrivateUrl' => [ + 'config' => [ + 'is_excluded' => false, + 'is_private' => true, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => true, diff --git a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index bb9d94499c..1d38cb7614 100644 --- a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -23,7 +23,7 @@ protected function setUp(): void { $this->sitemap_parser = Mockery::mock( SitemapParser::class ); $this->queue = Mockery::mock( Queue::class ); $this->query = $this->createMock( Cache::class ); - $this->controller = Mockery::mock( FetchSitemap::class . '[is_excluded_by_filter]', [$this->sitemap_parser, $this->queue, $this->query] )->shouldAllowMockingProtectedMethods(); + $this->controller = Mockery::mock( FetchSitemap::class . '[is_excluded_by_filter,is_private]', [$this->sitemap_parser, $this->queue, $this->query] )->shouldAllowMockingProtectedMethods(); } /** @@ -72,11 +72,20 @@ protected function configureParseSitemap($config) { ->andReturn( $config['children'] ); foreach ( $config['links'] as $index => $link ) { - $this->controller->expects()->is_excluded_by_filter( $link ) + $this->controller->expects()->is_private( $link ) ->once() - ->andReturn( $config['is_excluded'] ); + ->andReturn( $config['is_private'] ); - if ( ! $config['is_excluded'] ) { + if ( $config['is_private'] ) { + $this->controller->shouldReceive('is_excluded_by_filter')->never(); + } + else{ + $this->controller->expects()->is_excluded_by_filter( $link ) + ->once() + ->andReturn( $config['is_excluded'] ); + } + + if ( ! $config['is_excluded'] && ! $config['is_private'] ) { $this->query->expects( self::any() )->method( 'create_or_nothing' ) ->withConsecutive( ...$config['jobs'] ) ->willReturn( true ); From dcd141d0a700821142c56e02804040639def1eb4 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 1 Jun 2023 16:42:33 +0100 Subject: [PATCH 10/31] Check for private post --- inc/Engine/Preload/Controller/PreloadUrl.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inc/Engine/Preload/Controller/PreloadUrl.php b/inc/Engine/Preload/Controller/PreloadUrl.php index 0e105b9dbb..0518f6c1b4 100755 --- a/inc/Engine/Preload/Controller/PreloadUrl.php +++ b/inc/Engine/Preload/Controller/PreloadUrl.php @@ -207,6 +207,11 @@ public function process_pending_jobs() { $rows = $this->query->get_pending_jobs( $count ); foreach ( $rows as $row ) { + if ( $this->is_private( $row->url ) ) { + $this->query->delete_by_url( $row->url ); + continue; + } + if ( $this->is_excluded_by_filter( $row->url ) ) { $this->query->delete_by_url( $row->url ); continue; From 19be1fc74ea5d7e58a0796c9a6feb0d8d427a704 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 7 Jun 2023 12:55:35 +0100 Subject: [PATCH 11/31] Change method logic --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 966c9a63cd..de5fb081ef 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -73,12 +73,17 @@ protected function is_excluded_by_filter( string $url ): bool { * @return bool Return true if page is private; false otherwise. */ protected function is_private( string $url ) : bool { - $post_id = url_to_postid( $url ); + $private_post_urls = []; + $query = new \WP_Query( [ 'post_status' => 'private' ] ); - if ( empty( $post_id ) ) { + if ( empty( $query ) ) { return false; } - return 'private' === get_post_status( $post_id ); + foreach ( $query->posts as $post ) { + $private_post_urls[] = get_permalink( $post->ID ); + } + + return in_array( $url, $private_post_urls, true ); } } From 1872f1f6c46549ae032202d41cab1d7be0731c18 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 7 Jun 2023 13:01:23 +0100 Subject: [PATCH 12/31] Add trailing slash --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index de5fb081ef..8d6058ddc4 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -73,6 +73,7 @@ protected function is_excluded_by_filter( string $url ): bool { * @return bool Return true if page is private; false otherwise. */ protected function is_private( string $url ) : bool { + $url = user_trailingslashit( $url ); $private_post_urls = []; $query = new \WP_Query( [ 'post_status' => 'private' ] ); From 4d232ab6e49e16ae80eea9dea5cd8701166b75d8 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 7 Jun 2023 14:11:03 +0100 Subject: [PATCH 13/31] Cast publish status --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 8d6058ddc4..886c03c1a6 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -82,7 +82,9 @@ protected function is_private( string $url ) : bool { } foreach ( $query->posts as $post ) { - $private_post_urls[] = get_permalink( $post->ID ); + // Temporarily cast publish status to get pretty url. + $post->post_status = 'publish'; + $private_post_urls[] = get_permalink( $post ); } return in_array( $url, $private_post_urls, true ); From 144ba17a93b81996aa163fc10f5df02f626a93e3 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 7 Jun 2023 14:19:07 +0100 Subject: [PATCH 14/31] Updated tests --- .../Preload/Controller/PreloadUrl/processPendingJobs.php | 5 +++++ .../Preload/Controller/PreloadUrl/processPendingJobs.php | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php b/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php index b33125d6a4..b2e9398c1e 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php @@ -41,6 +41,11 @@ false, true, ], + 'is_private' => [ + false, + false, + true, + ], 'rows' => 101, 'jobs' => [ $row1, diff --git a/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php b/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php index 6baf7c9448..1a1ebe8557 100644 --- a/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php +++ b/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php @@ -31,7 +31,7 @@ protected function setUp(): void $this->query = $this->createMock(Cache::class); $this->queue = Mockery::mock(Queue::class); $this->file_system = Mockery::mock(WP_Filesystem_Direct::class); - $this->controller = Mockery::mock( PreloadUrl::class . '[is_excluded_by_filter]' , [$this->options, $this->queue, $this->query, $this->file_system])->shouldAllowMockingProtectedMethods(); + $this->controller = Mockery::mock( PreloadUrl::class . '[is_excluded_by_filter,is_private]' , [$this->options, $this->queue, $this->query, $this->file_system])->shouldAllowMockingProtectedMethods(); } /** @@ -46,6 +46,7 @@ public function testShouldDoAsExpected($config, $expected) { $this->query->expects(self::atLeastOnce())->method('make_status_inprogress')->withConsecutive(...$expected['job_ids']); $this->query->expects(self::atLeast(0))->method('delete_by_url')->withConsecutive($expected['job_deleted']); $this->controller->shouldReceive('is_excluded_by_filter')->zeroOrMoreTimes()->andReturnValues($config['excluded']); + $this->controller->shouldReceive('is_private')->zeroOrMoreTimes()->andReturnValues($config['is_private']); foreach ($expected['job_urls'] as $url) { $this->queue->expects()->add_job_preload_job_preload_url_async( $url ); } From 6c38e0dc4448b3efca94b65883447c09f344f446 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 16 Jun 2023 17:02:22 +0100 Subject: [PATCH 15/31] Updated to get private post of any post type --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 886c03c1a6..388aa5a691 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -75,7 +75,12 @@ protected function is_excluded_by_filter( string $url ): bool { protected function is_private( string $url ) : bool { $url = user_trailingslashit( $url ); $private_post_urls = []; - $query = new \WP_Query( [ 'post_status' => 'private' ] ); + + $arg = [ + 'post_type' => 'any', + 'post_status' => 'private', + ]; + $query = new \WP_Query( $arg ); if ( empty( $query ) ) { return false; From 647a388d6d85a846fa610876ecc8286890b35e9e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 12:04:37 +0100 Subject: [PATCH 16/31] Get all private posts --- inc/Engine/Preload/Controller/CheckExcludedTrait.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index 388aa5a691..ee53e5e56e 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -77,8 +77,9 @@ protected function is_private( string $url ) : bool { $private_post_urls = []; $arg = [ - 'post_type' => 'any', - 'post_status' => 'private', + 'post_type' => 'any', + 'post_status' => 'private', + 'posts_per_page' => -1, ]; $query = new \WP_Query( $arg ); From 64b1d4adc11b1d1b06342e14d29bd132c3df5aeb Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:03:43 +0100 Subject: [PATCH 17/31] Removed call to is_private method --- .../Preload/Controller/CheckExcludedTrait.php | 31 ------------------- inc/Engine/Preload/Controller/ClearCache.php | 4 --- inc/Engine/Preload/Controller/PreloadUrl.php | 6 ---- inc/Engine/Preload/Frontend/FetchSitemap.php | 4 --- 4 files changed, 45 deletions(-) diff --git a/inc/Engine/Preload/Controller/CheckExcludedTrait.php b/inc/Engine/Preload/Controller/CheckExcludedTrait.php index ee53e5e56e..815100d154 100644 --- a/inc/Engine/Preload/Controller/CheckExcludedTrait.php +++ b/inc/Engine/Preload/Controller/CheckExcludedTrait.php @@ -64,35 +64,4 @@ protected function is_excluded_by_filter( string $url ): bool { } return false; } - - /** - * Checks that a page is private. - * - * @param string $url Page url. - * - * @return bool Return true if page is private; false otherwise. - */ - protected function is_private( string $url ) : bool { - $url = user_trailingslashit( $url ); - $private_post_urls = []; - - $arg = [ - 'post_type' => 'any', - 'post_status' => 'private', - 'posts_per_page' => -1, - ]; - $query = new \WP_Query( $arg ); - - if ( empty( $query ) ) { - return false; - } - - foreach ( $query->posts as $post ) { - // Temporarily cast publish status to get pretty url. - $post->post_status = 'publish'; - $private_post_urls[] = get_permalink( $post ); - } - - return in_array( $url, $private_post_urls, true ); - } } diff --git a/inc/Engine/Preload/Controller/ClearCache.php b/inc/Engine/Preload/Controller/ClearCache.php index 856421d2ed..f900115c84 100644 --- a/inc/Engine/Preload/Controller/ClearCache.php +++ b/inc/Engine/Preload/Controller/ClearCache.php @@ -31,10 +31,6 @@ public function __construct( Cache $query ) { public function partial_clean( array $urls ) { foreach ( $urls as $url ) { - if ( $this->is_private( $url ) ) { - continue; - } - if ( ! $this->is_excluded_by_filter( $url ) ) { $this->query->create_or_update( [ diff --git a/inc/Engine/Preload/Controller/PreloadUrl.php b/inc/Engine/Preload/Controller/PreloadUrl.php index 0518f6c1b4..4bd3c16b23 100755 --- a/inc/Engine/Preload/Controller/PreloadUrl.php +++ b/inc/Engine/Preload/Controller/PreloadUrl.php @@ -206,12 +206,6 @@ public function process_pending_jobs() { } $rows = $this->query->get_pending_jobs( $count ); foreach ( $rows as $row ) { - - if ( $this->is_private( $row->url ) ) { - $this->query->delete_by_url( $row->url ); - continue; - } - if ( $this->is_excluded_by_filter( $row->url ) ) { $this->query->delete_by_url( $row->url ); continue; diff --git a/inc/Engine/Preload/Frontend/FetchSitemap.php b/inc/Engine/Preload/Frontend/FetchSitemap.php index b9d8c49aa4..be960a1750 100644 --- a/inc/Engine/Preload/Frontend/FetchSitemap.php +++ b/inc/Engine/Preload/Frontend/FetchSitemap.php @@ -64,10 +64,6 @@ public function parse_sitemap( string $url ) { $links = $this->sitemap_parser->get_links(); foreach ( $links as $link ) { - if ( $this->is_private( $link ) ) { - continue; - } - if ( ! $this->is_excluded_by_filter( $link ) ) { $this->query->create_or_nothing( [ From 98a4dfddb074cb058aa9b30ee42c4c655ada83f2 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:05:10 +0100 Subject: [PATCH 18/31] Updated tests --- .../Controller/ClearCache/partialClean.php | 4 --- .../PreloadUrl/processPendingJobs.php | 5 ---- .../Frontend/FetchSitemap/parseSitemap.php | 4 --- .../Controller/ClearCache/partialClean.php | 29 +++++-------------- .../PreloadUrl/processPendingJobs.php | 3 +- .../Frontend/FetchSitemap/parseSitemap.php | 14 ++------- 6 files changed, 12 insertions(+), 47 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php index 0a51c98048..5ea24fecdc 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -8,7 +8,6 @@ ], 'is_excluded' => false, 'is_excluded_by_filter' => false, - 'is_private' => false, ], 'expected' => [ 'urls' => [ @@ -35,7 +34,6 @@ ], 'is_excluded' => true, 'is_excluded_by_filter' => false, - 'is_private' => false, ], 'expected' => [ 'urls' => [ @@ -62,7 +60,6 @@ ], 'is_excluded' => true, 'is_excluded_by_filter' => true, - 'is_private' => false, ], 'expected' => [ 'urls' => [ @@ -83,7 +80,6 @@ ], 'is_excluded' => true, 'is_excluded_by_filter' => true, - 'is_private' => true, ], 'expected' => [ 'urls' => [ diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php b/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php index b2e9398c1e..b33125d6a4 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php @@ -41,11 +41,6 @@ false, true, ], - 'is_private' => [ - false, - false, - true, - ], 'rows' => 101, 'jobs' => [ $row1, diff --git a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 07ddd4fb66..1c2296685d 100644 --- a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -3,7 +3,6 @@ 'shouldNoParseOnRequestFailure' => [ 'config' => [ 'is_excluded' => false, - 'is_private' => false, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => false, @@ -16,7 +15,6 @@ 'shouldParseOnRequestSucceed' => [ 'config' => [ 'is_excluded' => false, - 'is_private' => false, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => true, @@ -43,7 +41,6 @@ 'shouldNotAddOnExcluded' => [ 'config' => [ 'is_excluded' => true, - 'is_private' => false, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => true, @@ -63,7 +60,6 @@ 'shouldNotAddIfPrivateUrl' => [ 'config' => [ 'is_excluded' => false, - 'is_private' => true, 'url' => 'http://example.com', 'response' => 'response', 'request_succeed' => true, diff --git a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php index d2ee1d9c7d..7aba722482 100644 --- a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -20,7 +20,7 @@ protected function setUp(): void { parent::setUp(); $this->query = $this->createMock(Cache::class); - $this->controller = Mockery::mock(ClearCache::class . '[is_excluded,is_excluded_by_filter,is_private]', [$this->query]) + $this->controller = Mockery::mock(ClearCache::class . '[is_excluded,is_excluded_by_filter]', [$this->query]) ->shouldAllowMockingProtectedMethods(); } @@ -31,28 +31,15 @@ protected function setUp(): void public function testShouldDoAsExpected($config, $expected) { foreach ($config['urls'] as $url) { - $this->controller->expects()->is_private($url)->andReturn($config['is_private']); - if ( ! $config['is_private'] ) { - $this->controller->expects()->is_excluded_by_filter($url)->andReturn($config['is_excluded']); - $this->controller->shouldReceive('is_excluded_by_filter')->with($url)->andReturn($config['is_excluded_by_filter']); - } - else{ - $this->controller->expects()->is_excluded_by_filter($url)->never(); - $this->controller->shouldReceive('is_excluded_by_filter')->never(); - } + $this->controller->expects()->is_excluded_by_filter($url)->andReturn($config['is_excluded']); + $this->controller->shouldReceive('is_excluded_by_filter')->with($url)->andReturn($config['is_excluded_by_filter']); } - if ( $config['is_private'] ) { - $this->query->expects($this->never())->method('create_or_update'); - $this->query->expects($this->never())->method('delete_by_url'); - } - else { - if(! $config['is_excluded']) { - if(! $config['is_excluded_by_filter']) { - $this->query->expects(self::atLeastOnce())->method('create_or_update')->withConsecutive(...$expected['urls']); - } else { - $this->query->expects(self::atLeastOnce())->method('delete_by_url')->withConsecutive(...$expected['urls']); - } + if(! $config['is_excluded']) { + if(! $config['is_excluded_by_filter']) { + $this->query->expects(self::atLeastOnce())->method('create_or_update')->withConsecutive(...$expected['urls']); + } else { + $this->query->expects(self::atLeastOnce())->method('delete_by_url')->withConsecutive(...$expected['urls']); } } diff --git a/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php b/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php index 1a1ebe8557..6baf7c9448 100644 --- a/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php +++ b/tests/Unit/inc/Engine/Preload/Controller/PreloadUrl/processPendingJobs.php @@ -31,7 +31,7 @@ protected function setUp(): void $this->query = $this->createMock(Cache::class); $this->queue = Mockery::mock(Queue::class); $this->file_system = Mockery::mock(WP_Filesystem_Direct::class); - $this->controller = Mockery::mock( PreloadUrl::class . '[is_excluded_by_filter,is_private]' , [$this->options, $this->queue, $this->query, $this->file_system])->shouldAllowMockingProtectedMethods(); + $this->controller = Mockery::mock( PreloadUrl::class . '[is_excluded_by_filter]' , [$this->options, $this->queue, $this->query, $this->file_system])->shouldAllowMockingProtectedMethods(); } /** @@ -46,7 +46,6 @@ public function testShouldDoAsExpected($config, $expected) { $this->query->expects(self::atLeastOnce())->method('make_status_inprogress')->withConsecutive(...$expected['job_ids']); $this->query->expects(self::atLeast(0))->method('delete_by_url')->withConsecutive($expected['job_deleted']); $this->controller->shouldReceive('is_excluded_by_filter')->zeroOrMoreTimes()->andReturnValues($config['excluded']); - $this->controller->shouldReceive('is_private')->zeroOrMoreTimes()->andReturnValues($config['is_private']); foreach ($expected['job_urls'] as $url) { $this->queue->expects()->add_job_preload_job_preload_url_async( $url ); } diff --git a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 1d38cb7614..4f949af1dd 100644 --- a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -23,7 +23,7 @@ protected function setUp(): void { $this->sitemap_parser = Mockery::mock( SitemapParser::class ); $this->queue = Mockery::mock( Queue::class ); $this->query = $this->createMock( Cache::class ); - $this->controller = Mockery::mock( FetchSitemap::class . '[is_excluded_by_filter,is_private]', [$this->sitemap_parser, $this->queue, $this->query] )->shouldAllowMockingProtectedMethods(); + $this->controller = Mockery::mock( FetchSitemap::class . '[is_excluded_by_filter]', [$this->sitemap_parser, $this->queue, $this->query] )->shouldAllowMockingProtectedMethods(); } /** @@ -72,20 +72,12 @@ protected function configureParseSitemap($config) { ->andReturn( $config['children'] ); foreach ( $config['links'] as $index => $link ) { - $this->controller->expects()->is_private( $link ) - ->once() - ->andReturn( $config['is_private'] ); - if ( $config['is_private'] ) { - $this->controller->shouldReceive('is_excluded_by_filter')->never(); - } - else{ - $this->controller->expects()->is_excluded_by_filter( $link ) + $this->controller->expects()->is_excluded_by_filter( $link ) ->once() ->andReturn( $config['is_excluded'] ); - } - if ( ! $config['is_excluded'] && ! $config['is_private'] ) { + if ( ! $config['is_excluded'] ) { $this->query->expects( self::any() )->method( 'create_or_nothing' ) ->withConsecutive( ...$config['jobs'] ) ->willReturn( true ); From f4ddde17bc455335faaa045147ecbfc9347750c9 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:06:13 +0100 Subject: [PATCH 19/31] Added new filter callback to remove private urls --- inc/Engine/Preload/Subscriber.php | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index ce76b7ad32..51631f64cc 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -120,6 +120,7 @@ public static function get_subscribed_events() { 'rocket_preload_exclude_urls' => [ [ 'add_preload_excluded_uri' ], [ 'add_cache_reject_uri_to_excluded' ], + [ 'exclude_private_post_url' ], ], 'rocket_rucss_after_clearing_failed_url' => [ 'clean_urls', 20 ], 'transition_post_status' => [ 'remove_private_post', 10, 3 ], @@ -473,4 +474,39 @@ public function remove_private_post( string $new_status, string $old_status, $po $this->delete_post_preload_cache( $post->ID ); } + + /** + * Exclude private urls. + * + * @param array $regexes regexes containing excluded uris. + * @return array + */ + public function exclude_private_post_url( $regexes ) : array { + static $private_urls; + + if ( isset( $private_urls ) ) { + return $private_urls; + } + + $arg = [ + 'post_type' => 'any', + 'post_status' => 'private', + 'posts_per_page' => -1, + ]; + $query = new \WP_Query( $arg ); + + if ( empty( $query ) ) { + return $regexes; + } + + foreach ( $query->posts as $post ) { + // Temporarily cast publish status to get pretty url. + $post->post_status = 'publish'; + $private_post_urls[] = get_permalink( $post ); + } + + $private_urls = array_merge( $regexes, $private_post_urls ); + + return $private_urls; + } } From 5d6a845db8846a426316971eb0abf3ef2c00985a Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:14:30 +0100 Subject: [PATCH 20/31] Updated tests --- inc/Engine/Preload/Controller/PreloadUrl.php | 1 + .../Controller/ClearCache/partialClean.php | 20 ------------------- .../Frontend/FetchSitemap/parseSitemap.php | 19 ------------------ .../Controller/ClearCache/partialClean.php | 2 -- .../Frontend/FetchSitemap/parseSitemap.php | 1 - 5 files changed, 1 insertion(+), 42 deletions(-) diff --git a/inc/Engine/Preload/Controller/PreloadUrl.php b/inc/Engine/Preload/Controller/PreloadUrl.php index 4bd3c16b23..0e105b9dbb 100755 --- a/inc/Engine/Preload/Controller/PreloadUrl.php +++ b/inc/Engine/Preload/Controller/PreloadUrl.php @@ -206,6 +206,7 @@ public function process_pending_jobs() { } $rows = $this->query->get_pending_jobs( $count ); foreach ( $rows as $row ) { + if ( $this->is_excluded_by_filter( $row->url ) ) { $this->query->delete_by_url( $row->url ); continue; diff --git a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php index 5ea24fecdc..83c3464a3a 100644 --- a/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Fixtures/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -72,24 +72,4 @@ ] ] ], - 'excludePostStatusWithPrivateStatus' => [ - 'config' => [ - 'urls' => [ - 'url', - 'url1', - ], - 'is_excluded' => true, - 'is_excluded_by_filter' => true, - ], - 'expected' => [ - 'urls' => [ - [ - 'url', - ], - [ - 'url1', - ] - ] - ] - ], ]; diff --git a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 1c2296685d..9fbf61c83e 100644 --- a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -57,23 +57,4 @@ ] ], ], - 'shouldNotAddIfPrivateUrl' => [ - 'config' => [ - 'is_excluded' => false, - 'url' => 'http://example.com', - 'response' => 'response', - 'request_succeed' => true, - 'status' => 200, - 'content' => '', - 'links' => [ - 'url1', - 'url2', - ], - 'jobs' => [], - 'children' => [ - 'children1', - 'children2', - ] - ], - ] ]; diff --git a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php index 7aba722482..839a8ba478 100644 --- a/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php +++ b/tests/Unit/inc/Engine/Preload/Controller/ClearCache/partialClean.php @@ -24,7 +24,6 @@ protected function setUp(): void ->shouldAllowMockingProtectedMethods(); } - /** * @dataProvider configTestData */ @@ -34,7 +33,6 @@ public function testShouldDoAsExpected($config, $expected) { $this->controller->expects()->is_excluded_by_filter($url)->andReturn($config['is_excluded']); $this->controller->shouldReceive('is_excluded_by_filter')->with($url)->andReturn($config['is_excluded_by_filter']); } - if(! $config['is_excluded']) { if(! $config['is_excluded_by_filter']) { $this->query->expects(self::atLeastOnce())->method('create_or_update')->withConsecutive(...$expected['urls']); diff --git a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 4f949af1dd..e13314061d 100644 --- a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -72,7 +72,6 @@ protected function configureParseSitemap($config) { ->andReturn( $config['children'] ); foreach ( $config['links'] as $index => $link ) { - $this->controller->expects()->is_excluded_by_filter( $link ) ->once() ->andReturn( $config['is_excluded'] ); From aaf38fdb1c950176f4849ac2d787d081e6147411 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:18:05 +0100 Subject: [PATCH 21/31] Updated tests --- .../inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php | 2 +- .../inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 9fbf61c83e..6442b49aa6 100644 --- a/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Fixtures/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -56,5 +56,5 @@ 'children2', ] ], - ], + ] ]; diff --git a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index e13314061d..2ef7500bee 100644 --- a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -73,8 +73,8 @@ protected function configureParseSitemap($config) { foreach ( $config['links'] as $index => $link ) { $this->controller->expects()->is_excluded_by_filter( $link ) - ->once() - ->andReturn( $config['is_excluded'] ); + ->once() + ->andReturn( $config['is_excluded'] ); if ( ! $config['is_excluded'] ) { $this->query->expects( self::any() )->method( 'create_or_nothing' ) From c4692a9b4735c768029b613cbf7213b6a7397565 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:19:58 +0100 Subject: [PATCH 22/31] Updated test --- .../inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php index 2ef7500bee..e13314061d 100644 --- a/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php +++ b/tests/Unit/inc/Engine/Preload/Frontend/FetchSitemap/parseSitemap.php @@ -73,8 +73,8 @@ protected function configureParseSitemap($config) { foreach ( $config['links'] as $index => $link ) { $this->controller->expects()->is_excluded_by_filter( $link ) - ->once() - ->andReturn( $config['is_excluded'] ); + ->once() + ->andReturn( $config['is_excluded'] ); if ( ! $config['is_excluded'] ) { $this->query->expects( self::any() )->method( 'create_or_nothing' ) From fec069cd8c97c3b5548e7696ff3734fc5b285411 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:36:13 +0100 Subject: [PATCH 23/31] Fixed failing test --- inc/Engine/Preload/Subscriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index 51631f64cc..93379e0e92 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -483,6 +483,7 @@ public function remove_private_post( string $new_status, string $old_status, $po */ public function exclude_private_post_url( $regexes ) : array { static $private_urls; + $private_post_urls = []; if ( isset( $private_urls ) ) { return $private_urls; From c29bec81aa2ee88bf6eeb219db4afa6761e54a58 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Jun 2023 19:36:55 +0100 Subject: [PATCH 24/31] Fixed failing test --- inc/Engine/Preload/Subscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index 93379e0e92..f4d8e7d8a4 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -483,7 +483,6 @@ public function remove_private_post( string $new_status, string $old_status, $po */ public function exclude_private_post_url( $regexes ) : array { static $private_urls; - $private_post_urls = []; if ( isset( $private_urls ) ) { return $private_urls; @@ -500,6 +499,7 @@ public function exclude_private_post_url( $regexes ) : array { return $regexes; } + $private_post_urls = []; foreach ( $query->posts as $post ) { // Temporarily cast publish status to get pretty url. $post->post_status = 'publish'; From 386396201ab3bf5599fd58037aeee14567dba65b Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 09:51:59 +0100 Subject: [PATCH 25/31] Fixed failing integration test --- .../DynamicLists/Subscriber/addPreloadExclusions.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Integration/inc/Engine/Optimization/DynamicLists/Subscriber/addPreloadExclusions.php b/tests/Integration/inc/Engine/Optimization/DynamicLists/Subscriber/addPreloadExclusions.php index f160244f6b..811f8e9c30 100644 --- a/tests/Integration/inc/Engine/Optimization/DynamicLists/Subscriber/addPreloadExclusions.php +++ b/tests/Integration/inc/Engine/Optimization/DynamicLists/Subscriber/addPreloadExclusions.php @@ -10,9 +10,18 @@ * @group DynamicLists */ class Test_AddPreloadExclusions extends TestCase { + + public function set_up() { + parent::set_up(); + + $this->unregisterAllCallbacksExcept( 'rocket_preload_exclude_urls', 'add_preload_exclusions', 10 ); + } + public function tear_down() { delete_transient( 'wpr_dynamic_lists' ); + $this->restoreWpFilter( 'rocket_preload_exclude_urls' ); + parent::tear_down(); } From 15ef5b3ec53875ff8e1a8f7656679f8b79bd8dd8 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 12:23:25 +0100 Subject: [PATCH 26/31] Updated callback method --- inc/Engine/Preload/Subscriber.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index f4d8e7d8a4..00455aba9b 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -120,7 +120,7 @@ public static function get_subscribed_events() { 'rocket_preload_exclude_urls' => [ [ 'add_preload_excluded_uri' ], [ 'add_cache_reject_uri_to_excluded' ], - [ 'exclude_private_post_url' ], + [ 'exclude_private_post_uri' ], ], 'rocket_rucss_after_clearing_failed_url' => [ 'clean_urls', 20 ], 'transition_post_status' => [ 'remove_private_post', 10, 3 ], @@ -481,7 +481,7 @@ public function remove_private_post( string $new_status, string $old_status, $po * @param array $regexes regexes containing excluded uris. * @return array */ - public function exclude_private_post_url( $regexes ) : array { + public function exclude_private_post_uri( $regexes ) : array { static $private_urls; if ( isset( $private_urls ) ) { From 1235cf39f8d7b5a059f231bfd37d52f889766bf1 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 20:19:47 +0100 Subject: [PATCH 27/31] Updated method --- inc/Engine/Preload/Subscriber.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Preload/Subscriber.php b/inc/Engine/Preload/Subscriber.php index 00455aba9b..bce6e74640 100644 --- a/inc/Engine/Preload/Subscriber.php +++ b/inc/Engine/Preload/Subscriber.php @@ -484,6 +484,10 @@ public function remove_private_post( string $new_status, string $old_status, $po public function exclude_private_post_uri( $regexes ) : array { static $private_urls; + if ( ! is_array( $regexes ) ) { + $regexes = (array) $regexes; + } + if ( isset( $private_urls ) ) { return $private_urls; } @@ -495,7 +499,7 @@ public function exclude_private_post_uri( $regexes ) : array { ]; $query = new \WP_Query( $arg ); - if ( empty( $query ) ) { + if ( ! $query->have_posts() ) { return $regexes; } From e5114559fae688f7d5de891bb48d4a3c43b92c4e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 20:20:24 +0100 Subject: [PATCH 28/31] Added tests --- .../Subscriber/excludePrivatePostUri.php | 37 ++++++++++ .../Subscriber/excludePrivatePostUri.php | 48 +++++++++++++ .../Subscriber/excludePrivatePostUri.php | 72 +++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 tests/Fixtures/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php create mode 100644 tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php create mode 100644 tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php diff --git a/tests/Fixtures/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php b/tests/Fixtures/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php new file mode 100644 index 0000000000..b2e2cbe4c6 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php @@ -0,0 +1,37 @@ + [ + 'config' => [ + 'regex' => [ + '/page/\d+', + ], + 'have_posts' => false, + 'posts' => [], + ], + 'expected' => [ + '/page/\d+', + ], + ], + 'testShouldReturnExpectedRegex' => [ + 'config' => [ + 'regex' => [ + '/page/\d+', + ], + 'have_posts' => true, + 'posts' => [ + (object) [ + 'ID' => 2, + 'post_status' => 'private', + ], + ], + 'get_permalink' => [ + 'http://example.org/test-4/', + ], + ], + 'expected' => [ + '/page/\d+', + 'http://example.org/test-4/', + ], + ], +]; \ No newline at end of file diff --git a/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php b/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php new file mode 100644 index 0000000000..c74719372e --- /dev/null +++ b/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php @@ -0,0 +1,48 @@ +set_permalink_structure( "/%postname%/" ); + parent::set_up(); + + $this->unregisterAllCallbacksExcept( 'rocket_preload_exclude_urls', 'exclude_private_post_uri', 10 ); + } + + public function tear_down() { + $this->restoreWpFilter( 'rocket_preload_exclude_urls' ); + + parent::tear_down(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldDoAsExpected( $config, $expected ) { + if ( $config['have_posts'] ) { + wp_insert_post( [ 'post_title' => 'test 4', 'post_status' => 'private' ] ); + } + + $this->assertSame( $expected, apply_filters( 'rocket_preload_exclude_urls', $config['regex'] ) ); + } +} diff --git a/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php b/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php new file mode 100644 index 0000000000..cab9ddc413 --- /dev/null +++ b/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php @@ -0,0 +1,72 @@ +options = Mockery::mock(Options_Data::class); + $this->controller = Mockery::mock(LoadInitialSitemap::class); + $this->query = $this->createMock(Cache::class); + $this->activation = Mockery::mock(Activation::class); + $this->mobile_detect = Mockery::mock(WP_Rocket_Mobile_Detect::class); + $this->clear_cache = Mockery::mock(ClearCache::class); + $this->queue = Mockery::mock(Queue::class); + $this->subscriber = new Subscriber($this->options, $this->controller, $this->query, $this->activation, $this->mobile_detect, $this->clear_cache, $this->queue ); + } + + /** + * @dataProvider configTestData + */ + public function testShouldDoAsExpected( $config, $expected) { + WP_Query::$have_posts = $config['have_posts']; + WP_Query::$set_posts = $config['posts']; + + if ( ! $config['have_posts'] ) { + Functions\expect( 'get_permalink' )->never(); + } + else { + Functions\expect( 'get_permalink' ) + ->twice() + ->andReturnValues( $config['get_permalink'] ); + } + + $this->assertSame( $expected, $this->subscriber->exclude_private_post_uri( $config['regex'] ) ); + } +} From 7048d8cf256ecb6e97a6502559b9125b6b9dbd37 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 20:23:15 +0100 Subject: [PATCH 29/31] Updated test --- .../inc/Engine/Preload/Subscriber/excludePrivatePostUri.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php b/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php index cab9ddc413..68e01627ee 100644 --- a/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php +++ b/tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php @@ -63,7 +63,7 @@ public function testShouldDoAsExpected( $config, $expected) { } else { Functions\expect( 'get_permalink' ) - ->twice() + ->once() ->andReturnValues( $config['get_permalink'] ); } From 6762e10104ba903b68e34376f3a88f75f1439678 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 23 Jun 2023 20:23:43 +0100 Subject: [PATCH 30/31] Added minimum mock for WP_Query class --- tests/Fixtures/WP_Query.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/Fixtures/WP_Query.php diff --git a/tests/Fixtures/WP_Query.php b/tests/Fixtures/WP_Query.php new file mode 100644 index 0000000000..335b53bdbc --- /dev/null +++ b/tests/Fixtures/WP_Query.php @@ -0,0 +1,22 @@ +posts(); + } + + public function posts() { + $this->posts = self::$set_posts; + } + + public function have_posts() : bool { + return self::$have_posts; + } + } +} \ No newline at end of file From 02baaf80638bb67719d55e248e43983d60161772 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Mon, 26 Jun 2023 10:22:05 +0100 Subject: [PATCH 31/31] Adjust test --- .../inc/Engine/Preload/Subscriber/excludePrivatePostUri.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php b/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php index c74719372e..ba421be108 100644 --- a/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php +++ b/tests/Integration/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php @@ -23,8 +23,8 @@ public static function tear_down_after_class() } public function set_up() { + parent::set_up(); $this->set_permalink_structure( "/%postname%/" ); - parent::set_up(); $this->unregisterAllCallbacksExcept( 'rocket_preload_exclude_urls', 'exclude_private_post_uri', 10 ); }