Skip to content
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
22 changes: 22 additions & 0 deletions .github/workflows/vip-policy-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: VIP policy preview tests
on:
pull_request:
paths:
- 'docs/integrations/wordpress-vip/**'
- '.github/workflows/vip-policy-preview.yml'
push:
branches: [main]
paths:
- 'docs/integrations/wordpress-vip/**'
- '.github/workflows/vip-policy-preview.yml'
permissions:
contents: read
jobs:
offline-preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
- run: node --test docs/integrations/wordpress-vip/plan.test.mjs
20 changes: 20 additions & 0 deletions .github/workflows/vip-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: VIP coding standards
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
vip-phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer
- name: Install isolated, locked VIP standards
run: composer --working-dir=docs/integrations/wordpress-vip install --no-interaction --no-progress --prefer-dist
- name: Require zero unreviewed VIP errors and warnings
run: docs/integrations/wordpress-vip/vendor/bin/phpcs --standard=WordPress-VIP-Go --extensions=php webdecoy.php uninstall.php admin includes public sdk/src templates
12 changes: 6 additions & 6 deletions admin/partials/blocked-ips-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
}

// Get blocked IPs
$page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$per_page = 20;
$wd_current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$wd_per_page = 20;
$blocked_ips = $blocker->get_blocked_ips([
'page' => $page,
'per_page' => $per_page,
'page' => $wd_current_page,
'per_page' => $wd_per_page,
]);
$total = $blocker->get_blocked_count();
$total_pages = ceil($total / $per_page);
$total_pages = ceil($total / $wd_per_page);

$stats = $blocker->get_stats();
?>
Expand Down Expand Up @@ -168,7 +168,7 @@ class="button button-small"
'prev_text' => '«',
'next_text' => '»',
'total' => $total_pages,
'current' => $page,
'current' => $wd_current_page,
]);
?>
</div>
Expand Down
20 changes: 11 additions & 9 deletions admin/partials/detections-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

$table = esc_sql($wpdb->prefix . 'webdecoy_detections');
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin list filtering via GET, no state change
$page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$per_page = 50;
$offset = ($page - 1) * $per_page;
$wd_current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$wd_per_page = 50;
$offset = ($wd_current_page - 1) * $wd_per_page;

// Filters
$threat_level = isset($_GET['threat_level']) ? sanitize_text_field(wp_unslash($_GET['threat_level'])) : '';
Expand Down Expand Up @@ -86,13 +86,15 @@
};

$output = fopen('php://output', 'w');
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fputcsv -- Writes exclusively to the already-open php://output CSV download stream; no local filesystem path is used.
fputcsv($output, ['Date', 'IP Address', 'Score', 'Threat Level', 'Source', 'User Agent', 'Flags']);

$export_query = "SELECT * FROM {$table} WHERE {$where} ORDER BY created_at DESC";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
$export_results = $wpdb->get_results($wpdb->prepare($export_query, $params), ARRAY_A);

