Plugin Directory

Changeset 2453874


Ignore:
Timestamp:
01/11/2021 12:19:23 PM (5 years ago)
Author:
audienceplayer
Message:
  • released version 1.10.7
Location:
audienceplayer
Files:
14 edited
1 copied

Legend:

Unmodified
Added
Removed
  • audienceplayer/tags/1.10.7/audienceplayer.php

    r2451356 r2453874  
    99Description: AudiencePlayer integration
    1010Author: AudiencePlayer
    11 Version: 1.10.6
     11Version: 1.10.7
    1212Author URI: https://www.audienceplayer.com
    1313Text Domain: audienceplayer
  • audienceplayer/tags/1.10.7/readme.txt

    r2451356 r2453874  
    11=== AudiencePlayer ===
    22Contributors: audienceplayer
    3 Stable tag: 1.10.6
     3Stable tag: 1.10.7
    44Tested up to: 5.5
    55Requires at least: 5.5
  • audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php

    r2451356 r2453874  
    3939
    4040        // 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',
    4242
    4343        // DB migration version number is maintained here
  • audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php

    r2451356 r2453874  
    139139        \add_action('wp_loaded', function () use ($self) {
    140140            if ($self->helper->isWordpressWebsiteContext()) {
    141                 $self->syncAudiencePlayerUser(0, [], false);
     141                $self->syncAudiencePlayerUserAuthentication(0, [], false);
    142142            }
    143143        });
     
    392392                    }
    393393                    // 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);
    395395                }
    396396
     
    423423            \add_action('login_form_lostpassword', $lostPasswordCallback);
    424424            \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            });
    425439        }
    426440    }
     
    502516                }
    503517
    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);
    505519
    506520            }, 10, 1);
     
    528542                    }
    529543                    if (trim($_REQUEST['pass2'] ?? '')) {
    530                         $userArgs['password'] = trim($_REQUEST['pass2'] ?? '');
     544                        $userArgs['password'] = trim($_REQUEST['pass2'] ?? $_REQUEST['user_pass'] ?? '');
    531545                    }
    532546                    if ($name = $self->helper->parseUserFullNameFromUserData($_REQUEST)) {
  • audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php

    r2450559 r2453874  
    3535use AudiencePlayer\AudiencePlayerApiClient\Resources\Globals;
    3636use AudiencePlayer\AudiencePlayerWordpressPlugin\AudiencePlayerWordpressPlugin;
     37use AudiencePlayer\AudiencePlayerWordpressPlugin\Config\Constants;
    3738use PHPMailer\PHPMailer\Exception;
    3839
     
    257258
    258259    /**
     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    /**
    259321     * Generates a slug (url-compatible string) from input string. E.g. replaces accented characters with
    260322     * equivalent ascii characters, whitespaces and dashes with underscores.
  • audienceplayer/tags/1.10.7/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php

    r2451356 r2453874  
    4848     * @return bool
    4949     */
    50     public function syncAudiencePlayerUser(
     50    public function syncAudiencePlayerUserAuthentication(
    5151        int $wordpressUserId = 0,
    5252        array $userArgs = [],
     
    151151                    foreach (Constants::USER_METADATA_SYNC_KEYS as $metaKey => $typeCast) {
    152152                        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;
    163154                        }
    164155                    }
    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']);
    179162
    180163                    if ($metadata) {
    181                         $ret = $this->api->updateUser($audiencePlayerUserId, $metadata);
     164                        $ret = $this->updateAudiencePlayerUser($audiencePlayerUserId, $metadata);
    182165                    } else {
    183166                        $ret = true;
     
    293276    /**
    294277     * @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
    295289     * @param string $wordpressUserEmail
    296290     * @return bool
     
    341335        }
    342336
     337        // Collect currently logged in user
     338        $currentWordpressUserId = \get_current_user_id();
     339
    343340        // create the new Wordpress user
    344341        $wordpressUserId = \wp_insert_user($userData);
     
    346343        // If successful, prevent auto-login of this user which is often set in the Wordpress theme on the 'user_register' event
    347344        // 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) {
    349346            \wp_logout();
    350347        }
     
    397394            // Validate Wordpress user first, and force sync with AudiencePlayer
    398395            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) &&
    400397                ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id'))
    401398            ) {
     
    417414                            // Query the API and check if new e-mail address is available
    418415                            $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'])
    421417                        ) {
    422418                            $ret = false;
     
    446442
    447443                // 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)) {
    449445
    450446                    // ERROR: updating remote user
     
    576572                } else {
    577573                    $this->writeLog(
    578                         Constants::STATUS_SERVER_ERROR_CODE,
     574                        Constants::LOG_LEVEL_SYSTEM_ERROR,
    579575                        'resource.user-sync.sync-audienceplayer-user-to-wordpress.wordpress-user-deletion-error',
    580576                        'Error, Wordpress user ' . $logEmail . ' with id [' . $logId . '] was not deleted',
     
    585581
    586582            } 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                }
    589591            }
    590592
     
    596598
    597599            // 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);
    599601
    600602        } else {
     
    620622    }
    621623
     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
    622682    // ### INTERNAL HELPERS
    623683
     684    /**
     685     * @param int $wordpressUserId
     686     * @return null|object
     687     */
    624688    protected function fetchWordpressUserObject(int $wordpressUserId = 0)
    625689    {
     
    660724        ) {
    661725            $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;
    662742        }
    663743
     
    733813                    $ret = \wp_update_user(['ID' => $wordpressUserId, 'email' => $value]);
    734814                    break;
     815                case 'user_pass':
     816                case 'user_password':
     817                case 'password':
     818                    \wp_set_password($value, $wordpressUserId);
     819                    $ret = true;
     820                    break;
    735821                default:
    736822                    $ret = \update_user_meta($wordpressUserId, $property, $value);
  • audienceplayer/tags/1.10.7/templates/js/audienceplayer-core.js

    r2451356 r2453874  
    756756                errorMsg = this.CONFIG.translations.dialogue_user_subscription_already_valid;
    757757
    758             } else if (errorCode === 3800) {
     758            } else if (errorCode === 3800 || errorCode === 409) {
    759759
    760760                errorMsg = this.CONFIG.translations.dialogue_product_already_purchased;
  • audienceplayer/trunk/audienceplayer.php

    r2451356 r2453874  
    99Description: AudiencePlayer integration
    1010Author: AudiencePlayer
    11 Version: 1.10.6
     11Version: 1.10.7
    1212Author URI: https://www.audienceplayer.com
    1313Text Domain: audienceplayer
  • audienceplayer/trunk/readme.txt

    r2451356 r2453874  
    11=== AudiencePlayer ===
    22Contributors: audienceplayer
    3 Stable tag: 1.10.6
     3Stable tag: 1.10.7
    44Tested up to: 5.5
    55Requires at least: 5.5
  • audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Config/Constants.php

    r2451356 r2453874  
    3939
    4040        // 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',
    4242
    4343        // DB migration version number is maintained here
  • audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/BootstrapTrait.php

    r2451356 r2453874  
    139139        \add_action('wp_loaded', function () use ($self) {
    140140            if ($self->helper->isWordpressWebsiteContext()) {
    141                 $self->syncAudiencePlayerUser(0, [], false);
     141                $self->syncAudiencePlayerUserAuthentication(0, [], false);
    142142            }
    143143        });
     
    392392                    }
    393393                    // 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);
    395395                }
    396396
     
    423423            \add_action('login_form_lostpassword', $lostPasswordCallback);
    424424            \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            });
    425439        }
    426440    }
     
    502516                }
    503517
    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);
    505519
    506520            }, 10, 1);
     
    528542                    }
    529543                    if (trim($_REQUEST['pass2'] ?? '')) {
    530                         $userArgs['password'] = trim($_REQUEST['pass2'] ?? '');
     544                        $userArgs['password'] = trim($_REQUEST['pass2'] ?? $_REQUEST['user_pass'] ?? '');
    531545                    }
    532546                    if ($name = $self->helper->parseUserFullNameFromUserData($_REQUEST)) {
  • audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/Helper.php

    r2450559 r2453874  
    3535use AudiencePlayer\AudiencePlayerApiClient\Resources\Globals;
    3636use AudiencePlayer\AudiencePlayerWordpressPlugin\AudiencePlayerWordpressPlugin;
     37use AudiencePlayer\AudiencePlayerWordpressPlugin\Config\Constants;
    3738use PHPMailer\PHPMailer\Exception;
    3839
     
    257258
    258259    /**
     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    /**
    259321     * Generates a slug (url-compatible string) from input string. E.g. replaces accented characters with
    260322     * equivalent ascii characters, whitespaces and dashes with underscores.
  • audienceplayer/trunk/src/AudiencePlayer/AudiencePlayerWordpressPlugin/Resources/UserSyncTrait.php

    r2451356 r2453874  
    4848     * @return bool
    4949     */
    50     public function syncAudiencePlayerUser(
     50    public function syncAudiencePlayerUserAuthentication(
    5151        int $wordpressUserId = 0,
    5252        array $userArgs = [],
     
    151151                    foreach (Constants::USER_METADATA_SYNC_KEYS as $metaKey => $typeCast) {
    152152                        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;
    163154                        }
    164155                    }
    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']);
    179162
    180163                    if ($metadata) {
    181                         $ret = $this->api->updateUser($audiencePlayerUserId, $metadata);
     164                        $ret = $this->updateAudiencePlayerUser($audiencePlayerUserId, $metadata);
    182165                    } else {
    183166                        $ret = true;
     
    293276    /**
    294277     * @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
    295289     * @param string $wordpressUserEmail
    296290     * @return bool
     
    341335        }
    342336
     337        // Collect currently logged in user
     338        $currentWordpressUserId = \get_current_user_id();
     339
    343340        // create the new Wordpress user
    344341        $wordpressUserId = \wp_insert_user($userData);
     
    346343        // If successful, prevent auto-login of this user which is often set in the Wordpress theme on the 'user_register' event
    347344        // 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) {
    349346            \wp_logout();
    350347        }
     
    397394            // Validate Wordpress user first, and force sync with AudiencePlayer
    398395            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) &&
    400397                ($audiencePlayerUserId = $this->fetchWordpressUserProperty($wordpressUserId, 'audienceplayer_user_id'))
    401398            ) {
     
    417414                            // Query the API and check if new e-mail address is available
    418415                            $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'])
    421417                        ) {
    422418                            $ret = false;
     
    446442
    447443                // 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)) {
    449445
    450446                    // ERROR: updating remote user
     
    576572                } else {
    577573                    $this->writeLog(
    578                         Constants::STATUS_SERVER_ERROR_CODE,
     574                        Constants::LOG_LEVEL_SYSTEM_ERROR,
    579575                        'resource.user-sync.sync-audienceplayer-user-to-wordpress.wordpress-user-deletion-error',
    580576                        'Error, Wordpress user ' . $logEmail . ' with id [' . $logId . '] was not deleted',
     
    585581
    586582            } 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                }
    589591            }
    590592
     
    596598
    597599            // 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);
    599601
    600602        } else {
     
    620622    }
    621623
     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
    622682    // ### INTERNAL HELPERS
    623683
     684    /**
     685     * @param int $wordpressUserId
     686     * @return null|object
     687     */
    624688    protected function fetchWordpressUserObject(int $wordpressUserId = 0)
    625689    {
     
    660724        ) {
    661725            $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;
    662742        }
    663743
     
    733813                    $ret = \wp_update_user(['ID' => $wordpressUserId, 'email' => $value]);
    734814                    break;
     815                case 'user_pass':
     816                case 'user_password':
     817                case 'password':
     818                    \wp_set_password($value, $wordpressUserId);
     819                    $ret = true;
     820                    break;
    735821                default:
    736822                    $ret = \update_user_meta($wordpressUserId, $property, $value);
  • audienceplayer/trunk/templates/js/audienceplayer-core.js

    r2451356 r2453874  
    756756                errorMsg = this.CONFIG.translations.dialogue_user_subscription_already_valid;
    757757
    758             } else if (errorCode === 3800) {
     758            } else if (errorCode === 3800 || errorCode === 409) {
    759759
    760760                errorMsg = this.CONFIG.translations.dialogue_product_already_purchased;
Note: See TracChangeset for help on using the changeset viewer.