| 1 |
<?php |
| 2 |
/** |
| 3 |
* Square Gateway Compatibility Wrapper. |
| 4 |
* |
| 5 |
* This class serves as a compatibility layer between Charitable and Square SDK, |
| 6 |
* checking PHP version requirements before loading Square components. |
| 7 |
* |
| 8 |
* @package Charitable/Classes |
| 9 |
* @author David Bisset |
| 10 |
* @copyright Copyright (c) 2024, WP Charitable LLC |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
* @since 1.8.7 |
| 13 |
* @version 1.8.7 |
| 14 |
*/ |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
if ( ! class_exists( 'Charitable_Square_Compatibility' ) ) : |
| 22 |
|
| 23 |
/** |
| 24 |
* Charitable_Square_Compatibility |
| 25 |
* |
| 26 |
* @since 1.8.7 |
| 27 |
*/ |
| 28 |
class Charitable_Square_Compatibility { |
| 29 |
|
| 30 |
/** |
| 31 |
* Minimum PHP version required for Square SDK. |
| 32 |
* |
| 33 |
* @since 1.8.7 |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
const MIN_PHP_VERSION = '8.1.0'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Single instance of this class. |
| 41 |
* |
| 42 |
* @since 1.8.7 |
| 43 |
* |
| 44 |
* @var Charitable_Square_Compatibility |
| 45 |
*/ |
| 46 |
private static $instance = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether Square functionality can be loaded. |
| 50 |
* |
| 51 |
* @since 1.8.7 |
| 52 |
* |
| 53 |
* @var bool |
| 54 |
*/ |
| 55 |
private static $is_compatible = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Check if the current PHP version is compatible with Square SDK. |
| 59 |
* |
| 60 |
* @since 1.8.7 |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public static function is_compatible() { |
| 65 |
if ( null === self::$is_compatible ) { |
| 66 |
// First check if our constant is defined (set in platform_check.php). |
| 67 |
if ( defined( 'CHARITABLE_SQUARE_AVAILABLE' ) ) { |
| 68 |
self::$is_compatible = CHARITABLE_SQUARE_AVAILABLE; |
| 69 |
} else { |
| 70 |
// Fallback to direct version comparison. |
| 71 |
self::$is_compatible = version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return apply_filters( 'charitable_square_is_compatible', self::$is_compatible ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Initialize Square functionality if PHP version is compatible. |
| 80 |
* |
| 81 |
* @since 1.8.7 |
| 82 |
* |
| 83 |
* @return bool True if Square was initialized, false otherwise. |
| 84 |
*/ |
| 85 |
public static function init() { |
| 86 |
if ( ! self::is_compatible() ) { |
| 87 |
// Make sure we're loading the admin notice at the right time. |
| 88 |
if ( is_admin() ) { |
| 89 |
// Register the admin notice - make sure this runs after admin is initialized. |
| 90 |
add_action( 'init', array( __CLASS__, 'display_version_notice' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
// Remove Square from available gateways. |
| 94 |
add_filter( 'charitable_payment_gateways', array( __CLASS__, 'remove_square_gateway' ), 20 ); |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
// If WordPress is not fully loaded yet, schedule initialization for later. |
| 100 |
if ( ! did_action( 'plugins_loaded' ) ) { |
| 101 |
add_action( 'plugins_loaded', array( __CLASS__, 'init' ), 10 ); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
// Load Square components. |
| 106 |
self::load_square_components(); |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Display Square compatibility version notice. |
| 112 |
* |
| 113 |
* @since 1.8.7 |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public static function display_version_notice_sitewide() { |
| 118 |
// We already handle this in the init hook with display_version_notice(). |
| 119 |
// This function exists for compatibility but should not duplicate the notice. |
| 120 |
return; // phpcs:ignore |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Load Square components. |
| 125 |
* |
| 126 |
* @since 1.8.7 |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private static function load_square_components() { |
| 131 |
|
| 132 |
// Check if Charitable is fully loaded. |
| 133 |
if ( ! function_exists( 'charitable' ) || ! charitable() ) { |
| 134 |
// Schedule to try again after the 'plugins_loaded' hook. |
| 135 |
add_action( 'plugins_loaded', array( __CLASS__, 'load_square_components' ), 20 ); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$includes_path = charitable()->get_path( 'includes' ); |
| 140 |
|
| 141 |
// Check if required Square classes are available. |
| 142 |
if ( ! class_exists( '\Square\SquareClient' ) ) { |
| 143 |
// Square SDK not loaded, cannot proceed with Square functionality. |
| 144 |
add_action( 'admin_notices', array( __CLASS__, 'display_sdk_missing_notice' ) ); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
require_once $includes_path . 'square/charitable-square-core-functions.php'; |
| 149 |
require_once $includes_path . 'square/helpers/class-charitable-donation-data-mapper.php'; |
| 150 |
require_once $includes_path . 'square/payment/class-charitable-payment-response.php'; |
| 151 |
require_once $includes_path . 'square/gateway/class-charitable-square-webhook-processor.php'; |
| 152 |
require_once $includes_path . 'square/donations/class-charitable-square-donation-log.php'; |
| 153 |
require_once $includes_path . 'square/gateway/class-charitable-square-gateway-processor.php'; |
| 154 |
require_once $includes_path . 'square/gateway/class-charitable-square-subscription-plan.php'; |
| 155 |
require_once $includes_path . 'square/donations/class-charitable-square-recurring-donation-log.php'; |
| 156 |
|
| 157 |
// Additional Square files from class map. |
| 158 |
require_once $includes_path . 'square/admin/class-charitable-square-connect.php'; |
| 159 |
require_once $includes_path . 'square/admin/class-charitable-square-connection.php'; |
| 160 |
require_once $includes_path . 'gateways/class-charitable-gateway-square.php'; |
| 161 |
require_once $includes_path . 'square/class-charitable-square-api.php'; |
| 162 |
require_once $includes_path . 'square/gateway/class-charitable-square-initialization.php'; |
| 163 |
require_once $includes_path . 'square/admin/class-charitable-square-helpers.php'; |
| 164 |
require_once $includes_path . 'square/admin/class-charitable-square-webhookshealthcheck.php'; |
| 165 |
require_once $includes_path . 'square/admin/class-charitable-square-webhooksmanager.php'; |
| 166 |
|
| 167 |
// Load admin files if we're in the admin area. |
| 168 |
if ( is_admin() ) { |
| 169 |
require_once $includes_path . 'square/admin/class-charitable-square-admin.php'; |
| 170 |
require_once $includes_path . 'square/admin/charitable-admin-square-gateway-hooks.php'; |
| 171 |
} |
| 172 |
|
| 173 |
require_once $includes_path . 'square/gateway/charitable-square-gateway-hooks.php'; // 1.8.7 |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Display admin notice about PHP version incompatibility. |
| 178 |
* |
| 179 |
* @since 1.8.7 |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public static function display_version_notice() { |
| 184 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
// Generate an admin notice so the admin knows that the Square gateway is disabled. |
| 189 |
$message = sprintf( |
| 190 |
/* translators: %1$s: Minimum PHP version required, %2$s: Current PHP version, %3$s: Alternative gateway URL, %4$s: Documentation URL */ |
| 191 |
__( 'Charitable: The Square payment gateway requires PHP %1$s or higher. Your site is running PHP %2$s. Please upgrade PHP to use Square or use an <a href="%3$s">alternative payment gateway</a>. See <a href="%4$s">our documentation</a>.', 'charitable' ), |
| 192 |
esc_html( self::MIN_PHP_VERSION ), |
| 193 |
esc_html( PHP_VERSION ), |
| 194 |
admin_url( 'admin.php?page=charitable-settings&tab=gateways' ), |
| 195 |
'https://www.wpcharitable.com/documentation/php-version-compatibility-square/' |
| 196 |
); |
| 197 |
charitable_get_admin_notices()->add_notice( $message, 'error', 'square-php-version-notice', true ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Remove Square payment gateway if PHP version is incompatible. |
| 202 |
* |
| 203 |
* @since 1.8.7 |
| 204 |
* |
| 205 |
* @param array $gateways Registered payment gateways. |
| 206 |
* @return array Modified gateways list. |
| 207 |
*/ |
| 208 |
public static function remove_square_gateway( $gateways ) { |
| 209 |
if ( isset( $gateways['square'] ) ) { |
| 210 |
unset( $gateways['square'] ); |
| 211 |
} |
| 212 |
|
| 213 |
return $gateways; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Display admin notice about missing Square SDK. |
| 218 |
* |
| 219 |
* @since 1.8.7 |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public static function display_sdk_missing_notice() { |
| 224 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
// Only show on Charitable pages. |
| 229 |
$screen = get_current_screen(); |
| 230 |
if ( ! $screen || strpos( $screen->id, 'charitable' ) === false ) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
?> |
| 235 |
<div class="notice notice-error is-dismissible"> |
| 236 |
<p> |
| 237 |
<strong><?php esc_html_e( 'Square Gateway Error:', 'charitable' ); ?></strong> |
| 238 |
<?php esc_html_e( 'The Square SDK could not be loaded. The Square payment gateway has been disabled.', 'charitable' ); ?> |
| 239 |
</p> |
| 240 |
</div> |
| 241 |
<?php |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns and/or create the single instance of this class. |
| 246 |
* |
| 247 |
* @since 1.8.7 |
| 248 |
* |
| 249 |
* @return Charitable_Square_Compatibility |
| 250 |
*/ |
| 251 |
public static function get_instance() { |
| 252 |
if ( is_null( self::$instance ) ) { |
| 253 |
self::$instance = new self(); |
| 254 |
} |
| 255 |
|
| 256 |
return self::$instance; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
endif; |