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