PluginProbe
WebberZone Top 10 — Popular Posts / 4.4.0
WebberZone Top 10 — Popular Posts v4.4.0
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / vendor / freemius / includes / managers / class-fs-checkout-manager.php

class-fs-checkout-manager.php in WebberZone Top 10 — Popular Posts 4.4.0, at vendor/freemius/includes/managers/class-fs-checkout-manager.php

306 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * BEGIN TOP 10 PRO MANUAL PATCH (WebberZone)
116 * Reason: Without this override, the checkout context is initialized for the
117 * parent product's plan instead of the selected add-on's, so add-on checkout
118 * lands on the wrong plan/pricing. Each overridden value is validated the same
119 * way the parent-product plan_id is validated above (line ~89) before being
120 * accepted, so a malformed/forged request value can't reach the checkout context.
121 * This block must be re-applied if vendor/freemius is ever manually re-vendored
122 * from a newer Freemius SDK release (it is not composer-managed).
123 */
124 $context_params['plugin_id'] = $plugin_id;
125
126 if ( fs_request_has( 'plan_id' ) && FS_Plugin_Plan::is_valid_id( fs_request_get( 'plan_id' ) ) ) {
127 $context_params['plan_id'] = fs_request_get( 'plan_id' );
128 }
129
130 if ( fs_request_has( 'pricing_id' ) && FS_Pricing::is_valid_id( fs_request_get( 'pricing_id' ) ) ) {
131 $context_params['pricing_id'] = fs_request_get( 'pricing_id' );
132 }
133
134 if ( fs_request_has( 'billing_cycle' ) && in_array( fs_request_get( 'billing_cycle' ), array( WP_FS__PERIOD_MONTHLY, WP_FS__PERIOD_ANNUALLY, WP_FS__PERIOD_LIFETIME ), true ) ) {
135 $context_params['billing_cycle'] = fs_request_get( 'billing_cycle' );
136 }
137
138 if ( fs_request_has( 'is_trial' ) ) {
139 $context_params['is_trial'] = fs_request_get_bool( 'is_trial' );
140 }
141 // END TOP 10 PRO MANUAL PATCH (WebberZone)
142 }
143
144 // Get site context secure params.
145 if ( $fs->is_registered() ) {
146 $site = $fs->get_site();
147
148 if ( $plugin_id != $fs->get_id() ) {
149 if ( $fs->is_addon_activated( $plugin_id ) ) {
150 $fs_addon = Freemius::get_instance_by_id( $plugin_id );
151 $addon_site = $fs_addon->get_site();
152 if ( is_object( $addon_site ) ) {
153 $site = $addon_site;
154 }
155 }
156 }
157
158 $context_params = array_merge(
159 $context_params,
160 FS_Security::instance()->get_context_params(
161 $site,
162 $timestamp,
163 'checkout'
164 )
165 );
166 } else {
167 $current_user = Freemius::_get_current_wp_user();
168
169 // Add site and user info to the request, this information
170 // is NOT being stored unless the user complete the purchase
171 // and agrees to the TOS.
172 $context_params = array_merge( $context_params, array(
173 'user_firstname' => $current_user->user_firstname,
174 'user_lastname' => $current_user->user_lastname,
175 'user_email' => $current_user->user_email,
176 'home_url' => home_url(),
177 ) );
178
179 $fs_user = Freemius::_get_user_by_email( $current_user->user_email );
180
181 if ( is_object( $fs_user ) && $fs_user->is_verified() ) {
182 $context_params = array_merge(
183 $context_params,
184 FS_Security::instance()->get_context_params(
185 $fs_user,
186 $timestamp,
187 'checkout'
188 )
189 );
190 }
191 }
192
193 if ( $fs->is_payments_sandbox() ) {
194 // Append plugin secure token for sandbox mode authentication.
195 $context_params['sandbox'] = FS_Security::instance()->get_secure_token(
196 $fs->get_plugin(),
197 $timestamp,
198 'checkout'
199 );
200
201 /**
202 * @since 1.1.7.3 Add security timestamp for sandbox even for anonymous user.
203 */
204 if ( empty( $context_params['s_ctx_ts'] ) ) {
205 $context_params['s_ctx_ts'] = $timestamp;
206 }
207 }
208
209 $can_user_install = (
210 ( $fs->is_plugin() && current_user_can( 'install_plugins' ) ) ||
211 ( $fs->is_theme() && current_user_can( 'install_themes' ) )
212 );
213
214 $filtered_params = $fs->apply_filters('checkout/parameters', $context_params);
215
216 // Allowlist only allowed query params.
217 $filtered_params = array_intersect_key($filtered_params, $this->_allowed_custom_params);
218
219 return array_merge( $_GET, $context_params, $filtered_params, array(
220 // Current plugin version.
221 'plugin_version' => $fs->get_plugin_version(),
222 'sdk_version' => WP_FS__SDK_VERSION,
223 'is_premium' => $is_premium ? 'true' : 'false',
224 'can_install' => $can_user_install ? 'true' : 'false',
225 ) );
226 }
227
228 /**
229 * The return URL to pass to the checkout when the checkout is loaded in "redirect" mode.
230 *
231 * @param Freemius $fs
232 *
233 * @return string
234 */
235 public function get_checkout_redirect_return_url( Freemius $fs ) {
236 $request_url = remove_query_arg( '_wp_http_referer' );
237
238 return fs_nonce_url(
239 $fs->checkout_url(
240 fs_request_get( 'billing_cycle' ),
241 fs_request_get_bool( 'trial' ),
242 array(
243 'process_redirect' => 'true',
244 '_wp_http_referer' => $request_url,
245 )
246 ),
247 $this->get_checkout_redirect_nonce_action( $fs )
248 );
249 }
250
251 /**
252 * @param array $query_params
253 * @param string $base_url
254 *
255 * @return string
256 */
257 public function get_full_checkout_url( array $query_params, $base_url = FS_CHECKOUT__ADDRESS ) {
258 return $base_url . '/?' . http_build_query( $query_params );
259 }
260
261 /**
262 * Verifies the redirect after a checkout with the nonce.
263 *
264 * @param Freemius $fs
265 */
266 public function verify_checkout_redirect_nonce( Freemius $fs ) {
267 check_admin_referer( $this->get_checkout_redirect_nonce_action( $fs ) );
268 }
269
270 /**
271 * Get the URL to process a new install after the checkout.
272 *
273 * @param Freemius $fs
274 * @param number $plugin_id
275 *
276 * @return string
277 */
278 public function get_install_url( Freemius $fs, $plugin_id ) {
279 return fs_nonce_url( $fs->_get_admin_page_url( 'account', array(
280 'fs_action' => $fs->get_unique_affix() . '_activate_new',
281 'plugin_id' => $plugin_id,
282 ) ), $fs->get_unique_affix() . '_activate_new' );
283 }
284
285 /**
286 * Get the URL to process a pending activation after the checkout.
287 *
288 * @param Freemius $fs
289 * @param number $plugin_id
290 *
291 * @return string
292 */
293 public function get_pending_activation_url( Freemius $fs, $plugin_id ) {
294 return fs_nonce_url( $fs->_get_admin_page_url( 'account', array(
295 'fs_action' => $fs->get_unique_affix() . '_activate_new',
296 'plugin_id' => $plugin_id,
297 'pending_activation' => true,
298 'has_upgrade_context' => true,
299 ) ), $fs->get_unique_affix() . '_activate_new' );
300 }
301
302 private function get_checkout_redirect_nonce_action( Freemius $fs ) {
303 return $fs->get_unique_affix() . '_checkout_redirect';
304 }
305 }
306