| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setting General - Ajax. |
| 4 |
* |
| 5 |
* @package Pages |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Pages\Settings; |
| 9 |
|
| 10 |
use LassoLite\Admin\Constant; |
| 11 |
|
| 12 |
use LassoLite\Classes\Amazon_Api; |
| 13 |
use LassoLite\Classes\Enum; |
| 14 |
use LassoLite\Classes\Helper; |
| 15 |
use LassoLite\Classes\License; |
| 16 |
use LassoLite\Classes\Page; |
| 17 |
use LassoLite\Classes\Setting; |
| 18 |
|
| 19 |
/** |
| 20 |
* Setting General - Ajax. |
| 21 |
*/ |
| 22 |
class Ajax { |
| 23 |
/** |
| 24 |
* Declare "SURLs ajax requests" to WordPress. |
| 25 |
*/ |
| 26 |
public function register_hooks() { |
| 27 |
add_action( 'wp_ajax_lasso_lite_save_settings_amazon', array( $this, 'lasso_lite_save_settings_amazon' ) ); |
| 28 |
add_action( 'wp_ajax_lasso_lite_validate_lite_account_email', array( $this, 'lasso_lite_validate_lite_account_email' ) ); |
| 29 |
add_action( 'wp_ajax_lasso_lite_sync_lite_account_via_existing_login', array( $this, 'lasso_lite_sync_lite_account_via_existing_login' ) ); |
| 30 |
add_action( 'wp_ajax_lasso_lite_verify_amazon_creators_credentials', array( $this, 'lasso_lite_verify_amazon_creators_credentials' ) ); |
| 31 |
add_action( 'wp_ajax_lasso_lite_save_settings_general', array( $this, 'lasso_lite_save_settings_general' ) ); |
| 32 |
add_action( 'wp_ajax_lasso_lite_store_settings', array( $this, 'lasso_lite_store_settings' ) ); |
| 33 |
add_action( 'wp_ajax_lasso_lite_reactivate_license', array( $this, 'lasso_lite_reactivate_license' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Add a Field to a Product |
| 38 |
*/ |
| 39 |
public function lasso_lite_save_settings_amazon() { |
| 40 |
Helper::verify_access_and_nonce(); |
| 41 |
|
| 42 |
$post = Helper::POST(); |
| 43 |
$amazon_tracking_id = sanitize_text_field( $post['amazon_tracking_id'] ?? '' ); |
| 44 |
$amazon_access_key_id = sanitize_text_field( $post['amazon_access_key_id'] ?? '' ); |
| 45 |
$amazon_secret_key = sanitize_text_field( $post['amazon_secret_key'] ?? '' ); |
| 46 |
$amazon_creators_credential_id = sanitize_text_field( $post['amazon_creators_credential_id'] ?? '' ); |
| 47 |
$amazon_creators_secret = sanitize_text_field( $post['amazon_creators_secret'] ?? '' ); |
| 48 |
$amazon_creators_version = sanitize_text_field( $post['amazon_creators_version'] ?? '' ); |
| 49 |
$amazon_creators_partner_tag = sanitize_text_field( $post['amazon_creators_partner_tag'] ?? '' ); |
| 50 |
$amazon_default_tracking_country = sanitize_text_field( $post['amazon_default_tracking_country'] ?? '' ); |
| 51 |
$amazon_pricing_daily = $post['amazon_pricing_daily'] ?? ''; |
| 52 |
$auto_monetize_amazon = $post['auto_monetize_amazon'] ?? ''; |
| 53 |
$auto_upgrade_eligible_links = $post['auto_upgrade_eligible_links'] ?? ''; |
| 54 |
|
| 55 |
$options['amazon_tracking_id'] = $amazon_tracking_id; |
| 56 |
$options['amazon_access_key_id'] = $amazon_access_key_id; |
| 57 |
$options['amazon_secret_key'] = $amazon_secret_key; |
| 58 |
$options['amazon_creators_credential_id'] = $amazon_creators_credential_id; |
| 59 |
$options['amazon_creators_secret'] = $amazon_creators_secret; |
| 60 |
$options['amazon_creators_version'] = $amazon_creators_version; |
| 61 |
$options['amazon_creators_partner_tag'] = $amazon_creators_partner_tag; |
| 62 |
$options['amazon_default_tracking_country'] = $amazon_default_tracking_country; |
| 63 |
$options['amazon_pricing_daily'] = 'on' === $amazon_pricing_daily; |
| 64 |
$options['auto_monetize_amazon'] = 'on' === $auto_monetize_amazon; |
| 65 |
$options['auto_upgrade_eligible_links'] = 'on' === $auto_upgrade_eligible_links; |
| 66 |
Setting::set_settings( $options ); |
| 67 |
$creators_validation = $this->maybe_validate_amazon_creators_credentials_on_save( $options ); |
| 68 |
|
| 69 |
if ( ! empty( $amazon_tracking_id ) ) { |
| 70 |
update_option( Enum::SETUP_AMZ_TRACKING_ID, true ); |
| 71 |
} |
| 72 |
|
| 73 |
$data['msg'] = 'All settings saved'; |
| 74 |
$data['creators_validation_attempted'] = $creators_validation['attempted']; |
| 75 |
$data['creators_validation_success'] = $creators_validation['success']; |
| 76 |
$data['creators_validation_msg'] = $creators_validation['msg']; |
| 77 |
$data['creators_validation_lite_account_cta'] = ! empty( $creators_validation['lite_account_validate_cta'] ); |
| 78 |
wp_send_json_success( $data ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Step 1: GET {LASSO_LINK}/account/existing — same as FastAPI `lite_existing_account_endpoint` for GET. |
| 83 |
* Required outbound headers: `site-url`, `email` (see lambda_redir_post_token `app.py`). |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function lasso_lite_validate_lite_account_email() { |
| 88 |
Helper::verify_access_and_nonce(); |
| 89 |
|
| 90 |
$post = Helper::POST(); |
| 91 |
$email = sanitize_email( $post['email'] ?? '' ); |
| 92 |
if ( empty( $email ) ) { |
| 93 |
wp_send_json_error( array( 'msg' => 'Email is required.' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
$lookup = Helper::send_request( |
| 97 |
'get', |
| 98 |
rtrim( Constant::LASSO_LINK, '/' ) . '/account/existing', |
| 99 |
array(), |
| 100 |
array( |
| 101 |
'site-url' => site_url(), |
| 102 |
'email' => $email, |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
$body = $lookup['response'] ?? null; |
| 107 |
$status = (int) ( $lookup['status_code'] ?? 500 ); |
| 108 |
|
| 109 |
if ( $status >= 400 || empty( $body ) ) { |
| 110 |
$msg = 'Unable to verify your email. Try again.'; |
| 111 |
if ( is_object( $body ) && ! empty( $body->message ) && is_string( $body->message ) ) { |
| 112 |
$msg = $body->message; |
| 113 |
} |
| 114 |
wp_send_json_error( array( 'msg' => $msg ) ); |
| 115 |
} |
| 116 |
|
| 117 |
$data = $this->normalize_lite_account_existing_response_data( $body ); |
| 118 |
if ( null === $data ) { |
| 119 |
wp_send_json_error( array( 'msg' => 'Unexpected response from Lasso.' ) ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! empty( $data->needs_signup ) ) { |
| 123 |
wp_send_json_success( |
| 124 |
array( |
| 125 |
'needs_signup' => true, |
| 126 |
'msg' => 'No Lasso account was found for that email. Create a free account below.', |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $data->exists ) ) { |
| 132 |
$account_email = sanitize_email( $data->email ?? '' ); |
| 133 |
$user_id = intval( $data->user_id ?? 0 ); |
| 134 |
if ( empty( $account_email ) || $user_id <= 0 ) { |
| 135 |
wp_send_json_error( array( 'msg' => 'Unexpected response from Lasso.' ) ); |
| 136 |
} |
| 137 |
Helper::update_option( Constant::LASSO_ACCOUNT_EMAIL, $account_email ); |
| 138 |
Helper::update_option( Constant::LASSO_ACCOUNT_USER_ID, $user_id ); |
| 139 |
Setting::set_setting( Enum::EMAIL_SUPPORT, $account_email ); |
| 140 |
wp_send_json_success( |
| 141 |
array( |
| 142 |
'linked' => true, |
| 143 |
'needs_signup' => false, |
| 144 |
'link_account_required' => false, |
| 145 |
'msg' => 'Your Lasso account is already linked for this site.', |
| 146 |
'email' => $account_email, |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
wp_send_json_success( |
| 152 |
array( |
| 153 |
'needs_signup' => false, |
| 154 |
'linked' => false, |
| 155 |
'link_account_required' => true, |
| 156 |
'msg' => 'Click the button again to link this site.', |
| 157 |
) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Step 2: POST {LASSO_LINK}/account/existing — same as FastAPI `lite_existing_account_endpoint` for POST. |
| 163 |
* JSON body: `email`, `site_url`, `mode` = `creators_api` (Lambda skips dim_sites binding when mode matches). |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function lasso_lite_sync_lite_account_via_existing_login() { |
| 168 |
Helper::verify_access_and_nonce(); |
| 169 |
|
| 170 |
$post = Helper::POST(); |
| 171 |
$email = sanitize_email( $post['email'] ?? '' ); |
| 172 |
|
| 173 |
if ( empty( $email ) ) { |
| 174 |
wp_send_json_error( array( 'msg' => 'Email is required.' ) ); |
| 175 |
} |
| 176 |
|
| 177 |
$url = rtrim( Constant::LASSO_LINK, '/' ) . '/account/existing'; |
| 178 |
$request_payload = array( |
| 179 |
'email' => $email, |
| 180 |
'site_url' => site_url(), |
| 181 |
'mode' => 'creators_api', |
| 182 |
); |
| 183 |
|
| 184 |
$response = Helper::send_request( |
| 185 |
'post', |
| 186 |
$url, |
| 187 |
$request_payload, |
| 188 |
array( |
| 189 |
'Content-Type' => 'application/json', |
| 190 |
) |
| 191 |
); |
| 192 |
|
| 193 |
$lookup_body = $response['response'] ?? null; |
| 194 |
$status = (int) ( $response['status_code'] ?? 500 ); |
| 195 |
|
| 196 |
if ( empty( $lookup_body ) || $status >= 400 ) { |
| 197 |
$error_message = 'Unable to verify your Lasso account. Try again.'; |
| 198 |
if ( is_object( $lookup_body ) && ! empty( $lookup_body->message ) && is_string( $lookup_body->message ) ) { |
| 199 |
$error_message = $lookup_body->message; |
| 200 |
} elseif ( is_object( $lookup_body ) && ! empty( $lookup_body->error ) && is_string( $lookup_body->error ) ) { |
| 201 |
$error_message = $lookup_body->error; |
| 202 |
} |
| 203 |
wp_send_json_error( array( 'msg' => $error_message ) ); |
| 204 |
} |
| 205 |
|
| 206 |
$data = $this->normalize_lite_account_existing_response_data( $lookup_body ); |
| 207 |
if ( null === $data ) { |
| 208 |
wp_send_json_error( array( 'msg' => 'Unexpected response from Lasso.' ) ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! empty( $data->needs_signup ) ) { |
| 212 |
wp_send_json_success( |
| 213 |
array( |
| 214 |
'needs_signup' => true, |
| 215 |
'msg' => 'No Lasso account was found for that email. Create a free account below.', |
| 216 |
) |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
if ( ! empty( $data->exists ) ) { |
| 221 |
$account_email = sanitize_email( $data->email ?? '' ); |
| 222 |
$user_id = intval( $data->user_id ?? 0 ); |
| 223 |
|
| 224 |
if ( empty( $account_email ) || $user_id <= 0 ) { |
| 225 |
wp_send_json_error( array( 'msg' => 'Unexpected response from Lasso.' ) ); |
| 226 |
} |
| 227 |
|
| 228 |
Helper::update_option( Constant::LASSO_ACCOUNT_EMAIL, $account_email ); |
| 229 |
Helper::update_option( Constant::LASSO_ACCOUNT_USER_ID, $user_id ); |
| 230 |
Setting::set_setting( Enum::EMAIL_SUPPORT, $account_email ); |
| 231 |
|
| 232 |
wp_send_json_success( |
| 233 |
array( |
| 234 |
'linked' => true, |
| 235 |
'needs_signup' => false, |
| 236 |
'link_account_required' => false, |
| 237 |
'msg' => 'Your Lasso account is now linked for this site.', |
| 238 |
'email' => $account_email, |
| 239 |
) |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
if ( ! empty( $data->link_account_required ) ) { |
| 244 |
wp_send_json_success( |
| 245 |
array( |
| 246 |
'needs_signup' => false, |
| 247 |
'linked' => false, |
| 248 |
'link_account_required' => true, |
| 249 |
'msg' => 'Click the button again to link this site.', |
| 250 |
) |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
wp_send_json_error( array( 'msg' => 'No Lasso account was found for this site for that email.' ) ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Verify Amazon Creators API credentials against Lambda. |
| 259 |
*/ |
| 260 |
public function lasso_lite_verify_amazon_creators_credentials() { |
| 261 |
Helper::verify_access_and_nonce(); |
| 262 |
|
| 263 |
$post = Helper::POST(); |
| 264 |
|
| 265 |
$credential_id = sanitize_text_field( $post['amazon_creators_credential_id'] ?? '' ); |
| 266 |
$secret = sanitize_text_field( $post['amazon_creators_secret'] ?? '' ); |
| 267 |
$credential_version = sanitize_text_field( $post['amazon_creators_version'] ?? '' ); |
| 268 |
$partner_tag = sanitize_text_field( $post['amazon_creators_partner_tag'] ?? '' ); |
| 269 |
$country = sanitize_text_field( $post['amazon_default_tracking_country'] ?? '' ); |
| 270 |
$email = $this->get_lite_account_email(); |
| 271 |
$normalized_email = is_string( $email ) ? strtolower( trim( $email ) ) : ''; |
| 272 |
$token = md5( $normalized_email ); |
| 273 |
|
| 274 |
$missing_fields = array(); |
| 275 |
if ( empty( $credential_id ) ) { |
| 276 |
$missing_fields[] = 'Credential ID'; |
| 277 |
} |
| 278 |
if ( empty( $secret ) ) { |
| 279 |
$missing_fields[] = 'Secret'; |
| 280 |
} |
| 281 |
if ( empty( $credential_version ) ) { |
| 282 |
$missing_fields[] = 'Version'; |
| 283 |
} |
| 284 |
if ( empty( $partner_tag ) ) { |
| 285 |
$missing_fields[] = 'Partner tag'; |
| 286 |
} |
| 287 |
|
| 288 |
if ( ! empty( $missing_fields ) ) { |
| 289 |
wp_send_json_error( |
| 290 |
array( |
| 291 |
'msg' => 'Missing required fields: ' . implode( ', ', $missing_fields ), |
| 292 |
) |
| 293 |
); |
| 294 |
} |
| 295 |
|
| 296 |
if ( empty( $normalized_email ) ) { |
| 297 |
wp_send_json_error( |
| 298 |
array( |
| 299 |
'msg' => 'Connect your Lite account to app.getlasso.co before validating Creators API credentials.', |
| 300 |
'lite_account_validate_cta' => true, |
| 301 |
) |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
$result = $this->verify_amazon_creators_credentials_with_lasso_api( |
| 306 |
$credential_id, |
| 307 |
$secret, |
| 308 |
$credential_version, |
| 309 |
$partner_tag, |
| 310 |
$country, |
| 311 |
$token |
| 312 |
); |
| 313 |
|
| 314 |
if ( ! $result['success'] ) { |
| 315 |
$this->clear_stored_amazon_creators_credentials(); |
| 316 |
wp_send_json_error( |
| 317 |
array( |
| 318 |
'msg' => $result['msg'], |
| 319 |
'status_code' => $result['status_code'], |
| 320 |
) |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
Setting::set_settings( |
| 325 |
array( |
| 326 |
'amazon_creators_credential_id' => $credential_id, |
| 327 |
'amazon_creators_secret' => $secret, |
| 328 |
'amazon_creators_version' => $credential_version, |
| 329 |
'amazon_creators_partner_tag' => $partner_tag, |
| 330 |
'amazon_default_tracking_country' => $country, |
| 331 |
) |
| 332 |
); |
| 333 |
$this->mark_amazon_credentials_notice_updated(); |
| 334 |
$this->update_amazon_creators_verified_signature( |
| 335 |
array( |
| 336 |
'amazon_creators_credential_id' => $credential_id, |
| 337 |
'amazon_creators_secret' => $secret, |
| 338 |
'amazon_creators_version' => $credential_version, |
| 339 |
'amazon_creators_partner_tag' => $partner_tag, |
| 340 |
'amazon_default_tracking_country' => $country, |
| 341 |
) |
| 342 |
); |
| 343 |
|
| 344 |
wp_send_json_success( |
| 345 |
array( |
| 346 |
'msg' => $result['msg'], |
| 347 |
) |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Lite account email from plugin options (LASSO_ACCOUNT_EMAIL). |
| 353 |
* |
| 354 |
* @return string Normalized use: empty string when not linked. |
| 355 |
*/ |
| 356 |
private function get_lite_account_email() { |
| 357 |
$email = Helper::get_option( Constant::LASSO_ACCOUNT_EMAIL, '' ); |
| 358 |
if ( is_string( $email ) && '' !== trim( $email ) ) { |
| 359 |
return $email; |
| 360 |
} |
| 361 |
return ''; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Extract the `data` object from lasso.link /account/existing JSON (Lambda/FastAPI). |
| 366 |
* |
| 367 |
* Supports: wrapped `{ "message", "data": { ... } }`, flattened `{ "exists", ... }`, |
| 368 |
* and `data` as associative array from json_decode. |
| 369 |
* |
| 370 |
* @param mixed $body Decoded JSON root from Helper::send_request(). |
| 371 |
* @return object|null Normalized object for Lite validate logic, or null if unusable. |
| 372 |
*/ |
| 373 |
private function normalize_lite_account_existing_response_data( $body ) { |
| 374 |
if ( ! is_object( $body ) ) { |
| 375 |
return null; |
| 376 |
} |
| 377 |
$data = null; |
| 378 |
if ( property_exists( $body, 'data' ) ) { |
| 379 |
$data = $body->data; |
| 380 |
} elseif ( property_exists( $body, 'exists' ) || property_exists( $body, 'needs_signup' ) || property_exists( $body, 'email_exists' ) ) { |
| 381 |
$data = $body; |
| 382 |
} |
| 383 |
if ( null === $data ) { |
| 384 |
return null; |
| 385 |
} |
| 386 |
if ( is_array( $data ) ) { |
| 387 |
return (object) $data; |
| 388 |
} |
| 389 |
if ( ! is_object( $data ) ) { |
| 390 |
return null; |
| 391 |
} |
| 392 |
return $data; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Validate Creators credentials after save; clears stored Creators fields if remote verification fails. |
| 397 |
* |
| 398 |
* @param array $settings Saved settings. |
| 399 |
* @return array |
| 400 |
*/ |
| 401 |
private function maybe_validate_amazon_creators_credentials_on_save( $settings ) { |
| 402 |
$result = array( |
| 403 |
'attempted' => false, |
| 404 |
'success' => null, |
| 405 |
'msg' => '', |
| 406 |
'credentials_cleared' => false, |
| 407 |
'lite_account_validate_cta' => false, |
| 408 |
); |
| 409 |
|
| 410 |
if ( ! $this->has_complete_amazon_creators_credentials( $settings ) ) { |
| 411 |
return $result; |
| 412 |
} |
| 413 |
|
| 414 |
if ( $this->has_verified_amazon_creators_signature( $settings ) ) { |
| 415 |
return $result; |
| 416 |
} |
| 417 |
|
| 418 |
$result['attempted'] = true; |
| 419 |
$email = $this->get_lite_account_email(); |
| 420 |
$normalized_email = is_string( $email ) ? strtolower( trim( $email ) ) : ''; |
| 421 |
|
| 422 |
if ( empty( $normalized_email ) ) { |
| 423 |
$result['success'] = false; |
| 424 |
$result['msg'] = 'Settings were saved, but Creators API credentials could not be validated because your Lite account is not connected.'; |
| 425 |
$result['lite_account_validate_cta'] = true; |
| 426 |
|
| 427 |
return $result; |
| 428 |
} |
| 429 |
|
| 430 |
$result = $this->verify_amazon_creators_credentials_with_lasso_api( |
| 431 |
$settings['amazon_creators_credential_id'], |
| 432 |
$settings['amazon_creators_secret'], |
| 433 |
$settings['amazon_creators_version'], |
| 434 |
$settings['amazon_creators_partner_tag'], |
| 435 |
$settings['amazon_default_tracking_country'] ?? '', |
| 436 |
md5( $normalized_email ) |
| 437 |
); |
| 438 |
$result['attempted'] = true; |
| 439 |
$result['credentials_cleared'] = false; |
| 440 |
|
| 441 |
if ( $result['success'] ) { |
| 442 |
$this->mark_amazon_credentials_notice_updated(); |
| 443 |
$this->update_amazon_creators_verified_signature( $settings ); |
| 444 |
} else { |
| 445 |
$result['msg'] = 'Settings were saved, but Creators API credentials could not be validated. ' . $result['msg']; |
| 446 |
$result['credentials_cleared'] = true; |
| 447 |
$this->clear_stored_amazon_creators_credentials(); |
| 448 |
} |
| 449 |
|
| 450 |
return $result; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Remove persisted Amazon Creators API credential fields after failed verification. |
| 455 |
* |
| 456 |
* @return void |
| 457 |
*/ |
| 458 |
private function clear_stored_amazon_creators_credentials() { |
| 459 |
Setting::set_settings( |
| 460 |
array( |
| 461 |
'amazon_creators_credential_id' => '', |
| 462 |
'amazon_creators_secret' => '', |
| 463 |
'amazon_creators_version' => '', |
| 464 |
'amazon_creators_partner_tag' => '', |
| 465 |
) |
| 466 |
); |
| 467 |
Helper::update_option( Constant::LASSO_OPTION_AMAZON_CREATORS_VERIFIED_SIGNATURE, '' ); |
| 468 |
Helper::update_option( Constant::LASSO_OPTION_AMAZON_CREDENTIALS_UPDATED, '0' ); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Extract a human-readable Creators API validation message. |
| 473 |
* |
| 474 |
* @param mixed $response_body Response body from Lambda proxy. |
| 475 |
* @param string $default_message Default message fallback. |
| 476 |
* @return string |
| 477 |
*/ |
| 478 |
private function get_creators_validate_message( $response_body, $default_message ) { |
| 479 |
return $this->extract_creators_validate_message_from_payload( $response_body, $default_message ); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Recursively extract a human-readable message from a nested/stringified payload. |
| 484 |
* |
| 485 |
* @param mixed $payload Payload from Lambda/FastAPI. |
| 486 |
* @param string $default_message Default message fallback. |
| 487 |
* @return string |
| 488 |
*/ |
| 489 |
private function extract_creators_validate_message_from_payload( $payload, $default_message ) { |
| 490 |
if ( is_string( $payload ) ) { |
| 491 |
$decoded_payload = json_decode( $payload ); |
| 492 |
if ( JSON_ERROR_NONE === json_last_error() && ( is_object( $decoded_payload ) || is_array( $decoded_payload ) ) ) { |
| 493 |
return $this->extract_creators_validate_message_from_payload( $decoded_payload, $default_message ); |
| 494 |
} |
| 495 |
|
| 496 |
return '' !== trim( $payload ) ? $payload : $default_message; |
| 497 |
} |
| 498 |
|
| 499 |
if ( is_array( $payload ) ) { |
| 500 |
$payload = json_decode( wp_json_encode( $payload ) ); |
| 501 |
} |
| 502 |
|
| 503 |
if ( ! is_object( $payload ) ) { |
| 504 |
return $default_message; |
| 505 |
} |
| 506 |
|
| 507 |
if ( ! empty( $payload->detail ) ) { |
| 508 |
return $this->extract_creators_validate_message_from_payload( $payload->detail, $default_message ); |
| 509 |
} |
| 510 |
|
| 511 |
$error_type = ''; |
| 512 |
if ( ! empty( $payload->type ) && is_string( $payload->type ) ) { |
| 513 |
$error_type = $payload->type; |
| 514 |
} |
| 515 |
|
| 516 |
if ( 'ThrottleException' === $error_type ) { |
| 517 |
return 'The system is busy right now. Please try again in about 1 minute.'; |
| 518 |
} |
| 519 |
|
| 520 |
if ( ! empty( $payload->message ) ) { |
| 521 |
$message = $this->extract_creators_validate_message_from_payload( $payload->message, $default_message ); |
| 522 |
if ( $default_message !== $message || is_string( $payload->message ) ) { |
| 523 |
return $message; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if ( ! empty( $payload->fieldList[0]->message ) && is_string( $payload->fieldList[0]->message ) ) { |
| 528 |
return $payload->fieldList[0]->message; |
| 529 |
} |
| 530 |
|
| 531 |
return $default_message; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Check whether Creators credentials are complete. |
| 536 |
* |
| 537 |
* @param array $settings Settings values. |
| 538 |
* @return bool |
| 539 |
*/ |
| 540 |
private function has_complete_amazon_creators_credentials( $settings ) { |
| 541 |
$credential_fields = array( |
| 542 |
'amazon_creators_credential_id', |
| 543 |
'amazon_creators_secret', |
| 544 |
'amazon_creators_version', |
| 545 |
'amazon_creators_partner_tag', |
| 546 |
); |
| 547 |
|
| 548 |
foreach ( $credential_fields as $field ) { |
| 549 |
if ( '' === trim( (string) ( $settings[ $field ] ?? '' ) ) ) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
return true; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Whether the current payload matches the last successful verification. |
| 559 |
* |
| 560 |
* @param array $settings Settings values. |
| 561 |
* @return bool |
| 562 |
*/ |
| 563 |
private function has_verified_amazon_creators_signature( $settings ) { |
| 564 |
$current_signature = $this->get_amazon_creators_signature( $settings ); |
| 565 |
$saved_signature = (string) Helper::get_option( Constant::LASSO_OPTION_AMAZON_CREATORS_VERIFIED_SIGNATURE, '' ); |
| 566 |
|
| 567 |
return '' !== $current_signature && '' !== $saved_signature && hash_equals( $saved_signature, $current_signature ); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Persist the last successful Creators verification signature. |
| 572 |
* |
| 573 |
* @param array $settings Settings values. |
| 574 |
* @return void |
| 575 |
*/ |
| 576 |
private function update_amazon_creators_verified_signature( $settings ) { |
| 577 |
Helper::update_option( Constant::LASSO_OPTION_AMAZON_CREATORS_VERIFIED_SIGNATURE, $this->get_amazon_creators_signature( $settings ) ); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Build a stable signature for the Creators payload. |
| 582 |
* |
| 583 |
* @param array $settings Settings values. |
| 584 |
* @return string |
| 585 |
*/ |
| 586 |
private function get_amazon_creators_signature( $settings ) { |
| 587 |
return Amazon_Api::get_amazon_creators_signature( $settings ); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Send Creators credentials to Lasso API for validation. |
| 592 |
* |
| 593 |
* @param string $credential_id Credential ID. |
| 594 |
* @param string $secret Credential secret. |
| 595 |
* @param string $credential_version Credential version. |
| 596 |
* @param string $partner_tag Partner tag. |
| 597 |
* @param string $country Tracking country. |
| 598 |
* @param string $token Lite account token. |
| 599 |
* @return array |
| 600 |
*/ |
| 601 |
private function verify_amazon_creators_credentials_with_lasso_api( $credential_id, $secret, $credential_version, $partner_tag, $country, $token ) { |
| 602 |
$headers = Helper::get_headers(); |
| 603 |
$headers['token'] = $token; |
| 604 |
$verify_payload = array( |
| 605 |
'credential_id' => $credential_id, |
| 606 |
'secret' => $secret, |
| 607 |
'credential_version' => $credential_version, |
| 608 |
'partner_tag' => $partner_tag, |
| 609 |
'country' => $country, |
| 610 |
); |
| 611 |
|
| 612 |
$response = Helper::send_request( |
| 613 |
'post', |
| 614 |
rtrim( Constant::LASSO_LINK, '/' ) . '/amazon/creators/credentials/verify', |
| 615 |
$verify_payload, |
| 616 |
$headers |
| 617 |
); |
| 618 |
|
| 619 |
$status_code = intval( $response['status_code'] ?? 500 ); |
| 620 |
$response_body = $response['response'] ?? null; |
| 621 |
$error_message = $this->get_creators_validate_message( $response_body, 'Unable to verify Creators API credentials.' ); |
| 622 |
$success_message = $this->get_creators_validate_message( $response_body, 'Creators API credentials verified.' ); |
| 623 |
|
| 624 |
if ( $status_code >= 400 || empty( $response_body ) || empty( $response_body->result ) ) { |
| 625 |
return array( |
| 626 |
'success' => false, |
| 627 |
'msg' => $error_message, |
| 628 |
'status_code' => $status_code, |
| 629 |
); |
| 630 |
} |
| 631 |
|
| 632 |
return array( |
| 633 |
'success' => true, |
| 634 |
'msg' => $success_message, |
| 635 |
'status_code' => $status_code, |
| 636 |
); |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* Permanently close the Amazon credentials update notice after successful migration. |
| 641 |
*/ |
| 642 |
private function mark_amazon_credentials_notice_updated() { |
| 643 |
Helper::update_option( Constant::LASSO_OPTION_AMAZON_CREDENTIALS_UPDATED, '1' ); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Save settings general |
| 648 |
*/ |
| 649 |
public function lasso_lite_save_settings_general() { |
| 650 |
Helper::verify_access_and_nonce(); |
| 651 |
|
| 652 |
$post = Helper::POST(); |
| 653 |
$general_disable_amazon_notifications = $post['general_disable_amazon_notifications'] ?? Constant::DEFAULT_SETTINGS['general_disable_amazon_notifications']; |
| 654 |
$general_disable_tooltip = $post['general_disable_tooltip'] ?? Constant::DEFAULT_SETTINGS['general_disable_tooltip']; |
| 655 |
$general_disable_notification = $post['general_disable_notification'] ?? Constant::DEFAULT_SETTINGS['general_disable_notification']; |
| 656 |
$general_enable_new_ui = $post['general_enable_new_ui'] ?? Constant::DEFAULT_SETTINGS['general_enable_new_ui']; |
| 657 |
$performance_event_tracking = $post['performance_event_tracking'] ?? Constant::DEFAULT_SETTINGS['performance_event_tracking']; |
| 658 |
|
| 659 |
$options['general_disable_amazon_notifications'] = $general_disable_amazon_notifications; |
| 660 |
$options['general_disable_tooltip'] = $general_disable_tooltip; |
| 661 |
$options['general_disable_notification'] = $general_disable_notification; |
| 662 |
$options['performance_event_tracking'] = $performance_event_tracking; |
| 663 |
|
| 664 |
Setting::set_settings( $options ); |
| 665 |
$data['msg'] = 'All settings saved'; |
| 666 |
|
| 667 |
// ? disable new UI |
| 668 |
if ( ! $general_enable_new_ui ) { |
| 669 |
update_option( Enum::LASSO_LITE_ACTIVE, 0 ); // ? fix conflict with L.235 |
| 670 |
update_option( Enum::SWITCH_TO_NEW_UI, 0 ); |
| 671 |
|
| 672 |
$data['redirect_url'] = Page::get_page_url(); |
| 673 |
} |
| 674 |
|
| 675 |
wp_send_json_success( $data ); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Store Lasso Lite settings |
| 680 |
*/ |
| 681 |
public function lasso_lite_store_settings() { |
| 682 |
Helper::verify_access_and_nonce(); |
| 683 |
|
| 684 |
$data = Helper::POST(); |
| 685 |
|
| 686 |
if ( empty( $data['settings'] ) ) { |
| 687 |
wp_send_json_error( 'No settings to save.' ); |
| 688 |
} |
| 689 |
|
| 690 |
// ? User can not change the Disclosure text |
| 691 |
unset( $data['settings']['disclosure_text'] ); |
| 692 |
|
| 693 |
$settings = $data['settings']; |
| 694 |
$options = $settings; |
| 695 |
|
| 696 |
// ? Loop and check for checkbox values, convert them to boolean. |
| 697 |
foreach ( $settings as $key => $value ) { |
| 698 |
if ( is_array( $value ) ) { |
| 699 |
$options[ $key ] = $value; |
| 700 |
} elseif ( 'true' === (string) $value ) { |
| 701 |
$options[ $key ] = true; |
| 702 |
} elseif ( 'false' === (string) $value ) { |
| 703 |
$options[ $key ] = false; |
| 704 |
} else { |
| 705 |
$options[ $key ] = trim( $value ); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
// ? update settings |
| 710 |
Setting::set_settings( $options ); |
| 711 |
|
| 712 |
wp_send_json_success( |
| 713 |
array( |
| 714 |
'options' => $options, |
| 715 |
'status' => 1, |
| 716 |
) |
| 717 |
); |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Re-activate license again in Setting page |
| 722 |
*/ |
| 723 |
public function lasso_lite_reactivate_license() { |
| 724 |
$data = Helper::POST(); |
| 725 |
$license = $data['license'] ?? ''; |
| 726 |
|
| 727 |
Setting::set_setting( 'license_serial', $license ); |
| 728 |
License::lasso_getinfo(); |
| 729 |
|
| 730 |
list($license_status, $error_code, $error_message) = License::check_license( $license ); |
| 731 |
|
| 732 |
wp_send_json_success( |
| 733 |
array( |
| 734 |
'status' => $license_status, |
| 735 |
'error_code' => $error_code, |
| 736 |
'error_message' => $error_message, |
| 737 |
) |
| 738 |
); |
| 739 |
} // @codeCoverageIgnore |
| 740 |
} |
| 741 |
|