Skip to content

Commit 4589bb0

Browse files
authored
Merge pull request #176 from gigya/develop
6.3.0
2 parents 0288029 + e1a5080 commit 4589bb0

File tree

13 files changed

+475
-66
lines changed

13 files changed

+475
-66
lines changed

admin/admin.GigyaSettings.php

Lines changed: 248 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Exception;
1010
use Gigya\CMSKit\GigyaApiHelper;
1111
use Gigya\CMSKit\GigyaCMS;
12+
use Gigya\PHP\GSException;
1213

1314
define( "GIGYA__PERMISSION_LEVEL", "manage_options" );
1415
define( "GIGYA__SECRET_PERMISSION_LEVEL", "install_plugins" ); // Network super admin + single site admin
@@ -39,8 +40,10 @@ public function __construct() {
3940
*/
4041
public function adminInit() {
4142

43+
//Adding variables to gigya_Admin.js
4244
$params = array(
43-
'max_execution_time' => intval( ini_get( 'max_execution_time' ) ) * 1000
45+
'max_execution_time' => intval( ini_get( 'max_execution_time' ) ) * 1000,
46+
'offline_sync_min_freq' => GIGYA__OFFLINE_SYNC_MIN_FREQ
4447
);
4548

4649
$params = apply_filters( 'gigya_admin_params', $params );
@@ -178,13 +181,14 @@ public static function adminPage() {
178181
* @throws Exception
179182
*/
180183
public static function onSave() {
184+
$cms = new gigyaCMS();
185+
181186
/* When a Gigya's setting page is submitted */
182187
if ( isset( $_POST['gigya_global_settings'] ) ) {
183-
$cms = new gigyaCMS();
184188

185189
$auth_field = 'api_secret';
186-
if ($_POST['gigya_global_settings']['auth_mode'] === 'user_rsa') {
187-
$auth_field = 'rsa_private_key';
190+
if ( $_POST['gigya_global_settings']['auth_mode'] === 'user_rsa' ) {
191+
$auth_field = 'rsa_private_key';
188192
$_POST['gigya_gigya_settings']['rsa_private_key'] = '';
189193
} else {
190194
$_POST['gigya_gigya_settings']['api_secret'] = '';
@@ -225,6 +229,8 @@ public static function onSave() {
225229
update_option( 'users_can_register', 0 );
226230
}
227231
} elseif ( isset( $_POST['gigya_field_mapping_settings'] ) ) {
232+
$has_error = false;
233+
228234
/* Validate field mapping settings, including offline sync */
229235
$data = $_POST['gigya_field_mapping_settings'];
230236
if ( $data['map_offline_sync_enable'] ) {
@@ -233,6 +239,7 @@ public static function onSave() {
233239
__( 'Error: Offline sync job frequency cannot be lower than ' . GIGYA__OFFLINE_SYNC_MIN_FREQ . ' minutes' ),
234240
'error' );
235241
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
242+
$has_error = true;
236243
}
237244

238245
$emails_are_valid = true;
@@ -244,6 +251,7 @@ public static function onSave() {
244251
if ( ! $emails_are_valid ) {
245252
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', __( 'Error: Invalid emails entered' ), 'error' );
246253
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
254+
$has_error = true;
247255
}
248256
}
249257

@@ -256,6 +264,40 @@ public static function onSave() {
256264
if ( $data['map_offline_sync_enable'] ) {
257265
wp_schedule_event( time(), 'gigya_offline_sync_custom', $cron_name );
258266
}
267+
268+
if ( $data['map_raas_full_map'] ) {
269+
$params = [ 'include' => 'profileSchema, dataSchema, subscriptionsSchema, preferencesSchema, systemSchema' ];
270+
$response = '';
271+
272+
try {
273+
$response = $cms->call( 'accounts.getSchema', $params );
274+
275+
} catch ( GSException $e ) {
276+
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', __( 'Settings saved.' ) . '<p>' . __( 'Warning: Can\'t reach SAP servers, please check the global configuration settings.' ) . '</p>', 'warning' );
277+
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
278+
$has_error = true;
279+
}
280+
if ( is_wp_error( $response ) ) {
281+
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', __( 'Settings saved.' ) . '<p>' . __( 'Warning: Can\'t reach SAP servers, please check the global configuration settings: ' ) . $response->get_error_message() . '</p>', 'warning' );
282+
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
283+
284+
} elseif ( $response['errorCode'] === 0 ) {
285+
$error_message = '';
286+
try {
287+
$error_message = static::getDuplicateAndMissingFields( $data['map_raas_full_map'], $response );
288+
} catch ( Exception $e ) {
289+
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', $e->getMessage(), 'error' );
290+
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
291+
$has_error = true;
292+
}
293+
294+
//Sending the warning message if necessary.
295+
if ( ! empty( $error_message ) and ( ! $has_error ) ) {
296+
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', $error_message, 'warning' );
297+
}
298+
}
299+
}
300+
259301
} elseif ( isset( $_POST['gigya_screenset_settings'] ) ) {
260302
/* Screen-set page validation */
261303
foreach ( $_POST['gigya_screenset_settings']['custom_screen_sets'] as $key => $screen_set ) {
@@ -277,6 +319,189 @@ public static function onSave() {
277319
}
278320
}
279321

322+
323+
/**
324+
* The function reads the field mapping map and searches for duplication in cmsName fields,
325+
* and missing fields in the gigyaCms property.
326+
*
327+
* @param $data string The data of field mapping setting page.
328+
* @param $response array The response from 'accounts.getSchema' query.
329+
*
330+
* @return string The error message should be printing. Empty string will return in case of no errors.
331+
* @throws Exception An exception will be thrown if there is an issue with the JSON, missing gigyName or cmsName, or if one of the properties is empty.
332+
*/
333+
private static function getDuplicateAndMissingFields( $data, $response ) {
334+
335+
336+
$array_of_wp_fields = array();
337+
$duplications_of_wp_fields = array();
338+
$unnecessary_fields = array();
339+
$not_existing_fields = array();
340+
$is_valid = true;
341+
$invalid_json_error_message = 'Error: The field mapping configuration must be an array of objects containing the following fields: cmsName, gigyaName.';
342+
$warning_message = __( 'Settings saved.' ) . '<p>' . __( 'Warning:' ) . '</p>';
343+
$json_block = json_decode( stripslashes( $data ), true );
344+
$does_have_several_warnings = false;
345+
346+
//JSON validations.
347+
if ( ( is_null( $json_block ) ) or ( $json_block === false ) or ( ! is_array( $json_block ) and ! empty( (array) $json_block ) ) ) {
348+
throw new Exception( $invalid_json_error_message );
349+
}
350+
351+
if ( is_array( $json_block ) ) {
352+
353+
//Searching each block.
354+
foreach ( $json_block as $meta_key ) {
355+
if ( ! is_array( $meta_key ) ) {
356+
throw new Exception( $invalid_json_error_message );
357+
}
358+
359+
$error = static::getBlockValidationError( $meta_key );
360+
if ( ! empty( $error ) ) {
361+
throw new Exception( $error );
362+
}
363+
364+
$gigya_name = $meta_key['gigyaName'];
365+
$cms_name = $meta_key['cmsName'];
366+
367+
368+
//Searching for duplications of WP fields.
369+
if ( array_key_exists( $cms_name, $array_of_wp_fields ) === false ) {
370+
$array_of_wp_fields[ $cms_name ] = $gigya_name;
371+
} elseif ( $array_of_wp_fields[ $cms_name ] !== $gigya_name ) {
372+
$duplications_of_wp_fields[] = $cms_name;
373+
}
374+
375+
//Searching for not existing fields in gigya
376+
if ( ! static::doesFieldExist( $response, $gigya_name ) ) {
377+
$not_existing_fields[] = $gigya_name;
378+
379+
}
380+
381+
//Getting unnecessary fields.
382+
if ( count( $meta_key ) > 2 ) {
383+
$fields = array_keys( $meta_key );
384+
foreach ( $fields as $field ) {
385+
if ( ( $field !== 'gigyaName' ) and ( $field !== 'cmsName' ) ) {
386+
$unnecessary_fields[] = $field;
387+
}
388+
}
389+
}
390+
}
391+
}
392+
393+
$duplications_of_wp_fields_str = implode( ', ', $duplications_of_wp_fields );
394+
$not_existing_fields_str = implode( ', ', $not_existing_fields );
395+
$unnecessary_fields_str = implode( ', ', $unnecessary_fields );
396+
397+
//Checking if there is two different warnings cases
398+
if ( ( ! empty( $duplications_of_wp_fields_str ) and ! empty( $not_existing_fields_str ) )
399+
or ( ! empty( $duplications_of_wp_fields_str ) and ! empty( $unnecessary_fields_str ) )
400+
or ( ! empty( $not_existing_fields_str ) and ! empty( $unnecessary_fields_str ) ) ) {
401+
402+
$warning_message = __( 'Settings saved.' ) . '<p>' . __( 'Warning:' ) . '</p>' . '<ol class="gigya-field-mapping-error-p">';
403+
$does_have_several_warnings = true;
404+
}
405+
406+
407+
//Builds the error message.
408+
if ( ! empty( $not_existing_fields_str ) ) {
409+
$case = __( 'The following fields were not found in Customer Data Cloud\'s account schema:' );
410+
$solution = __( 'Please make sure that the names are spelled correctly, or add the fields in the schema editor.' );
411+
$warning_message .= static:: fieldsMappingWarningsBuilder( $case, $not_existing_fields_str, $solution, $does_have_several_warnings );
412+
$is_valid = false;
413+
}
414+
415+
if ( ! empty( $duplications_of_wp_fields_str ) ) {
416+
417+
$case = __( 'The following duplicates have been found in the cmsName field:' );
418+
$solution = __( 'Field mapping for these fields may not work as expected.' );
419+
$warning_message .= static:: fieldsMappingWarningsBuilder( $case, $duplications_of_wp_fields_str, $solution, $does_have_several_warnings );
420+
$is_valid = false;
421+
}
422+
423+
if ( ! empty( $unnecessary_fields_str ) ) {
424+
425+
$case = __( 'The following fields will be ignored:' );
426+
$solution = '';
427+
$warning_message .= static:: fieldsMappingWarningsBuilder( $case, $unnecessary_fields_str, $solution, $does_have_several_warnings );
428+
$is_valid = false;
429+
}
430+
if ( $does_have_several_warnings ) {
431+
$warning_message .= '</ol>';
432+
}
433+
434+
if ( $is_valid ) {
435+
return '';
436+
} else {
437+
return $warning_message;
438+
}
439+
}
440+
441+
/**
442+
* @param $block array Array of JSON properties to validate that cmsName and gigyaName exist and are not empty.
443+
*
444+
* @return string The error message will be sent to the user, and empty string in case there is no error.
445+
*/
446+
447+
private static function getBlockValidationError( $block ) {
448+
if ( ( ! key_exists( 'gigyaName', $block ) ) and ( ! key_exists( 'cmsName', $block ) ) and ( count( $block ) === 0 ) ) { //empty JSON
449+
return '';
450+
} elseif ( ( ! key_exists( 'gigyaName', $block ) ) || ( ! key_exists( 'cmsName', $block ) ) ) { //Missing property
451+
return 'Error: gigyaName or cmsName does not exist in one of the blocks of the field mapping JSON.';
452+
} elseif ( key_exists( 'gigyaName', $block ) and empty( $block['gigyaName'] ) ) { // gigyaName is empty
453+
return 'Error: gigyaName is empty in the field mapping JSON, this property can\'t be empty. Please enter a value.';
454+
455+
} elseif ( key_exists( 'cmsName', $block ) and empty( $block['cmsName'] ) ) { //cmsName is empty
456+
return 'Error: cmsName is empty in the field mapping JSON, this property can\'t be empty. Please enter a value.';
457+
} else {
458+
return '';
459+
}
460+
}
461+
462+
/**
463+
* @param $response array The response from 'accounts.getSchema' query.
464+
* @param $key string Field to search for in Gigya schema, with dot notation.
465+
* Example: subscriptions.mySubscription.isSubscribed
466+
*
467+
* @return bool True if the field has been found false otherwise.
468+
*/
469+
private static function doesFieldExist( $response, $key ) {
470+
471+
if ( empty( $key ) ) {
472+
return false;
473+
}
474+
475+
$field_name_in_array = explode( '.', $key );
476+
$schema_type = $field_name_in_array[0];
477+
$field_name = substr( strpbrk( $key, '.' ), 1 );
478+
479+
480+
switch ( $schema_type ) {
481+
case 'profile':
482+
$array_of_keys = $response['profileSchema'] ['fields'];
483+
break;
484+
case'data':
485+
$array_of_keys = $response['dataSchema']['fields'];
486+
break;
487+
case 'subscriptions':
488+
$array_of_keys = $response['subscriptionsSchema']['fields'];
489+
break;
490+
case 'preferences':
491+
$array_of_keys = $response['preferencesSchema']['fields'];
492+
break;
493+
494+
//Should be the systemSchema (doesn't include specific name).
495+
default:
496+
$field_name = $key;
497+
$array_of_keys = $response['systemSchema']['fields'];
498+
break;
499+
500+
}
501+
502+
return ( ( ! empty ( $field_name ) ) and array_key_exists( $field_name, $array_of_keys ) );
503+
}
504+
280505
public static function generateMachineName( $desktop_screen_set_id, $serial ) {
281506
$machine_name = $desktop_screen_set_id;
282507
if ( $serial !== 0 ) {
@@ -286,6 +511,24 @@ public static function generateMachineName( $desktop_screen_set_id, $serial ) {
286511
return $machine_name;
287512
}
288513

514+
public static function fieldsMappingWarningsBuilder( $case_str, $fields, $solution, $does_have_several_warnings ) {
515+
516+
517+
$error = $case_str
518+
. '<br>&nbsp;&nbsp;&nbsp;'
519+
. '<i>' . $fields . '</i>'
520+
. '<br>'
521+
. $solution;
522+
523+
if ( $does_have_several_warnings ) {
524+
$error = '<li>' . $error . '</li>';
525+
} else {
526+
$error = '<p class="gigya-field-mapping-error-p">' . $error . '</p>';
527+
}
528+
529+
return $error;
530+
}
531+
289532
/**
290533
* Set the POSTed secret key.
291534
* If it's not submitted, take it from DB.
@@ -310,7 +553,7 @@ private static function _setObfuscatedField( $field ) {
310553
}
311554

312555
private static function setError( $errorCode, $errorMessage, $callId = null ) {
313-
$errorLink = "<a href='https://developers.gigya.com/display/GD/Response+Codes+and+Errors+REST' target='_blank' rel='noopener noreferrer'>Response_Codes_and_Errors</a>";
556+
$errorLink = "<a href='https://help.sap.com/viewer/8b8d6fffe113457094a17701f63e3d6a/GIGYA/en-US/416d41b170b21014bbc5a10ce4041860.html' target='_blank' rel='noopener noreferrer'>Response_Codes_and_Errors</a>";
314557
$message = "SAP CDC API error: {$errorCode} - {$errorMessage}.";
315558
add_settings_error( 'gigya_global_settings', 'api_validate', __( $message . " For more information please refer to {$errorLink}", 'error' ) );
316559
error_log( 'Error updating SAP CDC settings: ' . $message . ' Call ID: ' . $callId );

admin/forms/fieldMappingForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function fieldMappingForm() {
99

1010
if ( $login_options['mode'] == 'raas' ) {
1111
$form['map_fieldmapping_desc'] = [
12-
'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>' ),
12+
'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://github.com/gigya/wordpress/wiki#mapping-gigya-user-fields-to-wordpress-fields-1" target="_blank" rel="noopener noreferrer" />here</a>.</p>' ),
1313
];
1414

1515
$gigya_full_map = _gigParam( $values, 'map_raas_full_map', '' );
@@ -53,7 +53,7 @@ function fieldMappingForm() {
5353
];
5454
} elseif ( $login_options['mode'] == 'wp_sl' ) {
5555
$form['map_social_title'] = [
56-
'markup' => __( '<h4>Mapping SAP Customer Data Cloud user fields to WordPress fields</h4><p>Define which fields to map from SAP CDC 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>' ),
56+
'markup' => __( '<h4>Mapping SAP Customer Data Cloud user fields to WordPress fields</h4><p>Define which fields to map from SAP CDC to WordPress. The WordPress mapped target fields will be populated with data copied from the corresponding source fields. Learn more <a href="https://github.com/gigya/wordpress/wiki#mapping-gigya-user-fields-to-wordpress-fields-1" target="_blank" rel="noopener noreferrer" />here</a></p>' ),
5757
];
5858

5959
$form['map_social_first_name'] = [

admin/forms/globalSettingsForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function globalSettingsForm() {
101101
'type' => 'text',
102102
'label' => __( 'List of providers' ),
103103
'value' => _gigParam( $values, 'enabledProviders', '*' ),
104-
'desc' => __( 'Comma separated list of providers to include. For example: facebook,twitter,google. Leave empty or type * for all providers. See the entire ' ) . ' <a href="https://developers.gigya.com/display/GD/socialize.showLoginUI+JS">list of available providers</a>.'
104+
'desc' => __( 'Comma separated list of providers to include. For example: facebook,twitter,google. Leave empty or type * for all providers. See the entire ' ) . ' <a href="https://help.sap.com/viewer/8b8d6fffe113457094a17701f63e3d6a/GIGYA/en-US/417916f470b21014bbc5a10ce4041860.html">list of available providers</a>.'
105105
);
106106

107107
$form['lang'] = array(
@@ -149,7 +149,7 @@ function globalSettingsForm() {
149149
'class' => 'json',
150150
'value' => _gigParam( $values, 'advanced', '' ),
151151
'label' => __( 'Additional Parameters (advanced)' ),
152-
'desc' => sprintf( __( 'Enter valid %s. See list of available ' ), '<a class="gigya-json-example" href="javascript:void(0)">' . __( 'JSON format' ) . '</a>' ) . ' <a href="https://developers.gigya.com/display/GD/Global+Configuration#GlobalConfiguration-DataMembers" target="_blank" rel="noopener noreferrer">' . __( 'parameters' ) . '</a>'
152+
'desc' => sprintf( __( 'Enter valid %s. See list of available ' ), '<a class="gigya-json-example" href="javascript:void(0)">' . __( 'JSON format' ) . '</a>' ) . ' <a href="https://help.sap.com/viewer/8b8d6fffe113457094a17701f63e3d6a/GIGYA/en-US/417fa48b70b21014bbc5a10ce4041860.html" target="_blank" rel="noopener noreferrer">' . __( 'parameters' ) . '</a>'
153153
);
154154

155155
$form['google_analytics'] = array(
@@ -182,7 +182,7 @@ function globalSettingsForm() {
182182

183183
if ( get_option( 'gigya_settings_fields' ) ) {
184184
$form['clean_db'] = array(
185-
'markup' => '<a href="javascript:void(0)" class="clean-db">Database cleaner after upgrade</a><br><small>Press this button to remove all unnecessary elements of the previous version from your database.Please make sure to backup your database before performing the clean. Learn more about upgrading from the previous version <a href="https://developers.gigya.com/display/GD/WordPress+Plugin#WordPressPlugin-InstallingtheGigyaPluginforWordPress">here.</a></small>'
185+
'markup' => '<a href="javascript:void(0)" class="clean-db">Database cleaner after upgrade</a><br><small>Press this button to remove all unnecessary elements of the previous version from your database.Please make sure to backup your database before performing the clean.For more information about upgrading from the previous version <a href="https://github.com/gigya/wordpress/wiki#installing-the-gigya-plugin-for-wordpress-1">here.</a></small>'
186186
);
187187
}
188188

0 commit comments

Comments
 (0)