class-wp-sweep-admin.php
5 days ago
class-wp-sweep-api.php
2 weeks ago
class-wp-sweep-command.php
2 weeks ago
class-wp-sweep-list-table.php
5 days ago
class-wp-sweep.php
5 days ago
index.php
2 weeks ago
class-wp-sweep-command.php
136 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The WP-CLI command. |
| 4 | * |
| 5 | * @package WP-Sweep |
| 6 | */ |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * Cleans up unused, orphaned and duplicated data from the command line. |
| 12 | * |
| 13 | * Registered as `wp sweep` rather than `wp wp-sweep`. A `wp-` prefix is a |
| 14 | * wordpress.org directory convention for keeping one plugin's download page |
| 15 | * apart from another's, not a command-naming one, and after the `wp` binary it |
| 16 | * only stutters; WP-CLI's own ecosystem names commands after the brand -- `wp |
| 17 | * wc`, `wp yoast`, `wp jetpack`. It is also the name the released 1.2.0 |
| 18 | * shipped, so no script that already calls WP-Sweep has to be edited. WP-CLI |
| 19 | * gives a collision to whichever plugin registered last and a bare noun is |
| 20 | * claimable by anyone; that is the accepted trade for a word as distinctive as |
| 21 | * `sweep`. |
| 22 | */ |
| 23 | class WP_Sweep_Command extends WP_CLI_Command { |
| 24 | |
| 25 | /** |
| 26 | * Clean up unused, orphaned and duplicated data in your WordPress. |
| 27 | * |
| 28 | * ## OPTIONS |
| 29 | * |
| 30 | * [<name>...] |
| 31 | * : One or more items to sweep. Ignored when --all is given. |
| 32 | * |
| 33 | * [--all] |
| 34 | * : Sweep every item, in the order they have to run. |
| 35 | * |
| 36 | * ## AVAILABLE ITEMS |
| 37 | * |
| 38 | * revisions, auto_drafts, deleted_posts, unapproved_comments, |
| 39 | * spam_comments, deleted_comments, transient_options, orphan_postmeta, |
| 40 | * orphan_commentmeta, orphan_usermeta, orphan_termmeta, |
| 41 | * orphan_term_relationships, unused_terms, duplicated_postmeta, |
| 42 | * duplicated_commentmeta, duplicated_usermeta, duplicated_termmeta, |
| 43 | * optimize_database, oembed_postmeta |
| 44 | * |
| 45 | * ## EXAMPLES |
| 46 | * |
| 47 | * # Sweep everything. |
| 48 | * $ wp sweep --all |
| 49 | * |
| 50 | * # Sweep one item. |
| 51 | * $ wp sweep revisions |
| 52 | * |
| 53 | * # Sweep several items. |
| 54 | * $ wp sweep revisions auto_drafts deleted_posts |
| 55 | * |
| 56 | * @since 1.0.8 |
| 57 | * |
| 58 | * @param array $args Item names passed to the command. |
| 59 | * @param array $assoc_args Flags passed to the command. |
| 60 | * @return void |
| 61 | */ |
| 62 | public function __invoke( $args, $assoc_args ) { |
| 63 | $sweeps = WP_Sweep::get_instance()->get_sweep_names(); |
| 64 | |
| 65 | if ( ! empty( $assoc_args['all'] ) ) { |
| 66 | $this->run_sweep( $sweeps ); |
| 67 | |
| 68 | WP_CLI::success( __( 'Sweep Complete', 'wp-sweep' ) ); |
| 69 | |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $requested = array_values( array_unique( (array) $args ) ); |
| 74 | |
| 75 | /* |
| 76 | * Nothing named and no --all swept nothing and reported success, which is |
| 77 | * the same answer a real sweep gives. |
| 78 | */ |
| 79 | if ( empty( $requested ) ) { |
| 80 | WP_CLI::error( __( 'Name at least one item to sweep, or pass --all to sweep every one.', 'wp-sweep' ) ); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * A name that is not a sweep is a typo, and it used to be discarded in |
| 85 | * silence: `wp sweep revisons` filtered down to nothing, swept nothing |
| 86 | * and printed "Sweep Complete!", so the only report of the mistake was |
| 87 | * that the revisions were still there. On a command whose whole job is |
| 88 | * deleting data, "I did what you asked" is the one thing it must not say |
| 89 | * when it did not. |
| 90 | * |
| 91 | * The screen's bulk handler drops unknown names on purpose and says |
| 92 | * nothing, because there the names come from checkboxes it rendered |
| 93 | * itself -- an unexpected one is tampering, not a typo, and it has no |
| 94 | * user to correct. |
| 95 | */ |
| 96 | $unknown = array_diff( $requested, $sweeps ); |
| 97 | |
| 98 | if ( ! empty( $unknown ) ) { |
| 99 | WP_CLI::error( |
| 100 | sprintf( |
| 101 | /* translators: %s is a comma-separated list of item names that were not recognised. */ |
| 102 | __( 'Not something this plugin sweeps: %s. Run `wp help sweep` for the list.', 'wp-sweep' ), |
| 103 | implode( ', ', $unknown ) |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | // Filtered from the canonical list rather than taken as given, so the |
| 109 | // items run in the order they have to: posts are deleted before the |
| 110 | // sweeps that hunt for the meta that deleting them just orphaned. |
| 111 | $this->run_sweep( array_values( array_intersect( $sweeps, $requested ) ) ); |
| 112 | |
| 113 | WP_CLI::success( __( 'Sweep Complete!', 'wp-sweep' ) ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Run each named sweep that has anything to remove. |
| 118 | * |
| 119 | * @since 1.0.8 |
| 120 | * |
| 121 | * @param array $items Sweep names. |
| 122 | * @return void |
| 123 | */ |
| 124 | public function run_sweep( $items ) { |
| 125 | $sweep = WP_Sweep::get_instance(); |
| 126 | |
| 127 | foreach ( $items as $item ) { |
| 128 | if ( 0 === (int) $sweep->count( $item ) ) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | WP_CLI::success( $sweep->sweep( $item ) ); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |