| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2024, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 2.9.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class FS_Checkout_Manager { |
| 14 |
|
| 15 |
/** |
| 16 |
* Allowlist of query parameters for checkout. |
| 17 |
*/ |
| 18 |
private $_allowed_custom_params = array( |
| 19 |
// currency |
| 20 |
'currency' => true, |
| 21 |
'default_currency' => true, |
| 22 |
// cart |
| 23 |
'always_show_renewals_amount' => true, |
| 24 |
'annual_discount' => true, |
| 25 |
'billing_cycle' => true, |
| 26 |
'billing_cycle_selector' => true, |
| 27 |
'bundle_discount' => true, |
| 28 |
'maximize_discounts' => true, |
| 29 |
'multisite_discount' => true, |
| 30 |
'show_inline_currency_selector' => true, |
| 31 |
'show_monthly' => true, |
| 32 |
// appearance |
| 33 |
'form_position' => true, |
| 34 |
'is_bundle_collapsed' => true, |
| 35 |
'layout' => true, |
| 36 |
'refund_policy_position' => true, |
| 37 |
'show_refund_badge' => true, |
| 38 |
'show_reviews' => true, |
| 39 |
'show_upsells' => true, |
| 40 |
'title' => true, |
| 41 |
); |
| 42 |
|
| 43 |
|
| 44 |
# region Singleton |
| 45 |
|
| 46 |
/** |
| 47 |
* @var FS_Checkout_Manager |
| 48 |
*/ |
| 49 |
private static $_instance; |
| 50 |
|
| 51 |
/** |
| 52 |
* @return FS_Checkout_Manager |
| 53 |
*/ |
| 54 |
static function instance() { |
| 55 |
if ( ! isset( self::$_instance ) ) { |
| 56 |
self::$_instance = new FS_Checkout_Manager(); |
| 57 |
} |
| 58 |
|
| 59 |
return self::$_instance; |
| 60 |
} |
| 61 |
|
| 62 |
private function __construct() { |
| 63 |
} |
| 64 |
|
| 65 |
#endregion |
| 66 |
|
| 67 |
/** |
| 68 |
* Retrieves the query params needed to load the Freemius Checkout in the context of the plugin. |
| 69 |
* |
| 70 |
* @param Freemius $fs |
| 71 |
* @param number $plugin_id |
| 72 |
* @param number $plan_id |
| 73 |
* @param number $licenses |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function get_query_params( Freemius $fs, $plugin_id, $plan_id, $licenses ) { |
| 78 |
$timestamp = time(); |
| 79 |
|
| 80 |
$context_params = array( |
| 81 |
'plugin_id' => $fs->get_id(), |
| 82 |
'public_key' => $fs->get_public_key(), |
| 83 |
'plugin_version' => $fs->get_plugin_version(), |
| 84 |
'mode' => 'dashboard', |
| 85 |
'trial' => fs_request_get_bool( 'trial' ), |
| 86 |
'is_ms' => ( fs_is_network_admin() && $fs->is_network_active() ), |
| 87 |
); |
| 88 |
|
| 89 |
if ( FS_Plugin_Plan::is_valid_id( $plan_id ) ) { |
| 90 |
$context_params['plan_id'] = $plan_id; |
| 91 |
} |
| 92 |
|
| 93 |
if ( $licenses === strval( intval( $licenses ) ) && $licenses > 0 ) { |
| 94 |
$context_params['licenses'] = $licenses; |
| 95 |
} |
| 96 |
|
| 97 |
if ( $plugin_id == $fs->get_id() ) { |
| 98 |
$is_premium = $fs->is_premium(); |
| 99 |
|
| 100 |
$bundle_id = $fs->get_bundle_id(); |
| 101 |
if ( ! is_null( $bundle_id ) ) { |
| 102 |
$context_params['bundle_id'] = $bundle_id; |
| 103 |
} |
| 104 |
} else { |
| 105 |
// Identify the module code version of the checkout context module. |
| 106 |
if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 107 |
$fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 108 |
$is_premium = $fs_addon->is_premium(); |
| 109 |
} else { |
| 110 |
// If add-on isn't activated assume the premium version isn't installed. |
| 111 |
$is_premium = false; |
| 112 |
} |
| 113 |
|
| 114 |
// Override the checkout context with the add-on's purchase details so the checkout flow is initialized for the selected add-on instead of the parent product. |
| 115 |
$context_params['plugin_id'] = $plugin_id; |
| 116 |
|
| 117 |
foreach ( array( 'plan_id', 'pricing_id', 'billing_cycle', 'is_trial' ) as $param ) { |
| 118 |
if ( fs_request_has( $param ) ) { |
| 119 |
$context_params[ $param ] = fs_request_get( $param ); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Get site context secure params. |
| 125 |
if ( $fs->is_registered() ) { |
| 126 |
$site = $fs->get_site(); |
| 127 |
|
| 128 |
if ( $plugin_id != $fs->get_id() ) { |
| 129 |
if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 130 |
$fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 131 |
$addon_site = $fs_addon->get_site(); |
| 132 |
if ( is_object( $addon_site ) ) { |
| 133 |
$site = $addon_site; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$context_params = array_merge( |
| 139 |
$context_params, |
| 140 |
FS_Security::instance()->get_context_params( |
| 141 |
$site, |
| 142 |
$timestamp, |
| 143 |
'checkout' |
| 144 |
) |
| 145 |
); |
| 146 |
} else { |
| 147 |
$current_user = Freemius::_get_current_wp_user(); |
| 148 |
|
| 149 |
// Add site and user info to the request, this information |
| 150 |
// is NOT being stored unless the user complete the purchase |
| 151 |
// and agrees to the TOS. |
| 152 |
$context_params = array_merge( $context_params, array( |
| 153 |
'user_firstname' => $current_user->user_firstname, |
| 154 |
'user_lastname' => $current_user->user_lastname, |
| 155 |
'user_email' => $current_user->user_email, |
| 156 |
'home_url' => home_url(), |
| 157 |
) ); |
| 158 |
|
| 159 |
$fs_user = Freemius::_get_user_by_email( $current_user->user_email ); |
| 160 |
|
| 161 |
if ( is_object( $fs_user ) && $fs_user->is_verified() ) { |
| 162 |
$context_params = array_merge( |
| 163 |
$context_params, |
| 164 |
FS_Security::instance()->get_context_params( |
| 165 |
$fs_user, |
| 166 |
$timestamp, |
| 167 |
'checkout' |
| 168 |
) |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( $fs->is_payments_sandbox() ) { |
| 174 |
// Append plugin secure token for sandbox mode authentication. |
| 175 |
$context_params['sandbox'] = FS_Security::instance()->get_secure_token( |
| 176 |
$fs->get_plugin(), |
| 177 |
$timestamp, |
| 178 |
'checkout' |
| 179 |
); |
| 180 |
|
| 181 |
/** |
| 182 |
* @since 1.1.7.3 Add security timestamp for sandbox even for anonymous user. |
| 183 |
*/ |
| 184 |
if ( empty( $context_params['s_ctx_ts'] ) ) { |
| 185 |
$context_params['s_ctx_ts'] = $timestamp; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$can_user_install = ( |
| 190 |
( $fs->is_plugin() && current_user_can( 'install_plugins' ) ) || |
| 191 |
( $fs->is_theme() && current_user_can( 'install_themes' ) ) |
| 192 |
); |
| 193 |
|
| 194 |
$filtered_params = $fs->apply_filters('checkout/parameters', $context_params); |
| 195 |
|
| 196 |
// Allowlist only allowed query params. |
| 197 |
$filtered_params = array_intersect_key($filtered_params, $this->_allowed_custom_params); |
| 198 |
|
| 199 |
return array_merge( $_GET, $context_params, $filtered_params, array( |
| 200 |
// Current plugin version. |
| 201 |
'plugin_version' => $fs->get_plugin_version(), |
| 202 |
'sdk_version' => WP_FS__SDK_VERSION, |
| 203 |
'is_premium' => $is_premium ? 'true' : 'false', |
| 204 |
'can_install' => $can_user_install ? 'true' : 'false', |
| 205 |
) ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* The return URL to pass to the checkout when the checkout is loaded in "redirect" mode. |
| 210 |
* |
| 211 |
* @param Freemius $fs |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
public function get_checkout_redirect_return_url( Freemius $fs ) { |
| 216 |
$request_url = remove_query_arg( '_wp_http_referer' ); |
| 217 |
|
| 218 |
return fs_nonce_url( |
| 219 |
$fs->checkout_url( |
| 220 |
fs_request_get( 'billing_cycle' ), |
| 221 |
fs_request_get_bool( 'trial' ), |
| 222 |
array( |
| 223 |
'process_redirect' => 'true', |
| 224 |
'_wp_http_referer' => $request_url, |
| 225 |
) |
| 226 |
), |
| 227 |
$this->get_checkout_redirect_nonce_action( $fs ) |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @param array $query_params |
| 233 |
* @param string $base_url |
| 234 |
* |
| 235 |
* @return string |
| 236 |
*/ |
| 237 |
public function get_full_checkout_url( array $query_params, $base_url = FS_CHECKOUT__ADDRESS ) { |
| 238 |
return $base_url . '/?' . http_build_query( $query_params ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Verifies the redirect after a checkout with the nonce. |
| 243 |
* |
| 244 |
* @param Freemius $fs |
| 245 |
*/ |
| 246 |
public function verify_checkout_redirect_nonce( Freemius $fs ) { |
| 247 |
check_admin_referer( $this->get_checkout_redirect_nonce_action( $fs ) ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get the URL to process a new install after the checkout. |
| 252 |
* |
| 253 |
* @param Freemius $fs |
| 254 |
* @param number $plugin_id |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
public function get_install_url( Freemius $fs, $plugin_id ) { |
| 259 |
return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 260 |
'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 261 |
'plugin_id' => $plugin_id, |
| 262 |
) ), $fs->get_unique_affix() . '_activate_new' ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get the URL to process a pending activation after the checkout. |
| 267 |
* |
| 268 |
* @param Freemius $fs |
| 269 |
* @param number $plugin_id |
| 270 |
* |
| 271 |
* @return string |
| 272 |
*/ |
| 273 |
public function get_pending_activation_url( Freemius $fs, $plugin_id ) { |
| 274 |
return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 275 |
'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 276 |
'plugin_id' => $plugin_id, |
| 277 |
'pending_activation' => true, |
| 278 |
'has_upgrade_context' => true, |
| 279 |
) ), $fs->get_unique_affix() . '_activate_new' ); |
| 280 |
} |
| 281 |
|
| 282 |
private function get_checkout_redirect_nonce_action( Freemius $fs ) { |
| 283 |
return $fs->get_unique_affix() . '_checkout_redirect'; |
| 284 |
} |
| 285 |
} |
| 286 |
|