@@ -81,6 +81,10 @@ public function __construct() {
8181 add_filter ( 'get_avatar ' , array ( $ this , 'getGigyaAvatar ' ), 10 , 5 );
8282 add_filter ( 'login_message ' , 'raas_wp_login_custom_message ' );
8383 add_filter ( 'cron_schedules ' , array ( $ this , 'getOfflineSyncSchedules ' ) );
84+ add_action ( 'wp_ajax_get_out_of_sync_users ' , array ( $ this , 'getOutOfSyncUsers ' ) );
85+ add_action ( 'get_out_of_sync_users ' , array ( $ this , 'getOutOfSyncUsers ' ) );
86+ add_action ( 'wp_ajax_nopriv_get_out_of_sync_users ' , array ( $ this , 'getOutOfSyncUsers ' ) );
87+
8488
8589 /* Plugins shortcode activation switches */
8690 require_once GIGYA__PLUGIN_DIR . 'features/gigyaPluginsShortcodes.php ' ;
@@ -203,6 +207,8 @@ public function init()
203207 if ( is_admin () ) {
204208 /* Loads requirements for the admin settings section. */
205209 require_once GIGYA__PLUGIN_DIR . 'admin/admin.GigyaSettings.php ' ;
210+ require_once GIGYA__PLUGIN_DIR . 'admin/features/GigyaReportGenerator.php ' ;
211+
206212 new GigyaSettings ;
207213 }
208214 }
@@ -629,6 +635,79 @@ public function ajaxCleanDB() {
629635 }
630636 }
631637
638+ /**
639+ * Get GIGYA__SYNC_REPORT_MAX_USERS from Gigya and check if the users exist at WP DB. and that their UIDs match.
640+ * The same idea with getting the same amount of users from Gigya and searching in WP DB.
641+ * the file will be generated inside GIGYA__USER_FILES folder.
642+ */
643+ public function getOutOfSyncUsers () {
644+
645+ if ( ! is_dir ( GIGYA__USER_FILES ) ) {
646+ $ message = "Could not generate report: The path: " . GIGYA__USER_FILES . " does not exist " ;
647+ error_log ( $ message );
648+ wp_send_json_error ( $ message );
649+
650+ return ;
651+ };
652+
653+ try {
654+ $ wp_to_gigya_compare = GigyaReportGenerator::getWPUsersNotInGigya ();
655+ $ gigya_to_wp_compare = GigyaReportGenerator::getGigyaUsersNotInWP ();
656+ } catch ( GSApiException $ e ) {
657+ $ message = "There was an error getting the data from SAP servers, callID: " . $ e ->getCallId () . ', Error Code: ' . $ e ->getErrorCode ();
658+
659+ wp_send_json_error ( $ message );
660+ error_log ( $ message );
661+ return ;
662+
663+ } catch ( GSException $ e ) {
664+ $ message = "Could not reach SAP server: " . $ e ->errorMessage ;
665+
666+ wp_send_json_error ( $ message );
667+ error_log ( $ message );
668+ return ;
669+ }
670+
671+ $ message = '' ;
672+ $ files_names = array_keys ( array_merge ( $ wp_to_gigya_compare , $ gigya_to_wp_compare ) );
673+
674+ /*generating files for each file_name*/
675+ foreach ( $ files_names as $ file_name ) {
676+
677+ if ( array_key_exists ( $ file_name , $ wp_to_gigya_compare ) and array_key_exists ( $ file_name , $ gigya_to_wp_compare ) ) {
678+ $ merged_array = array_merge ( $ wp_to_gigya_compare [ $ file_name ], $ gigya_to_wp_compare [ $ file_name ] );
679+ } else if ( array_key_exists ( $ file_name , $ wp_to_gigya_compare ) ) {
680+ $ merged_array = $ wp_to_gigya_compare [ $ file_name ];
681+ } else if ( array_key_exists ( $ file_name , $ gigya_to_wp_compare ) ) {
682+ $ merged_array = $ gigya_to_wp_compare [ $ file_name ];
683+ } else {
684+ $ merged_array = array ();
685+ }
686+
687+ $ file = fopen ( GIGYA__USER_FILES . $ file_name . '_ ' . date ( "Y-m-d_H-i-s " ) . ".csv " , 'w ' );
688+ if ( strstr ( $ file_name , GigyaReportGenerator::$ SAP_users_not_existing_in_WP ) !== false ) {
689+ fputcsv ( $ file , array ( 'UID ' , 'Email ' ) );
690+ } else {
691+ fputcsv ( $ file , array ( 'ID ' , 'Email ' ) );
692+ }
693+ if ( ! empty ( $ merged_array ) ) {
694+ $ message .= '<br>* ' . $ file_name . '_ ' . date ( "Y-m-d_H-i-s " ) . '.csv ' ;
695+ foreach ( $ merged_array as $ user ) {
696+ fputcsv ( $ file , $ user );
697+ }
698+ }
699+ fclose ( $ file );
700+ }
701+
702+ if ( empty ( $ message ) ) {
703+ $ message = 'All ' . number_format ( GIGYA__SYNC_REPORT_MAX_USERS ) . ' users that have been checked are in sync. ' ;
704+ } else {
705+ $ message = 'The report has been generated successfully and saved to: ' . GIGYA__USER_FILES . '<br> Generated filenames are below. Note, this list does not include empty files. <br> ' . $ message ;
706+ }
707+
708+ wp_send_json_success ( $ message );
709+ }
710+
632711 /**
633712 * Get WordPress user object by Gigya UID
634713 *
@@ -710,6 +789,7 @@ public function executeOfflineSyncCron() {
710789 $ enable_job = $ config ['map_offline_sync_enable ' ];
711790 $ email_on_success = $ config ['map_offline_sync_email_on_success ' ];
712791 $ email_on_failure = $ config ['map_offline_sync_email_on_failure ' ];
792+ $ required_field = 'profile ' ; /* Offline sync might not work on users without a profile */
713793
714794 $ helper = new GigyaOfflineSync ();
715795
@@ -723,7 +803,7 @@ public function executeOfflineSyncCron() {
723803 }
724804 $ gigya_query .= " ORDER BY lastUpdatedTimestamp ASC LIMIT " . GIGYA__OFFLINE_SYNC_MAX_USERS ;
725805 $ gigya_cms = new GigyaCMS ();
726- $ gigya_users = $ gigya_cms ->searchGigyaUsers ( [ 'query ' => $ gigya_query ] );
806+ $ gigya_users = $ gigya_cms ->searchGigyaUsers ( [ 'query ' => $ gigya_query ], $ required_field );
727807 $ processed_users = 0 ;
728808 $ users_not_found = 0 ;
729809 $ uids_not_found = [];
0 commit comments