Skip to content

Commit 72965dc

Browse files
committed
* Variable 'debug' has been removed.
* _gigya_error_log function has been removed. * New GigyaLogger class added. * There is no 'error_log' used except while checking the new gigya_log.txt file. * Drop list of log level added in global settings with dynamic description.
1 parent e1a5080 commit 72965dc

14 files changed

+268
-105
lines changed

GigyaAction.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Gigya\WordPress\Admin\GigyaSettings;
1111
use GigyaHookException;
1212
use GigyaInstall;
13+
1314
use WP_User;
1415

1516
/**
@@ -19,6 +20,7 @@ class GigyaAction {
1920
protected $login_options;
2021
protected $global_options;
2122
protected $session_options;
23+
protected $logger;
2224

2325
/**
2426
* Constructor.
@@ -38,7 +40,7 @@ public function __construct() {
3840
define( 'GIGYA__API_SECRET', $this->global_options['api_secret'] );
3941
define( 'GIGYA__PRIVATE_KEY', $this->global_options['rsa_private_key'] ?? '' );
4042
define( 'GIGYA__API_DOMAIN', _gigya_data_center( $this->global_options ) );
41-
define( 'GIGYA__API_DEBUG', $this->global_options['debug'] );
43+
define( 'GIGYA__LOG_LEVEL', $this->global_options['logLevel'] );
4244
define( 'GIGYA__JS_CDN', 'https://cdns.' . GIGYA__API_DOMAIN . '/js/socialize.js' );
4345
} else {
4446
define( 'GIGYA__API_KEY', '' );
@@ -48,7 +50,7 @@ public function __construct() {
4850
define( 'GIGYA__API_SECRET', '' );
4951
define( 'GIGYA__PRIVATE_KEY', '' );
5052
define( 'GIGYA__API_DOMAIN', '' );
51-
define( 'GIGYA__API_DEBUG', '' );
53+
define( 'GIGYA__LOG_LEVEL', 'info' );
5254
define( 'GIGYA__JS_CDN', GIGYA__DEFAULT_JS_CDN );
5355
}
5456

@@ -113,6 +115,7 @@ public function init()
113115
require_once GIGYA__PLUGIN_DIR . 'cms_kit/GigyaAuthRequest.php';
114116
require_once GIGYA__PLUGIN_DIR . 'cms_kit/GSApiException.php';
115117
require_once GIGYA__PLUGIN_DIR . 'cms_kit/GSFactory.php';
118+
require_once GIGYA__PLUGIN_DIR . 'features/GigyaLogger.php';
116119

117120
GSResponse::init();
118121

@@ -211,6 +214,7 @@ public function init()
211214

212215
new GigyaSettings;
213216
}
217+
$this->logger = new GigyaLogger();
214218
}
215219

216220
/**
@@ -271,7 +275,7 @@ public function ajaxProcessFieldMapping() {
271275

272276
wp_send_json_success();
273277
} catch ( Exception $e ) {
274-
error_log( 'Unable to process field mapping for SAP Customer Data Cloud user ' . $gigya_uid );
278+
$this->logger->error( 'Unable to process field mapping for SAP Customer Data Cloud user ' . $gigya_uid );
275279

276280
wp_send_json_error( [ 'msg' => $generic_msg ] );
277281
}
@@ -502,7 +506,7 @@ public function wpLogin( $user_login, $account ) {
502506
}
503507

504508
if ( empty( $_POST['action'] ) ) {
505-
error_log( 'Login: No POST action specified' );
509+
$this->logger->error( 'Login: No POST action specified' );
506510
} else {
507511
/* RaaS Login */
508512
if ( $_POST['action'] === 'gigya_raas' ) {
@@ -644,7 +648,7 @@ public function getOutOfSyncUsers() {
644648

645649
if ( ! is_dir( GIGYA__USER_FILES ) ) {
646650
$message = "Could not generate report: The path: " . GIGYA__USER_FILES . " does not exist";
647-
error_log( $message );
651+
$this->logger->error( $message );
648652
wp_send_json_error( $message );
649653

650654
return;
@@ -657,14 +661,14 @@ public function getOutOfSyncUsers() {
657661
$message = "There was an error getting the data from SAP servers, callID: " . $e->getCallId() . ', Error Code: ' . $e->getErrorCode();
658662

659663
wp_send_json_error( $message );
660-
error_log( $message );
664+
$this->logger->error( $message );
661665
return;
662666

663667
} catch ( GSException $e ) {
664668
$message = "Could not reach SAP server: " . $e->errorMessage;
665669

666670
wp_send_json_error( $message );
667-
error_log( $message );
671+
$this->logger->error( $message );
668672
return;
669673
}
670674

@@ -826,30 +830,30 @@ public function executeOfflineSyncCron() {
826830
$uids_not_found[] = $gigya_user['UID'];
827831
}
828832
} else {
829-
error_log( 'Gigya offline sync: unable to process user due to a lack of essential data. User data received: ' . json_encode( $gigya_user,
833+
$this->logger->error( 'Gigya offline sync: unable to process user due to a lack of essential data. User data received: ' . json_encode( $gigya_user,
830834
JSON_PRETTY_PRINT ) );
831835
}
832836
}
833837

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

837-
error_log( 'Gigya offline sync completed. Users processed: ' . $processed_users . ( ( $users_not_found )
841+
$this->logger->error( 'Gigya offline sync completed. Users processed: ' . $processed_users . ( ( $users_not_found )
838842
? '. Users not found: ' . $users_not_found . PHP_EOL . implode( ',' . PHP_EOL, $uids_not_found )
839843
: '' ) );
840844

841845
$status = ( $users_not_found > 0 ) ? 'completed with errors' : 'succeeded';
842846
$helper->sendCronEmail( 'offline sync', $status, $email_on_success, $processed_users, $users_not_found );
843847
} catch ( GigyaHookException $e ) {
844-
error_log( 'Gigya offline sync: There was a problem adding custom data to field mapping: ' . $e->getMessage() );
848+
$this->logger->error( 'Gigya offline sync: There was a problem adding custom data to field mapping: ' . $e->getMessage() );
845849
$status = 'failed';
846850
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
847851
} catch ( GSApiException $e ) {
848-
error_log( 'Offline sync failed: ' . $e->getErrorCode() . '' . $e->getMessage() . '. Call ID: ' . $e->getCallId() );
852+
$this->logger->error( 'Offline sync failed: ' . $e->getErrorCode() . '' . $e->getMessage() . '. Call ID: ' . $e->getCallId() );
849853
$status = 'failed';
850854
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
851855
} catch ( Exception $e ) {
852-
error_log( 'Offline sync failed: ' . $e->getMessage() );
856+
$this->logger->error( 'Offline sync failed: ' . $e->getMessage() );
853857
$status = 'failed';
854858
$helper->sendCronEmail( 'offline sync', $status, $email_on_failure );
855859
}

admin/admin.GigyaSettings.php

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Gigya\CMSKit\GigyaApiHelper;
1111
use Gigya\CMSKit\GigyaCMS;
1212
use Gigya\PHP\GSException;
13+
use Gigya\WordPress\GigyaLogger;
1314

1415
define( "GIGYA__PERMISSION_LEVEL", "manage_options" );
1516
define( "GIGYA__SECRET_PERMISSION_LEVEL", "install_plugins" ); // Network super admin + single site admin
@@ -181,10 +182,13 @@ public static function adminPage() {
181182
* @throws Exception
182183
*/
183184
public static function onSave() {
184-
$cms = new gigyaCMS();
185+
$cms = new gigyaCMS();
186+
$logger = new GigyaLogger();
187+
185188

186189
/* When a Gigya's setting page is submitted */
187190
if ( isset( $_POST['gigya_global_settings'] ) ) {
191+
$is_gigya_log_fie_valid = static:: doesGigyaErrorLogValid();
188192

189193
$auth_field = 'api_secret';
190194
if ( $_POST['gigya_global_settings']['auth_mode'] === 'user_rsa' ) {
@@ -215,10 +219,18 @@ public static function onSave() {
215219
}
216220
} else {
217221
add_settings_error( 'gigya_global_settings', 'api_validate', __( 'Error sending request to SAP CDC' ), 'error' );
222+
return;
218223
}
219224
} else {
220225
add_settings_error( 'gigya_global_settings', 'api_validate', __( 'Error retrieving existing secret key or private key from the database. This is normal if you have a multisite setup. Please re-enter the key.' ), 'error' );
226+
return;
221227
}
228+
if ( ! $is_gigya_log_fie_valid ) {
229+
add_settings_error( 'gigya_global_settings', 'gigya_validate', __( 'Settings saved.' ) . '<p>' . __( 'Warning: Can\'t reach SAP CDC "gigya_log" file, please make sure the file exists at the path' . GIGYA__LOG_FILE . '.' ) . '</p>', 'warning' );
230+
} else {
231+
232+
$logger->info( '"Global Settings" page was saved successfully.' );
233+
};
222234
} elseif ( isset( $_POST['gigya_login_settings'] ) ) {
223235
/*
224236
* When we turn on the Gigya's social login plugin, we also turn on the WP 'Membership: Anyone can register' option
@@ -228,8 +240,9 @@ public static function onSave() {
228240
} elseif ( $_POST['gigya_login_settings']['mode'] == 'raas' ) {
229241
update_option( 'users_can_register', 0 );
230242
}
243+
244+
$logger->info('"User Management" settings page was saved successfully.');
231245
} elseif ( isset( $_POST['gigya_field_mapping_settings'] ) ) {
232-
$has_error = false;
233246

234247
/* Validate field mapping settings, including offline sync */
235248
$data = $_POST['gigya_field_mapping_settings'];
@@ -239,7 +252,7 @@ public static function onSave() {
239252
__( 'Error: Offline sync job frequency cannot be lower than ' . GIGYA__OFFLINE_SYNC_MIN_FREQ . ' minutes' ),
240253
'error' );
241254
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
242-
$has_error = true;
255+
return;
243256
}
244257

245258
$emails_are_valid = true;
@@ -251,7 +264,7 @@ public static function onSave() {
251264
if ( ! $emails_are_valid ) {
252265
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', __( 'Error: Invalid emails entered' ), 'error' );
253266
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
254-
$has_error = true;
267+
return;
255268
}
256269
}
257270

@@ -266,38 +279,37 @@ public static function onSave() {
266279
}
267280

268281
if ( $data['map_raas_full_map'] ) {
269-
$params = [ 'include' => 'profileSchema, dataSchema, subscriptionsSchema, preferencesSchema, systemSchema' ];
270-
$response = '';
282+
$error_message = '';
283+
$params = [ 'include' => 'profileSchema, dataSchema, subscriptionsSchema, preferencesSchema, systemSchema' ];
271284

272285
try {
273286
$response = $cms->call( 'accounts.getSchema', $params );
274287

275288
} 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;
289+
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' );
290+
return;
279291
}
280292
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' );
293+
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' );
294+
return;
283295

284296
} elseif ( $response['errorCode'] === 0 ) {
285-
$error_message = '';
286297
try {
287298
$error_message = static::getDuplicateAndMissingFields( $data['map_raas_full_map'], $response );
288299
} catch ( Exception $e ) {
289300
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', $e->getMessage(), 'error' );
290301
static::_keepOldApiValues( 'gigya_field_mapping_settings' );
291-
$has_error = true;
302+
return;
292303
}
304+
}
293305

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-
}
306+
//Sending the warning message if necessary.
307+
if ( ! empty( $error_message ) ) {
308+
add_settings_error( 'gigya_field_mapping_settings', 'gigya_validate', $error_message, 'warning' );
298309
}
299310
}
300311

312+
$logger->info( '"Field Mapping" settings page was saved successfully.' );
301313
} elseif ( isset( $_POST['gigya_screenset_settings'] ) ) {
302314
/* Screen-set page validation */
303315
foreach ( $_POST['gigya_screenset_settings']['custom_screen_sets'] as $key => $screen_set ) {
@@ -316,6 +328,7 @@ public static function onSave() {
316328
unset( $_POST['gigya_screenset_settings']['custom_screen_sets'][ $key ] );
317329
}
318330
}
331+
$logger->info('"Screen-Sets" settings page was saved successfully.');
319332
}
320333
}
321334

