| 1 |
<?php |
| 2 |
/** |
| 3 |
* Configures WP Proxies for the Symfony HTTP Client. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Preload; |
| 11 |
|
| 12 |
use WP_HTTP_Proxy; |
| 13 |
|
| 14 |
/** |
| 15 |
* Configures WP Proxies for the Symfony HTTP Client. |
| 16 |
* |
| 17 |
* @package SolidWP\Performance |
| 18 |
*/ |
| 19 |
final class Proxy { |
| 20 |
|
| 21 |
/** |
| 22 |
* @var WP_HTTP_Proxy |
| 23 |
*/ |
| 24 |
private WP_HTTP_Proxy $proxy; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param WP_HTTP_Proxy $proxy The WP Proxy object. |
| 28 |
*/ |
| 29 |
public function __construct( WP_HTTP_Proxy $proxy ) { |
| 30 |
$this->proxy = $proxy; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a configured WP Proxy URL. |
| 35 |
* |
| 36 |
* @example username:password@192.168.12.14:8080 |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function url(): string { |
| 41 |
if ( ! $this->proxy->is_enabled() ) { |
| 42 |
return ''; |
| 43 |
} |
| 44 |
|
| 45 |
$parts = []; |
| 46 |
|
| 47 |
if ( $this->proxy->use_authentication() ) { |
| 48 |
$parts[] = $this->proxy->authentication(); |
| 49 |
$parts[] = '@'; |
| 50 |
} |
| 51 |
|
| 52 |
$parts[] = $this->proxy->host(); |
| 53 |
|
| 54 |
$port = $this->proxy->port(); |
| 55 |
|
| 56 |
if ( strlen( $port ) ) { |
| 57 |
$parts[] = ':' . $port; |
| 58 |
} |
| 59 |
|
| 60 |
return implode( '', $parts ); |
| 61 |
} |
| 62 |
} |
| 63 |
|