wp-sweep
Last commit date
inc
3 years ago
js
9 years ago
CLAUDE.md
1 month ago
admin.php
7 years ago
composer.json
6 years ago
composer.lock
6 years ago
index.php
7 years ago
readme.txt
1 month ago
uninstall.php
7 years ago
wp-sweep.php
1 month ago
CLAUDE.md
78 lines
| 1 | # CLAUDE.md |
| 2 | |
| 3 | This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | |
| 5 | ## Commands |
| 6 | |
| 7 | **Install dev dependencies (PHP coding standards):** |
| 8 | ```bash |
| 9 | composer install |
| 10 | ``` |
| 11 | |
| 12 | **Lint PHP against WordPress Coding Standards:** |
| 13 | ```bash |
| 14 | ./vendor/bin/phpcs --standard=WordPress-Core,WordPress-Docs,WordPress-Extra wp-sweep.php inc/ admin.php |
| 15 | ``` |
| 16 | |
| 17 | **Fix auto-fixable coding standards violations:** |
| 18 | ```bash |
| 19 | ./vendor/bin/phpcbf --standard=WordPress-Core,WordPress-Docs,WordPress-Extra wp-sweep.php inc/ admin.php |
| 20 | ``` |
| 21 | |
| 22 | **WP-CLI sweep commands (requires a running WordPress install):** |
| 23 | ```bash |
| 24 | wp sweep --all # Sweep everything |
| 25 | wp sweep revisions auto_drafts # Sweep specific items |
| 26 | ``` |
| 27 | |
| 28 | **REST API endpoints (requires authentication with `activate_plugins` capability):** |
| 29 | ``` |
| 30 | GET /wp-json/sweep/v1/count/<name> |
| 31 | GET /wp-json/sweep/v1/details/<name> |
| 32 | DELETE /wp-json/sweep/v1/sweep/<name> |
| 33 | ``` |
| 34 | |
| 35 | There is no automated test suite. The JS minified file (`js/wp-sweep.min.js`) must be updated manually when `js/wp-sweep.js` changes. |
| 36 | |
| 37 | ## Architecture |
| 38 | |
| 39 | ### Entry point and bootstrapping |
| 40 | |
| 41 | `wp-sweep.php` defines `WP_SWEEP_VERSION` and `WP_SWEEP_MAIN_FILE`, then requires the two core class files and instantiates them. `WPSweep` is a singleton; `WPSweep_Api` is instantiated directly in the entry point. |
| 42 | |
| 43 | ### Core class: `inc/class-wpsweep.php` |
| 44 | |
| 45 | `WPSweep` is a singleton accessed via `WPSweep::get_instance()`. All sweep logic lives in three parallel `switch` statements keyed on a string sweep name: |
| 46 | |
| 47 | - `count($name)` — returns how many items would be swept |
| 48 | - `details($name)` — returns up to `$limit_details` (500) sample items |
| 49 | - `sweep($name)` — performs the deletion and returns a translated result message |
| 50 | - `total_count($name)` — counts total rows in a given table (used for the "% of" column) |
| 51 | |
| 52 | When `post_id`, `comment_id`, `user_id`, or `term_id` is `0` in orphaned meta, a direct SQL `DELETE` is used instead of the WordPress API functions, because the API functions won't act on ID 0. |
| 53 | |
| 54 | Two filters control what gets excluded from certain sweeps: |
| 55 | - `wp_sweep_excluded_taxonomies` — taxonomies excluded from orphaned term relationships check (default: `link_category`) |
| 56 | - `wp_sweep_excluded_termids` — term IDs excluded from unused terms sweep (default: default taxonomy terms + terms that are parents of other terms) |
| 57 | |
| 58 | ### REST API: `inc/class-wpsweep-api.php` |
| 59 | |
| 60 | Registers three routes under the `sweep/v1` namespace. All routes require `activate_plugins` capability. The `name` parameter is validated against the hardcoded `$sweeps` array. All routes delegate to the `WPSweep` singleton. |
| 61 | |
| 62 | ### WP-CLI: `inc/class-wpsweep-command.php` |
| 63 | |
| 64 | Loaded and registered only when `WP_CLI` is defined. Iterates the same hardcoded list of sweep names in order, skipping items with a count of 0. |
| 65 | |
| 66 | ### Admin UI: `admin.php` + `js/wp-sweep.js` |
| 67 | |
| 68 | `admin.php` is a template file loaded by `add_management_page` (Tools → Sweep). It calls `count()` and `total_count()` on page load to populate counts. Buttons carry `data-sweep_name`, `data-sweep_type`, and `data-nonce` attributes. |
| 69 | |
| 70 | `wp-sweep.js` uses jQuery AJAX against `wp-admin/admin-ajax.php` with actions `sweep` and `sweep_details`. Nonces follow the pattern `wp_sweep_{name}` (sweep) and `wp_sweep_details_{name}` (details). "Sweep All" chains individual sweep promises sequentially using `.reduce()`. |
| 71 | |
| 72 | ### Adding a new sweep type |
| 73 | |
| 74 | 1. Add the name string to the `$sweeps` array in `WPSweep_Api` and `$default_items` in `WPSweep_Command`. |
| 75 | 2. Add a `case` for the name in `WPSweep::count()`, `details()`, and `sweep()`. |
| 76 | 3. Add a `case` for the related table type in `WPSweep::total_count()` if needed. |
| 77 | 4. Add the corresponding row to `admin.php` with the correct `data-sweep_type` matching a `total_count()` key. |
| 78 |