Skip to content

Commit b847ab0

Browse files
Added checkout customization parameters
1 parent 564b9aa commit b847ab0

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

includes/class-freemius.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ 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+
368377
#region Uninstall Reasons IDs
369378

370379
const REASON_NO_LONGER_NEEDED = 1;
@@ -5181,6 +5190,10 @@ private function parse_settings( &$plugin_info ) {
51815190
) );
51825191
}
51835192

5193+
if ( isset( $plugin_info['checkout'] ) && is_array( $plugin_info['checkout'] ) ) {
5194+
$this->_checkout_config = $this->validate_checkout_config( $plugin_info['checkout'] );
5195+
}
5196+
51845197
$plugin = ( $this->_plugin instanceof FS_Plugin ) ?
51855198
$this->_plugin :
51865199
new FS_Plugin();
@@ -5322,6 +5335,93 @@ private function parse_settings( &$plugin_info ) {
53225335
);
53235336
}
53245337

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ 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+
156161
return array_merge( $context_params, $_GET, array(
157162
// Current plugin version.
158163
'plugin_version' => $fs->get_plugin_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.2';
18+
$this_sdk_version = '2.12.1.3';
1919

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

0 commit comments

Comments
 (0)