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

Closes #5885: Preload: Don't try to preload private pages #5920

Merged
merged 34 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8609ad2
Updated to not cache private pages
jeawhanlee May 12, 2023
549660f
Updated tests
jeawhanlee May 12, 2023
b74ea74
Update test case
jeawhanlee May 12, 2023
c6574d7
Return early when not a post
jeawhanlee May 16, 2023
46e18bf
Enhance code readability
jeawhanlee May 16, 2023
9a0f29d
Remove post changed to private from cache table
jeawhanlee May 18, 2023
7273824
Updated method
jeawhanlee May 18, 2023
09ff7ac
Check for private post
jeawhanlee Jun 1, 2023
1006e72
Updated tests
jeawhanlee Jun 1, 2023
dcd141d
Check for private post
jeawhanlee Jun 1, 2023
27cbaf9
Merge branch 'develop' into enhancement/5885-do-not-preload-private-p…
jeawhanlee Jun 2, 2023
19be1fc
Change method logic
jeawhanlee Jun 7, 2023
1872f1f
Add trailing slash
jeawhanlee Jun 7, 2023
4d232ab
Cast publish status
jeawhanlee Jun 7, 2023
144ba17
Updated tests
jeawhanlee Jun 7, 2023
504369c
Merge branch 'develop' into enhancement/5885-do-not-preload-private-p…
jeawhanlee Jun 13, 2023
6c38e0d
Updated to get private post of any post type
jeawhanlee Jun 16, 2023
647a388
Get all private posts
jeawhanlee Jun 22, 2023
64b1d4a
Removed call to is_private method
jeawhanlee Jun 22, 2023
98a4dfd
Updated tests
jeawhanlee Jun 22, 2023
f4ddde1
Added new filter callback to remove private urls
jeawhanlee Jun 22, 2023
5d6a845
Updated tests
jeawhanlee Jun 22, 2023
aaf38fd
Updated tests
jeawhanlee Jun 22, 2023
c4692a9
Updated test
jeawhanlee Jun 22, 2023
fec069c
Fixed failing test
jeawhanlee Jun 22, 2023
c29bec8
Fixed failing test
jeawhanlee Jun 22, 2023
3863962
Fixed failing integration test
jeawhanlee Jun 23, 2023
15ef5b3
Updated callback method
jeawhanlee Jun 23, 2023
1235cf3
Updated method
jeawhanlee Jun 23, 2023
e511455
Added tests
jeawhanlee Jun 23, 2023
7048d8c
Updated test
jeawhanlee Jun 23, 2023
6762e10
Added minimum mock for WP_Query class
jeawhanlee Jun 23, 2023
02baaf8
Adjust test
jeawhanlee Jun 26, 2023
8826018
Merge branch 'develop' into enhancement/5885-do-not-preload-private-p…
vmanthos Jun 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions inc/Engine/Preload/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ public static function get_subscribed_events() {
'rocket_preload_exclude_urls' => [
[ 'add_preload_excluded_uri' ],
[ 'add_cache_reject_uri_to_excluded' ],
[ 'exclude_private_post_uri' ],
],
'rocket_rucss_after_clearing_failed_url' => [ 'clean_urls', 20 ],
'transition_post_status' => [ 'remove_private_post', 10, 3 ],
];
}

Expand Down Expand Up @@ -451,4 +453,65 @@ 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, $post ) {

if ( $new_status === $old_status ) {
return;
}

if ( 'private' !== $new_status ) {
return;
}

$this->delete_post_preload_cache( $post->ID );
}

/**
* Exclude private urls.
*
* @param array $regexes regexes containing excluded uris.
* @return array
*/
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;
}

$arg = [
'post_type' => 'any',
'post_status' => 'private',
'posts_per_page' => -1,
];
$query = new \WP_Query( $arg );

if ( ! $query->have_posts() ) {
return $regexes;
}

$private_post_urls = [];
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;
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/WP_Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

if ( ! class_exists( 'WP_Query' ) ) {
class WP_Query {
public static $set_posts = [];
public $posts = [];

public static $have_posts = false;

public function __construct( array $arg ) {
$this->posts();
}

public function posts() {
$this->posts = self::$set_posts;
}

public function have_posts() : bool {
return self::$have_posts;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

return [
'testShouldReturnOriginRegexIfNoPrivatePost' => [
'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/',
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\Engine\Preload\Subscriber;

use WP_Rocket\Tests\Integration\AdminTestCase;

/**
* @covers \WP_Rocket\Engine\Preload\Subscriber::exclude_private_post_uri
* @group Preload
*/
class Test_ExcludePrivatePostUri extends AdminTestCase
{
public static function set_up_before_class()
{
parent::set_up_before_class();
self::installFresh();
}

public static function tear_down_after_class()
{
self::uninstallAll();
parent::tear_down_after_class();
}

public function set_up() {
parent::set_up();
$this->set_permalink_structure( "/%postname%/" );

$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'] ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ 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' )
->withConsecutive( ...$config['jobs'] )
Expand Down
72 changes: 72 additions & 0 deletions tests/Unit/inc/Engine/Preload/Subscriber/excludePrivatePostUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace WP_Rocket\Tests\Unit\inc\Engine\Preload\Subscriber;

use Mockery;
use Brain\Monkey\Functions;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Preload\Activation\Activation;
use WP_Rocket\Engine\Preload\Controller\ClearCache;
use WP_Rocket\Engine\Preload\Controller\LoadInitialSitemap;
use WP_Rocket\Engine\Preload\Controller\Queue;
use WP_Rocket\Engine\Preload\Database\Queries\Cache;
use WP_Rocket\Engine\Preload\Subscriber;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket_Mobile_Detect;
use WP_Query;

/**
* @covers \WP_Rocket\Engine\Preload\Subscriber::exclude_private_post_uri
*
* @group Preload
*/
class Test_ExcludePrivatePostUri extends TestCase
{
protected $subscriber;

protected $options;
protected $controller;
protected $query;
protected $activation;
protected $mobile_detect;
protected $clear_cache;
protected $queue;
protected $wp_query;

public static function setUpBeforeClass() : void {
parent::setUpBeforeClass();

require_once WP_ROCKET_TESTS_FIXTURES_DIR . '/WP_Query.php';
}

protected function setUp(): void
{
parent::setUp();
$this->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' )
->once()
->andReturnValues( $config['get_permalink'] );
}

$this->assertSame( $expected, $this->subscriber->exclude_private_post_uri( $config['regex'] ) );
}
}