@@ -553,10 +566,11 @@ private static function _setObfuscatedField( $field ) {
553566
}
554567

555568
private static function setError( $errorCode, $errorMessage, $callId = null ) {
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>";
557-
$message = "SAP CDC API error: {$errorCode} - {$errorMessage}.";
569+
$logger = new GigyaLogger();
570+
$errorLink = "<a href='https://help.sap.com/viewer/8b8d6fffe113457094a17701f63e3d6a/GIGYA/en-US/416d41b170b21014bbc5a10ce4041860.html' target='_blank' rel='noopener noreferrer'>Response_Codes_and_Errors</a>";
571+
$message = "SAP CDC API error: {$errorCode} - {$errorMessage}.";
558572
add_settings_error( 'gigya_global_settings', 'api_validate', __( $message . " For more information please refer to {$errorLink}", 'error' ) );
559-
error_log( 'Error updating SAP CDC settings: ' . $message . ' Call ID: ' . $callId );
573+
$logger->error( 'Error updating SAP CDC settings: ' . $message . ' Call ID: ' . $callId );
560574
}
561575

562576
/**
@@ -607,4 +621,20 @@ public static function _getSiteOptions( $option = GIGYA__SETTINGS_GLOBAL ) {
607621
return $options;
608622
}
609623

624+
private static function doesGigyaErrorLogValid() {
625+
if ( ! is_dir( GIGYA__LOG_FILE ) ) {
626+
$error_message = "Could not open the SAP CDC log file at: " . GIGYA__LOG_FILE . " The parent directory of the file does not exist, or the file is not writable.";
627+
error_log( $error_message );
628+
629+
return false;
630+
};
631+
$file = fopen( GIGYA__LOG_FILE . 'gigya_log.txt', 'a' );
632+
633+
if ( ! fclose( $file ) ) {
634+
return false;
635+
}
636+
637+
return true;
638+
639+
}
610640
}

admin/forms/globalSettingsForm.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,30 @@ function globalSettingsForm() {
152152
'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

155-
$form['google_analytics'] = array(
156-
'type' => 'checkbox',
157-
'label' => __( "Enable Google Social Analytics" ),
158-
'value' => _gigParam( $values, 'google_analytics', 0 )
155+
$form['logLevel'] = array(
156+
'type' => 'select',
157+
'options' => array(
158+
'error' => 'Error Only',
159+
'info' => 'Info',
160+
'debug' => 'Debug'
161+
),
162+
'value' => _gigParam( $values, 'logLevel', 'info' ),
163+
'label' => __( 'Log Level' ),
159164
);
165+
$global_log_level_messag = '<br>' . __( 'for more information click ' ) . '<a href="">here</a>' . '.';
166+
if ( $form['logLevel']['value'] == 'error' ) {
167+
$form['logLevel']['markup'] = __( 'This level is for general site errors only.' ) . '<br>' . __( 'It will include any error shown to the administrator by the SAP CDC plugin in the admin control panel. ' );
168+
} elseif ( $form['logLevel']['value'] == 'info' ) {
169+
$form['logLevel']['markup'] = __( 'Logs all actions done by the administrator in the SAP CDC plugin, including all the errors log that logs in the "Error Only" option. ' );
170+
} elseif ( $form['logLevel']['value'] == 'debug' ) {
171+
$form['logLevel']['markup'] = __( 'Logs all interactions with SAP CDC, i.e. every call made to SAP CDC is logged.' ) . '<br>' . __( ' Also including the "Debug" and "Error Only" levels.' );
172+
}
173+
$form['logLevel']['markup'] = '<small id="gigya_logLevel_markup">' . $form['logLevel']['markup'] . $global_log_level_messag . '</small>';
160174

161-
$form['debug'] = array(
162-
'type' => 'checkbox',
163-
'label' => __( 'Enable SAP CDC debug log' ),
164-
'value' => _gigParam( $values, 'debug', 0 ),
165-
'desc' => __( 'Log all SAP Customer Data Cloud\'s requests and responses. You can then find the log' ) . ' <a href="javascript:void(0)" class="gigya-debug-log">' . __( 'here' ) . '</a>'
175+
$form['google_analytics'] = array(
176+
'type' => 'checkbox',
177+
'label' => __( "Enable Google Social Analytics" ),
178+
'value' => _gigParam( $values, 'google_analytics', 0 )
166179
);
167180

168181
/* Use this field in multisite to flag when sub site settings are saved locally for site */

