class-fs-admin-menu-manager.php
5 days ago
class-fs-admin-notice-manager.php
5 days ago
class-fs-cache-manager.php
5 days ago
class-fs-checkout-manager.php
5 days ago
class-fs-clone-manager.php
5 days ago
class-fs-contact-form-manager.php
5 days ago
class-fs-debug-manager.php
5 days ago
class-fs-gdpr-manager.php
5 days ago
class-fs-key-value-storage.php
5 days ago
class-fs-license-manager.php
5 days ago
class-fs-option-manager.php
5 days ago
class-fs-permission-manager.php
5 days ago
class-fs-plan-manager.php
5 days ago
class-fs-plugin-manager.php
5 days ago
index.php
5 days ago
class-fs-checkout-manager.php
277 lines
| 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 | |
| 115 | // Get site context secure params. |
| 116 | if ( $fs->is_registered() ) { |
| 117 | $site = $fs->get_site(); |
| 118 | |
| 119 | if ( $plugin_id != $fs->get_id() ) { |
| 120 | if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 121 | $fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 122 | $addon_site = $fs_addon->get_site(); |
| 123 | if ( is_object( $addon_site ) ) { |
| 124 | $site = $addon_site; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | $context_params = array_merge( |
| 130 | $context_params, |
| 131 | FS_Security::instance()->get_context_params( |
| 132 | $site, |
| 133 | $timestamp, |
| 134 | 'checkout' |
| 135 | ) |
| 136 | ); |
| 137 | } else { |
| 138 | $current_user = Freemius::_get_current_wp_user(); |
| 139 | |
| 140 | // Add site and user info to the request, this information |
| 141 | // is NOT being stored unless the user complete the purchase |
| 142 | // and agrees to the TOS. |
| 143 | $context_params = array_merge( $context_params, array( |
| 144 | 'user_firstname' => $current_user->user_firstname, |
| 145 | 'user_lastname' => $current_user->user_lastname, |
| 146 | 'user_email' => $current_user->user_email, |
| 147 | 'home_url' => home_url(), |
| 148 | ) ); |
| 149 | |
| 150 | $fs_user = Freemius::_get_user_by_email( $current_user->user_email ); |
| 151 | |
| 152 | if ( is_object( $fs_user ) && $fs_user->is_verified() ) { |
| 153 | $context_params = array_merge( |
| 154 | $context_params, |
| 155 | FS_Security::instance()->get_context_params( |
| 156 | $fs_user, |
| 157 | $timestamp, |
| 158 | 'checkout' |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if ( $fs->is_payments_sandbox() ) { |
| 165 | // Append plugin secure token for sandbox mode authentication. |
| 166 | $context_params['sandbox'] = FS_Security::instance()->get_secure_token( |
| 167 | $fs->get_plugin(), |
| 168 | $timestamp, |
| 169 | 'checkout' |
| 170 | ); |
| 171 | |
| 172 | /** |
| 173 | * @since 1.1.7.3 Add security timestamp for sandbox even for anonymous user. |
| 174 | */ |
| 175 | if ( empty( $context_params['s_ctx_ts'] ) ) { |
| 176 | $context_params['s_ctx_ts'] = $timestamp; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | $can_user_install = ( |
| 181 | ( $fs->is_plugin() && current_user_can( 'install_plugins' ) ) || |
| 182 | ( $fs->is_theme() && current_user_can( 'install_themes' ) ) |
| 183 | ); |
| 184 | |
| 185 | $filtered_params = $fs->apply_filters('checkout/parameters', $context_params); |
| 186 | |
| 187 | // Allowlist only allowed query params. |
| 188 | $filtered_params = array_intersect_key($filtered_params, $this->_allowed_custom_params); |
| 189 | |
| 190 | return array_merge( $_GET, $context_params, $filtered_params, array( |
| 191 | // Current plugin version. |
| 192 | 'plugin_version' => $fs->get_plugin_version(), |
| 193 | 'sdk_version' => WP_FS__SDK_VERSION, |
| 194 | 'is_premium' => $is_premium ? 'true' : 'false', |
| 195 | 'can_install' => $can_user_install ? 'true' : 'false', |
| 196 | ) ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * The return URL to pass to the checkout when the checkout is loaded in "redirect" mode. |
| 201 | * |
| 202 | * @param Freemius $fs |
| 203 | * |
| 204 | * @return string |
| 205 | */ |
| 206 | public function get_checkout_redirect_return_url( Freemius $fs ) { |
| 207 | $request_url = remove_query_arg( '_wp_http_referer' ); |
| 208 | |
| 209 | return fs_nonce_url( |
| 210 | $fs->checkout_url( |
| 211 | fs_request_get( 'billing_cycle' ), |
| 212 | fs_request_get_bool( 'trial' ), |
| 213 | array( |
| 214 | 'process_redirect' => 'true', |
| 215 | '_wp_http_referer' => $request_url, |
| 216 | ) |
| 217 | ), |
| 218 | $this->get_checkout_redirect_nonce_action( $fs ) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param array $query_params |
| 224 | * @param string $base_url |
| 225 | * |
| 226 | * @return string |
| 227 | */ |
| 228 | public function get_full_checkout_url( array $query_params, $base_url = FS_CHECKOUT__ADDRESS ) { |
| 229 | return $base_url . '/?' . http_build_query( $query_params ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Verifies the redirect after a checkout with the nonce. |
| 234 | * |
| 235 | * @param Freemius $fs |
| 236 | */ |
| 237 | public function verify_checkout_redirect_nonce( Freemius $fs ) { |
| 238 | check_admin_referer( $this->get_checkout_redirect_nonce_action( $fs ) ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get the URL to process a new install after the checkout. |
| 243 | * |
| 244 | * @param Freemius $fs |
| 245 | * @param number $plugin_id |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | public function get_install_url( Freemius $fs, $plugin_id ) { |
| 250 | return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 251 | 'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 252 | 'plugin_id' => $plugin_id, |
| 253 | ) ), $fs->get_unique_affix() . '_activate_new' ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the URL to process a pending activation after the checkout. |
| 258 | * |
| 259 | * @param Freemius $fs |
| 260 | * @param number $plugin_id |
| 261 | * |
| 262 | * @return string |
| 263 | */ |
| 264 | public function get_pending_activation_url( Freemius $fs, $plugin_id ) { |
| 265 | return fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 266 | 'fs_action' => $fs->get_unique_affix() . '_activate_new', |
| 267 | 'plugin_id' => $plugin_id, |
| 268 | 'pending_activation' => true, |
| 269 | 'has_upgrade_context' => true, |
| 270 | ) ), $fs->get_unique_affix() . '_activate_new' ); |
| 271 | } |
| 272 | |
| 273 | private function get_checkout_redirect_nonce_action( Freemius $fs ) { |
| 274 | return $fs->get_unique_affix() . '_checkout_redirect'; |
| 275 | } |
| 276 | } |
| 277 |