foreach ($export_results as $row) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fputcsv -- Writes exclusively to the already-open php://output CSV download stream; no local filesystem path is used.
fputcsv($output, [
$csv_safe($row['created_at']),
$csv_safe($row['ip_address']),
Expand All @@ -110,14 +112,14 @@

// Get total - always use prepare()
$total_query = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
$total = $wpdb->get_var($wpdb->prepare($total_query, $params));
$total_pages = ceil($total / $per_page);
$total_pages = ceil($total / $wd_per_page);

// Get detections - always use prepare()
$query = "SELECT * FROM {$table} WHERE {$where} ORDER BY created_at DESC LIMIT %d OFFSET %d";
$query_params = array_merge($params, [$per_page, $offset]);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
$query_params = array_merge($params, [$wd_per_page, $offset]);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Query built dynamically with safe table/column names
$detections = $wpdb->get_results($wpdb->prepare($query, $query_params), ARRAY_A);

// Get stats
Expand Down Expand Up @@ -536,7 +538,7 @@ class="button button-small">
'prev_text' => '&laquo;',
'next_text' => '&raquo;',
'total' => $total_pages,
'current' => $page,
'current' => $wd_current_page,
];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- paginate_links returns safe HTML
echo paginate_links($pagination_args);
Expand Down
198 changes: 108 additions & 90 deletions admin/partials/statistics-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,107 +20,124 @@
$blocked_table = esc_sql($wpdb->prefix . 'webdecoy_blocked_ips');
$checkout_table = esc_sql($wpdb->prefix . 'webdecoy_checkout_attempts');

// 30-day detection trend
$thirty_days_ago = gmdate('Y-m-d H:i:s', strtotime('-30 days'));
$daily_counts = $wpdb->get_results($wpdb->prepare(
"SELECT DATE(created_at) as date, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY DATE(created_at) ORDER BY date ASC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);
// Read-only admin aggregates may lag writes by at most one minute. No enforcement uses them.
$wd_stats_key = 'admin_statistics_' . gmdate('Y-m-d') . '_' . (int) class_exists('WooCommerce');
$wd_stats_cached = wp_cache_get($wd_stats_key, 'webdecoy_analytics');
if (is_array($wd_stats_cached)) {
[$daily_data, $threat_distribution, $top_signals, $top_ips, $source_distribution, $total_30d, $total_7d, $total_today, $active_blocks, $woo_stats] = $wd_stats_cached;
} else {
// 30-day detection trend
$thirty_days_ago = gmdate('Y-m-d H:i:s', strtotime('-30 days'));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$daily_counts = $wpdb->get_results($wpdb->prepare(
"SELECT DATE(created_at) as date, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY DATE(created_at) ORDER BY date ASC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);

// Fill in missing days with 0
$daily_data = [];
for ($i = 29; $i >= 0; $i--) {
$date = gmdate('Y-m-d', strtotime("-{$i} days"));
$daily_data[$date] = 0;
}
foreach ($daily_counts as $row) {
if (isset($daily_data[$row['date']])) {
$daily_data[$row['date']] = (int) $row['count'];
// Fill in missing days with 0
$daily_data = [];
for ($i = 29; $i >= 0; $i--) {
$date = gmdate('Y-m-d', strtotime("-{$i} days"));
$daily_data[$date] = 0;
}
foreach ($daily_counts as $row) {
if (isset($daily_data[$row['date']])) {
$daily_data[$row['date']] = (int) $row['count'];
}
}
}

// Threat level distribution
$threat_distribution = $wpdb->get_results($wpdb->prepare(
"SELECT threat_level, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY threat_level ORDER BY count DESC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);
// Threat level distribution
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$threat_distribution = $wpdb->get_results($wpdb->prepare(
"SELECT threat_level, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY threat_level ORDER BY count DESC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);

// Top flagged signals (parse from JSON flags)
$recent_flags = $wpdb->get_results($wpdb->prepare(
"SELECT flags FROM {$detections_table} WHERE created_at > %s AND flags IS NOT NULL AND flags != '' LIMIT 500", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);
// Top flagged signals (parse from JSON flags)
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$recent_flags = $wpdb->get_results($wpdb->prepare(
"SELECT flags FROM {$detections_table} WHERE created_at > %s AND flags IS NOT NULL AND flags != '' LIMIT 500", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);

$signal_counts = [];
foreach ($recent_flags as $row) {
// flags is TEXT DEFAULT NULL; cast so a null row never reaches json_decode()
// (a deprecation on PHP 8.1+). '' decodes to null and is skipped just below.
$flags_data = json_decode((string) ($row['flags'] ?? ''), true);
if (!is_array($flags_data)) {
continue;
}
// Handle structured format
$flags_arr = [];
if (isset($flags_data['flags']) && is_array($flags_data['flags'])) {
$flags_arr = $flags_data['flags'];
} elseif (isset($flags_data['f']) && is_array($flags_data['f'])) {
$flags_arr = $flags_data['f'];
} elseif (!isset($flags_data['flags']) && !isset($flags_data['f']) && !isset($flags_data['metadata'])) {
// Plain array of flags
$flags_arr = array_filter($flags_data, 'is_string');
}
foreach ($flags_arr as $flag) {
if (is_string($flag)) {
$signal_counts[$flag] = ($signal_counts[$flag] ?? 0) + 1;
$signal_counts = [];
foreach ($recent_flags as $row) {
// flags is TEXT DEFAULT NULL; cast so a null row never reaches json_decode()
// (a deprecation on PHP 8.1+). '' decodes to null and is skipped just below.
$flags_data = json_decode((string) ($row['flags'] ?? ''), true);
if (!is_array($flags_data)) {
continue;
}
// Handle structured format
$flags_arr = [];
if (isset($flags_data['flags']) && is_array($flags_data['flags'])) {
$flags_arr = $flags_data['flags'];
} elseif (isset($flags_data['f']) && is_array($flags_data['f'])) {
$flags_arr = $flags_data['f'];
} elseif (!isset($flags_data['flags']) && !isset($flags_data['f']) && !isset($flags_data['metadata'])) {
// Plain array of flags
$flags_arr = array_filter($flags_data, 'is_string');
}
foreach ($flags_arr as $flag) {
if (is_string($flag)) {
$signal_counts[$flag] = ($signal_counts[$flag] ?? 0) + 1;
}
}
}
}
arsort($signal_counts);
$top_signals = array_slice($signal_counts, 0, 10, true);
arsort($signal_counts);
$top_signals = array_slice($signal_counts, 0, 10, true);

// Top blocked IPs
$top_ips = $wpdb->get_results($wpdb->prepare(
"SELECT ip_address, COUNT(*) as count, MAX(created_at) as last_seen FROM {$detections_table} WHERE created_at > %s GROUP BY ip_address ORDER BY count DESC LIMIT 10", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);
// Top blocked IPs
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$top_ips = $wpdb->get_results($wpdb->prepare(
"SELECT ip_address, COUNT(*) as count, MAX(created_at) as last_seen FROM {$detections_table} WHERE created_at > %s GROUP BY ip_address ORDER BY count DESC LIMIT 10", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);

// Source distribution
$source_distribution = $wpdb->get_results($wpdb->prepare(
"SELECT source, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY source ORDER BY count DESC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);
// Source distribution
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$source_distribution = $wpdb->get_results($wpdb->prepare(
"SELECT source, COUNT(*) as count FROM {$detections_table} WHERE created_at > %s GROUP BY source ORDER BY count DESC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
), ARRAY_A);

// Overall stats
$total_30d = array_sum($daily_data);
$total_7d = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$detections_table} WHERE created_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d H:i:s', strtotime('-7 days'))
));
$total_today = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$detections_table} WHERE created_at >= %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d 00:00:00')
));
// expires_at is stored in UTC, so it must be compared against a UTC value. MySQL's
// NOW() resolves in the database session time zone (SYSTEM by default, which
// WordPress never sets), so it disagrees with the column on any DB host not on UTC.
$active_blocks = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$blocked_table} WHERE expires_at IS NULL OR expires_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d H:i:s')
));
// Overall stats
$total_30d = array_sum($daily_data);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$total_7d = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$detections_table} WHERE created_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d H:i:s', strtotime('-7 days'))
));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$total_today = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$detections_table} WHERE created_at >= %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d 00:00:00')
));
// expires_at is stored in UTC, so it must be compared against a UTC value. MySQL's
// NOW() resolves in the database session time zone (SYSTEM by default, which
// WordPress never sets), so it disagrees with the column on any DB host not on UTC.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
$active_blocks = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$blocked_table} WHERE expires_at IS NULL OR expires_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
gmdate('Y-m-d H:i:s')
));

// WooCommerce stats (if active)
$woo_stats = null;
if (class_exists('WooCommerce')) {
$woo_stats = [
'attempts' => (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$checkout_table} WHERE created_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
)),
'blocked' => (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$checkout_table} WHERE created_at > %s AND status = 'blocked'", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
)),
];
// WooCommerce stats (if active)
$woo_stats = null;
if (class_exists('WooCommerce')) {
$woo_stats = [
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom-table admin aggregates are cached together in webdecoy_analytics for 60 seconds above/below this query.
'attempts' => (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$checkout_table} WHERE created_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
)),
'blocked' => (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$checkout_table} WHERE created_at > %s AND status = 'blocked'", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input
$thirty_days_ago
)),
];
}
wp_cache_set($wd_stats_key, [$daily_data, $threat_distribution, $top_signals, $top_ips, $source_distribution, $total_30d, $total_7d, $total_today, $active_blocks, $woo_stats], 'webdecoy_analytics', 60);
}

// Known crawlers, AI agents and bot tools over 7 days (counters only).
Expand All @@ -135,6 +152,7 @@

<div class="wrap">
<h1><?php esc_html_e('WebDecoy Statistics', 'webdecoy'); ?></h1>
<p class="description"><?php esc_html_e('Summary statistics may take up to one minute to refresh. Protection checks remain live.', 'webdecoy'); ?></p>

<!-- Summary Stats -->
<div class="webdecoy-stats-summary">
Expand Down
2 changes: 2 additions & 0 deletions docs/integrations/wordpress-vip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
!composer.lock
Loading
Loading