"use strict"; var itsecLogsPage = { init: function() { this.bindEvents(); var id = itsecUtil.getUrlParameter( 'id' ); if ( false !== id ) { itsecLogsPage.showLog( id ); } itsecLogsPage.originalHREF = jQuery( '.itsec-module-cards-container .subsubsub a.current' ).attr( 'href' ); this.migrateOldLogs(); }, bindEvents: function() { var $container = jQuery( '#wpcontent' ); $container.on( 'click', '.itsec-logs-view-details', this.showModal ); $container.on( 'click', '.itsec-close-modal, .itsec-modal-background', this.closeModal ); $container.on( 'click', '.itsec-log-raw-details-toggle', this.toggleRawDetails ); $container.on( 'keyup', this.closeModal ); }, toggleRawDetails: function( e ) { e.preventDefault(); if ( jQuery( '.itsec-log-raw-details' ).is( ':visible' ) ) { jQuery( this ).html( itsec_page.translations.show_raw_details ); jQuery( '.itsec-log-raw-details' ).hide(); } else { jQuery( this ).html( itsec_page.translations.hide_raw_details ); jQuery( '.itsec-log-raw-details' ).show(); } }, showModal: function( e ) { e.preventDefault(); var id = jQuery( this ).parent().parent().find( 'td.column-id' ).html(); try { if ( '' != itsecLogsPage.originalHREF ) { window.history.replaceState( {}, '', itsecLogsPage.originalHREF + '&id=' + id ); } } catch( err ) {} itsecLogsPage.showLog( id ); }, showLog: function( id ) { var $modalBackground = jQuery( '.itsec-modal-background' ), $detailsContainer = jQuery( '#itsec-log-details-container' ); jQuery( '#itsec-log-details-container .itsec-module-messages-container' ).html( '' ); jQuery( '#itsec-log-details-container .itsec-module-settings-content-main' ).html( itsec_page.translations.loading ); $modalBackground.fadeIn(); $detailsContainer.fadeIn( 200 ); jQuery( 'body' ).addClass( 'itsec-modal-open' ); var $cached_data = jQuery( '#itsec-logs-cache-id-' + id ); if ( $cached_data.length ) { jQuery( '#itsec-log-details-container' ).html( $cached_data.html() ); jQuery( '.itsec-log-raw-details' ).hide(); return; } var postData = { 'action': itsec_page.ajax_action, 'nonce': itsec_page.ajax_nonce, 'id': id, }; jQuery.post( ajaxurl, postData ) .always(function( a, status, b ) { itsecLogsPage.updateDetails( a, status, b, id ); }); }, updateDetails: function( a, status, b, id ) { var results = { 'id': id, 'status': status, 'jqxhr': null, 'success': false, 'response': null, 'errors': [], 'warnings': [], 'messages': [], 'functionCalls': [], 'redirect': false, 'closeModal': true }; if ( 'ITSEC_Response' === a.source && 'undefined' !== a.response ) { // Successful response with a valid format. results.jqxhr = b; results.success = a.success; results.response = a.response; results.errors = a.errors; results.warnings = a.warnings; results.messages = a.messages; results.functionCalls = a.functionCalls; results.redirect = a.redirect; results.closeModal = a.closeModal; } else if ( a.responseText ) { // Failed response. results.jqxhr = a; var errorThrown = b; if ( 'undefined' === typeof results.jqxhr.status ) { results.jqxhr.status = -1; } if ( 'timeout' === status ) { var error = itsec_page.translations.ajax_timeout; } else if ( 'parsererror' === status ) { var error = itsec_page.translations.ajax_parsererror; } else if ( 403 == results.jqxhr.status ) { var error = itsec_page.translations.ajax_forbidden; } else if ( 404 == results.jqxhr.status ) { var error = itsec_page.translations.ajax_not_found; } else if ( 500 == results.jqxhr.status ) { var error = itsec_page.translations.ajax_server_error; } else { var error = itsec_page.translations.ajax_unknown; } error = error.replace( '%1$s', status ); error = error.replace( '%2$s', errorThrown ); results.errors = [ error ]; } else { // Successful response with an invalid format. results.jqxhr = b; results.response = a; results.errors = [ itsec_page.translations.ajax_invalid ]; } if ( results.redirect ) { window.location = results.redirect; } var $messages = jQuery( '#itsec-log-details-container .itsec-module-messages-container' ), $content = jQuery( '#itsec-log-details-container .itsec-module-settings-content-main' ); $messages.html( '' ); for ( var i = 0; i < results.errors.length; i++ ) { $messages.append( '
' + results.errors[i] + '
' + results.warnings[i] + '
' + results.messages[i] + '
' + message + '
' + itsec_page.translations.log_migration_failed + '
' + warning + '