/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/Square
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/Square/Helpers.php (11916B)
Payments settings page URL.
*
* @since 1.9.5
*
* @return string
*/
public static function get_settings_page_url(): string {
return add_query_arg(
[
'page' => 'wpforms-settings',
'view' => 'payments',
],
admin_url( 'admin.php' )
);
}
/**
* The `array_key_first` polyfill.
*
* @since 1.9.5
*
* @param array $arr Input array.
*
* @return mixed|null
*/
public static function array_key_first( array $arr ) {
if ( function_exists( 'array_key_first' ) ) {
return array_key_first( $arr );
}
foreach ( $arr as $key => $unused ) {
return $key;
}
return null;
}
/**
* Determine if webhook ID and secret are set in WPForms settings.
*
* @since 1.9.5
*
* @return bool
*/
public static function is_webhook_configured(): bool {
$mode = self::get_mode();
return wpforms_setting( 'square-webhooks-id-' . $mode ) && wpforms_setting( 'square-webhooks-secret-' . $mode );
}
/**
* Determine if Square is configured and valid.
*
* @since 1.9.5
*
* @return bool
*/
public static function is_square_configured(): bool {
$connection = Connection::get();
// Check if connection is configured and valid.
return ! ( ! $connection || ! $connection->is_configured() || ! $connection->is_valid() );
}
/**
* Get webhook URL for REST API.
*
* @since 1.9.5
*
* @return string
*/
public static function get_webhook_url_for_rest(): string {
$path = implode(
'/',
[
self::get_webhook_endpoint_data()['namespace'],
self::get_webhook_endpoint_data()['route'],
]
);
return rest_url( $path );
}
/**
* Reset Square webhooks settings.
*
* @since 1.9.5
*
* @param bool $reset_enable Optional. Whether to reset the webhook enabled status. Default is false.
*
* @return bool
*/
public static function reset_webhook_configuration( bool $reset_enable = false ): bool {
$settings = (array) get_option( 'wpforms_settings', [] );
$mode = self::get_mode();
if ( $reset_enable ) {
$settings['square-webhooks-enabled'] = false; // Switch off webhooks.
}
$settings[ 'square-webhooks-id-' . $mode ] = '';
$settings[ 'square-webhooks-secret-' . $mode ] = '';
return update_option( 'wpforms_settings', $settings );
}
/**
* Determine the billing cadences of a Subscription.
*
* @since 1.9.5
*
* @return array
*/
public static function get_subscription_cadences(): array {
/**
* Filter the available billing cadences of a Subscription.
*
* @since 1.9.5
*
* @param array $cadences Subscription billing cadences.
*/
return (array) apply_filters(
'wpforms_integrations_square_helpers_get_subscription_cadences',
[
'daily' => [
'slug' => 'daily',
'name' => esc_html__( 'Daily', 'wpforms-lite' ),
'value' => SubscriptionCadence::DAILY,
],
'weekly' => [
'slug' => 'weekly',
'name' => esc_html__( 'Weekly', 'wpforms-lite' ),
'value' => SubscriptionCadence::WEEKLY,
],
'monthly' => [
'slug' => 'monthly',
'name' => esc_html__( 'Monthly', 'wpforms-lite' ),
'value' => SubscriptionCadence::MONTHLY,
],
'quarterly' => [
'slug' => 'quarterly',
'name' => esc_html__( 'Quarterly', 'wpforms-lite' ),
'value' => SubscriptionCadence::QUARTERLY,
],
'semiyearly' => [
'slug' => 'semiyearly',
'name' => esc_html__( 'Semi-Yearly', 'wpforms-lite' ),
'value' => SubscriptionCadence::EVERY_SIX_MONTHS,
],
'yearly' => [
'slug' => 'yearly',
'name' => esc_html__( 'Yearly', 'wpforms-lite' ),
'value' => SubscriptionCadence::ANNUAL,
],
]
);
}
/**
* Return a formatted amount required by Square API.
*
* @since 1.9.5
*
* @param string $amount Price amount.
*
* @return float|int
*/
public static function format_amount( string $amount ) {
return wpforms_sanitize_amount( $amount ) * wpforms_get_currency_multiplier();
}
/**
* Get Square webhook endpoint data.
*
* @since 1.9.5
*
* @return array
*/
public static function get_webhook_endpoint_data(): array {
return [
'namespace' => 'wpforms',
'route' => 'square/webhooks',
'fallback' => 'wpforms_square_webhooks',
];
}
/**
* Get Square webhook endpoint URL.
*
* If the constant WPFORMS_SQUARE_WHURL is defined, it will be used as the webhook URL.
*
* @since 1.9.5
*
* @return string
*/
public static function get_webhook_url(): string {
if ( defined( 'WPFORMS_SQUARE_WHURL' ) ) {
return WPFORMS_SQUARE_WHURL;
}
if ( self::is_rest_api_set() ) {
return self::get_webhook_url_for_rest();
}
return self::get_webhook_url_for_curl();
}
/**
* Determine if the REST API is set in WPForms settings.
*
* @since 1.9.5
*
* @return bool
*/
public static function is_rest_api_set(): bool {
return wpforms_setting( 'square-webhooks-communication', 'rest' ) === 'rest';
}
/**
* Get webhook URL for cURL fallback.
*
* @since 1.9.5
*
* @return string
*/
public static function get_webhook_url_for_curl(): string {
return add_query_arg( self::get_webhook_endpoint_data()['fallback'], '1', site_url() );
}
/**
* Determine if webhooks are enabled in WPForms settings.
*
* @since 1.9.5
*
* @return bool
*/
public static function is_webhook_enabled(): bool {
return wpforms_setting( 'square-webhooks-enabled' );
}
/**
* Determine whether the application fee is supported.
*
* @since 1.9.5
*
* @param string $currency Currency.
*
* @return bool
*/
public static function is_application_fee_supported( string $currency = '' ): bool {
$currency = ! $currency ? wpforms_get_currency() : $currency;
return strtoupper( $currency ) === 'USD';
}
}