Skip to content

Commit

Permalink
Update scheduling mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsoo committed Nov 2, 2023
1 parent 2545829 commit 375e767
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 54 deletions.
3 changes: 0 additions & 3 deletions config/dependency-injection/loader-pass.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ private function process_definition( Definition $definition, Definition $loader_

$definition->setPublic( true );
}
if ( \strpos( $path, 'src' . \DIRECTORY_SEPARATOR . 'indexables' ) ) {
$definition->setPublic( false );
}
} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Catch all for non-existing classes.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Yoast\WP\SEO\Indexables\Application;

use Yoast\WP\SEO\Helpers\Options_Helper;

/**
* The Verification_Cron_Schedule_Handler class.
*/
Expand All @@ -24,13 +26,21 @@ class Verification_Cron_Schedule_Handler {
*/
private $cron_verification_gate;

/**
* The Options_Helper instance.
*
* @var Options_Helper
*/
private $options_helper;

/**

Check failure on line 36 in src/indexables/application/verification-cron-schedule-handler.php

View workflow job for this annotation

GitHub Actions / Check code style

Doc comment for parameter "$options_helper" missing
* The constructor.
*
* @param Cron_Verification_Gate $cron_verification_gate The cron verification gate.
*/
public function __construct( Cron_Verification_Gate $cron_verification_gate ) {
public function __construct( Cron_Verification_Gate $cron_verification_gate, Options_Helper $options_helper ) {
$this->cron_verification_gate = $cron_verification_gate;
$this->options_helper = $options_helper;
}

/**
Expand All @@ -39,6 +49,9 @@ public function __construct( Cron_Verification_Gate $cron_verification_gate ) {
* @return void
*/
public function schedule_indexable_verification(): void {
if ( $this->options_helper->get( 'activation_redirect_timestamp_free', 0 ) === 0 ) {
return;
}

if ( $this->cron_verification_gate->should_verify_on_cron() && ! \wp_next_scheduled( self::INDEXABLE_VERIFY_POST_INDEXABLES_NAME ) ) {
\wp_schedule_event( ( \time() + \HOUR_IN_SECONDS ), 'fifteen_minutes', self::INDEXABLE_VERIFY_POST_INDEXABLES_NAME );
Expand Down
4 changes: 2 additions & 2 deletions src/indexables/domain/abstract-indexables-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ abstract class Abstract_Indexables_Command {
/**
* The constructor.
*
* @param int $batch_size The batch size.
* @param int $last_batch The last batch count.
* @param int $batch_size The batch size.
* @param int $last_batch The last batch count.
*/
public function __construct( int $batch_size, int $last_batch ) {
$this->last_batch_count = new Last_Batch_Count( $last_batch );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,19 @@ class Schedule_Verification_Cron_Integration implements Integration_Interface {
*/
protected $cron_schedule_handler;

/**
* The options helper.
*
* @var Options_Helper
*/
private $options_helper;

/**
* The constructor.
*
* @param Options_Helper $options_helper The options helper.
* @param Verification_Cron_Schedule_Handler $cron_schedule_handler The cron schedule handler.
*/
public function __construct( Options_Helper $options_helper, Verification_Cron_Schedule_Handler $cron_schedule_handler ) {
$this->options_helper = $options_helper;
public function __construct( Verification_Cron_Schedule_Handler $cron_schedule_handler ) {
$this->cron_schedule_handler = $cron_schedule_handler;
}

/**
* Registers the action with WordPress.
*/
public function register_hooks() {
if ( $this->options_helper->get( 'first_time_install', true ) === false ) {
\add_action( 'wpseo_activate', [ $this->cron_schedule_handler, 'schedule_indexable_verification' ] );
}
\add_action( 'wpseo_activate', [ $this->cron_schedule_handler, 'schedule_indexable_verification' ] );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,12 @@ public function register_hooks() {
* @return void
*/
public function start_verify_non_timestamped_indexables() {
if ( \wp_doing_cron() || ! $this->cron_verification_gate->should_verify_on_cron() ) {
if ( \wp_doing_cron() && ! $this->cron_verification_gate->should_verify_on_cron() ) {
$this->cron_schedule_handler->unschedule_verify_non_timestamped_indexables_cron();

return;
}

// @todo add filter here.
$batch_size = 10;
$current_batch = $this->cron_batch_handler->get_current_non_timestamped_indexables_batch();
$action = $this->verification_action_handler->get_current_verification_action();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public function register_hooks() {
* @return void
*/
public function start_verify_posts(): void {
if ( \wp_doing_cron() || ! $this->cron_verification_gate->should_verify_on_cron() ) {
if ( \wp_doing_cron() && ! $this->cron_verification_gate->should_verify_on_cron() ) {
$this->cron_schedule_handler->unschedule_verify_post_indexables_cron();

return;
}
$batch_size = 10;

$batch_size = 10;
$last_batch = $this->cron_batch_handler->get_current_post_indexables_batch();

$this->verify_post_indexables_command_handler->handle( new Verify_Post_Indexables_Command( $batch_size, $last_batch ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Brain\Monkey;
use Mockery;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Indexables\Application\Cron_Verification_Gate;
use Yoast\WP\SEO\Indexables\Application\Verification_Cron_Schedule_Handler;
use Yoast\WP\SEO\Tests\Unit\TestCase;
Expand Down Expand Up @@ -40,8 +41,10 @@ protected function setUp(): void {
parent::setUp();

$this->cron_verification_gate = Mockery::mock( Cron_Verification_Gate::class );
$options_helper = Mockery::mock( Options_Helper::class );
$options_helper->expects( 'get' )->with( 'activation_redirect_timestamp_free', 0 )->andReturn( 2 );

$this->instance = new Verification_Cron_Schedule_Handler( $this->cron_verification_gate );
$this->instance = new Verification_Cron_Schedule_Handler( $this->cron_verification_gate, $options_helper );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ public function test_get_conditionals() {
);
}

/**
* Tests the `start_verify_non_timestamped_indexables` while a cron is already running.
*
* @covers ::start_verify_non_timestamped_indexables
*
* @return void
*/
public function test_start_verify_non_timestamped_indexables_doing_cron() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_schedule_handler->expects( 'unschedule_verify_non_timestamped_indexables_cron' )->once();
$this->instance->start_verify_non_timestamped_indexables();
}

/**
* Tests the `start_verify_non_timestamped_indexables` when no cron is running and indexables are enabled.
*
Expand All @@ -146,7 +133,7 @@ public function test_start_verify_non_timestamped_indexables_doing_cron() {
* @return void
*/
public function test_start_verify_non_timestamped_indexables_indexables_disabled() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnFalse();
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_verification_gate->expects( 'should_verify_on_cron' )->andReturnFalse();
$this->cron_schedule_handler->expects( 'unschedule_verify_non_timestamped_indexables_cron' )->once();
$this->instance->start_verify_non_timestamped_indexables();
Expand All @@ -160,7 +147,7 @@ public function test_start_verify_non_timestamped_indexables_indexables_disabled
* @return void
*/
public function test_start_verify_non_timestamped_indexables() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnFalse();
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_verification_gate->expects( 'should_verify_on_cron' )->andReturnTrue();
$this->cron_schedule_handler->expects( 'unschedule_verify_non_timestamped_indexables_cron' )->never();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,6 @@ public function test_get_conditionals() {
);
}

/**
* Tests the `start_verify_posts` while a cron is already running.
*
* @covers ::start_verify_posts
*
* @return void
*/
public function test_start_verify_posts_indexables_doing_cron() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_schedule_handler->expects( 'unschedule_verify_post_indexables_cron' )->once();
$this->instance->start_verify_posts();
}

/**
* Tests the `start_verify_posts` when no cron is running and indexables are enabled.
*
Expand All @@ -139,7 +126,7 @@ public function test_start_verify_posts_indexables_doing_cron() {
* @return void
*/
public function test_start_verify_posts_indexables_indexables_disabled() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnFalse();
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_verification_gate->expects( 'should_verify_on_cron' )->andReturnFalse();
$this->cron_schedule_handler->expects( 'unschedule_verify_post_indexables_cron' )->once();
$this->instance->start_verify_posts();
Expand All @@ -153,7 +140,7 @@ public function test_start_verify_posts_indexables_indexables_disabled() {
* @return void
*/
public function test_start_verify_posts_indexables() {
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnFalse();
Monkey\Functions\expect( 'wp_doing_cron' )->andReturnTrue();
$this->cron_verification_gate->expects( 'should_verify_on_cron' )->andReturnTrue();

$this->verification_cron_batch_handler->expects( 'get_current_post_indexables_batch' )->andReturn( 10 );
Expand Down

0 comments on commit 375e767

Please sign in to comment.