| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cloudflare detection for WCPOS settings. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
/** |
| 11 |
* Detects Cloudflare proxy and official plugin status. |
| 12 |
*/ |
| 13 |
class Cloudflare_Detector { |
| 14 |
/** |
| 15 |
* Detect Cloudflare from the current request or injected inputs. |
| 16 |
* |
| 17 |
* @param array|null $server Server values, defaults to $_SERVER. |
| 18 |
* @param array|null $active_plugins Active plugin basenames, defaults to WordPress detection. |
| 19 |
* @return array{proxied:bool,plugin_active:bool} Cloudflare status. |
| 20 |
*/ |
| 21 |
public function detect( ?array $server = null, ?array $active_plugins = null ): array { |
| 22 |
$server = $server ?? $_SERVER; |
| 23 |
if ( null !== $active_plugins ) { |
| 24 |
$plugin_active = \in_array( 'cloudflare/cloudflare.php', $active_plugins, true ); |
| 25 |
} else { |
| 26 |
if ( ! \function_exists( 'is_plugin_active' ) ) { |
| 27 |
$file = ABSPATH . 'wp-admin/includes/plugin.php'; |
| 28 |
if ( is_readable( $file ) ) { |
| 29 |
require_once $file; |
| 30 |
} |
| 31 |
} |
| 32 |
$plugin_active = \function_exists( 'is_plugin_active' ) && is_plugin_active( 'cloudflare/cloudflare.php' ); |
| 33 |
} |
| 34 |
|
| 35 |
return array( |
| 36 |
'proxied' => ! empty( $server['HTTP_CF_RAY'] ), |
| 37 |
'plugin_active' => $plugin_active, |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|