wp-sweep
Last commit date
js
11 years ago
admin.php
9 years ago
class-command.php
9 years ago
composer.json
10 years ago
index.php
11 years ago
readme.txt
9 years ago
uninstall.php
11 years ago
wp-sweep.php
9 years ago
class-command.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | class WPSweep_Command extends WP_CLI_Command { |
| 4 | /** |
| 5 | * Clean up unused, orphaned and duplicated data in your WordPress |
| 6 | * |
| 7 | * ## OPTIONS |
| 8 | * |
| 9 | * [--all] |
| 10 | * Sweep all the orphaned data at once. |
| 11 | * |
| 12 | * Name of the items selected individually |
| 13 | * Available Items = |
| 14 | * revisions |
| 15 | * auto_drafts |
| 16 | * deleted_posts |
| 17 | * unapproved_comments |
| 18 | * spam_comments |
| 19 | * deleted_comments |
| 20 | * transient_options |
| 21 | * orphan_postmeta |
| 22 | * orphan_commentmeta |
| 23 | * orphan_usermeta |
| 24 | * orphan_termmeta |
| 25 | * orphan_term_relationships |
| 26 | * unused_terms |
| 27 | * duplicated_postmeta |
| 28 | * duplicated_commentmeta |
| 29 | * duplicated_usermeta |
| 30 | * duplicated_termmeta |
| 31 | * optimize_database |
| 32 | * oembed_postmet |
| 33 | * |
| 34 | * ## EXAMPLES |
| 35 | * |
| 36 | * 1. wp sweep --all |
| 37 | * - Run Sweep for all the items. |
| 38 | * 2. wp sweep revisions |
| 39 | * - Sweep only Revision |
| 40 | * 3. wp sweep revisions auto_drafts deleted_posts unapproved_comments spam_comments deleted_comments transient_options orphan_postmeta orphan_commentmeta orphan_usermeta orphan_termmeta orphan_term_relationships unused_terms duplicated_postmeta duplicated_commentmeta duplicated_usermeta duplicated_termmeta optimize_database oembed_postmet |
| 41 | * - Sweep the selected items |
| 42 | * |
| 43 | * |
| 44 | */ |
| 45 | public function __invoke( $args, $assoc_args ) { |
| 46 | |
| 47 | $items = array(); |
| 48 | |
| 49 | $default_items = array( |
| 50 | "0" => 'revisions', |
| 51 | "1" => 'auto_drafts', |
| 52 | "2" => 'deleted_posts', |
| 53 | "3" => 'unapproved_comments', |
| 54 | "4" => 'spam_comments', |
| 55 | "5" => 'deleted_comments', |
| 56 | "6" => 'transient_options', |
| 57 | "7" => 'orphan_postmeta', |
| 58 | "8" => 'orphan_commentmeta', |
| 59 | "9" => 'orphan_usermeta', |
| 60 | "10" => 'orphan_termmeta', |
| 61 | "11" => 'orphan_term_relationships', |
| 62 | "12" => 'unused_terms', |
| 63 | "13" => 'duplicated_postmeta', |
| 64 | "14" => 'duplicated_commentmeta', |
| 65 | "15" => 'duplicated_usermeta', |
| 66 | "16" => 'duplicated_termmeta', |
| 67 | "17" => 'optimize_database', |
| 68 | "18" => 'oembed_postmeta' |
| 69 | ); |
| 70 | |
| 71 | if ( isset( $assoc_args['all'] ) && $assoc_args['all'] == true ) { |
| 72 | $this->run_sweep( $default_items ); |
| 73 | WP_CLI::success( "Sweep Complete" ); |
| 74 | |
| 75 | return; |
| 76 | } else { |
| 77 | foreach ( $default_items as $key => $item ) { |
| 78 | if ( in_array( $item, $args ) ) { |
| 79 | array_push( $items, $item ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $this->run_sweep( $items ); |
| 84 | WP_CLI::success( "Sweep Complete!" ); |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | public function run_sweep( $items ) { |
| 92 | |
| 93 | $sweep = new WPSweep(); |
| 94 | |
| 95 | foreach ( $items as $key => $value ) { |
| 96 | $count = $sweep->count( $value ); |
| 97 | if ( $count !== 0 && $count !== "0" ) { |
| 98 | $message = $sweep->sweep( $value ); |
| 99 | WP_CLI::success( $message ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | WP_CLI::add_command( 'sweep', 'WPSweep_Command' ); |