|
1 | 1 | # Orphan Command |
2 | 2 |
|
3 | | -> List and delete orphan entities and metadata. |
| 3 | +Stable tag: 1.0.0 |
| 4 | +Requires at least: 3.3 |
| 5 | +Tested up to: 5.6 |
| 6 | +Requires PHP: 7.2 |
| 7 | +License: GPL v3 or later |
| 8 | +Contributors: tfrommen, humanmade |
| 9 | + |
| 10 | +WP-CLI command to list and delete orphan WordPress entities and metadata. |
4 | 11 |
|
5 | 12 | ---- |
6 | 13 |
|
| 14 | +## Introduction |
| 15 | + |
| 16 | +WordPress offers dedicated APIs that are to be used for CRUD operations on the various core data structures. |
| 17 | +When deleting a comment via `wp_delete_comment( $comment_id, true )`, WordPress takes care that all metadata for that comment gets automatically deleted as well. |
| 18 | +Great! |
| 19 | + |
| 20 | +However, people not always do what is right, and so it's not a rare situation that you have to face a database that is full of orphaned entries. |
| 21 | +Comment metadata for comments that no longer exist, comments for non-existing posts, or revisions of posts that don't exist anymore. |
| 22 | + |
| 23 | +Orphan Command provides a new WP-CLI command, `wp orphan`, that lets you easily spot orphans, and even delete them, if you want. |
| 24 | + |
| 25 | +## Table of Contents |
| 26 | + |
| 27 | +* [Installation](#installation) |
| 28 | + * [Composer](#composer) |
| 29 | + * [Manual](#manual) |
| 30 | +* [Requirements](#requirements) |
| 31 | + * [PHP](#php) |
| 32 | + * [WordPress](#wordpress) |
| 33 | + * [WP-CLI](#wp-cli) |
| 34 | +* [Commands](#commands) |
| 35 | + * [`wp-orphan-blog-meta`](#wp-orphan-blog-meta) |
| 36 | + * [`wp-orphan-comment`](#wp-orphan-comment) |
| 37 | + * [`wp-orphan-comment-meta`](#wp-orphan-comment-meta) |
| 38 | + * [`wp-orphan-post`](#wp-orphan-post) |
| 39 | + * [`wp-orphan-post-meta`](#wp-orphan-post-meta) |
| 40 | + * [`wp-orphan-term-meta`](#wp-orphan-term-meta) |
| 41 | + * [`wp-orphan-user-meta`](#wp-orphan-user-meta) |
| 42 | +* [Extending Orphan Command](#extending-orphan-command) |
| 43 | +* [Frequently Asked Questions](#frequently-asked-questions) |
| 44 | + |
| 45 | +## Installation |
| 46 | + |
| 47 | +### Composer |
| 48 | + |
| 49 | +Install with [Composer](https://getcomposer.org): |
| 50 | + |
| 51 | +```shell |
| 52 | +composer require humanmade/orphan-command |
| 53 | +``` |
| 54 | + |
| 55 | +By default, Orphan Command will be installed as WP-CLI package. |
| 56 | +However, it **can** also be installed as WordPress plugin, for example, by using [a custom install path](https://github.com/composer/installers#custom-install-paths). |
| 57 | + |
| 58 | +### Manual |
| 59 | + |
| 60 | +In case you're not managing your entire site via Composer, you can also clone this repository into your site's plugins directory. |
| 61 | + |
| 62 | +```shell |
| 63 | +cd /path/to/plugins |
| 64 | + |
| 65 | +git clone git@github.com:humanmade/orphan-command.git |
| 66 | +``` |
| 67 | + |
| 68 | +Then, install and set up PHP auto-loading: |
| 69 | + |
| 70 | +```shell |
| 71 | +cd orphan-command |
| 72 | + |
| 73 | +composer install --prefer-dist --no-dev |
| 74 | +``` |
| 75 | + |
| 76 | +Finally, go to your site's Plugins page, and activate Orphan Command. |
| 77 | + |
| 78 | +## Requirements |
| 79 | + |
| 80 | +### PHP |
| 81 | + |
| 82 | +Orphan Command **requires PHP 7.2 or higher**. |
| 83 | + |
| 84 | +### WordPress |
| 85 | + |
| 86 | +Orphan Command **requires WordPress 3.3 or higher**. |
| 87 | + |
| 88 | +### WP-CLI |
| 89 | + |
| 90 | +Orphan Command **requires WP-CLI 2.5 or higher**. |
| 91 | + |
| 92 | +## Commands |
| 93 | + |
| 94 | +In general, all commands support the following three **sub-commands**: |
| 95 | + |
| 96 | +* **`delete`**: Delete all orphans of the respective entity type. |
| 97 | +* **`list`**: List all orphans of the respective entity type. |
| 98 | +* **`query`**: Print the MySQL query to list all orphans of the respective entity type. |
| 99 | + |
| 100 | +By default, the output of `list` is a comma-separated list of IDs. |
| 101 | +This can be changed by using the `--format` option, which supports the following values: |
| 102 | + |
| 103 | +* **`count`**: The number of orphans. |
| 104 | +* **`csv`**: Orphan IDs to be exported into a CSV file. |
| 105 | + * Sample usage: `wp orphan post list --format=csv > orphan-posts.csv` |
| 106 | +* **`ids`**: Orphan IDs as a single comma-separated string. |
| 107 | +* **`json`**: Orphan IDs to be exported into a JSON file. |
| 108 | + * Sample usage: `wp orphan post list --format=json > orphan-posts.json` |
| 109 | +* **`table`**: Orphan IDs printed as a table (with a single column only). |
| 110 | +* **`yaml`**: Orphan IDs to be exported into a YAML file. |
| 111 | + * Sample usage: `wp orphan post list --format=yaml > orphan-posts.yaml` |
| 112 | + |
| 113 | +Some commands support additional options that are explained in the following sections. |
| 114 | + |
| 115 | +### `wp orphan blog meta` |
| 116 | + |
| 117 | +The `wp orphan blog meta` command lets you list and delete all **blog metadata** referencing **blogs** that don't exist anymore. |
| 118 | + |
| 119 | +**List all orphan blog metadata:** |
| 120 | + |
| 121 | +```shell |
| 122 | +wp orphan blog meta list |
| 123 | +``` |
| 124 | + |
| 125 | +**Delete all orphan blog metadata:** |
| 126 | + |
| 127 | +```shell |
| 128 | +wp orphan blog meta delete |
| 129 | +``` |
| 130 | + |
| 131 | +### `wp orphan comment` |
| 132 | + |
| 133 | +The `wp orphan comment` command lets you list and delete all **comments** referencing **posts** that don't exist anymore. |
| 134 | + |
| 135 | +In addition to `--format`, the `wp orphan comment` command also supports the following options: |
| 136 | + |
| 137 | +* **`--type`**: Comma-separated list of comment type slugs. |
| 138 | + * Sample usage: `--type=comment` or `--type=comment,reaction` |
| 139 | + |
| 140 | +**List all orphan comments of any comment type:** |
| 141 | + |
| 142 | +```shell |
| 143 | +wp orphan comment list |
| 144 | +``` |
| 145 | + |
| 146 | +**List all orphan reactions:** |
| 147 | + |
| 148 | +```shell |
| 149 | +wp orphan comment list --type=reaction |
| 150 | +``` |
| 151 | + |
| 152 | +**Delete all orphan comments of any comment type:** |
| 153 | + |
| 154 | +```shell |
| 155 | +wp orphan comment delete |
| 156 | +``` |
| 157 | + |
| 158 | +**Delete all orphan default comments only:** |
| 159 | + |
| 160 | +```shell |
| 161 | +wp orphan comment delete --type=comment |
| 162 | +``` |
| 163 | + |
| 164 | +**Note:** Since comments can be nested (i.e., a comment can have a parent comment), an orphan comment _could_ also be a comment referencing another comment that does not exist anymore. |
| 165 | +This is **not** what this command does, though. |
| 166 | +The main reason is that a comment referencing a non-existing post will usually not be exposed to site visitors. |
| 167 | + |
| 168 | +A future version of Orphan Command might allow to also list/delete comments referencing non-existing parent comments. |
| 169 | + |
| 170 | +### `wp orphan comment meta` |
| 171 | + |
| 172 | +The `wp orphan comment meta` command lets you list and delete all **comment metadata** referencing **comments** that don't exist anymore. |
| 173 | + |
| 174 | +**List all orphan comment metadata:** |
| 175 | + |
| 176 | +```shell |
| 177 | +wp orphan comment meta list |
| 178 | +``` |
| 179 | + |
| 180 | +**Delete all orphan comment metadata:** |
| 181 | + |
| 182 | +```shell |
| 183 | +wp orphan comment meta delete |
| 184 | +``` |
| 185 | + |
| 186 | +### `wp orphan post` |
| 187 | + |
| 188 | +The `wp orphan post` command lets you list and delete all **posts** referencing parent **posts** that don't exist anymore. |
| 189 | + |
| 190 | +In addition to `--format`, the `wp orphan post` command also supports the following options: |
| 191 | + |
| 192 | +* **`--type`**: Comma-separated list of post type slugs. |
| 193 | + * Sample usage: `--type=post` or `--type=post,page` |
| 194 | + |
| 195 | +**List all orphan posts of any post type:** |
| 196 | + |
| 197 | +```shell |
| 198 | +wp orphan post list |
| 199 | +``` |
| 200 | + |
| 201 | +**List all orphan pages:** |
| 202 | + |
| 203 | +```shell |
| 204 | +wp orphan post list --type=page |
| 205 | +``` |
| 206 | + |
| 207 | +**Delete all orphan posts of any post type:** |
| 208 | + |
| 209 | +```shell |
| 210 | +wp orphan post delete |
| 211 | +``` |
| 212 | + |
| 213 | +**Delete all orphan default posts only:** |
| 214 | + |
| 215 | +```shell |
| 216 | +wp orphan post delete --type=post |
| 217 | +``` |
| 218 | + |
| 219 | +### `wp orphan post meta` |
| 220 | + |
| 221 | +The `wp orphan post meta` command lets you list and delete all **post metadata** referencing **posts** that don't exist anymore. |
| 222 | + |
| 223 | +**List all orphan post metadata:** |
| 224 | + |
| 225 | +```shell |
| 226 | +wp orphan post meta list |
| 227 | +``` |
| 228 | + |
| 229 | +**Delete all orphan post metadata:** |
| 230 | + |
| 231 | +```shell |
| 232 | +wp orphan post meta delete |
| 233 | +``` |
| 234 | + |
| 235 | +### `wp orphan term meta` |
| 236 | + |
| 237 | +The `wp orphan term meta` command lets you list and delete all **term metadata** referencing **terms** that don't exist anymore. |
| 238 | + |
| 239 | +**List all orphan term metadata:** |
| 240 | + |
| 241 | +```shell |
| 242 | +wp orphan term meta list |
| 243 | +``` |
| 244 | + |
| 245 | +**Delete all orphan term metadata:** |
| 246 | + |
| 247 | +```shell |
| 248 | +wp orphan term meta delete |
| 249 | +``` |
| 250 | + |
| 251 | +### `wp orphan user meta` |
| 252 | + |
| 253 | +The `wp orphan user meta` command lets you list and delete all **user metadata** referencing **users** that don't exist anymore. |
| 254 | + |
| 255 | +**List all orphan user metadata:** |
| 256 | + |
| 257 | +```shell |
| 258 | +wp orphan user meta list |
| 259 | +``` |
| 260 | + |
| 261 | +**Delete all orphan user metadata:** |
| 262 | + |
| 263 | +```shell |
| 264 | +wp orphan user meta delete |
| 265 | +``` |
| 266 | + |
| 267 | +## Extending Orphan Command |
| 268 | + |
| 269 | +If you want to customize or extend the functionality of Orphan Command, you can either extend any of the actual command classes, or you could write your own based on either the `Orphan_Command` or `Orphan_Meta_Command` class included in Orphan Command. |
| 270 | + |
| 271 | +All relevant class methods are marked `protected` or `public`, so you can redefine or decorate any behavior. |
| 272 | +For example, the `Orphan_Post_Command` class enhances the `get_query` method to inject the post type passed to the command, if any. |
| 273 | + |
| 274 | +## Frequently Asked Questions |
| 275 | + |
| 276 | +> **What about terms?** |
| 277 | +
|
| 278 | +For terms, there is no clear definition of orphans. |
| 279 | +An orphan term could be defined in one of several ways: |
| 280 | + |
| 281 | +* An entry in the `wp_terms` table that is not referenced at all in the `wp_term_taxonomy` table. (This is most likely **not** what you want, most of the time.) |
| 282 | +* An entry in the `wp_term_taxonomy` table referencing a non-existing term (i.e., `wp_term_taxonomy.term_id` does not exist in `wp_terms.term_id`). |
| 283 | +* An entry in the `wp_term_taxonomy` table referencing a non-existing parent term (i.e., `wp_term_taxonomy.parent` does not exist in `wp_terms.term_id`). |
| 284 | +* An entry in the `wp_term_relationships` table referencing a non-existing object. (This would either require the taxonomy, or the object type to then use all registered taxonomies.) |
| 285 | + |
| 286 | +To some extent, this is similar to comments. |
| 287 | +However, there it is more of an interpretation issue, which is why Orphan Command, by default, defines orphan comments as comments referencing non-existing posts. |
| 288 | + |
| 289 | +A future version of Orphan Command might allow to also list/delete orphan terms. |
| 290 | + |
| 291 | +> **What about site/network metadata?** |
| 292 | +
|
| 293 | +The terminology in WordPress around blog metadata and options, network options, and site metadata and options is quite confusing! |
| 294 | + |
| 295 | +> **What about use case XYZ?** |
| 296 | +
|
| 297 | +Yes, there are most certainly several possible use cases around orphan data and metadata missing. |
| 298 | +However, this is on purpose. |
| 299 | + |
| 300 | +While it may be a good idea to list all users of a specific role that are not added to any site, or to delete all orphan posts with a specific status, this would be out of scope. |
| 301 | + |
| 302 | +Orphan Command provides easy access to tasks that a lot of people might want to perform a lot of times; not more. |
| 303 | + |
| 304 | +That said, you should be able to use existing WP-CLI commands such as `wp <entity> list|delete` or `wp db query` to accomplish any of the above examples quite easily. |
| 305 | + |
| 306 | +## License |
| 307 | + |
| 308 | +This program is free software; you can redistribute it and/or modify |
| 309 | +it under the terms of the GNU General Public License as published by |
| 310 | +the Free Software Foundation; either version 3 of the License, or |
| 311 | +(at your option) any later version. |
| 312 | + |
| 313 | +This program is distributed in the hope that it will be useful, |
| 314 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 315 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 316 | +GNU General Public License for more details. |
0 commit comments