| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
use const OPENSSL_VERSION_TEXT; |
| 7 |
use const PHP_VERSION; |
| 8 |
use function sprintf; |
| 9 |
use function var_dump; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class PayplugGatewayRequirements { |
| 16 |
|
| 17 |
const PHP_MIN = '5.6'; |
| 18 |
const OPENSSL_MIN = 268439567; |
| 19 |
const OPENSSL_MIN_TEXT = 'OpenSSL 1.0.1 14 Mar 2012'; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var PayplugGateway |
| 23 |
*/ |
| 24 |
private $gateway; |
| 25 |
|
| 26 |
/** |
| 27 |
* PayplugGatewayRequirements constructor. |
| 28 |
* |
| 29 |
* @param PayplugGateway $gateway |
| 30 |
*/ |
| 31 |
public function __construct( PayplugGateway $gateway ) { |
| 32 |
$this->gateway = $gateway; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if all gateway requirements are fulfilled. |
| 37 |
* |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public function satisfy_requirements() { |
| 41 |
return $this->valid_php() |
| 42 |
&& $this->valid_curl() |
| 43 |
&& $this->valid_openssl() |
| 44 |
&& $this->valid_currency() |
| 45 |
&& $this->valid_account(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function curl_requirement() { |
| 52 |
return ( $this->valid_curl() ) |
| 53 |
? '<p class="success">' . __( 'The PHP cURL extension is installed and activated on your server.', 'payplug' ) . '</p>' |
| 54 |
: '<p class="failed">' . __( 'The PHP cURL extension must be installed and activated on your server.', 'payplug' ) . '</p>'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function php_requirement() { |
| 61 |
return ( $this->valid_php() ) |
| 62 |
? '<p class="success">' . __( 'The PHP version on your server is valid.', 'payplug' ) . '</p>' |
| 63 |
/* translators: %s: minimum required PHP version */ |
| 64 |
: '<p class="failed">' . sprintf( __( 'The PHP version on your server is not supported. Your server must run PHP %s or greater.', 'payplug' ), self::PHP_MIN ) . '</p>'; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function openssl_requirement() { |
| 71 |
return ( $this->valid_openssl() ) |
| 72 |
? '<p class="success">' . __( 'OpenSSL is up to date.', 'payplug' ) . '</p>' |
| 73 |
/* translators: %s: minimum required OpenSSL version */ |
| 74 |
: '<p class="failed">' . sprintf( __( 'OpenSSL is not up to date. Please update to OpenSSL %s or later.', 'payplug' ), self::OPENSSL_MIN . ' ( ' . self::OPENSSL_MIN_TEXT . ' )' ) . '</p>'; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function currency_requirement() { |
| 81 |
return ( $this->valid_currency() ) |
| 82 |
? '<p class="success">' . __( 'Your shop currency has been set up with Euro.', 'payplug' ) . '</p>' |
| 83 |
: '<p class="failed">' . __( 'Your shop currency must be set up with Euro.', 'payplug' ) . '</p>'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function account_requirement() { |
| 90 |
return ( $this->valid_account() ) |
| 91 |
? '<p class="success">' . __( 'You are logged in with your PayPlug account.', 'payplug' ) . '</p>' |
| 92 |
: '<p class="failed">' . __( 'You must be logged in with your PayPlug account.', 'payplug' ) . '</p>'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if CURL is available and support SSL |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function valid_curl() { |
| 101 |
if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Also Check for SSL support |
| 106 |
$curl_version = curl_version(); |
| 107 |
if ( ! ( CURL_VERSION_SSL & $curl_version['features'] ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Check if PHP version is equal or above the minimum supported. |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public function valid_php() { |
| 120 |
return version_compare( PHP_VERSION, self::PHP_MIN, '>=' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if OPENSSL version is equal or above the minimum supported. |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function valid_openssl() { |
| 129 |
return OPENSSL_VERSION_NUMBER >= self::OPENSSL_MIN; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if the shop currency is Euro. |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public function valid_currency() { |
| 138 |
return 'EUR' === get_woocommerce_currency(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if the user is logged in. |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public function valid_account() { |
| 147 |
return $this->gateway->user_logged_in(); |
| 148 |
} |
| 149 |
} |