Skip to content

feat: add country-based data deletion for GDPR erasure (closes #42)#50

Open
Suraj80 wants to merge 2 commits intoguibranco:mainfrom
Suraj80:feat/country-based-deletion
Open

feat: add country-based data deletion for GDPR erasure (closes #42)#50
Suraj80 wants to merge 2 commits intoguibranco:mainfrom
Suraj80:feat/country-based-deletion

Conversation

@Suraj80
Copy link
Copy Markdown

@Suraj80 Suraj80 commented May 2, 2026

Closes #42

📑 Description

Adds country-based visitor data deletion to support GDPR right-to-erasure workflows.

Changes made across 3 files:

  • includes/class-ipquery-db.php

    • Added delete_by_country(string $country_code): int|false — deletes all visitor rows matching a given ISO country code
    • Added get_distinct_countries(): array — returns distinct countries that have records, used to populate the dropdown
    • Added log_action(string $message): void — writes audit entries to the WordPress debug log when WP_DEBUG_LOG is enabled
  • includes/class-ipquery-admin.php

    • Registered admin_post_ipquery_delete_by_country hook in init()
    • Added handle_delete_by_country() — validates nonce, checks capability, calls IpQuery_DB::delete_by_country(), logs the action, and redirects with a success notice
  • admin/views/visitors.php

    • Added success notice for country_deleted redirect param
    • Added "Delete by Country" panel at the bottom with a dropdown populated from actual DB records, nonce protection, required attribute, and an onclick confirmation dialog

Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

Does this introduce a breaking change?

  • Yes
  • No

Additional Information

  • The dropdown only shows countries that actually have records in the database — no point showing countries with nothing to delete
  • Deletion is logged via IpQuery_DB::log_action() which only writes when WP_DEBUG_LOG is true, consistent with WordPress best practices. Note: existing handlers (handle_delete_ip, handle_purge) do not currently log — this PR introduces logging specifically to satisfy the acceptance criteria of issue [FEATURE] Add country-based data deletion support #42
  • A $deleted !== false check distinguishes a real DB failure from a legitimate "0 rows deleted" result
  • No breaking changes — all existing functionality is untouched

Summary by Sourcery

Add support for deleting visitor records by country to help fulfill GDPR right-to-erasure requests.

New Features:

  • Introduce a delete-by-country admin action and handler for removing all visitor records for a selected country.
  • Add a visitor admin UI panel with a country dropdown and confirmation flow to trigger country-based deletion.
  • Expose database methods to delete visitor records by country and to retrieve distinct countries with stored records.

Enhancements:

  • Log admin-initiated deletion actions to the WordPress debug log when enabled.
  • Display a success notice summarizing the outcome of country-based deletions in the visitors admin screen.

Summary by CodeRabbit

  • New Features
    • Added bulk deletion tool for visitor records filtered by country in the admin panel
    • Includes confirmation prompt for permanent deletion safety
    • Displays summary of deletion results with country and record count

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 2, 2026

Reviewer's Guide

Implements GDPR-oriented visitor data erasure by country by wiring a new admin-post handler, DB utilities for country-based deletion and logging, and a UI panel on the visitors admin screen to trigger the operation and display feedback.

Sequence diagram for GDPR delete-by-country admin workflow

sequenceDiagram
    actor Admin
    participant VisitorsAdminScreen
    participant Browser
    participant WordPressAdminPost
    participant IpQuery_Admin
    participant IpQuery_DB
    participant WPDB
    participant WPDebugLog

    Admin->>VisitorsAdminScreen: Open visitors.php
    VisitorsAdminScreen->>IpQuery_DB: get_distinct_countries()
    IpQuery_DB->>WPDB: SELECT DISTINCT country, country_code ...
    WPDB-->>IpQuery_DB: Result rows
    IpQuery_DB-->>VisitorsAdminScreen: Countries array
    VisitorsAdminScreen-->>Admin: Render Delete by Country panel

    Admin->>Browser: Select country and click Delete Records
    Browser->>Browser: Show confirm dialog
    Browser->>WordPressAdminPost: POST action=ipquery_delete_by_country

    WordPressAdminPost->>IpQuery_Admin: handle_delete_by_country()
    IpQuery_Admin->>IpQuery_Admin: check_admin_referer()
    IpQuery_Admin->>IpQuery_Admin: current_user_can(manage_options)
    IpQuery_Admin->>IpQuery_Admin: Sanitize and normalize country_code
    IpQuery_Admin->>IpQuery_DB: delete_by_country(country_code)
    IpQuery_DB->>WPDB: DELETE FROM table WHERE country_code = ?
    WPDB-->>IpQuery_DB: rows_deleted or false
    IpQuery_DB-->>IpQuery_Admin: rows_deleted or false

    alt rows_deleted is not false
        IpQuery_Admin->>IpQuery_DB: log_action(message)
        IpQuery_DB->>WPDebugLog: error_log([IpQuery] message) (if WP_DEBUG_LOG)
    end

    IpQuery_Admin->>WordPressAdminPost: wp_safe_redirect(...country_deleted, country_code)
    WordPressAdminPost-->>Browser: Redirect to visitors.php

    Browser->>VisitorsAdminScreen: GET with country_deleted and country_code
    VisitorsAdminScreen->>VisitorsAdminScreen: Render success notice
    VisitorsAdminScreen-->>Admin: Show "%d record(s) deleted for country" notice
Loading

Class diagram for IpQuery_Admin and IpQuery_DB GDPR additions

classDiagram

    class IpQuery_Admin {
        +init() void
        +handle_settings_save() void
        +enqueue_assets() void
        +handle_delete_ip() void
        +handle_delete_by_country() void
        +handle_purge() void
        +handle_manual_lookup() void
        +ajax_chart_data() void
    }

    class IpQuery_DB {
        +delete_ip(ip string) void
        +delete_by_country(country_code string) "int|false"
        +get_distinct_countries() array
        +log_action(message string) void
    }

    IpQuery_Admin --> IpQuery_DB : uses delete_by_country
    IpQuery_Admin --> IpQuery_DB : uses log_action
    IpQuery_Admin --> IpQuery_DB : uses get_distinct_countries
Loading

File-Level Changes

Change Details Files
Add admin handler and wiring for deleting visitor records by country code.
  • Register admin_post_ipquery_delete_by_country action in init() so WordPress routes form submissions to the new handler.
  • Implement handle_delete_by_country() to validate nonce, enforce manage_options capability, normalize and validate the submitted country code, invoke IpQuery_DB::delete_by_country(), conditionally log the deletion, and redirect back to the visitors page with result query args.
includes/class-ipquery-admin.php
Extend the visitors admin view with a country-based deletion UI and success notice.
  • Display a success notice when the country_deleted query arg is present, including the deleted row count and uppercased country code, with appropriate escaping and basic sanitization of query parameters.
  • Add a ‘Delete by Country’ panel with a nonce-protected form posting to admin-post.php, including a required country dropdown populated via IpQuery_DB::get_distinct_countries(), and a submit button with a JavaScript confirm dialog describing the permanence of the action.
admin/views/visitors.php
Add DB-layer helpers for logging, deleting by country, and fetching distinct countries.
  • Introduce IpQuery_DB::log_action() which conditionally writes audit messages to the WordPress debug log when WP_DEBUG_LOG is enabled, prefixed with [IpQuery].
  • Implement IpQuery_DB::delete_by_country() using $wpdb->delete() on the plugin table filtering by sanitized country_code, returning the deleted row count or false on error.
  • Implement IpQuery_DB::get_distinct_countries() to SELECT DISTINCT country and country_code where country_code is non-empty, ordered by country, and return an ARRAY_A result or an empty array on failure.
includes/class-ipquery-db.php

Assessment against linked issues

Issue Objective Addressed Explanation
#42 Implement a country-filtered deletion tool that can bulk delete all stored visitor records for a selected country.
#42 Log each country-based deletion action for audit purposes.
#42 Require a user confirmation dialog before executing the country-based deletion.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 2, 2026

Warning

Rate limit exceeded

@Suraj80 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 19 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c172cc30-27d7-4582-89cf-ecc6a22a343d

📥 Commits

Reviewing files that changed from the base of the PR and between aa75377 and 88bd1c6.

📒 Files selected for processing (3)
  • admin/views/visitors.php
  • assets/css/admin.css
  • includes/class-ipquery-admin.php

Walkthrough

This PR adds country-based bulk deletion capability to the IP Query admin panel. A new admin form allows authorized users to select a country and permanently delete all associated visitor records, with a confirmation dialog, nonce validation, optional audit logging, and a success notice displaying the count deleted.

Changes

Country-Based Data Deletion

Layer / File(s) Summary
Database Layer
includes/class-ipquery-db.php
Three new methods added: get_distinct_countries() retrieves all unique country/country_code pairs; delete_by_country($country_code) deletes rows matching a sanitized country code; log_action($message) optionally writes audit entries to WordPress debug log.
Admin Handler
includes/class-ipquery-admin.php
New handle_delete_by_country() method validates nonce and admin capability, sanitizes and uppercases the country code, calls the DB deletion method, logs the action on success, and redirects back to the Visitors page with deletion count and code parameters.
Admin Panel UI
admin/views/visitors.php
New "Delete by Country" form section with nonce protection, country dropdown populated from DB, and confirmation dialog warning of permanent deletion. Success notice displays the count of deleted records and country code when redirected back.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With whiskers twitching at compliance law,
We bundle countries into bins so clean,
One click, one nonce, one great big whoosh of awe—
GDPR's wish becomes the finest scene!
Delete by nation, logged with care,
A rabbit's gift of data that's not there. 🌍

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding country-based data deletion for GDPR support, with issue reference.
Linked Issues check ✅ Passed All acceptance criteria from issue #42 are met: country filter deletion tool implemented, bulk delete supported, action logged, and confirmation dialog required.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #42 objectives. No unrelated or out-of-scope modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 25 minutes and 19 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@penify-dev
Copy link
Copy Markdown

penify-dev Bot commented May 2, 2026

Failed to generate code suggestions for PR

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In handle_delete_by_country(), the redirect always includes country_deleted even when $deleted === false, which makes a DB failure indistinguishable from a successful operation that deleted 0 rows; consider branching the redirect (or notice) to separately handle the error case.
  • The delete-by-country panel in visitors.php uses an inline style="margin-top:24px;"; consider moving this into an existing CSS class or shared stylesheet to keep presentational concerns out of the view markup.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `handle_delete_by_country()`, the redirect always includes `country_deleted` even when `$deleted === false`, which makes a DB failure indistinguishable from a successful operation that deleted 0 rows; consider branching the redirect (or notice) to separately handle the error case.
- The delete-by-country panel in `visitors.php` uses an inline `style="margin-top:24px;"`; consider moving this into an existing CSS class or shared stylesheet to keep presentational concerns out of the view markup.

## Individual Comments

### Comment 1
<location path="includes/class-ipquery-admin.php" line_range="225-231" />
<code_context>
+	 *
+	 * @return void
+	 */
+	public static function handle_delete_by_country(): void {
+		check_admin_referer( 'ipquery_delete_by_country' );
+		if ( ! current_user_can( 'manage_options' ) ) {
+			wp_die( esc_html__( 'Not allowed.', 'ipquery' ) );
+		}
+
+		$country_code = strtoupper(
+			sanitize_text_field( wp_unslash( $_POST['country_code'] ?? '' ) )
+		);
</code_context>
<issue_to_address>
**issue (bug_risk):** Uppercasing the country code before deletion may prevent matching existing rows.

`country_code` is already sourced from a DB-backed `<select>`, so changing it to uppercase here assumes all stored values are uppercase. If any existing or future rows use lowercase/mixed-case codes (e.g. `us`), the delete will silently affect 0 rows. Either drop `strtoupper()` for the DB operation (only uppercase for display), or ensure `country_code` is consistently normalized at write-time throughout the codebase.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +225 to +231
public static function handle_delete_by_country(): void {
check_admin_referer( 'ipquery_delete_by_country' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'ipquery' ) );
}

$country_code = strtoupper(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Uppercasing the country code before deletion may prevent matching existing rows.

country_code is already sourced from a DB-backed <select>, so changing it to uppercase here assumes all stored values are uppercase. If any existing or future rows use lowercase/mixed-case codes (e.g. us), the delete will silently affect 0 rows. Either drop strtoupper() for the DB operation (only uppercase for display), or ensure country_code is consistently normalized at write-time throughout the codebase.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@admin/views/visitors.php`:
- Around line 30-34: Replace the hardcoded translatable string using "record(s)"
with a proper singular/plural localization via _n(); specifically, call _n( '1
record deleted for country: %s.', '%d records deleted for country: %s.', (int)
$_GET['country_deleted'], 'ipquery' ) (or similar) to choose the correct form
based on (int) $_GET['country_deleted'], then pass the resulting localized
string into printf/sprintf and substitute the count and the esc_html(
strtoupper( sanitize_text_field( wp_unslash( $_GET['country_code'] ?? '' ) ) )
); keep existing escaping and nonce-ignore comments and ensure the number
argument used for _n() is the same (int) $_GET['country_deleted'].

In `@includes/class-ipquery-admin.php`:
- Around line 254-259: The redirect currently casts $deleted to (int) which
converts a DB failure (false) into 0, losing the false-vs-0 distinction; update
the code that builds the 'country_deleted' query value (used in the
wp_safe_redirect/admin_url call in class-ipquery-admin.php) to preserve false by
explicitly checking $deleted === false and setting the param to a string like
'false' (otherwise cast to (int) or string for numeric counts), e.g. compute
$deleted_param = $deleted === false ? 'false' : (string) (int) $deleted and use
that instead of (int) $deleted so the UI can distinguish failure from zero rows.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c0b9ab1c-300d-46a4-9dc1-24ba8130b56f

📥 Commits

Reviewing files that changed from the base of the PR and between b33922d and aa75377.

📒 Files selected for processing (3)
  • admin/views/visitors.php
  • includes/class-ipquery-admin.php
  • includes/class-ipquery-db.php

Comment thread admin/views/visitors.php
Comment thread includes/class-ipquery-admin.php
@Suraj80
Copy link
Copy Markdown
Author

Suraj80 commented May 2, 2026

Thanks for the detailed review! I've addressed all the feedback in the latest commit

@gstraccini gstraccini Bot added hacktoberfest Participation in the Hacktoberfest event ✨ feature New feature requests or implementations 📝 documentation Tasks related to writing or updating documentation 🧪 tests Tasks related to testing labels May 5, 2026
@gstraccini gstraccini Bot requested a review from guibranco May 5, 2026 04:38
@gstraccini gstraccini Bot added the 🚦 awaiting triage Items that are awaiting triage or categorization label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🚦 awaiting triage Items that are awaiting triage or categorization 📝 documentation Tasks related to writing or updating documentation ✨ feature New feature requests or implementations hacktoberfest Participation in the Hacktoberfest event 🧪 tests Tasks related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add country-based data deletion support

2 participants