Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner Interactivity #395

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion css/spbc-settings.min.css

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion css/src/spbc-settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -989,4 +989,19 @@ button#spbc_setting_get_key_auto:disabled {
background: 0 0;
color: #787c82;
cursor: pointer;
}
}

.spbc_accordion_header_caption__update_status {
display: inline-flex;
flex-direction: row;
flex-wrap: nowrap;
width: 45%;
align-items: flex-end;
justify-content: flex-end;
}

.spbc_accordion_header_caption__updated {
display: inline;
color:#FFFFFF;
font-weight: bold;
}
4 changes: 1 addition & 3 deletions inc/spbc-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ function spbc_admin_init()

// Settings
add_action('wp_ajax_spbc_settings__draw_elements', 'spbc_settings__draw_elements');
if (! empty($spbc->data['scanner']['last_scan'])) {
add_action('wp_ajax_spbc_scanner_tab__reload_accordion', 'spbc_field_scanner__show_accordion');
}
add_action('wp_ajax_spbc_scanner_tab__reload_accordion', 'spbc_field_scanner__show_accordion');

// SPBC Table
add_action('wp_ajax_spbc_tbl-action--bulk', array(ListTable::class, 'ajaxBulkActionHandler'));
Expand Down
12 changes: 10 additions & 2 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3485,14 +3485,21 @@ function spbc_field_scanner__show_accordion($direct_call = false)
if ( isset($data['display']) && ! $data['display'] ) {
continue;
}
echo '<div class="spbc_accordion_category_wrapper">';
echo '<div refresh_control_group="' . $_category . '" class="spbc_accordion_category_wrapper">';
echo '<h4 class="spbc_accordion_category_header">' . $data['category_description'] . '</h4>';
foreach ($data['types'] as $type_name) {
if ( !isset($tables_files[$type_name]) ) {
continue;
}
//todo spbc_scanner_get_files_by_category() has internal SQL error on 'approved' column
if ( $type_name !== 'frontend_malware' && $type_name !== 'frontend_scan_results_approved' ) {
if (empty(spbc_scanner_get_files_by_category($type_name))) {
continue;
}
}
$description = $tables_files[$type_name];
$args = spbc_list_table__get_args_by_type($type_name);

$args['id'] = 'spbc_tbl__scanner_' . $type_name;
$args['type'] = $type_name;

Expand All @@ -3511,6 +3518,7 @@ function spbc_field_scanner__show_accordion($direct_call = false)

// Pass output if empty and said to do so
if ( $args['if_empty_items'] !== false || $table->items_total !== 0 ) {
echo '<div refresh_control_tab="' . $type_name . '">';
echo '<h3><a href="#">' . ucwords(str_replace('_', ' ', $type_name))
. ' (<span class="spbc_bad_type_count '
. $type_name . '_counter">' . $table->items_total . '</span>)</a>'
Expand All @@ -3521,7 +3529,7 @@ function spbc_field_scanner__show_accordion($direct_call = false)
. $description
. '</p>';
$table->display();

echo '</div>';
echo "</div>";
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/spbc-scanner-plugin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/spbc-scanner-plugin.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/spbc-table.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions js/spbc-table.min.js.map

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions js/src/spbc-scanner-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ class SpbcMalwareScanner {/* eslint-disable-line no-unused-vars */
successCallback( result ) {
console.log( result );

this.interactAccordion(result);

if ( typeof result.total !== 'undefined' ) {
this.scan_percent = 100 / result.total;
}
Expand Down Expand Up @@ -366,6 +368,28 @@ class SpbcMalwareScanner {/* eslint-disable-line no-unused-vars */
}
};

/**
* Run interactive refresh for accordion.
* @param {obj|string[]} result
*/
interactAccordion(result) {
// validation control
if (result.hasOwnProperty('interactivity_data') &&
result.interactivity_data.hasOwnProperty('update_text') &&
result.interactivity_data.update_text &&
result.interactivity_data.hasOwnProperty('refresh_data') &&
result.interactivity_data.refresh_data.hasOwnProperty('do_refresh') &&
result.interactivity_data.refresh_data.do_refresh &&
result.interactivity_data.refresh_data.hasOwnProperty('control_tab') &&
result.interactivity_data.refresh_data.control_tab
) {
spbcReloadAccordion(
result.interactivity_data.refresh_data.control_tab,
result.interactivity_data.update_text,
);
}
}

/**
* Function Error
* @param {object} xhr
Expand Down
61 changes: 60 additions & 1 deletion js/src/spbc-table.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
let spbcBulkAction = null;


/**
* Reloads scanner accordion and set all the listeners
* @param {string|null} interactControlTab ajaxResult If is set, will use this var as interactivity data.
* @param {string|null} updateText
*/
function spbcReloadAccordion() {
function spbcReloadAccordion(interactControlTab = null, updateText = '') {
spbcSendAJAXRequest(
{action: 'spbc_scanner_tab__reload_accordion'},
{
Expand All @@ -22,12 +25,68 @@ function spbcReloadAccordion() {
spbcTblPaginationListen();
spbcTblSortListen();
spbcStartShowHide();
if (interactControlTab !== null && updateText !== null) {
interactScannerTab(interactControlTab, updateText);
}
},
},
jQuery('#spbc_scan_accordion'),
);
}

/**
* Open refreshed tab after scanner reload.
* @param {string} controlTab
* @param {string} updateText
*/
function interactScannerTab(controlTab, updateText) {
let refreshControlTabSelector;
let headerSelector;
let headerUpdateStatus;

if (
typeof(controlTab) !== 'string' ||
controlTab === '' ||
typeof(updateText) !== 'string' ||
updateText === ''
) {
return;
}

// init refresh controlled tabs
refreshControlTabSelector = jQuery('#spbc_scan_accordion div[refresh_control_tab="' + controlTab + '"]')[0];

// init header to click
headerSelector = 'h3[aria-controls=spbc_scan_accordion_tab_' + controlTab + ']';
headerSelector = jQuery(headerSelector)[0];

if (
typeof(refreshControlTabSelector) === 'undefined' ||
typeof(headerSelector) === 'undefined'
) {
return;
}

// show updated caption
jQuery('<div class="spbc_accordion_header_caption__update_status"></div>')
.appendTo(headerSelector);
headerUpdateStatus = jQuery('.spbc_accordion_header_caption__update_status');
jQuery('<div class="spbc_accordion_header_caption__updated">' + updateText + '</div>')
.appendTo(headerUpdateStatus);

// do click to open tab
headerSelector.click();

// highlight header
refreshControlTabSelector.setAttribute(
'class',
'ui-accordion-header ui-corner-top ui-state-default ui-accordion-icons ui-accordion-header-active',
);

// hide all actions on partial reload
jQuery('.row-actions').hide();
}

/**
* Table bulk actions
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\ScannerInteractivity;

use CleantalkSP\Templates\DTO;

class RefreshDataDTO extends DTO
{
public $do_refresh = false;
public $control_tab = '';

protected $obligatory_properties = array(
'do_refresh',
'control_tab',
);

/**
* @throws \Exception
*/
public function __construct($data)
{
parent::__construct($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CleantalkSP\SpbctWP\Scanner\ScannerInteractivity;

class ScannerInteractivityData
{
/**
* @var string
*/
public static $scanner_stage;

/**
* @var RefreshDataDTO
*/
public static $refresh_data;

public static function prepare($stage, array $refresh_data)
{
self::$scanner_stage = $stage;

try {
self::$refresh_data = new RefreshDataDTO($refresh_data);
} catch (\Exception $e) {
self::$refresh_data = null;
}

return array(
'scanner_stage' => self::$scanner_stage,
'refresh_data' => self::$refresh_data instanceof RefreshDataDTO
? self::getDataArray(self::$refresh_data)
: array(),
'update_text' => __('Updated!', 'security_malware_firewall')
);
}

public static function getDataArray(RefreshDataDTO $refresh_data)
{
return array(
'do_refresh' => $refresh_data->do_refresh,
'control_tab' => $refresh_data->control_tab,
);
}
}
Loading
Loading