| 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 |
# region Singleton |
| 16 |
|
| 17 |
/** |
| 18 |
* @var FS_Checkout_Manager |
| 19 |
*/ |
| 20 |
private static $_instance; |
| 21 |
|
| 22 |
/** |
| 23 |
* @return FS_Checkout_Manager |
| 24 |
*/ |
| 25 |
static function instance() { |
| 26 |
if ( ! isset( self::$_instance ) ) { |
| 27 |
self::$_instance = new FS_Checkout_Manager(); |
| 28 |
} |
| 29 |
|
| 30 |
return self::$_instance; |
| 31 |
} |
| 32 |
|
| 33 |
private function __construct() { |
| 34 |
} |
| 35 |
|
| 36 |
#endregion |
| 37 |
|
| 38 |
/** |
| 39 |
* Retrieves the query params needed to load the Freemius Checkout in the context of the plugin. |
| 40 |
* |
| 41 |
* @param Freemius $fs |
| 42 |
* @param number $plugin_id |
| 43 |
* @param number $plan_id |
| 44 |
* @param number $licenses |
| 45 |
* |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
public function get_query_params( Freemius $fs, $plugin_id, $plan_id, $licenses ) { |
| 49 |
$timestamp = time(); |
| 50 |
|
| 51 |
$context_params = array( |
| 52 |
'plugin_id' => $fs->get_id(), |
| 53 |
'public_key' => $fs->get_public_key(), |
| 54 |
'plugin_version' => $fs->get_plugin_version(), |
| 55 |
'mode' => 'dashboard', |
| 56 |
'trial' => fs_request_get_bool( 'trial' ), |
| 57 |
'is_ms' => ( fs_is_network_admin() && $fs->is_network_active() ), |
| 58 |
); |
| 59 |
|
| 60 |
if ( FS_Plugin_Plan::is_valid_id( $plan_id ) ) { |
| 61 |
$context_params['plan_id'] = $plan_id; |
| 62 |
} |
| 63 |
|
| 64 |
if ( $licenses === strval( intval( $licenses ) ) && $licenses > 0 ) { |
| 65 |
$context_params['licenses'] = $licenses; |
| 66 |
} |
| 67 |
|
| 68 |
if ( $plugin_id == $fs->get_id() ) { |
| 69 |
$is_premium = $fs->is_premium(); |
| 70 |
|
| 71 |
$bundle_id = $fs->get_bundle_id(); |
| 72 |
if ( ! is_null( $bundle_id ) ) { |
| 73 |
$context_params['bundle_id'] = $bundle_id; |
| 74 |
} |
| 75 |
} else { |
| 76 |
// Identify the module code version of the checkout context module. |
| 77 |
if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 78 |
$fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 79 |
$is_premium = $fs_addon->is_premium(); |
| 80 |
} else { |
| 81 |
// If add-on isn't activated assume the premium version isn't installed. |
| 82 |
$is_premium = false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Get site context secure params. |
| 87 |
if ( $fs->is_registered() ) { |
| 88 |
$site = $fs->get_site(); |
| 89 |
|
| 90 |
if ( $plugin_id != $fs->get_id() ) { |
| 91 |
if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 92 |
$fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 93 |
$addon_site = $fs_addon->get_site(); |
| 94 |
if ( is_object( $addon_site ) ) { |
| 95 |
$site = $addon_site; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$context_params = array_merge( |
| 101 |
$context_params, |
| 102 |
FS_Security::instance()->get_context_params( |
| 103 |
$site, |
| 104 |
$timestamp, |
| 105 |
'checkout' |
| 106 |
) |
| 107 |
); |
| 108 |
} else { |
| 109 |
$current_user = Freemius::_get_current_wp_user(); |
| 110 |
|
| 111 |
// Add site and user info to the request, this information |
| 112 |
// is NOT being stored unless the user complete the purchase |
| 113 |
// and agrees to the TOS. |
| 114 |
$context_params = array_merge( $context_params, array( |
| 115 |
'user_firstname' => $current_user->user_firstname, |
| 116 |
'user_lastname' => $current_user->user_lastname, |
| 117 |
'user_email' => $current_user->user_email, |
| 118 |
'home_url' => home_url(), |
| 119 |
) ); |
| 120 |
|
| 121 |
$fs_user = Freemius::_get_user_by_email( $current_user->user_email ); |
| 122 |
|
| 123 |
if ( is_object( $fs_user ) && $fs_user->is_verified() ) { |
| 124 |
$context_params = array_merge( |
| 125 |
$context_params, |
| 126 |
FS_Security::instance()->get_context_params( |
| 127 |
$fs_user, |
| 128 |
$timestamp, |
| 129 |
'checkout' |
| 130 |
) |
| 131 |
); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if ( $fs->is_payments_sandbox() ) { |
| 136 |
// Append plugin secure token for sandbox mode authentication. |
| 137 |
$context_params['sandbox'] = FS_Security::instance()->get_secure_token( |
| 138 |
$fs->get_plugin(), |
| 139 |
$timestamp, |
| 140 |
'checkout' |
| 141 |
); |
| 142 |
|
| 143 |
/** |
| 144 |
* @since 1.1.7.3 Add security timestamp for sandbox even for anonymous user. |
| 145 |
*/ |
| 146 |
if ( empty( $context_params['s_ctx_ts'] ) ) { |
| 147 |
$context_params['s_ctx_ts'] = $timestamp; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$can_user_install = ( |
| 152 |
( $fs->is_plugin() && current_user_can( 'install_plugins' ) ) || |
| 153 |
( $fs->is_theme() && current_user_can( 'install_themes' ) ) |
| 154 |
); |
| 155 |
|
| 156 |
return array_merge( $context_params, $_GET, array( |
| 157 |
// Current plugin version. |
| 158 |
'plugin_version' => $fs->get_plugin_version(), |
| 159 |
'sdk_version' => WP_FS__SDK_VERSION, |
| 160 |
'is_premium' => $is_premium ? 'true' : 'false', |
| 161 |
'can_install' => $can_user_install ? 'true' : 'false', |
| 162 |
) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* The return URL to pass to the checkout when the checkout is loaded in "redirect" mode. |
| 167 |
* |
| 168 |
* @param Freemius $fs |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function get_checkout_redirect_return_url( Freemius $fs ) { |
| 173 |
$request_url = remove_query_arg( '_wp_http_referer' ); |
| 174 |
|
| 175 |
return fs_nonce_url( |
| 176 |
$fs->checkout_url( |
| 177 |
fs_request_get( 'billing_cycle' ), |
| 178 |
fs_request_get_bool( 'trial' ), |
| 179 |
array( |
| 180 |
'process_redirect' => 'true', |
| 181 |
'_wp_http_referer' => $request_url, |
| 182 |
) |
| 183 |
), |
| 184 |
$this->get_checkout_redirect_nonce_action( $fs ) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @param array $query_params |
| 190 |
* @param string $base_url |
| 191 |
* |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public function get_full_checkout_url( array $query_params, $base_url = FS_CHECKOUT__ADDRESS ) { |
| 195 |
return $base_url . '/?' . http_build_query( $query_params ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Verifies the redirect after a checkout with the nonce. |
| 200 |
* |
| 201 |
* @param Freemius $fs |
| 202 |
*/ |
| 203 |
public function verify_checkout_redirect_nonce( Freemius $fs ) { |
| 204 |
check_admin_referer( $this->get_checkout_redirect_nonce_action( $fs ) ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get the URL to process a new install after the checkout. |
| 209 |
* |
| 210 |
* @param Freemius $fs |
| 211 |
* @param number $plugin_id |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
public function get_install_url( Freemius $fs, $plugin_id ) { |
| 216 |
return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 217 |
'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 218 |
'plugin_id' => $plugin_id, |
| 219 |
) ), $fs->get_unique_affix() . '_activate_new' ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Get the URL to process a pending activation after the checkout. |
| 224 |
* |
| 225 |
* @param Freemius $fs |
| 226 |
* @param number $plugin_id |
| 227 |
* |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
public function get_pending_activation_url( Freemius $fs, $plugin_id ) { |
| 231 |
return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 232 |
'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 233 |
'plugin_id' => $plugin_id, |
| 234 |
'pending_activation' => true, |
| 235 |
'has_upgrade_context' => true, |
| 236 |
) ), $fs->get_unique_affix() . '_activate_new' ); |
| 237 |
} |
| 238 |
|
| 239 |
private function get_checkout_redirect_nonce_action( Freemius $fs ) { |
| 240 |
return $fs->get_unique_affix() . '_checkout_redirect'; |
| 241 |
} |
| 242 |
} |