Changeset 2453874
- Timestamp:
- 01/11/2021 12:19:23 PM (5 years ago)
- Location:
- audienceplayer
- Files:
-
- 14 edited
- 1 copied
-
tags/1.10.7 (copied) (copied from audienceplayer/trunk)
-
tags/1.10.7/audienceplayer.php (modified) (1 diff)
-
tags/1.10.7/readme.txt (modified) (1 diff)
-
tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php (modified) (1 diff)
-
tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php (modified) (5 diffs)
-
tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php (modified) (2 diffs)
-
tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php (modified) (14 diffs)
-
tags/1.10.7/templates/js/audienceplayer-core.js (modified) (1 diff)
-
trunk/audienceplayer.php (modified) (1 diff)
-
trunk/readme.txt (modified) (1 diff)
-
trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php (modified) (1 diff)
-
trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php (modified) (5 diffs)
-
trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php (modified) (2 diffs)
-
trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php (modified) (14 diffs)
-
trunk/templates/js/audienceplayer-core.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
audienceplayer/tags/1.10.7/audienceplayer.php
r2451356 r2453874 9 9 Description: AudiencePlayer integration 10 10 Author: AudiencePlayer 11 Version: 1.10. 611 Version: 1.10.7 12 12 Author URI: https://www.audienceplayer.com 13 13 Text Domain: audienceplayer -
audienceplayer/tags/1.10.7/readme.txt
r2451356 r2453874 1 1 === AudiencePlayer === 2 2 Contributors: audienceplayer 3 Stable tag: 1.10. 63 Stable tag: 1.10.7 4 4 Tested up to: 5.5 5 5 Requires at least: 5.5 -
audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php
r2451356 r2453874 39 39 40 40 // Main plugin version number here and in main plugin file header are automatically overwritten in gulp-build script 41 PLUGIN_VERSION = '1.10. 6',41 PLUGIN_VERSION = '1.10.7', 42 42 43 43 // DB migration version number is maintained here -
audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php
r2451356 r2453874 139 139 \add_action('wp_loaded', function () use ($self) { 140 140 if ($self->helper->isWordpressWebsiteContext()) { 141 $self->syncAudiencePlayerUser (0, [], false);141 $self->syncAudiencePlayerUserAuthentication(0, [], false); 142 142 } 143 143 }); … … 392 392 } 393 393 // user was just successfully authenticated and logged in Wordpress and must now be forcefully synched with AudiencePlayer 394 $self->syncAudiencePlayerUser ($user->ID, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A);394 $self->syncAudiencePlayerUserAuthentication($user->ID, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A); 395 395 } 396 396 … … 423 423 \add_action('login_form_lostpassword', $lostPasswordCallback); 424 424 \add_action('login_form_retrievepassword', $lostPasswordCallback); 425 426 // "after_password_reset": Runs immediately upon completion of function \reset_password. 427 // Update the password for given user 428 \add_action('after_password_reset', function ($user = null, $newPassword = '') use ($self) { 429 if ( 430 $user && 431 ($user->ID ?? null) && 432 ($newPassword = trim($newPassword) ?: trim($_REQUEST['pass1'] ?? $_REQUEST['pass2'] ?? '')) 433 ) { 434 $self->syncWordpressUserToAudiencePlayer($user->ID, ['password' => $newPassword]); 435 } 436 437 return true; 438 }); 425 439 } 426 440 } … … 502 516 } 503 517 504 return $self->syncAudiencePlayerUser ($wordpressUserId, $userArgs, $self->isNewUserRegistrationRequest, false, Constants::USER_METADATA_SYNC_DIRECTION_WP2A);518 return $self->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, $self->isNewUserRegistrationRequest, false, Constants::USER_METADATA_SYNC_DIRECTION_WP2A); 505 519 506 520 }, 10, 1); … … 528 542 } 529 543 if (trim($_REQUEST['pass2'] ?? '')) { 530 $userArgs['password'] = trim($_REQUEST['pass2'] ?? '');544 $userArgs['password'] = trim($_REQUEST['pass2'] ?? $_REQUEST['user_pass'] ?? ''); 531 545 } 532 546 if ($name = $self->helper->parseUserFullNameFromUserData($_REQUEST)) { -
audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php
r2450559 r2453874 35 35 use AudiencePlayer\AudiencePlayerApiClient\Resources\Globals; 36 36 use AudiencePlayer\AudiencePlayerWordpressPlugin\AudiencePlayerWordpressPlugin; 37 use AudiencePlayer\AudiencePlayerWordpressPlugin\Config\Constants; 37 38 use PHPMailer\PHPMailer\Exception; 38 39 … … 257 258 258 259 /** 260 * Parse Wordpress user arguments into remote AudiencePlayer user properties 261 * 262 * @param $userArgs 263 * @param array $forceArgs 264 * @return array 265 */ 266 public function parseAudiencePlayerUserArgs(array $userArgs, array $forceArgs = []) 267 { 268 $ret = []; 269 270 $translationsMatrix = [ 271 'email' => ['email', 'user_email'], 272 'password' => ['user_pass', 'user_password', 'password'], 273 ]; 274 275 foreach ($translationsMatrix as $key => $fields) { 276 foreach ($fields as $fieldKey) { 277 if ($userArgs[$fieldKey] ?? null) { 278 $ret[$key] = $userArgs[$fieldKey]; 279 break; 280 } 281 } 282 } 283 284 foreach ($userArgs as $key => $value) { 285 if ( 286 isset(Constants::USER_METADATA_SYNC_KEYS[$key]) && 287 ($typeCast = Constants::USER_METADATA_SYNC_KEYS[$key]) 288 ) { 289 if ($key === 'gender') { 290 // only parse gender value if it is a property enum value 291 if (in_array($value, ['male', 'female', 'non_binary', 'unknown'])) { 292 $ret[$key] = ['value' => strval($value), 'type' => 'enum']; 293 } 294 } elseif ($key === 'phone' && $this->validatePhoneNumberFormat(strval($value))) { 295 $ret[$key] = strval($value); 296 } else { 297 $ret[$key] = $this->typeCastValue($value, $typeCast); 298 } 299 } 300 } 301 302 // do not sync name if it is the same as the e-mail address 303 if (isset($ret['name'], $ret['email']) && $ret['name'] === $ret['email']) { 304 unset($ret['name']); 305 } 306 307 // parse name from first/last name arugs 308 if (!isset($ret['name']) && ($value = trim(($userArgs['first_name'] ?? '') . ' ' . ($userArgs['last_name'] ?? '')))) { 309 $ret['name'] = $value; 310 } 311 312 // add required args 313 foreach ($forceArgs as $key => $value) { 314 $ret[$key] = $value; 315 } 316 317 return $ret; 318 } 319 320 /** 259 321 * Generates a slug (url-compatible string) from input string. E.g. replaces accented characters with 260 322 * equivalent ascii characters, whitespaces and dashes with underscores. -
audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php
r2451356 r2453874 48 48 * @return bool 49 49 */ 50 public function syncAudiencePlayerUser (50 public function syncAudiencePlayerUserAuthentication( 51 51 int $wordpressUserId = 0, 52 52 array $userArgs = [], … … 151 151 foreach (Constants::USER_METADATA_SYNC_KEYS as $metaKey => $typeCast) { 152 152 if ($value = trim($this->fetchWordpressUserProperty($wordpressUserObject, $metaKey))) { 153 if ($metaKey === 'gender') { 154 // only parse gender value if it is a propery enum value 155 if (in_array($value, ['male', 'female', 'non_binary', 'unknown'])) { 156 $metadata[$metaKey] = ['value' => strval($value), 'type' => 'enum']; 157 } 158 } elseif ($metaKey === 'phone' && $this->helper->validatePhoneNumberFormat(strval($value))) { 159 $metadata[$metaKey] = strval($value); 160 } else { 161 $metadata[$metaKey] = $this->helper->typeCastValue($value, $typeCast); 162 } 153 $metadata[$metaKey] = $value; 163 154 } 164 155 } 165 166 if (isset($metadata['name']) && $metadata['name'] === $this->fetchWordpressUserProperty($wordpressUserObject, 'email')) { 167 168 // do not sync name if it is the same as the e-mail address 169 unset($metadata['name']); 170 171 } elseif ( 172 !isset($metadata['name']) && ($value = trim( 173 $this->fetchWordpressUserProperty($wordpressUserObject, 'first_name') . ' ' . 174 $this->fetchWordpressUserProperty($wordpressUserObject, 'last_name') 175 )) 176 ) { 177 $metadata['name'] = $value; 178 } 156 $metadata['first_name'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'first_name')); 157 $metadata['last_name'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'last_name')); 158 $metadata['email'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'email')); 159 160 $metadata = $this->helper->parseAudiencePlayerUserArgs($metadata); 161 unset($metadata['email']); 179 162 180 163 if ($metadata) { 181 $ret = $this-> api->updateUser($audiencePlayerUserId, $metadata);164 $ret = $this->updateAudiencePlayerUser($audiencePlayerUserId, $metadata); 182 165 } else { 183 166 $ret = true; … … 293 276 /** 294 277 * @param int $audiencePlayerUserId 278 * @param array $userArgs 279 * @return bool 280 */ 281 protected function updateAudiencePlayerUser(int $audiencePlayerUserId, array $userArgs = []) 282 { 283 // Try to update AudiencePlayer user 284 return $audiencePlayerUserId && $userArgs && $this->api->updateUser($audiencePlayerUserId, $userArgs); 285 } 286 287 /** 288 * @param int $audiencePlayerUserId 295 289 * @param string $wordpressUserEmail 296 290 * @return bool … … 341 335 } 342 336 337 // Collect currently logged in user 338 $currentWordpressUserId = \get_current_user_id(); 339 343 340 // create the new Wordpress user 344 341 $wordpressUserId = \wp_insert_user($userData); … … 346 343 // If successful, prevent auto-login of this user which is often set in the Wordpress theme on the 'user_register' event 347 344 // to allow newly registered users to become auto-logged in 348 if ($isForceSilentCreation && is_int($wordpressUserId) && \get_current_user_id() === $wordpressUserId) {345 if ($isForceSilentCreation && \get_current_user_id() !== $currentWordpressUserId) { 349 346 \wp_logout(); 350 347 } … … 397 394 // Validate Wordpress user first, and force sync with AudiencePlayer 398 395 if ( 399 $this->syncAudiencePlayerUser ($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) &&396 $this->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) && 400 397 ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id')) 401 398 ) { … … 417 414 // Query the API and check if new e-mail address is available 418 415 $userArgs['email'] !== $this->fetchWordpressUserProperty($wordpressUserId, 'email') && 419 ($result = $this->api->fetchUser(0, $userArgs['email'], ['id', 'email'])) && 420 ($result->getData(true)->id ?? null) 416 false === $this->isAudiencePlayerEmailAddressAvailable($userArgs['email']) 421 417 ) { 422 418 $ret = false; … … 446 442 447 443 // Try to update AudiencePlayer e-mail address 448 if ($ret && $updateArgs && false === $this-> api->updateUser($audiencePlayerUserId, $updateArgs)) {444 if ($ret && $updateArgs && false === $this->updateAudiencePlayerUser($audiencePlayerUserId, $updateArgs)) { 449 445 450 446 // ERROR: updating remote user … … 576 572 } else { 577 573 $this->writeLog( 578 Constants:: STATUS_SERVER_ERROR_CODE,574 Constants::LOG_LEVEL_SYSTEM_ERROR, 579 575 'resource.user-sync.sync-audienceplayer-user-to-wordpress.wordpress-user-deletion-error', 580 576 'Error, Wordpress user ' . $logEmail . ' with id [' . $logId . '] was not deleted', … … 585 581 586 582 } else { 587 // sync existing user 588 $ret = $this->syncAudiencePlayerUser($user->ID, $userArgs, false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 583 584 $ret = $this->syncAudiencePlayerUserAuthentication($user->ID, $userArgs, false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 585 $args = $this->helper->arrayOnly($this->helper->parseWordpressUserArgs($userArgs), ['user_pass']); 586 587 // metadata already handled in syncAudiencePlayerUserAuthentication, but password may also have been changed 588 if ($ret && $args) { 589 $ret = $this->updateWordpressUser($user->ID, $args); 590 } 589 591 } 590 592 … … 596 598 597 599 // sync newly created user 598 $ret = $this->syncAudiencePlayerUser ($userId, [], false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP);600 $ret = $this->syncAudiencePlayerUserAuthentication($userId, [], false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 599 601 600 602 } else { … … 620 622 } 621 623 624 /** 625 * @param int $wordpressUserId 626 * @param array $userArgs 627 * @return bool 628 */ 629 public function syncWordpressUserToAudiencePlayer(int $wordpressUserId, array $userArgs = []) 630 { 631 $ret = false; 632 633 if ( 634 $wordpressUserId && 635 $this->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) && 636 ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id')) 637 ) { 638 639 $userArgs = $this->helper->parseAudiencePlayerUserArgs($userArgs); 640 641 // check for e-mail change first 642 if ( 643 isset($userArgs['email']) && 644 ($emailAddress = $this->fetchWordpressUserProperty($wordpressUserId, 'email')) && 645 $userArgs['email'] !== $emailAddress && 646 false === $this->isAudiencePlayerEmailAddressAvailable($userArgs['email']) 647 ) { 648 $ret = false; 649 } else { 650 $ret = $userArgs ? $this->updateAudiencePlayerUser($audiencePlayerUserId, $userArgs) : true; 651 } 652 } 653 654 return $ret; 655 } 656 657 /** 658 * @param $email 659 * @return bool 660 */ 661 public function isAudiencePlayerEmailAddressAvailable($email) 662 { 663 $ret = true; 664 665 if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 666 667 if ( 668 // Query the API and check if new e-mail address is available 669 ($result = $this->api->fetchUser(0, $email, ['id', 'email'])) && 670 ($result->getData(true)->id ?? null) 671 ) { 672 $ret = false; 673 } 674 675 } else { 676 $ret = false; 677 } 678 679 return $ret; 680 } 681 622 682 // ### INTERNAL HELPERS 623 683 684 /** 685 * @param int $wordpressUserId 686 * @return null|object 687 */ 624 688 protected function fetchWordpressUserObject(int $wordpressUserId = 0) 625 689 { … … 660 724 ) { 661 725 $ret = true; 726 } 727 728 return $ret; 729 } 730 731 /** 732 * @param int $wordpressUserId 733 * @param array $userArgs 734 * @return bool 735 */ 736 protected function updateWordpressUser(int $wordpressUserId, array $userArgs = []) 737 { 738 $ret = true; 739 740 foreach ($userArgs as $property => $value) { 741 $ret = $this->updateWordpressUserProperty($wordpressUserId, $property, $value) && $ret; 662 742 } 663 743 … … 733 813 $ret = \wp_update_user(['ID' => $wordpressUserId, 'email' => $value]); 734 814 break; 815 case 'user_pass': 816 case 'user_password': 817 case 'password': 818 \wp_set_password($value, $wordpressUserId); 819 $ret = true; 820 break; 735 821 default: 736 822 $ret = \update_user_meta($wordpressUserId, $property, $value); -
audienceplayer/tags/1.10.7/templates/js/audienceplayer-core.js
r2451356 r2453874 756 756 errorMsg = this.CONFIG.translations.dialogue_user_subscription_already_valid; 757 757 758 } else if (errorCode === 3800 ) {758 } else if (errorCode === 3800 || errorCode === 409) { 759 759 760 760 errorMsg = this.CONFIG.translations.dialogue_product_already_purchased; -
audienceplayer/trunk/audienceplayer.php
r2451356 r2453874 9 9 Description: AudiencePlayer integration 10 10 Author: AudiencePlayer 11 Version: 1.10. 611 Version: 1.10.7 12 12 Author URI: https://www.audienceplayer.com 13 13 Text Domain: audienceplayer -
audienceplayer/trunk/readme.txt
r2451356 r2453874 1 1 === AudiencePlayer === 2 2 Contributors: audienceplayer 3 Stable tag: 1.10. 63 Stable tag: 1.10.7 4 4 Tested up to: 5.5 5 5 Requires at least: 5.5 -
audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php
r2451356 r2453874 39 39 40 40 // Main plugin version number here and in main plugin file header are automatically overwritten in gulp-build script 41 PLUGIN_VERSION = '1.10. 6',41 PLUGIN_VERSION = '1.10.7', 42 42 43 43 // DB migration version number is maintained here -
audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php
r2451356 r2453874 139 139 \add_action('wp_loaded', function () use ($self) { 140 140 if ($self->helper->isWordpressWebsiteContext()) { 141 $self->syncAudiencePlayerUser (0, [], false);141 $self->syncAudiencePlayerUserAuthentication(0, [], false); 142 142 } 143 143 }); … … 392 392 } 393 393 // user was just successfully authenticated and logged in Wordpress and must now be forcefully synched with AudiencePlayer 394 $self->syncAudiencePlayerUser ($user->ID, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A);394 $self->syncAudiencePlayerUserAuthentication($user->ID, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A); 395 395 } 396 396 … … 423 423 \add_action('login_form_lostpassword', $lostPasswordCallback); 424 424 \add_action('login_form_retrievepassword', $lostPasswordCallback); 425 426 // "after_password_reset": Runs immediately upon completion of function \reset_password. 427 // Update the password for given user 428 \add_action('after_password_reset', function ($user = null, $newPassword = '') use ($self) { 429 if ( 430 $user && 431 ($user->ID ?? null) && 432 ($newPassword = trim($newPassword) ?: trim($_REQUEST['pass1'] ?? $_REQUEST['pass2'] ?? '')) 433 ) { 434 $self->syncWordpressUserToAudiencePlayer($user->ID, ['password' => $newPassword]); 435 } 436 437 return true; 438 }); 425 439 } 426 440 } … … 502 516 } 503 517 504 return $self->syncAudiencePlayerUser ($wordpressUserId, $userArgs, $self->isNewUserRegistrationRequest, false, Constants::USER_METADATA_SYNC_DIRECTION_WP2A);518 return $self->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, $self->isNewUserRegistrationRequest, false, Constants::USER_METADATA_SYNC_DIRECTION_WP2A); 505 519 506 520 }, 10, 1); … … 528 542 } 529 543 if (trim($_REQUEST['pass2'] ?? '')) { 530 $userArgs['password'] = trim($_REQUEST['pass2'] ?? '');544 $userArgs['password'] = trim($_REQUEST['pass2'] ?? $_REQUEST['user_pass'] ?? ''); 531 545 } 532 546 if ($name = $self->helper->parseUserFullNameFromUserData($_REQUEST)) { -
audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php
r2450559 r2453874 35 35 use AudiencePlayer\AudiencePlayerApiClient\Resources\Globals; 36 36 use AudiencePlayer\AudiencePlayerWordpressPlugin\AudiencePlayerWordpressPlugin; 37 use AudiencePlayer\AudiencePlayerWordpressPlugin\Config\Constants; 37 38 use PHPMailer\PHPMailer\Exception; 38 39 … … 257 258 258 259 /** 260 * Parse Wordpress user arguments into remote AudiencePlayer user properties 261 * 262 * @param $userArgs 263 * @param array $forceArgs 264 * @return array 265 */ 266 public function parseAudiencePlayerUserArgs(array $userArgs, array $forceArgs = []) 267 { 268 $ret = []; 269 270 $translationsMatrix = [ 271 'email' => ['email', 'user_email'], 272 'password' => ['user_pass', 'user_password', 'password'], 273 ]; 274 275 foreach ($translationsMatrix as $key => $fields) { 276 foreach ($fields as $fieldKey) { 277 if ($userArgs[$fieldKey] ?? null) { 278 $ret[$key] = $userArgs[$fieldKey]; 279 break; 280 } 281 } 282 } 283 284 foreach ($userArgs as $key => $value) { 285 if ( 286 isset(Constants::USER_METADATA_SYNC_KEYS[$key]) && 287 ($typeCast = Constants::USER_METADATA_SYNC_KEYS[$key]) 288 ) { 289 if ($key === 'gender') { 290 // only parse gender value if it is a property enum value 291 if (in_array($value, ['male', 'female', 'non_binary', 'unknown'])) { 292 $ret[$key] = ['value' => strval($value), 'type' => 'enum']; 293 } 294 } elseif ($key === 'phone' && $this->validatePhoneNumberFormat(strval($value))) { 295 $ret[$key] = strval($value); 296 } else { 297 $ret[$key] = $this->typeCastValue($value, $typeCast); 298 } 299 } 300 } 301 302 // do not sync name if it is the same as the e-mail address 303 if (isset($ret['name'], $ret['email']) && $ret['name'] === $ret['email']) { 304 unset($ret['name']); 305 } 306 307 // parse name from first/last name arugs 308 if (!isset($ret['name']) && ($value = trim(($userArgs['first_name'] ?? '') . ' ' . ($userArgs['last_name'] ?? '')))) { 309 $ret['name'] = $value; 310 } 311 312 // add required args 313 foreach ($forceArgs as $key => $value) { 314 $ret[$key] = $value; 315 } 316 317 return $ret; 318 } 319 320 /** 259 321 * Generates a slug (url-compatible string) from input string. E.g. replaces accented characters with 260 322 * equivalent ascii characters, whitespaces and dashes with underscores. -
audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php
r2451356 r2453874 48 48 * @return bool 49 49 */ 50 public function syncAudiencePlayerUser (50 public function syncAudiencePlayerUserAuthentication( 51 51 int $wordpressUserId = 0, 52 52 array $userArgs = [], … … 151 151 foreach (Constants::USER_METADATA_SYNC_KEYS as $metaKey => $typeCast) { 152 152 if ($value = trim($this->fetchWordpressUserProperty($wordpressUserObject, $metaKey))) { 153 if ($metaKey === 'gender') { 154 // only parse gender value if it is a propery enum value 155 if (in_array($value, ['male', 'female', 'non_binary', 'unknown'])) { 156 $metadata[$metaKey] = ['value' => strval($value), 'type' => 'enum']; 157 } 158 } elseif ($metaKey === 'phone' && $this->helper->validatePhoneNumberFormat(strval($value))) { 159 $metadata[$metaKey] = strval($value); 160 } else { 161 $metadata[$metaKey] = $this->helper->typeCastValue($value, $typeCast); 162 } 153 $metadata[$metaKey] = $value; 163 154 } 164 155 } 165 166 if (isset($metadata['name']) && $metadata['name'] === $this->fetchWordpressUserProperty($wordpressUserObject, 'email')) { 167 168 // do not sync name if it is the same as the e-mail address 169 unset($metadata['name']); 170 171 } elseif ( 172 !isset($metadata['name']) && ($value = trim( 173 $this->fetchWordpressUserProperty($wordpressUserObject, 'first_name') . ' ' . 174 $this->fetchWordpressUserProperty($wordpressUserObject, 'last_name') 175 )) 176 ) { 177 $metadata['name'] = $value; 178 } 156 $metadata['first_name'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'first_name')); 157 $metadata['last_name'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'last_name')); 158 $metadata['email'] = trim($this->fetchWordpressUserProperty($wordpressUserObject, 'email')); 159 160 $metadata = $this->helper->parseAudiencePlayerUserArgs($metadata); 161 unset($metadata['email']); 179 162 180 163 if ($metadata) { 181 $ret = $this-> api->updateUser($audiencePlayerUserId, $metadata);164 $ret = $this->updateAudiencePlayerUser($audiencePlayerUserId, $metadata); 182 165 } else { 183 166 $ret = true; … … 293 276 /** 294 277 * @param int $audiencePlayerUserId 278 * @param array $userArgs 279 * @return bool 280 */ 281 protected function updateAudiencePlayerUser(int $audiencePlayerUserId, array $userArgs = []) 282 { 283 // Try to update AudiencePlayer user 284 return $audiencePlayerUserId && $userArgs && $this->api->updateUser($audiencePlayerUserId, $userArgs); 285 } 286 287 /** 288 * @param int $audiencePlayerUserId 295 289 * @param string $wordpressUserEmail 296 290 * @return bool … … 341 335 } 342 336 337 // Collect currently logged in user 338 $currentWordpressUserId = \get_current_user_id(); 339 343 340 // create the new Wordpress user 344 341 $wordpressUserId = \wp_insert_user($userData); … … 346 343 // If successful, prevent auto-login of this user which is often set in the Wordpress theme on the 'user_register' event 347 344 // to allow newly registered users to become auto-logged in 348 if ($isForceSilentCreation && is_int($wordpressUserId) && \get_current_user_id() === $wordpressUserId) {345 if ($isForceSilentCreation && \get_current_user_id() !== $currentWordpressUserId) { 349 346 \wp_logout(); 350 347 } … … 397 394 // Validate Wordpress user first, and force sync with AudiencePlayer 398 395 if ( 399 $this->syncAudiencePlayerUser ($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) &&396 $this->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) && 400 397 ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id')) 401 398 ) { … … 417 414 // Query the API and check if new e-mail address is available 418 415 $userArgs['email'] !== $this->fetchWordpressUserProperty($wordpressUserId, 'email') && 419 ($result = $this->api->fetchUser(0, $userArgs['email'], ['id', 'email'])) && 420 ($result->getData(true)->id ?? null) 416 false === $this->isAudiencePlayerEmailAddressAvailable($userArgs['email']) 421 417 ) { 422 418 $ret = false; … … 446 442 447 443 // Try to update AudiencePlayer e-mail address 448 if ($ret && $updateArgs && false === $this-> api->updateUser($audiencePlayerUserId, $updateArgs)) {444 if ($ret && $updateArgs && false === $this->updateAudiencePlayerUser($audiencePlayerUserId, $updateArgs)) { 449 445 450 446 // ERROR: updating remote user … … 576 572 } else { 577 573 $this->writeLog( 578 Constants:: STATUS_SERVER_ERROR_CODE,574 Constants::LOG_LEVEL_SYSTEM_ERROR, 579 575 'resource.user-sync.sync-audienceplayer-user-to-wordpress.wordpress-user-deletion-error', 580 576 'Error, Wordpress user ' . $logEmail . ' with id [' . $logId . '] was not deleted', … … 585 581 586 582 } else { 587 // sync existing user 588 $ret = $this->syncAudiencePlayerUser($user->ID, $userArgs, false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 583 584 $ret = $this->syncAudiencePlayerUserAuthentication($user->ID, $userArgs, false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 585 $args = $this->helper->arrayOnly($this->helper->parseWordpressUserArgs($userArgs), ['user_pass']); 586 587 // metadata already handled in syncAudiencePlayerUserAuthentication, but password may also have been changed 588 if ($ret && $args) { 589 $ret = $this->updateWordpressUser($user->ID, $args); 590 } 589 591 } 590 592 … … 596 598 597 599 // sync newly created user 598 $ret = $this->syncAudiencePlayerUser ($userId, [], false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP);600 $ret = $this->syncAudiencePlayerUserAuthentication($userId, [], false, true, Constants::USER_METADATA_SYNC_DIRECTION_A2WP); 599 601 600 602 } else { … … 620 622 } 621 623 624 /** 625 * @param int $wordpressUserId 626 * @param array $userArgs 627 * @return bool 628 */ 629 public function syncWordpressUserToAudiencePlayer(int $wordpressUserId, array $userArgs = []) 630 { 631 $ret = false; 632 633 if ( 634 $wordpressUserId && 635 $this->syncAudiencePlayerUserAuthentication($wordpressUserId, $userArgs, true, true, Constants::USER_METADATA_SYNC_DIRECTION_WP2A) && 636 ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id')) 637 ) { 638 639 $userArgs = $this->helper->parseAudiencePlayerUserArgs($userArgs); 640 641 // check for e-mail change first 642 if ( 643 isset($userArgs['email']) && 644 ($emailAddress = $this->fetchWordpressUserProperty($wordpressUserId, 'email')) && 645 $userArgs['email'] !== $emailAddress && 646 false === $this->isAudiencePlayerEmailAddressAvailable($userArgs['email']) 647 ) { 648 $ret = false; 649 } else { 650 $ret = $userArgs ? $this->updateAudiencePlayerUser($audiencePlayerUserId, $userArgs) : true; 651 } 652 } 653 654 return $ret; 655 } 656 657 /** 658 * @param $email 659 * @return bool 660 */ 661 public function isAudiencePlayerEmailAddressAvailable($email) 662 { 663 $ret = true; 664 665 if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 666 667 if ( 668 // Query the API and check if new e-mail address is available 669 ($result = $this->api->fetchUser(0, $email, ['id', 'email'])) && 670 ($result->getData(true)->id ?? null) 671 ) { 672 $ret = false; 673 } 674 675 } else { 676 $ret = false; 677 } 678 679 return $ret; 680 } 681 622 682 // ### INTERNAL HELPERS 623 683 684 /** 685 * @param int $wordpressUserId 686 * @return null|object 687 */ 624 688 protected function fetchWordpressUserObject(int $wordpressUserId = 0) 625 689 { … … 660 724 ) { 661 725 $ret = true; 726 } 727 728 return $ret; 729 } 730 731 /** 732 * @param int $wordpressUserId 733 * @param array $userArgs 734 * @return bool 735 */ 736 protected function updateWordpressUser(int $wordpressUserId, array $userArgs = []) 737 { 738 $ret = true; 739 740 foreach ($userArgs as $property => $value) { 741 $ret = $this->updateWordpressUserProperty($wordpressUserId, $property, $value) && $ret; 662 742 } 663 743 … … 733 813 $ret = \wp_update_user(['ID' => $wordpressUserId, 'email' => $value]); 734 814 break; 815 case 'user_pass': 816 case 'user_password': 817 case 'password': 818 \wp_set_password($value, $wordpressUserId); 819 $ret = true; 820 break; 735 821 default: 736 822 $ret = \update_user_meta($wordpressUserId, $property, $value); -
audienceplayer/trunk/templates/js/audienceplayer-core.js
r2451356 r2453874 756 756 errorMsg = this.CONFIG.translations.dialogue_user_subscription_already_valid; 757 757 758 } else if (errorCode === 3800 ) {758 } else if (errorCode === 3800 || errorCode === 409) { 759 759 760 760 errorMsg = this.CONFIG.translations.dialogue_product_already_purchased;
Note: See TracChangeset
for help on using the changeset viewer.