Skip to content

Commit

Permalink
Do not activate the plugin on cron requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 22, 2024
1 parent 463f7d1 commit e4b8970
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
24 changes: 24 additions & 0 deletions .tests/php/integration/Helpers/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public function tearDown(): void {
parent::tearDown();
}

/**
* Test is_xml_rpc().
*/
public function test_is_xml_rpc(): void {
self::assertFalse( Request::is_xml_rpc() );

FunctionMocker::replace(
'defined',
static function ( $constant_name ) {
return 'XMLRPC_REQUEST' === $constant_name;
}
);

FunctionMocker::replace(
'constant',
static function ( $name ) {
return 'XMLRPC_REQUEST' === $name;
}
);

self::assertTrue( Request::is_xml_rpc() );
}


/**
* Test is_rest().
*
Expand Down
26 changes: 0 additions & 26 deletions .tests/php/unit/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@
*/
class MainTest extends HCaptchaTestCase {

/**
* Test init().
*/
public function test_is_xml_rpc(): void {
$mock = Mockery::mock( Main::class )->makePartial();

$mock->shouldAllowMockingProtectedMethods();

self::assertFalse( $mock->is_xml_rpc() );

FunctionMocker::replace(
'defined',
static function ( $constant_name ) {
return 'XMLRPC_REQUEST' === $constant_name;
}
);

FunctionMocker::replace(
'constant',
static function ( $name ) {
return 'XMLRPC_REQUEST' === $name;
}
);

self::assertTrue( $mock->is_xml_rpc() );
}
/**
* Test declare_wc_compatibility().
*
Expand Down
34 changes: 23 additions & 11 deletions src/php/Helpers/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ class Request {
* @return bool
*/
public static function is_frontend(): bool {
return ! ( self::is_cli() || is_admin() || wp_doing_ajax() || self::is_wc_ajax() || self::is_rest() );
return ! (
self::is_xml_rpc() || self::is_cli() || self::is_wc_ajax() || is_admin() ||
wp_doing_ajax() || wp_doing_cron() || self::is_rest()
);
}

/**
* Check if it is the xml-rpc request.
*
* @return bool
*/
public static function is_xml_rpc(): bool {
return defined( 'XMLRPC_REQUEST' ) && constant( 'XMLRPC_REQUEST' );
}

/**
Expand All @@ -32,6 +44,16 @@ public static function is_cli(): bool {
return defined( 'WP_CLI' ) && constant( 'WP_CLI' );
}

/**
* Check if it is a WooCommerce AJAX request.
*
* @return bool
*/
public static function is_wc_ajax(): bool {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset( $_GET['wc-ajax'] );
}

/**
* Checks if the current request is a WP REST API request.
*
Expand Down Expand Up @@ -91,14 +113,4 @@ public static function is_post(): bool {

return 'POST' === $request_method;
}

/**
* Check if it is a WooCommerce AJAX request.
*
* @return bool
*/
public static function is_wc_ajax(): bool {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset( $_GET['wc-ajax'] );
}
}
12 changes: 2 additions & 10 deletions src/php/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use HCaptcha\DownloadManager\DownloadManager;
use HCaptcha\ElementorPro\HCaptchaHandler;
use HCaptcha\Helpers\HCaptcha;
use HCaptcha\Helpers\Request;
use HCaptcha\Jetpack\JetpackForm;
use HCaptcha\Migrations\Migrations;
use HCaptcha\NF\NF;
Expand Down Expand Up @@ -122,7 +123,7 @@ class Main {
* @return void
*/
public function init(): void {
if ( $this->is_xml_rpc() ) {
if ( Request::is_xml_rpc() || wp_doing_cron() ) {
return;
}

Expand Down Expand Up @@ -1359,13 +1360,4 @@ public function load_textdomain(): void {
dirname( plugin_basename( HCAPTCHA_FILE ) ) . '/languages/'
);
}

/**
* Check if it is the xml-rpc request.
*
* @return bool
*/
protected function is_xml_rpc(): bool {
return defined( 'XMLRPC_REQUEST' ) && constant( 'XMLRPC_REQUEST' );
}
}

0 comments on commit e4b8970

Please sign in to comment.