admin/forms/screenSetSettingsForm.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Gigya\CMSKit\GigyaCMS;
4+
use Gigya\WordPress\GigyaLogger;
45

56
function buildCustomScreenSetRow( $screenSetList, $values = array(), $more_options = array(), $more_field_options = array() ) {
67
$more_field_options = array_values( $more_field_options );
@@ -236,14 +237,12 @@ function screenSetSettingsForm() {
236237
'attrs' => array( 'class' => 'notice notice-error is-dismissible' )
237238
);
238239

239-
if ( ! empty( get_option( GIGYA__SETTINGS_GLOBAL )['debug'] ) ) {
240-
if ( $screenset_list !== false ) {
241-
error_log( 'The current site has no screen sets defined. Either the screen-sets are at the parent site level, or they have not been initialized. Check Screen-Set settings in the SAP CDC console.' );
242-
}
243-
$connection_error['error_message'] = array( $first_line, $second_line );
244-
} else {
245-
$connection_error['error_message'] = array( $first_line );
246-
};
240+
if ( $screenset_list !== false ) {
241+
$logger = new GigyaLogger();
242+
$logger->error( 'The current site has no screen sets defined. Either the screen-sets are at the parent site level, or they have not been initialized. Check Screen-Set settings in the SAP CDC console.' );
243+
}
244+
245+
$connection_error['error_message'] = array( $first_line, $second_line );
247246

248247
echo _gigya_render_tpl( 'admin/tpl/error-message.tpl.php', $connection_error );
249248
}

admin/gigya_admin.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,30 @@
455455
});
456456
});
457457

458+
// --------------------------------------------------------------------
459+
460+
/*
461+
* Global Settings page
462+
*/
463+
464+
var endDisc = '<br>for more information click <a href="">here</a> ';
465+
var description = '';
466+
467+
$('#gigya_logLevel').on('change', function () {
468+
console.log(this.value);
469+
if (this.value === 'error') {
470+
description = 'This level is for general site errors only.<br>It will include any error shown to the administrator by the SAP CDC plugin in the admin control panel.';
471+
472+
} else if (this.value === 'info') {
473+
description = 'Logs all actions done by the administrator in the SAP CDC plugin, including all the errors log that logs in the "Error Only" option.';
474+
} else if (this.value === 'debug') {
475+
description = 'Logs all interactions with SAP CDC, i.e. every call made to SAP CDC is logged.<br>Also including the "Debug" and "Error Only" levels.';
476+
}
477+
478+
$('#gigya_logLevel_markup').html(description + endDisc);
479+
480+
});
481+
458482
// --------------------------------------------------------------------
459483
/*
460484
* Field-Mapping Settings page

0 commit comments

Comments
 (0)