-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9808 from google/enhancement/9768-health-check-cron
- Loading branch information
Showing
6 changed files
with
192 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
includes/Core/Tags/First_Party_Mode/First_Party_Mode_Cron.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Class Google\Site_Kit\Core\Tags\First_Party_Mode\First_Party_Mode_Cron | ||
* | ||
* @package Google\Site_Kit\Core\Tags\First_Party_Mode | ||
* @copyright 2024 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Core\Tags\First_Party_Mode; | ||
|
||
/** | ||
* Class to manage First Party Mode cron tasks. | ||
* | ||
* @since n.e.x.t | ||
* @access private | ||
* @ignore | ||
*/ | ||
class First_Party_Mode_Cron { | ||
|
||
const CRON_ACTION = 'googlesitekit_cron_first_party_mode_healthchecks'; | ||
|
||
/** | ||
* Cron callback reference. | ||
* | ||
* @var callable | ||
*/ | ||
private $cron_callback; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param callable $callback Function to call on the cron action. | ||
*/ | ||
public function __construct( callable $callback ) { | ||
$this->cron_callback = $callback; | ||
} | ||
|
||
/** | ||
* Registers functionality through WordPress hooks. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function register() { | ||
add_action( self::CRON_ACTION, $this->cron_callback ); | ||
} | ||
|
||
/** | ||
* Schedules cron if not already set. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function maybe_schedule_cron() { | ||
if ( ! wp_next_scheduled( self::CRON_ACTION ) && ! wp_installing() ) { | ||
wp_schedule_event( time(), 'hourly', self::CRON_ACTION ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/phpunit/integration/Core/Tags/First_Party_Mode/First_Party_Mode_CronTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* First_Party_Mode_CronTest | ||
* | ||
* @package Google\Site_Kit\Tests\Core\Tags\First_Party_Mode | ||
* @copyright 2024 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Tests\Core\Tags\First_Party_Mode; | ||
|
||
use Google\Site_Kit\Core\Tags\First_Party_Mode\First_Party_Mode_Cron; | ||
use Google\Site_Kit\Tests\MethodSpy; | ||
use Google\Site_Kit\Tests\TestCase; | ||
|
||
class First_Party_Mode_CronTest extends TestCase { | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
remove_all_actions( First_Party_Mode_Cron::CRON_ACTION ); | ||
} | ||
|
||
public function test_register() { | ||
$cron = new First_Party_Mode_Cron( '__return_true' ); | ||
$this->assertFalse( has_action( First_Party_Mode_Cron::CRON_ACTION ) ); | ||
|
||
$cron->register(); | ||
|
||
$this->assertTrue( has_action( First_Party_Mode_Cron::CRON_ACTION ) ); | ||
} | ||
|
||
public function test_register__given_callable() { | ||
$spy = new MethodSpy(); | ||
$cron = new First_Party_Mode_Cron( array( $spy, 'func' ) ); | ||
$cron->register(); | ||
$this->assertTrue( empty( $spy->invocations['func'] ) ); | ||
|
||
do_action( First_Party_Mode_Cron::CRON_ACTION ); | ||
|
||
$this->assertCount( 1, $spy->invocations['func'] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters