Skip to content
Open
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
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ parameters:
scanFiles:
- vendor/wp-cli/wp-cli/php/class-wp-cli.php
bootstrapFiles:
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
- vendor-prefixed/autoload.php
paths:
- src/
Expand Down
4 changes: 4 additions & 0 deletions src/Activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use EightshiftForms\Config\Config;
use EightshiftForms\Db\CreateActivityLogsTable;
use EightshiftForms\Db\CreateEntriesTable;
use EightshiftForms\Db\CreateRateLimitingTable;
use EightshiftForms\Permissions\Permissions;
use EightshiftFormsVendor\EightshiftLibs\Plugin\HasActivationInterface;
use WP_Role;
Expand Down Expand Up @@ -44,6 +45,9 @@ public function activate(): void
// Create DB table.
CreateActivityLogsTable::createTable();

// Create DB table.
CreateRateLimitingTable::createTable();

// Do a cleanup.
\flush_rewrite_rules();
}
Expand Down
90 changes: 90 additions & 0 deletions src/CronJobs/LogEntryCleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* A cleanup service for log entries.
*
* @package EightshiftForms\CronJobs
*/

declare(strict_types=1);

namespace EightshiftForms\CronJobs;

use EightshiftForms\Security\RateLimitingLogEntry;
use EightshiftFormsVendor\EightshiftLibs\Services\ServiceInterface;

/**
* A log entry cleanup service.
*/
class LogEntryCleanupJob implements ServiceInterface
{
public const string LOG_ENTRY_CLEANUP_ACTION = 'es_forms_cleanup_log_entries';

/**
* Register all the hooks
*
* @return void
*/
public function register(): void
{
\add_action('init', [$this, 'maybeCleanupLogEntries']);
\add_filter('cron_schedules', [$this, 'addJobToSchedule']); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected
\add_action(self::LOG_ENTRY_CLEANUP_ACTION, [$this, 'cleanupLogEntries']);
}

/**
* Cleans up log entries using a recurring action if available, or immediately if not.
* Recurring actions are scheduled using Action Scheduler if available and run daily.
*
* @return void
*/
public function maybeCleanupLogEntries(): void
{
if (\function_exists('as_schedule_recurring_action')) {
\as_schedule_recurring_action(
\time(),
\DAY_IN_SECONDS,
self::LOG_ENTRY_CLEANUP_ACTION
);

return;
} else {
if (!\wp_next_scheduled(self::LOG_ENTRY_CLEANUP_ACTION)) {
\wp_schedule_event(
\strtotime('tomorrow', \time()),
'daily',
self::LOG_ENTRY_CLEANUP_ACTION
);
}
}

$this->cleanupLogEntries();
}

/**
* Add job to schedule.
*
* @param array<mixed> $schedules WP schedules list.
*
* @return array<mixed>
*/
public function addJobToSchedule(array $schedules): array
{
$schedules['daily'] = [
'interval' => \DAY_IN_SECONDS,
'display' => \esc_html__('Every day at midnight', 'eightshift-forms'),
];

return $schedules;
}

/**
* Cleans up log entries older than a day.
*
* @return void
*/
public function cleanupLogEntries(): void
{
RateLimitingLogEntry::cleanup(\DAY_IN_SECONDS);
}
}
88 changes: 0 additions & 88 deletions src/CronJobs/SecurityJob.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/Db/CreateRateLimitingTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Class that holds DB table creation for rate limiting.
*
* @package EightshiftForms\Db
*/

declare(strict_types=1);

namespace EightshiftForms\Db;

/**
* CreateRateLimitingTable class.
*/
class CreateRateLimitingTable
{
/**
* Table name
*
* @var string
*/
public const string RATE_LIMITING_TABLE = 'es_forms_rate_limiting';

/**
* Create DB table.
*
* @return void
*/
public static function createTable(): void
{
require_once(\ABSPATH . 'wp-admin/includes/upgrade.php'); // @phpstan-ignore-line

global $wpdb;

$tableName = $wpdb->prefix . self::RATE_LIMITING_TABLE;

$charsetCollate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE {$tableName} (
`log_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`form_id` int(11) DEFAULT NULL,
`user_token` varchar(256) NOT NULL,
`activity_type` varchar(256) NOT NULL,
`created_at` bigint(20) NOT NULL,
PRIMARY KEY (`log_id`),
KEY `token_time` (`user_token`,`created_at`),
KEY `token_form_time` (`user_token`,`form_id`,`created_at`),
KEY `token_activity_time` (`user_token`,`activity_type`,`created_at`),
KEY `token_form_activity_time` (`user_token`,`form_id`,`activity_type`,`created_at`)
) $charsetCollate;";

\maybe_create_table($tableName, $sql);
}
}
Loading