class-wpsweep-api.php
3 years ago
class-wpsweep-command.php
3 years ago
class-wpsweep.php
3 years ago
class-wpsweep-command.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP-Sweep WP-CLI |
| 4 | * |
| 5 | * @package wp-sweep |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class WPSweep_Command |
| 10 | */ |
| 11 | class WPSweep_Command extends WP_CLI_Command { |
| 12 | /** |
| 13 | * Clean up unused, orphaned and duplicated data in your WordPress |
| 14 | * |
| 15 | * ## OPTIONS |
| 16 | * |
| 17 | * [--all] |
| 18 | * Sweep all the orphaned data at once. |
| 19 | * |
| 20 | * Name of the items selected individually |
| 21 | * Available Items = |
| 22 | * revisions |
| 23 | * auto_drafts |
| 24 | * deleted_posts |
| 25 | * unapproved_comments |
| 26 | * spam_comments |
| 27 | * deleted_comments |
| 28 | * transient_options |
| 29 | * orphan_postmeta |
| 30 | * orphan_commentmeta |
| 31 | * orphan_usermeta |
| 32 | * orphan_termmeta |
| 33 | * orphan_term_relationships |
| 34 | * unused_terms |
| 35 | * duplicated_postmeta |
| 36 | * duplicated_commentmeta |
| 37 | * duplicated_usermeta |
| 38 | * duplicated_termmeta |
| 39 | * optimize_database |
| 40 | * oembed_postmeta |
| 41 | * |
| 42 | * ## EXAMPLES |
| 43 | * |
| 44 | * 1. wp sweep --all |
| 45 | * - Run Sweep for all the items. |
| 46 | * 2. wp sweep revisions |
| 47 | * - Sweep only Revision |
| 48 | * 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 |
| 49 | * - Sweep the selected items |
| 50 | * |
| 51 | * @since 1.0.8 |
| 52 | * |
| 53 | * @access public |
| 54 | * |
| 55 | * @param array $args array Arguments passed to command. Generally unused. |
| 56 | * @param array $assoc_args Parameters passed to command to be passed to callback. |
| 57 | * @return void |
| 58 | */ |
| 59 | public function __invoke( $args, $assoc_args ) { |
| 60 | |
| 61 | $items = array(); |
| 62 | |
| 63 | $default_items = array( |
| 64 | '0' => 'revisions', |
| 65 | '1' => 'auto_drafts', |
| 66 | '2' => 'deleted_posts', |
| 67 | '3' => 'unapproved_comments', |
| 68 | '4' => 'spam_comments', |
| 69 | '5' => 'deleted_comments', |
| 70 | '6' => 'transient_options', |
| 71 | '7' => 'orphan_postmeta', |
| 72 | '8' => 'orphan_commentmeta', |
| 73 | '9' => 'orphan_usermeta', |
| 74 | '10' => 'orphan_termmeta', |
| 75 | '11' => 'orphan_term_relationships', |
| 76 | '12' => 'unused_terms', |
| 77 | '13' => 'duplicated_postmeta', |
| 78 | '14' => 'duplicated_commentmeta', |
| 79 | '15' => 'duplicated_usermeta', |
| 80 | '16' => 'duplicated_termmeta', |
| 81 | '17' => 'optimize_database', |
| 82 | '18' => 'oembed_postmeta', |
| 83 | ); |
| 84 | |
| 85 | if ( isset( $assoc_args['all'] ) && true === $assoc_args['all'] ) { |
| 86 | $this->run_sweep( $default_items ); |
| 87 | WP_CLI::success( 'Sweep Complete' ); |
| 88 | |
| 89 | return; |
| 90 | } else { |
| 91 | foreach ( $default_items as $key => $item ) { |
| 92 | if ( in_array( $item, $args, true ) ) { |
| 93 | array_push( $items, $item ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | $this->run_sweep( $items ); |
| 98 | WP_CLI::success( 'Sweep Complete!' ); |
| 99 | |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Run WP-Sweep |
| 107 | * |
| 108 | * @since 1.0.8 |
| 109 | * |
| 110 | * @access public |
| 111 | * |
| 112 | * @param array $items Sweep items. |
| 113 | * @return void |
| 114 | */ |
| 115 | public function run_sweep( $items ) { |
| 116 | |
| 117 | $sweep = WPSweep::get_instance(); |
| 118 | |
| 119 | foreach ( $items as $key => $value ) { |
| 120 | $count = $sweep->count( $value ); |
| 121 | if ( 0 !== $count && '0' !== $count ) { |
| 122 | $message = $sweep->sweep( $value ); |
| 123 | WP_CLI::success( $message ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |