Skip to content

Commit

Permalink
Fixed numerous issues, streamlining
Browse files Browse the repository at this point in the history
  • Loading branch information
Ynhockey committed Sep 4, 2019
1 parent 8cec26b commit f038279
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 58 deletions.
66 changes: 32 additions & 34 deletions GigyaAction.php
Expand Up @@ -579,7 +579,7 @@ public function deleteUser( $user_id ) {
elseif ( $this->login_options['mode'] == 'raas' )
{
$account = get_userdata( $user_id );
$gigyaCMS->deleteAccountByEmail( $account );
$gigyaCMS->deleteAccountByEmail( $account->data->user_email );
}
}

Expand Down Expand Up @@ -833,15 +833,15 @@ public function getOfflineSyncSchedules( $schedules ) {

public function executeOfflineSyncCron() {
/* Retrieve config variables */
$config = get_option( 'gigya_field_mapping_settings' );
$job_config = get_option( 'gigya_offline_sync_params' );
$enable_job = $config['map_offline_sync_enable'];
$config = get_option( 'gigya_field_mapping_settings' );
$job_config = get_option( 'gigya_offline_sync_params' );
$enable_job = $config['map_offline_sync_enable'];
$email_on_success = $config['map_offline_sync_email_on_success'];
$email_on_failure = $config['map_offline_sync_email_on_failure'];

$helper = new GigyaOfflineSync();

if ($enable_job) {
if ( $enable_job ) {
try {
$last_customer_update = null;
$gigya_query = "SELECT * FROM accounts";
Expand All @@ -854,54 +854,52 @@ public function executeOfflineSyncCron() {
$gigya_users = $gigya_cms->searchGigyaUsers( [ 'query' => $gigya_query ] );
$processed_users = 0;
$users_not_found = 0;
$uids_not_found = [];
$uids_not_found = [];

foreach ($gigya_users as $gigya_user) {
$gigya_uid = $gigya_user['UID'];
foreach ( $gigya_users as $gigya_user ) {
$gigya_uid = $gigya_user['UID'];
$gigya_last_updated_timestamp = $gigya_user['lastUpdatedTimestamp'];

if (!empty($gigya_uid) and !empty($gigya_last_updated_timestamp)) {
$wp_user = $this->getWPUserByGigyaUid($gigya_uid);
if (!empty($wp_user)) {
if ( ! empty( $gigya_uid ) and ! empty( $gigya_last_updated_timestamp ) ) {
$wp_user = $this->getWPUserByGigyaUid( $gigya_uid );
if ( ! empty( $wp_user ) ) {
_gigya_add_to_wp_user_meta( $gigya_user, $wp_user->ID );

if ($gigya_last_updated_timestamp) {
$job_config['last_customer_update'] = $gigya_last_updated_timestamp - GIGYA__OFFLINE_SYNC_UPDATE_DELAY;
update_option('gigya_offline_sync_params', $job_config);
}
$job_config['last_customer_update'] = $gigya_last_updated_timestamp - GIGYA__OFFLINE_SYNC_UPDATE_DELAY;
update_option( 'gigya_offline_sync_params', $job_config );

$processed_users++;
$processed_users ++;
} else {
$users_not_found++;
$users_not_found ++;
$uids_not_found[] = $gigya_user['UID'];
}
}
else {
error_log('Gigya offline sync: unable to process user due to a lack of essential data. User data received: ' . json_encode($gigya_user, JSON_PRETTY_PRINT));
} else {
error_log( 'Gigya offline sync: unable to process user due to a lack of essential data. User data received: ' . json_encode( $gigya_user,
JSON_PRETTY_PRINT ) );
}
}

$job_config['last_run'] = round( microtime( true ) * 1000 );
update_option( 'gigya_offline_sync_params', $job_config );

error_log( 'Gigya offline sync completed. Users processed: ' . $processed_users . (( $users_not_found )
? '. Users not found: ' . $users_not_found . PHP_EOL . implode( ',' . PHP_EOL, $uids_not_found )
: '' ));
error_log( 'Gigya offline sync completed. Users processed: ' . $processed_users . ( ( $users_not_found )
? '. Users not found: ' . $users_not_found . PHP_EOL . implode( ',' . PHP_EOL, $uids_not_found )
: '' ) );

$status = ($users_not_found > 0) ? 'completed with errors' : 'succeeded';
$helper->sendCronEmail('offline sync', $status, $email_on_success, $processed_users, $users_not_found);
} catch (GigyaHookException $e) {
error_log('Gigya offline sync: There was a problem adding custom data to field mapping: ' . $e->getMessage());
$status = ( $users_not_found > 0 ) ? 'completed with errors' : 'succeeded';
$helper->sendCronEmail( 'offline sync', $status, $email_on_success, $processed_users, $users_not_found );
} catch ( GigyaHookException $e ) {
error_log( 'Gigya offline sync: There was a problem adding custom data to field mapping: ' . $e->getMessage() );
$status = 'failed';
$helper->sendCronEmail('offline sync', $status, $email_on_failure);
} catch (GSApiException $e) {
error_log('Offline sync failed: ' . $e->getErrorCode() . ' – ' . $e->getMessage() . '. Call ID: ' . $e->getCallId());
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
} catch ( GSApiException $e ) {
error_log( 'Offline sync failed: ' . $e->getErrorCode() . ' – ' . $e->getMessage() . '. Call ID: ' . $e->getCallId() );
$status = 'failed';
$helper->sendCronEmail('offline sync', $status, $email_on_failure);
} catch (GSException $e) {
error_log('Offline sync failed: ' . $e->getMessage());
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
} catch ( GSException | Exception $e ) {
error_log( 'Offline sync failed: ' . $e->getMessage() );
$status = 'failed';
$helper->sendCronEmail('offline sync', $status, $email_on_failure);
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions admin/admin.GigyaSettings.php
Expand Up @@ -175,7 +175,7 @@ public static function onSave() {
if ( $gigyaErrCode > 0 ) {
$gigyaErrMsg = $res->getErrorMessage();
$errorsLink = "<a href='https://developers.gigya.com/display/GD/Response+Codes+and+Errors+REST' target='_blank' rel='noopener noreferrer'>Response_Codes_and_Errors</a>";
$message = "SAP CDC API error: {$gigyaErrCode} - {$gigyaErrMsg}.";
$message = "SAP CDC API error: {$gigyaErrCode} - {$gigyaErrMsg}.";
add_settings_error( 'gigya_global_settings', 'api_validate', __( $message . " For more information please refer to {$errorsLink}", 'error' ) );
error_log( 'Error updating SAP CDC settings: ' . $message . ' Call ID: ' . $res->getString( "callId", "N/A" ) );

Expand Down Expand Up @@ -256,29 +256,26 @@ public static function _setSecret() {
/**
* Set the posted api related values to the old (from DB) values
*
* @param null|string|array $settings Tells the function which old values to get.
* If null / empty string, it has the old hard-coded values (API key etc.),
* If string then it returns values from a specific wp_options setting,
* If array then it returns an array of specific settings
* @param null|string $option Relevant (and required) only if $settings is an array. Tells which settings to retrieve under this option.
* @param string $option The option under which to keep the settings
* @param null|string|array $settings Tells the function which specific old values to get, if we don't want all of them.
*/
public static function _keepOldApiValues( $settings = '', $option = null ) {
if ( ! $settings ) {
public static function _keepOldApiValues( $option = '', $settings = [] ) {
if ( ! $option ) {
$options = self::_getSiteOptions();
$_POST['gigya_global_settings']['api_key'] = $options['api_key'];
$_POST['gigya_global_settings']['user_key'] = $options['user_key'];
$_POST['gigya_global_settings']['api_secret'] = $options['api_secret'];
$_POST['gigya_global_settings']['data_center'] = $options['data_center'];
$_POST['gigya_global_settings']['sub_site_settings_saved'] = $options['sub_site_settings_saved'];
} elseif ( ! is_array( $settings ) ) {
$_POST[ $settings ] = self::_getSiteOptions( $settings );
} else { /* $settings is an array--retrieve specific options */
} elseif ( ! empty( $settings ) ) { /* $settings is an array--retrieve specific options */
if ( $option ) {
$options = self::_getSiteOptions( $settings );
$options = self::_getSiteOptions( $option );
foreach ( $settings as $setting ) {
$_POST[ $option ][ $setting ] = $options[ $setting ];
}
}
} else {
$_POST[ $option ] = self::_getSiteOptions( $option );
}
}

Expand Down
6 changes: 3 additions & 3 deletions admin/forms/fieldMappingForm.php
Expand Up @@ -9,7 +9,7 @@ function fieldMappingForm() {

if ( $login_options['mode'] == 'raas' ) {
$form['map_fieldmapping_desc'] = [
'markup' => __( '<p>Define which fields to map from SAP Customer Data Cloud to WordPress. The WordPress mapped target fields will be populated with data copied from the corresponding source fields. Learn more <a href="https://developers.gigya.com/display/GD/WordPress+Plugin#WordPressPlugin-UserManagementSettings" target="_blank" rel="noopener noreferrer" />here</a></p>' ),
'markup' => __( '<p>Define which fields to map from SAP Customer Data Cloud to WordPress. The WordPress mapped target fields will be populated with data copied from the corresponding source fields. Learn more <a href="https://developers.gigya.com/display/GD/WordPress+Plugin#WordPressPlugin-UserManagementSettings" target="_blank" rel="noopener noreferrer" />here</a>.</p>' ),
];

$gigya_full_map = _gigParam( $values, 'map_raas_full_map', '' );
Expand All @@ -21,7 +21,7 @@ function fieldMappingForm() {
];

$form['map_offline_sync_title'] = [
'markup' => __( '<h4>Offline Sync</h4><p>Define whether to perform field mapping operations periodically even without a specific user action</p>' ),
'markup' => __( '<h4>User Sync</h4><p>Schedule a recurring user sync. In addition to the recurring sync, users are synced when they perform an action (such as updating their profiles).</p>' ),
];

$form['map_offline_sync_enable'] = [
Expand All @@ -33,7 +33,7 @@ function fieldMappingForm() {
$form['map_offline_sync_frequency'] = [
'type' => 'text',
'size' => 10,
'label' => __( 'Job frequency' ),
'label' => __( 'Job Frequency' ),
'value' => _gigParam( $values, 'map_offline_sync_frequency', 10 ),
'markup' => __( 'minutes' ),
'desc' => 'This setting relies on the WordPress cron mechanism. Minimum value: ' . GIGYA__OFFLINE_SYNC_MIN_FREQ,
Expand Down
16 changes: 13 additions & 3 deletions admin/forms/loginSettingsForm.php
Expand Up @@ -22,9 +22,19 @@ function loginSettingsForm() {
$c = new GigyaCMS();
try {
$is_raas = $c->isRaaS();
} catch ( GSException $e ) {
$is_raas = false;
wp_send_json_error( 'Error from Gigya while determining RaaS status: ' . $e->getMessage() );
}
catch ( GSException $e ) {
$is_raas = true;
$form['raas_error'] = [
'markup' => '<div id="setting-error-api_validate" class="error settings-error notice is-dismissible">
<p>
<strong>' . __( 'Error determining RaaS status. There could be an issue with your machine or SAP CDC account. Please contact support if the problem persists. Message from SAP CDC') . ': ' . $e->getMessage() . '.
For more information please refer to <a href="https://developers.gigya.com/display/GD/Response+Codes+and+Errors+REST" target="_blank" rel="noopener noreferrer">Response_Codes_and_Errors</a>.
</strong>
</p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
</div>',
];
}

if ( $is_raas ) {
Expand Down
1 change: 1 addition & 0 deletions admin/gigya_admin.js
Expand Up @@ -21,6 +21,7 @@
$('.social-login-wrapper').removeClass('hidden');
$('.raas-login-wrapper').addClass('hidden');
$('.raas_enabled').find('.gigya-raas-warn').removeClass('hidden');
$('#tab-gigya_field_mapping_settings').removeClass('hidden');
}
else if ($el.val() === 'raas') {
$('.global-login-wrapper').removeClass('hidden');
Expand Down
2 changes: 1 addition & 1 deletion features/raas/GigyaOfflineSync.php
Expand Up @@ -23,7 +23,7 @@ public function sendCronEmail( $job_type, $job_status, $to, $processed_items = n
$email_body .= PHP_EOL . $processed_items . ' ' . ( ( $processed_items > 1 ) ? 'items' : 'item' ) . ' successfully processed, ' . $failed_items . ' failed.';
}
} elseif ( $job_status == 'failed' ) {
$email_body = 'Job failed. No items were processed. Please consult the Drupal log (Administration > Reports > Recent log messages) for more info.';
$email_body = 'Job failed. No items were processed. If the WordPress debug log is enabled, please consult it for more information. For information on enabling the log, please consult the WordPress documentation.';
}

wp_mail( $to, 'SAP CDC cron job of type ' . $job_type . ' ' . $job_status . ' on website ' . get_bloginfo(), $email_body );
Expand Down
1 change: 1 addition & 0 deletions gigya.php
Expand Up @@ -23,6 +23,7 @@
define( 'GIGYA__JS_CDN', GIGYA__CDN_PROTOCOL . '.gigya.com/js/socialize.js?apiKey=' );
define( 'GIGYA__LOG_LIMIT', 50 );
define( 'GIGYA__DEFAULT_COOKIE_EXPIRATION', 1800 ); /* WordPress defaults to 172800 (48 hours) */
define( 'GIGYA__ERROR_UNAUTHORIZED_PARTNER', 403036 );

/**
* Gigya constants for admin settings sections.
Expand Down
2 changes: 1 addition & 1 deletion sdk/README.md
Expand Up @@ -2,7 +2,7 @@ gigyaCMS class
==============

Is a class with extra API functions and other utils,
which build especially for working with PHP CMS like Drupal and Wordpress.
which build especially for working with PHP CMS such as Wordpress.

The class contains 4 calls for constant variables which is
NOT defined in the file, and you MUST define in your code.
Expand Down
10 changes: 6 additions & 4 deletions sdk/gigyaCMS.php
Expand Up @@ -357,9 +357,11 @@ public function deleteUser( $uid ) {
public function isRaaS() {
$res = $this->call( 'accounts.getSchema', array() );
if ( is_wp_error( $res )) {
if ( $res->get_error_code() === 403036) {
if ( $res->get_error_code() === GIGYA__ERROR_UNAUTHORIZED_PARTNER) {
return false;
}

throw new GSException( $res->get_error_code() . ': ' . $res->get_error_message() );
}

return true;
Expand Down Expand Up @@ -420,13 +422,13 @@ public function searchGigyaUsers( $params ) {
}

/**
* @param $account
* @param $email
*
* @throws Exception
*/
public function deleteAccountByEmail( $account ) {
public function deleteAccountByEmail( $email ) {
/* Get info about the primary account */
$email = $this->cleanEmail( $account->data->user_email );
$email = $this->cleanEmail( $email );
$query = "select UID from accounts where loginIDs.emails = '{$email}'";

/* Get the UID from Email */
Expand Down

0 comments on commit f038279

Please sign in to comment.