| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Admin; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Classes\Plugin_Usage_Tracker; |
| 6 |
use Better_Payment\Lite\Traits\Helper as TraitsHelper; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} // Exit if accessed directly. |
| 11 |
|
| 12 |
/** |
| 13 |
* Setup Wizard |
| 14 |
* |
| 15 |
* The wizard UI is the React quick-setup app (src/admin/quick-setup), mounted by |
| 16 |
* views/better-payment-quick-setup.php. The PHP-rendered steps, footer and their |
| 17 |
* jQuery script (src/js/setup-wizard.js) predated it and were no longer called, |
| 18 |
* so they have been removed. |
| 19 |
* |
| 20 |
* @since 0.0.2 |
| 21 |
*/ |
| 22 |
class Setup_Wizard |
| 23 |
{ |
| 24 |
use TraitsHelper; |
| 25 |
|
| 26 |
protected $file_version; |
| 27 |
|
| 28 |
/** |
| 29 |
* Setup Wizard constructor. |
| 30 |
* |
| 31 |
* @since 0.0.2 |
| 32 |
*/ |
| 33 |
public function __construct() |
| 34 |
{ |
| 35 |
$this->file_version = defined('WP_DEBUG') && WP_DEBUG ? time() : BETTER_PAYMENT_VERSION; |
| 36 |
add_action('admin_enqueue_scripts', array($this, 'setup_wizard_scripts')); |
| 37 |
add_action('admin_menu', array($this, 'admin_menu')); |
| 38 |
add_action('wp_ajax_save_setup_wizard_data', [$this, 'save_setup_wizard_data']); |
| 39 |
add_action( 'wp_ajax_bp_quick_setup_save_tracking', [ $this, 'bp_quick_setup_save_tracking' ] ); |
| 40 |
add_action('in_admin_header', [$this, 'remove_notice'], 1000); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Remove all notice in setup wizard page |
| 45 |
* |
| 46 |
* @since 0.0.2 |
| 47 |
*/ |
| 48 |
public function remove_notice() |
| 49 |
{ |
| 50 |
if (isset($_GET['page']) && $_GET['page'] == 'better-payment-setup-wizard') { |
| 51 |
remove_all_actions('admin_notices'); |
| 52 |
remove_all_actions('all_admin_notices'); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* setup_wizard_scripts |
| 58 |
* @param $hook |
| 59 |
* @return array |
| 60 |
* |
| 61 |
* @since 0.0.2 |
| 62 |
*/ |
| 63 |
public function setup_wizard_scripts($hook) |
| 64 |
{ |
| 65 |
if (isset($hook) && $hook == 'admin_page_better-payment-setup-wizard') { |
| 66 |
// Determine the correct file extension based on debug mode |
| 67 |
$js_ext = file_exists(BETTER_PAYMENT_PATH . '/assets/admin/quick-setup/quick-setup.min.js') ? '.min.js' : '.js'; |
| 68 |
$css_ext = file_exists(BETTER_PAYMENT_PATH . '/assets/admin/quick-setup/quick-setup.min.css') ? '.min.css' : '.css'; |
| 69 |
|
| 70 |
// Enqueue React Quick Setup CSS |
| 71 |
wp_enqueue_style( |
| 72 |
'bp-quick-setup-css', |
| 73 |
BETTER_PAYMENT_ASSETS . '/admin/quick-setup/quick-setup' . $css_ext, |
| 74 |
array(), |
| 75 |
$this->file_version |
| 76 |
); |
| 77 |
|
| 78 |
// Enqueue React Quick Setup JS with WordPress dependencies |
| 79 |
wp_enqueue_script( |
| 80 |
'bp-quick-setup-js', |
| 81 |
BETTER_PAYMENT_ASSETS . '/admin/quick-setup/quick-setup' . $js_ext, |
| 82 |
// wp-hooks: the PayPal step's refund-credentials slot, which Pro fills (utils/proHandoff.js). |
| 83 |
array('wp-element', 'wp-i18n', 'wp-hooks'), |
| 84 |
$this->file_version, |
| 85 |
true |
| 86 |
); |
| 87 |
|
| 88 |
// Localize script with the data the React wizard reads. |
| 89 |
wp_localize_script('bp-quick-setup-js', 'betterPaymentObjWizard', array( |
| 90 |
'admin_nonce' => wp_create_nonce('better_payment_admin_nonce'), |
| 91 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 92 |
'settings' => DB::get_settings(), |
| 93 |
'is_tracking' => get_option('better_payment_settings_opt_in') === 'yes' ? 1 : 0, |
| 94 |
'currencies' => $this->get_currency_list(), |
| 95 |
'is_pro' => defined('BETTER_PAYMENT_PRO_VERSION') && BETTER_PAYMENT_PRO_VERSION ? true : false, |
| 96 |
// Arrives as '1' / '' (wp_localize_script casts scalars); read it with parseProEnabled(). |
| 97 |
'pro_enabled' => \Better_Payment\Lite\Classes\ProCompatibility::pro_enabled(), |
| 98 |
)); |
| 99 |
} |
| 100 |
return []; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Create admin menu for setup wizard |
| 105 |
* |
| 106 |
* @since 0.0.2 |
| 107 |
*/ |
| 108 |
public function admin_menu() |
| 109 |
{ |
| 110 |
|
| 111 |
add_submenu_page( |
| 112 |
"admin.php?page=better-payment-setup-wizard", |
| 113 |
esc_html__('Better Payment ', 'better-payment'), |
| 114 |
esc_html__('Better Payment ', 'better-payment'), |
| 115 |
'manage_options', |
| 116 |
'better-payment-setup-wizard', |
| 117 |
[$this, 'render_wizard'] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* render_wizard |
| 123 |
* |
| 124 |
* @since 0.0.1 |
| 125 |
*/ |
| 126 |
public function render_wizard() |
| 127 |
{ |
| 128 |
?> |
| 129 |
<div class="better-payment-setup-wizard-wrap"> |
| 130 |
<?php |
| 131 |
include BETTER_PAYMENT_ADMIN_VIEWS_PATH . '/better-payment-quick-setup.php'; |
| 132 |
?> |
| 133 |
</div> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
public function bp_quick_setup_save_tracking() { |
| 138 |
// Verify the Nonce |
| 139 |
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'better_payment_admin_nonce')) { |
| 140 |
wp_send_json_error( __( 'Nonce verification failed.', 'better-payment' ) ); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (!current_user_can('manage_options')) { |
| 145 |
wp_send_json_error( __( 'Insufficient permissions.', 'better-payment' ) ); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$context = isset( $_POST['context'] ) ? sanitize_key( wp_unslash( $_POST['context'] ) ) : 'proceed'; |
| 150 |
|
| 151 |
if ( isset( $_POST[ 'is_tracking' ] ) && $_POST[ 'is_tracking' ] === 'true' ) { |
| 152 |
/** |
| 153 |
* `better_payment_settings_opt_in` is NOT the tracking gate — that is |
| 154 |
* `wpins_allow_tracking`, written by wpins_process() below. This option |
| 155 |
* is wizard UI state: it is localized as `is_tracking` (see |
| 156 |
* setup_wizard_scripts()) and makes QuickSetupWizard open on the |
| 157 |
* Settings step with back-navigation to Start disabled. |
| 158 |
* |
| 159 |
* So the Skip path deliberately does not write it. Skip must leave the |
| 160 |
* wizard behaving exactly as it does today — Start screen shown, Back |
| 161 |
* still available — while still turning tracking on. |
| 162 |
* |
| 163 |
* @since 2.3.2 |
| 164 |
*/ |
| 165 |
if ( 'skip' !== $context ) { |
| 166 |
update_option('better_payment_settings_opt_in', 'yes'); |
| 167 |
} |
| 168 |
|
| 169 |
self::wpins_process(); |
| 170 |
wp_send_json_success( __( 'Tracking data saved successfully.', 'better-payment' ) ); |
| 171 |
} else { |
| 172 |
wp_send_json_error( __( 'Invalid tracking parameter.', 'better-payment' ) ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Save setup wizard data |
| 178 |
* |
| 179 |
* @since 0.0.1 |
| 180 |
*/ |
| 181 |
public static function save_setup_wizard_data() |
| 182 |
{ |
| 183 |
/** |
| 184 |
* Verify the Nonce |
| 185 |
*/ |
| 186 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'better_payment_admin_nonce')) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if (!current_user_can('manage_options')) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
try{ |
| 195 |
Settings::save_default_settings(1); |
| 196 |
Settings::save_settings(json_decode(stripslashes($_POST['form_data']), true)); |
| 197 |
|
| 198 |
update_option( 'better_payment_setup_wizard', 'complete' ); |
| 199 |
|
| 200 |
// if (!did_action('elementor/loaded')) { |
| 201 |
// wp_send_json_success( [ 'redirect_url' => admin_url() ] ); |
| 202 |
// }else { |
| 203 |
wp_send_json_success( [ 'redirect_url' => admin_url( 'admin.php?page=better-payment-admin&tab=dashboard' ) ] ); |
| 204 |
// } |
| 205 |
}catch (\Exception $e) { |
| 206 |
wp_send_json_error($e->getMessage()); |
| 207 |
} |
| 208 |
|
| 209 |
die; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* WPIns process |
| 214 |
* |
| 215 |
* The single tracking-activation routine. Every path that turns usage |
| 216 |
* tracking on goes through here — the wizard's "Proceed To Next Step" and |
| 217 |
* "Skip It" buttons, and the automatic opt-in for fresh installs in |
| 218 |
* Traits\Helper::maybe_auto_enable_usage_tracking(). |
| 219 |
* |
| 220 |
* @param bool $force_send Send the payload immediately. True for the wizard |
| 221 |
* buttons, which run inside an AJAX request. Pass |
| 222 |
* FALSE from anything that runs during a page render: |
| 223 |
* send_data() posts with `blocking => true` and a 30s |
| 224 |
* timeout, so a forced send there would stall the |
| 225 |
* response. Consent alone is enough — the caller's |
| 226 |
* Plugin_Usage_Tracker::init() schedules the daily |
| 227 |
* event and the first run sends. |
| 228 |
* |
| 229 |
* @return void |
| 230 |
* @since 0.0.1 |
| 231 |
*/ |
| 232 |
public static function wpins_process( $force_send = true ){ |
| 233 |
$plugin_name = basename( BETTER_PAYMENT_FILE, '.php' ); |
| 234 |
|
| 235 |
if ( class_exists( '\Better_Payment\Lite\Classes\Plugin_Usage_Tracker' ) ){ |
| 236 |
$tracker = Plugin_Usage_Tracker::get_instance( BETTER_PAYMENT_FILE, [ |
| 237 |
'opt_in' => true, |
| 238 |
'goodbye_form' => true, |
| 239 |
'item_id' => '64e1f724b5e14edb343e' |
| 240 |
] ); |
| 241 |
$tracker->set_is_tracking_allowed( true ); |
| 242 |
|
| 243 |
if ( $force_send ) { |
| 244 |
$tracker->do_tracking( true ); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Redirect |
| 251 |
* |
| 252 |
* @since 0.0.1 |
| 253 |
*/ |
| 254 |
public static function redirect() |
| 255 |
{ |
| 256 |
update_option('better_payment_setup_wizard', 'init'); |
| 257 |
wp_redirect(admin_url('admin.php?page=better-payment-setup-wizard')); |
| 258 |
} |
| 259 |
} |
| 260 |
|