| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class responsible for adding & saving extra settings in the Charitable admin. |
| 4 |
* |
| 5 |
* @package Charitable Square/Classes/Charitable_Square_Helpers |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2021-2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.1.0 |
| 10 |
* @version 1.3.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
use Square\Types\SubscriptionCadence; |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! class_exists( 'Charitable_Square_Helpers' ) ) : |
| 21 |
|
| 22 |
/** |
| 23 |
* Charitable_Square_Helpers |
| 24 |
* |
| 25 |
* @since 1.1.0 |
| 26 |
*/ |
| 27 |
class Charitable_Square_Helpers { |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Determine whether the addon is activated and appropriate license is set. |
| 32 |
* |
| 33 |
* @since 1.8.7 |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
public static function is_pro(): bool { |
| 38 |
|
| 39 |
return self::is_addon_active() && self::is_allowed_license_type(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Determine whether the addon is activated. |
| 44 |
* |
| 45 |
* @since 1.8.7 |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
public static function is_addon_active(): bool { |
| 50 |
return is_plugin_active( 'charitable-square/charitable-square.php' ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Determine whether a license is ok. |
| 55 |
* |
| 56 |
* @since 1.8.7 |
| 57 |
* |
| 58 |
* @return bool |
| 59 |
*/ |
| 60 |
public static function is_license_ok(): bool { |
| 61 |
|
| 62 |
return self::is_license_active() && self::is_allowed_license_type(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Determine whether a license type is allowed. |
| 67 |
* For future use, for 'pro', 'elite', 'basic', 'plus' license types. |
| 68 |
* |
| 69 |
* @since 1.8.7 |
| 70 |
* |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public static function is_allowed_license_type(): bool { |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Determine whether a license key is active. |
| 79 |
* |
| 80 |
* @since 1.8.7 |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public static function is_license_active(): bool { |
| 85 |
return charitable_is_pro(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Determine whether Square is in sandbox mode. |
| 90 |
* |
| 91 |
* @since 1.8.7 |
| 92 |
* |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public static function is_sandbox_mode(): bool { |
| 96 |
|
| 97 |
return self::get_mode() === 'test'; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Determine whether Square is in production mode. |
| 102 |
* |
| 103 |
* @since 1.8.7 |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public static function is_production_mode(): bool { |
| 108 |
|
| 109 |
return self::get_mode() === 'live'; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Retrieve Square mode from Charitable settings. |
| 114 |
* |
| 115 |
* @since 1.8.7 |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
public static function get_mode(): string { |
| 120 |
return charitable_get_option( 'test_mode' ) ? 'test' : 'live'; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Retrieve Square Business Location ID from Charitable settings. |
| 125 |
* |
| 126 |
* @since 1.8.7 |
| 127 |
* |
| 128 |
* @param string $mode Square mode. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public static function get_location_id( string $mode = '' ): string { |
| 133 |
|
| 134 |
$mode = self::validate_mode( $mode ); |
| 135 |
|
| 136 |
$option = get_option( 'charitable_square_location_id_' . $mode ); |
| 137 |
|
| 138 |
if ( $option ) { |
| 139 |
return $option; |
| 140 |
} |
| 141 |
|
| 142 |
return ''; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Retrieve Square available modes. |
| 147 |
* |
| 148 |
* @since 1.8.7 |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public static function get_available_modes(): array { |
| 153 |
|
| 154 |
return array( 'test', 'live' ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Validate Square mode to ensure it's either 'production' or 'sandbox'. |
| 159 |
* If given mode is invalid, fetches current Square mode. |
| 160 |
* |
| 161 |
* @since 1.8.7 |
| 162 |
* |
| 163 |
* @param string $mode Square mode to validate. |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public static function validate_mode( string $mode ): string { |
| 168 |
|
| 169 |
return in_array( $mode, self::get_available_modes(), true ) ? $mode : self::get_mode(); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* The `array_key_first` polyfill. |
| 174 |
* |
| 175 |
* @since 1.8.7 |
| 176 |
* |
| 177 |
* @param array $arr Input array. |
| 178 |
* |
| 179 |
* @return mixed|null |
| 180 |
*/ |
| 181 |
public static function array_key_first( array $arr ) { |
| 182 |
|
| 183 |
if ( function_exists( 'array_key_first' ) ) { |
| 184 |
return array_key_first( $arr ); |
| 185 |
} |
| 186 |
|
| 187 |
foreach ( $arr as $key => $unused ) { |
| 188 |
return $key; |
| 189 |
} |
| 190 |
|
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Determine if webhook ID and secret are set in Charitable settings. |
| 196 |
* |
| 197 |
* @since 1.8.7 |
| 198 |
* |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
public static function is_webhook_configured(): bool { |
| 202 |
|
| 203 |
$mode = self::get_mode(); |
| 204 |
|
| 205 |
$settings = charitable_get_option( 'gateways_square' ); |
| 206 |
|
| 207 |
return ! empty( $settings[ $mode ]['webhooks-id'] ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Determine if Square is configured and valid. |
| 212 |
* |
| 213 |
* @since 1.8.7 |
| 214 |
* |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public static function is_square_configured(): bool { |
| 218 |
|
| 219 |
$connection = Charitable_Square_Connection::get(); |
| 220 |
|
| 221 |
// Check if connection is configured and valid. |
| 222 |
return ! ( ! $connection || ! $connection->is_configured() || ! $connection->is_valid() ); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get webhook URL for REST API. |
| 227 |
* |
| 228 |
* @since 1.8.7 |
| 229 |
* |
| 230 |
* @return string |
| 231 |
*/ |
| 232 |
public static function get_webhook_url_for_rest(): string { |
| 233 |
return esc_url_raw( add_query_arg( array( 'charitable-listener' => Charitable_Gateway_Square::ID ), trailingslashit( home_url() ) ) ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Reset Square webhooks settings. |
| 238 |
* |
| 239 |
* @since 1.8.7 |
| 240 |
* |
| 241 |
* @param bool $reset_enable Optional. Whether to reset the webhook enabled status. Default is false. |
| 242 |
* |
| 243 |
* @return bool |
| 244 |
*/ |
| 245 |
public static function reset_webhook_configuration( bool $reset_enable = false ): bool { |
| 246 |
|
| 247 |
$settings = (array) get_option( 'charitable_settings', array() ); |
| 248 |
|
| 249 |
$mode = self::get_mode(); |
| 250 |
|
| 251 |
if ( $reset_enable ) { |
| 252 |
$settings['gateways_square']['square_enable_webhooks'] = 0; // Switch off webhooks. |
| 253 |
} |
| 254 |
|
| 255 |
$settings['gateways_square'][ $mode ]['webhooks-id'] = ''; |
| 256 |
$settings['gateways_square'][ $mode ]['webhooks-secret'] = ''; |
| 257 |
|
| 258 |
return update_option( 'charitable_settings', $settings ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Determine the billing cadences of a Subscription. |
| 263 |
* |
| 264 |
* @since 1.8.7 |
| 265 |
* |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
public static function get_subscription_cadences(): array { |
| 269 |
|
| 270 |
/** |
| 271 |
* Filter the available billing cadences of a Subscription. |
| 272 |
* |
| 273 |
* @since 1.8.7 |
| 274 |
* |
| 275 |
* @param array $cadences Subscription billing cadences. |
| 276 |
*/ |
| 277 |
return (array) apply_filters( |
| 278 |
'charitable_integrations_square_helpers_get_subscription_cadences', |
| 279 |
array( |
| 280 |
'daily' => array( |
| 281 |
'slug' => 'daily', |
| 282 |
'name' => esc_html__( 'Daily', 'charitable' ), |
| 283 |
'value' => SubscriptionCadence::Daily->value, |
| 284 |
), |
| 285 |
'weekly' => array( |
| 286 |
'slug' => 'weekly', |
| 287 |
'name' => esc_html__( 'Weekly', 'charitable' ), |
| 288 |
'value' => SubscriptionCadence::Weekly->value, |
| 289 |
), |
| 290 |
'monthly' => array( |
| 291 |
'slug' => 'monthly', |
| 292 |
'name' => esc_html__( 'Monthly', 'charitable' ), |
| 293 |
'value' => SubscriptionCadence::Monthly->value, |
| 294 |
), |
| 295 |
'quarterly' => array( |
| 296 |
'slug' => 'quarterly', |
| 297 |
'name' => esc_html__( 'Quarterly', 'charitable' ), |
| 298 |
'value' => SubscriptionCadence::Quarterly->value, |
| 299 |
), |
| 300 |
'semiyearly' => array( |
| 301 |
'slug' => 'semiyearly', |
| 302 |
'name' => esc_html__( 'Semi-Yearly', 'charitable' ), |
| 303 |
'value' => SubscriptionCadence::EverySixMonths->value, |
| 304 |
), |
| 305 |
'yearly' => array( |
| 306 |
'slug' => 'yearly', |
| 307 |
'name' => esc_html__( 'Yearly', 'charitable' ), |
| 308 |
'value' => SubscriptionCadence::Annual->value, |
| 309 |
), |
| 310 |
) |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Get Square webhook endpoint URL. |
| 316 |
* |
| 317 |
* If the constant CHARITABLE_SQUARE_WHURL is defined, it will be used as the webhook URL. |
| 318 |
* |
| 319 |
* @since 1.8.7 |
| 320 |
* |
| 321 |
* @return string |
| 322 |
*/ |
| 323 |
public static function get_webhook_url(): string { |
| 324 |
|
| 325 |
if ( defined( 'CHARITABLE_SQUARE_WHURL' ) ) { |
| 326 |
return CHARITABLE_SQUARE_WHURL; |
| 327 |
} |
| 328 |
|
| 329 |
if ( self::is_rest_api_set() ) { |
| 330 |
return self::get_webhook_url_for_rest(); |
| 331 |
} |
| 332 |
|
| 333 |
return false; |
| 334 |
|
| 335 |
// self::get_webhook_url_for_curl(); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Determine if the REST API is set in Charitable settings. |
| 340 |
* Designed in case we want to experiment with different webhook communication methods. |
| 341 |
* |
| 342 |
* @since 1.8.7 |
| 343 |
* |
| 344 |
* @return bool |
| 345 |
*/ |
| 346 |
public static function is_rest_api_set(): bool { |
| 347 |
return true; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Determine if webhooks are enabled in Charitable settings. |
| 352 |
* |
| 353 |
* @since 1.8.7 |
| 354 |
* |
| 355 |
* @return bool |
| 356 |
*/ |
| 357 |
public static function is_webhook_enabled(): bool { |
| 358 |
|
| 359 |
$settings = charitable_get_option( 'gateways_square' ); |
| 360 |
|
| 361 |
if ( ! is_array( $settings ) ) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
$square_enable_webhooks = $settings['square_enable_webhooks'] ?? null; |
| 366 |
|
| 367 |
return ! empty( $square_enable_webhooks ); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Determine whether the application fee is supported. |
| 372 |
* |
| 373 |
* @since 1.8.7 |
| 374 |
* |
| 375 |
* @param string $currency Currency. |
| 376 |
* |
| 377 |
* @return bool |
| 378 |
*/ |
| 379 |
public static function is_application_fee_supported( string $currency = '' ): bool { |
| 380 |
|
| 381 |
$currency = ! $currency ? charitable_get_currency() : $currency; |
| 382 |
|
| 383 |
return strtoupper( $currency ) === 'USD'; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
endif; |
| 388 |
|