Skip to content

Commit d667357

Browse files
committed
Run only one request to CH Apis on single WP request
1 parent e752190 commit d667357

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

admin/class-crowdhandler-admin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,12 @@ public function handleIndexFilesOverrides()
310310
311311
require_once \$config['plugin_path'] . 'vendor/autoload.php';
312312
313-
\$ch = new CrowdHandlerGateKeeper(\$config['options']['crowdhandler_settings_field_public_key']);
314-
\$ch->checkRequest();
313+
\$crowdHandlerGateKeeper = new CrowdHandlerGateKeeper(\$config['options']);
314+
\$crowdHandlerGateKeeper->checkRequest();
315315
316316
include 'wp-index.php';
317317
318-
\$ch->recordPerformance(http_response_code());
318+
\$crowdHandlerGateKeeper->recordPerformance(http_response_code());
319319
320320
PHP
321321
);

includes/class-crowdhandler-gatekeeper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ class CrowdHandlerGateKeeper
1515
*/
1616
private $options;
1717

18+
/**
19+
* @var bool
20+
*/
21+
private $requestChecked = false;
22+
23+
/**
24+
* @var bool
25+
*/
26+
private $requestPerformanceRecorded = false;
27+
1828
/**
1929
* @param array $options
2030
*/
@@ -29,7 +39,7 @@ public function __construct($options = null)
2939

3040
public function checkRequest()
3141
{
32-
if (!$this->isEnabled()) {
42+
if ((function_exists('is_admin') && is_admin()) || !$this->isEnabled() || $this->requestChecked) {
3343
return $this;
3444
}
3545

@@ -43,12 +53,14 @@ public function checkRequest()
4353
$this->gateKeeper->redirectIfNotPromoted();
4454
$this->gateKeeper->setCookie();
4555

56+
$this->requestChecked = true;
57+
4658
return $this;
4759
}
4860

4961
public function recordPerformance($code)
5062
{
51-
if (!$this->isEnabled()) {
63+
if ((function_exists('is_admin') && is_admin()) || !$this->isEnabled() || $this->requestPerformanceRecorded) {
5264
return false;
5365
}
5466

@@ -57,6 +69,7 @@ public function recordPerformance($code)
5769
}
5870

5971
try {
72+
$this->requestPerformanceRecorded = true;
6073
$this->gateKeeper->recordPerformance($code);
6174
} catch (\Exception $e) {
6275
return false;

includes/class-crowdhandler.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ private function define_public_hooks()
165165

166166
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
167167
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
168-
$this->loader->add_action('muplugins_loaded', $plugin_public, 'checkRequest');
169-
$this->loader->add_action('plugins_loaded', $plugin_public, 'checkRequest');
170-
$this->loader->add_action('shutdown', $plugin_public, 'recordPerformance');
168+
169+
global $crowdHandlerGateKeeper;
170+
if (!isset($crowdHandlerGateKeeper) || !$crowdHandlerGateKeeper instanceof CrowdHandlerGateKeeper) {
171+
$this->loader->add_action('muplugins_loaded', $plugin_public, 'checkRequest');
172+
$this->loader->add_action('plugins_loaded', $plugin_public, 'checkRequest');
173+
$this->loader->add_action('shutdown', $plugin_public, 'recordPerformance');
174+
}
171175
}
172176

173177
/**

0 commit comments

Comments
 (0)