Skip to content

Commit

Permalink
Merge pull request #17 from GDATASoftwareAG/apply-wordpress-review-su…
Browse files Browse the repository at this point in the history
…ggestions

Apply wordpress review suggestions
  • Loading branch information
unglaublicherdude authored Sep 2, 2024
2 parents 9a4ad7e + 50fcb2c commit d8d4553
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .zipignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.git/*
wordpress/*
tests/*
compose*
compose.yml
Dockerfile
test.sh
xdebug.ini
Expand Down
6 changes: 3 additions & 3 deletions PluginPage/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static function add_notice( $text ) {
}

public function save_notices() {
update_option('GdataAntivirusMenuNotices', self::$notices);
update_option('gdatacyberdefenseag_antivirus_notices', self::$notices);
}

public function output_notices() {
$notices = maybe_unserialize(get_option('GdataAntivirusMenuNotices'));
$notices = maybe_unserialize(get_option('gdatacyberdefenseag_antivirus_notices'));

if (! empty($notices)) {
echo '<div id="notice" class="notice notice-info is-dismissible">';
Expand All @@ -34,7 +34,7 @@ public function output_notices() {

echo '</div>';

delete_option('GdataAntivirusMenuNotices');
delete_option('gdatacyberdefenseag_antivirus_notices');
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions PluginPage/Findings/FindingsMenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct(
$this->logger = $logger;
$this->admin_notices = $admin_notices;

register_activation_hook(WORDPRESS_GDATA_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'create_findings_table' ));
register_deactivation_hook(WORDPRESS_GDATA_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'remove_findings_table' ));
register_activation_hook(GDATACYBERDEFENCEAG_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'create_findings_table' ));
register_deactivation_hook(GDATACYBERDEFENCEAG_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'remove_findings_table' ));

if ($this->get_findings_count() === 0) {
return;
Expand All @@ -41,7 +41,7 @@ public function __construct(

private function get_table_name(): string {
$this->logger->debug('FindingsMenuPage::get_table_name');
return $this->database->get_prefix(). WORDPRESS_GDATA_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
return $this->database->get_prefix(). GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
}

public function create_findings_table() {
Expand Down Expand Up @@ -134,11 +134,11 @@ public function get_findings_count(): int {

public function setup_menu(): void {
\add_submenu_page(
WORDPRESS_GDATA_ANTIVIRUS_MENU_SLUG,
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_SLUG,
'Scan Findings',
'Scan Findings <span class="awaiting-mod">' . $this->get_findings_count() . '</span>',
'manage_options',
WORDPRESS_GDATA_ANTIVIRUS_MENU_FINDINGS_SLUG,
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_SLUG,
array( $this, 'findings_list' )
);
}
Expand Down
138 changes: 69 additions & 69 deletions PluginPage/FullScan/FullScanMenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __construct(
$this->logger = $logger;
$this->database = $database;

register_activation_hook(WORDPRESS_GDATA_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'create_full_scan_operations_table' ));
register_deactivation_hook(WORDPRESS_GDATA_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'remove_full_scan_operations_table' ));
register_activation_hook(GDATACYBERDEFENCEAG_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'create_full_scan_operations_table' ));
register_deactivation_hook(GDATACYBERDEFENCEAG_ANTIVIRUS_PLUGIN_WITH_CLASSES__FILE__, array( $this, 'remove_full_scan_operations_table' ));

if (! $vaas_options->credentials_configured()) {
return;
Expand All @@ -44,36 +44,36 @@ public function __construct(
\add_action('init', array( $this, 'setup_fields' ));
\add_action('admin_menu', array( $this, 'setup_menu' ));
\add_action('admin_post_full_scan', array( $this, 'full_scan_interactive' ));
\add_action('wordpress_gdata_antivirus_scheduled_full_scan', array( $this, 'full_scan' ));
\add_action('gdatacyberdefenseag_antivirus_scheduled_full_scan', array( $this, 'full_scan' ));
\add_action(
'wordpress_gdata_antivirus_scan_batch',
'gdatacyberdefenseag_antivirus_scan_batch',
array( $this, 'scan_batch' ),
);

$this->setup_scheduled_scan();
}

private function setup_scheduled_scan() {
$full_scan_enabled = (bool) \get_option('wordpress_gdata_antivirus_options_full_scan_schedule_enabled', false);
$schedule_start = \get_option('wordpress_gdata_antivirus_options_full_scan_schedule_start', '01:00');
$next = wp_next_scheduled('wordpress_gdata_antivirus_scheduled_full_scan');
$full_scan_enabled = (bool) \get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled', false);
$schedule_start = \get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_start', '01:00');
$next = wp_next_scheduled('gdatacyberdefenseag_antivirus_scheduled_full_scan');

if (! $full_scan_enabled && $next) {
\wp_unschedule_event($next, 'wordpress_gdata_antivirus_scheduled_full_scan');
\wp_unschedule_event($next, 'gdatacyberdefenseag_antivirus_scheduled_full_scan');
return;
}

if ($full_scan_enabled && ! $next) {
$timestamp = strtotime($schedule_start);
$this->logger->debug('schedule start timestamp: ' . $timestamp);
\wp_schedule_event($timestamp, 'daily', 'wordpress_gdata_antivirus_scheduled_full_scan');
\wp_schedule_event($timestamp, 'daily', 'gdatacyberdefenseag_antivirus_scheduled_full_scan');
return;
}
$nextschedule_start = gmdate('H:i', $next);
if ($nextschedule_start !== $schedule_start) {
\wp_unschedule_event($next, 'wordpress_gdata_antivirus_scheduled_full_scan');
\wp_unschedule_event($next, 'gdatacyberdefenseag_antivirus_scheduled_full_scan');
$timestamp = strtotime($schedule_start);
\wp_schedule_event($timestamp, 'daily', 'wordpress_gdata_antivirus_scheduled_full_scan');
\wp_schedule_event($timestamp, 'daily', 'gdatacyberdefenseag_antivirus_scheduled_full_scan');
}
}

Expand All @@ -91,7 +91,7 @@ public function create_full_scan_operations_table() {
}

private function get_table_name(): string {
return $this->database->get_prefix() . WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
return $this->database->get_prefix() . GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
}

public function lock_scan_operations_table() {
Expand Down Expand Up @@ -128,26 +128,26 @@ public function reset_scan_operations(): void {

public function setup_fields(): void {
\register_setting(
'wordpress_gdata_antivirus_options_full_scan_run',
'wordpress_gdata_antivirus_options_full_scan_batch_size',
'gdatacyberdefenseag_antivirus_options_full_scan_run',
'gdatacyberdefenseag_antivirus_options_full_scan_batch_size',
array(
'type' => 'number',
'default' => 100,
'sanitize_callback' => array( $this, 'wordpress_gdata_antivirus_options_full_scan_batch_size_validation' ),
'sanitize_callback' => array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_batch_size_validation' ),
)
);
\register_setting(
'wordpress_gdata_antivirus_options_full_scan_run',
'wordpress_gdata_antivirus_options_full_scan_schedule_start',
'gdatacyberdefenseag_antivirus_options_full_scan_run',
'gdatacyberdefenseag_antivirus_options_full_scan_schedule_start',
array(
'type' => 'string',
'default' => '01:00',
'sanitize_callback' => array( $this, 'wordpress_gdata_antivirus_options_full_scan_schedule_start_validation' ),
'sanitize_callback' => array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_schedule_start_validation' ),
)
);
\register_setting(
'wordpress_gdata_antivirus_options_full_scan_run',
'wordpress_gdata_antivirus_options_full_scan_schedule_enabled',
'gdatacyberdefenseag_antivirus_options_full_scan_run',
'gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled',
array(
'type' => 'boolean',
'default' => false,
Expand All @@ -157,106 +157,106 @@ public function setup_fields(): void {

public function setup_menu(): void {
\add_settings_section(
'wordpress_gdata_antivirus_options_full_scan',
'gdatacyberdefenseag_antivirus_options_full_scan',
esc_html__('Full Scan', 'gdata-antivirus'),
array( $this, 'wordpress_gdata_antivirus_options_full_scan_text' ),
WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG
array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_text' ),
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG
);

\add_settings_field(
'wordpress_gdata_antivirus_options_full_scan_batch_size',
'gdatacyberdefenseag_antivirus_options_full_scan_batch_size',
esc_html__('Batch Size', 'gdata-antivirus'),
array( $this, 'wordpress_gdata_antivirus_options_full_scan_batch_size_text' ),
WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'wordpress_gdata_antivirus_options_full_scan'
array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_batch_size_text' ),
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'gdatacyberdefenseag_antivirus_options_full_scan'
);

\add_settings_field(
'wordpress_gdata_antivirus_options_full_scan_schedule_enabled',
'gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled',
esc_html__('Scheduled Scan enabled', 'gdata-antivirus'),
array( $this, 'wordpress_gdata_antivirus_options_full_scan_schedule_enabled_text' ),
WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'wordpress_gdata_antivirus_options_full_scan'
array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled_text' ),
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'gdatacyberdefenseag_antivirus_options_full_scan'
);

\add_settings_field(
'wordpress_gdata_antivirus_options_full_scan_schedule_start',
'gdatacyberdefenseag_antivirus_options_full_scan_schedule_start',
esc_html__('Scheduled Scan starting Hour', 'gdata-antivirus'),
array( $this, 'wordpress_gdata_antivirus_options_full_scan_schedule_start_text' ),
WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'wordpress_gdata_antivirus_options_full_scan'
array( $this, 'gdatacyberdefenseag_antivirus_options_full_scan_schedule_start_text' ),
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
'gdatacyberdefenseag_antivirus_options_full_scan'
);

\add_submenu_page(
WORDPRESS_GDATA_ANTIVIRUS_MENU_SLUG,
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_SLUG,
'Full scan',
'Full scan',
'manage_options',
WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG,
array( $this, 'full_scan_menu' )
);
}

public function wordpress_gdata_antivirus_options_full_scan_batch_size_validation( $value ) {
$option = get_option('wordpress_gdata_antivirus_options_full_scan_batch_size', 100);
public function gdatacyberdefenseag_antivirus_options_full_scan_batch_size_validation( $value ) {
$option = get_option('gdatacyberdefenseag_antivirus_options_full_scan_batch_size', 100);
if (0 === $value) {
$value = $option;
add_settings_error(
'wordpress_gdata_antivirus_options_full_scan_batch_size',
'wordpress_gdata_antivirus_options_full',
__('batch_size cannot be 0')
'gdatacyberdefenseag_antivirus_options_full_scan_batch_size',
'gdatacyberdefenseag_antivirus_options_full',
__('batch_size cannot be 0', 'gdata-antivirus')
);
}
if ($value < 100) {
$value = $option;
add_settings_error(
'wordpress_gdata_antivirus_options_full_scan_batch_size',
'wordpress_gdata_antivirus_options_full',
__('batch_size should be at least 100')
'gdatacyberdefenseag_antivirus_options_full_scan_batch_size',
'gdatacyberdefenseag_antivirus_options_full',
__('batch_size should be at least 100', 'gdata-antivirus')
);
}
return $value;
}

public function wordpress_gdata_antivirus_options_full_scan_schedule_start_validation( $value ) {
$option = get_option('wordpress_gdata_antivirus_options_full_scan_schedule_start', '01:00');
$full_scan_enabled = get_option('wordpress_gdata_antivirus_options_full_scan_schedule_enabled', false);
public function gdatacyberdefenseag_antivirus_options_full_scan_schedule_start_validation( $value ) {
$option = get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_start', '01:00');
$full_scan_enabled = get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled', false);

if (! $full_scan_enabled) {
return $option;
}
if (preg_match('#^[0-9]{2}:[0-9]{2}$#', $value) !== 1) {
$value = $option;
add_settings_error(
'wordpress_gdata_antivirus_options_full_scan_schedule_start',
'wordpress_gdata_antivirus_options_full_scan',
__('schedule start must be of format H:i')
'gdatacyberdefenseag_antivirus_options_full_scan_schedule_start',
'gdatacyberdefenseag_antivirus_options_full_scan',
__('schedule start must be of format H:i', 'gdata-antivirus')
);
}
return $value;
}

public function wordpress_gdata_antivirus_options_full_scan_text() {
public function gdatacyberdefenseag_antivirus_options_full_scan_text() {
echo '<p>' . esc_html__('Here you can set options for the full scan', 'gdata-antivirus') . '</p>';
}

public function wordpress_gdata_antivirus_options_full_scan_schedule_enabled_text() {
$full_scan_enabled = (bool) \get_option('wordpress_gdata_antivirus_options_full_scan_schedule_enabled', false);
echo "<input id='wordpress_gdata_antivirus_options_full_scan_schedule_enabled' name='wordpress_gdata_antivirus_options_full_scan_schedule_enabled' type='checkbox' value='true' " . \checked(true, $full_scan_enabled, false) . "' />";
public function gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled_text() {
$full_scan_enabled = (bool) \get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled', false);
echo "<input id='gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled' name='gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled' type='checkbox' value='true' " . \checked(true, $full_scan_enabled, false) . "' />";
}

public function wordpress_gdata_antivirus_options_full_scan_batch_size_text() {
$batch_size = \get_option('wordpress_gdata_antivirus_options_full_scan_batch_size', 100);
echo "<input id='wordpress_gdata_antivirus_options_full_scan_batch_size' name='wordpress_gdata_antivirus_options_full_scan_batch_size' type='text' value='" . \esc_attr($batch_size) . "' />";
public function gdatacyberdefenseag_antivirus_options_full_scan_batch_size_text() {
$batch_size = \get_option('gdatacyberdefenseag_antivirus_options_full_scan_batch_size', 100);
echo "<input id='gdatacyberdefenseag_antivirus_options_full_scan_batch_size' name='gdatacyberdefenseag_antivirus_options_full_scan_batch_size' type='text' value='" . \esc_attr($batch_size) . "' />";
}

public function wordpress_gdata_antivirus_options_full_scan_schedule_start_text() {
$schedule_start = \get_option('wordpress_gdata_antivirus_options_full_scan_schedule_start', '01:00');
public function gdatacyberdefenseag_antivirus_options_full_scan_schedule_start_text() {
$schedule_start = \get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_start', '01:00');
$full_scan_enabled =
(bool) \get_option('wordpress_gdata_antivirus_options_full_scan_schedule_enabled', false);
(bool) \get_option('gdatacyberdefenseag_antivirus_options_full_scan_schedule_enabled', false);
$this->logger->debug('schedule_start: ' . $schedule_start);

echo "<input id='wordpress_gdata_antivirus_options_full_scan_schedule_start' name='wordpress_gdata_antivirus_options_full_scan_schedule_start' type='text' value='" . \esc_attr($schedule_start) . "' " . ( $full_scan_enabled ? '' : 'disabled' ) . '/>';
echo "<input id='gdatacyberdefenseag_antivirus_options_full_scan_schedule_start' name='gdatacyberdefenseag_antivirus_options_full_scan_schedule_start' type='text' value='" . \esc_attr($schedule_start) . "' " . ( $full_scan_enabled ? '' : 'disabled' ) . '/>';
}

public function full_scan_interactive(): void {
Expand Down Expand Up @@ -285,7 +285,7 @@ public function full_scan_interactive(): void {
public function full_scan(): void {
$this->admin_notices->add_notice(__('Full Scan started', 'gdata-antivirus'));

$batch_size = \get_option('wordpress_gdata_antivirus_options_full_scan_batch_size', 100);
$batch_size = \get_option('gdatacyberdefenseag_antivirus_options_full_scan_batch_size', 100);
$it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(ABSPATH, \FilesystemIterator::SKIP_DOTS));
$files = array();
foreach ($it as $file_path) {
Expand All @@ -300,13 +300,13 @@ public function full_scan(): void {
if (count($files) >= $batch_size) {
$this->increase_scheduled_scans();

\wp_schedule_single_event(time(), 'wordpress_gdata_antivirus_scan_batch', array( 'files' => $files ));
\wp_schedule_single_event(time(), 'gdatacyberdefenseag_antivirus_scan_batch', array( 'files' => $files ));
$files = array();
}
}
if (count($files) > 0) {
$this->increase_scheduled_scans();
\wp_schedule_single_event(time(), 'wordpress_gdata_antivirus_scan_batch', array( 'files' => $files ));
\wp_schedule_single_event(time(), 'gdatacyberdefenseag_antivirus_scan_batch', array( 'files' => $files ));
}
}

Expand Down Expand Up @@ -339,14 +339,14 @@ public function scan_batch( array $files ): void {
}

public function full_scan_menu(): void {
settings_errors('wordpress_gdata_antivirus_options_full_scan_schedule_start');
settings_errors('wordpress_gdata_antivirus_options_full_scan_batch_size');
settings_errors('gdatacyberdefenseag_antivirus_options_full_scan_schedule_start');
settings_errors('gdatacyberdefenseag_antivirus_options_full_scan_batch_size');
?>
<h2>Full Scan Settings</h2>
<form action="options.php" method="post">
<?php
\settings_fields('wordpress_gdata_antivirus_options_full_scan_run');
\do_settings_sections(WORDPRESS_GDATA_ANTIVIRUS_MENU_FULL_SCAN_SLUG);
\settings_fields('gdatacyberdefenseag_antivirus_options_full_scan_run');
\do_settings_sections(GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_SLUG);
?>
<input name="submit" class="button button-primary" type="submit" value="<?php \esc_attr_e('Save', 'gdata-antivirus'); ?>" />
</form>
Expand Down
Loading

0 comments on commit d8d4553

Please sign in to comment.