Skip to content

Commit 0ee6bea

Browse files
feat(checkout): allow customizing checkout query params via filter
Introduce a namespaced filter to let developers add/modify query params sent to Freemius Checkout. • Add fs_apply_filter( $fs->get_unique_affix(), 'checkout_query_params', ... )
1 parent f97701f commit 0ee6bea

File tree

3 files changed

+32
-101
lines changed

3 files changed

+32
-101
lines changed

includes/class-freemius.php

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,6 @@ class Freemius extends Freemius_Abstract {
365365
*/
366366
private $_is_bundle_license_auto_activation_enabled = false;
367367

368-
/**
369-
* Developer-defined config overrides for checkout.
370-
*
371-
* @var array
372-
*/
373-
protected $_checkout_config = [];
374-
375-
/**
376-
377368
#region Uninstall Reasons IDs
378369

379370
const REASON_NO_LONGER_NEEDED = 1;
@@ -5190,9 +5181,6 @@ private function parse_settings( &$plugin_info ) {
51905181
) );
51915182
}
51925183

5193-
// Extracts, validate and save checkout-specific settings.
5194-
$this->_checkout_config = $this->validate_checkout_config( $plugin_info );
5195-
51965184
$plugin = ( $this->_plugin instanceof FS_Plugin ) ?
51975185
$this->_plugin :
51985186
new FS_Plugin();
@@ -5334,88 +5322,6 @@ private function parse_settings( &$plugin_info ) {
53345322
);
53355323
}
53365324

5337-
/**
5338-
* Validates and filters developer-provided checkout config.
5339-
*
5340-
* @param array $config
5341-
*
5342-
* @return array
5343-
*/
5344-
protected function validate_checkout_config($config)
5345-
{
5346-
$schema = array(
5347-
// currency
5348-
'currency' => 'string',
5349-
'default_currency' => 'string',
5350-
// cart
5351-
'always_show_renewals_amount' => 'bool',
5352-
'annual_discount' => 'bool',
5353-
'billing_cycle' => ['string', 'int'],
5354-
'bundle_discount' => 'float',
5355-
'maximize_discounts' => 'bool',
5356-
'multisite_discount' => ['bool', 'string'], // string expected to be "auto"
5357-
'show_inline_currency_selector' => 'bool',
5358-
'show_monthly' => 'bool',
5359-
// appearance
5360-
'form_position' => 'string',
5361-
'is_bundle_collapsed' => 'bool',
5362-
'layout' => 'string',
5363-
'refund_policy_position' => 'string',
5364-
'show_refund_badge' => 'bool',
5365-
'show_reviews' => 'bool',
5366-
'show_upsells' => 'bool',
5367-
'title' => 'string',
5368-
);
5369-
5370-
$result = array();
5371-
5372-
foreach ($schema as $key => $expected_type)
5373-
{
5374-
if (array_key_exists($key, $config))
5375-
{
5376-
$value = $config[$key];
5377-
$types = is_array($expected_type) ? $expected_type : [$expected_type];
5378-
$valid = false;
5379-
5380-
foreach ($types as $type)
5381-
{
5382-
switch ($type)
5383-
{
5384-
case 'bool':
5385-
if (is_bool($value))
5386-
$valid = true;
5387-
break;
5388-
case 'string':
5389-
if (is_string($value))
5390-
$valid = true;
5391-
break;
5392-
case 'int':
5393-
if (is_int($value))
5394-
$valid = true;
5395-
break;
5396-
case 'float':
5397-
if (is_float($value) || is_int($value))
5398-
$valid = true;
5399-
break;
5400-
}
5401-
}
5402-
5403-
if ($valid)
5404-
$result[$key] = $value;
5405-
}
5406-
}
5407-
5408-
return $result;
5409-
}
5410-
5411-
/**
5412-
* @return array
5413-
*/
5414-
public function get_checkout_config()
5415-
{
5416-
return $this->_checkout_config;
5417-
}
5418-
54195325
/**
54205326
* @param string[] $options
54215327
* @param string $key

includes/managers/class-fs-checkout-manager.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,37 @@ public function get_query_params( Freemius $fs, $plugin_id, $plan_id, $licenses
153153
( $fs->is_theme() && current_user_can( 'install_themes' ) )
154154
);
155155

156-
// Add developer-defined checkout overrides directly to context.
157-
foreach ( $fs->get_checkout_config() as $key => $value ) {
158-
$context_params[ $key ] = $value;
159-
}
160-
161-
return array_merge( $context_params, $_GET, array(
156+
/**
157+
* Allow developers to customize the checkout query params before final validation,
158+
* so custom keys can be included and known keys can be overridden.
159+
* We then validate the merged params and re-attach any unknown custom keys that
160+
* validation intentionally ignores, preserving developer-provided extras while
161+
* keeping core keys safe.
162+
*
163+
* Usage example (in a plugin/theme):
164+
*
165+
* add_filter( 'fs_checkout_query_params_' . fs()->get_unique_affix(), function( $params ) {
166+
* // Add or modify query params passed to the Freemius Checkout.
167+
* $params['coupon'] = 'WELCOME10';
168+
* $params['utm_source'] = 'my-plugin';
169+
* return $params;
170+
* }, 10, 5 );
171+
*
172+
* @since 2.12.1.3
173+
*
174+
* @param array $context_params The params prepared by the SDK before validation.
175+
* @param Freemius $fs The Freemius instance of the calling module.
176+
* @param int|mixed $plugin_id The target plugin/add-on ID for the checkout context.
177+
* @param int|mixed $plan_id The selected plan ID (if any).
178+
* @param int|mixed $licenses The requested number of licenses (if provided).
179+
*/
180+
$filtered_params = fs_apply_filter(
181+
$fs->get_unique_affix(),
182+
'checkout_query_params',
183+
$context_params
184+
);
185+
186+
return array_merge( $filtered_params, $_GET, array(
162187
// Current plugin version.
163188
'plugin_version' => $fs->get_plugin_version(),
164189
'sdk_version' => WP_FS__SDK_VERSION,

start.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @var string
1717
*/
18-
$this_sdk_version = '2.12.1.3';
18+
$this_sdk_version = '2.12.2.4';
1919

2020
#region SDK Selection Logic --------------------------------------------------------------------
2121

0 commit comments

Comments
 (0)