WP-Sweep - Version 1.0.8

Version Description

  • NEW: Added wp_sweep_excluded_taxonomies filter to allow more than just link_category taxonomy
  • NEW: Support for WP-CLI wp sweep
Download this release

Release Info

Developer GamerZ
Plugin Icon WP-Sweep
Version 1.0.8
Comparing to
See all releases

Code changes from version 1.0.6 to 1.0.8

Files changed (4) hide show
  1. admin.php +5 -3
  2. class-command.php +106 -0
  3. readme.txt +10 -3
  4. wp-sweep.php +35 -14
admin.php CHANGED
@@ -64,8 +64,10 @@ $transient_options = WPSweep::get_instance()->count( 'transient_options
64
  </style>
65
  <div class="wrap">
66
  <h2><?php _e( 'WP-Sweep', 'wp-sweep' ); ?></h2>
67
- <div class="update-nag">
68
- <?php printf( __( 'Before you do any sweep, please <a href="%s" target="%s">backup your database</a> first because any sweep done is irreversible.', 'wp-sweep' ), 'https://wordpress.org/plugins/wp-dbmanager/', '_blank' ); ?>
 
 
69
  </div>
70
  <p>
71
  <?php printf( __( 'For performance reasons, only %s items will be shown if you click Details', 'wp-sweep' ), number_format_i18n( WPSweep::get_instance()->limit_details ) ); ?>
@@ -560,4 +562,4 @@ $transient_options = WPSweep::get_instance()->count( 'transient_options
560
  <button class="button button-primary btn-sweep-all"><?php _e( 'Sweep All', 'wp-sweep' ); ?></button>
561
  </p>
562
  </div>
563
- </div>
64
  </style>
65
  <div class="wrap">
66
  <h2><?php _e( 'WP-Sweep', 'wp-sweep' ); ?></h2>
67
+ <div class="notice notice-warning">
68
+ <p>
69
+ <?php printf( __( 'Before you do any sweep, please <a href="%s" target="%s">backup your database</a> first because any sweep done is irreversible.', 'wp-sweep' ), 'https://wordpress.org/plugins/wp-dbmanager/', '_blank' ); ?>
70
+ </p>
71
  </div>
72
  <p>
73
  <?php printf( __( 'For performance reasons, only %s items will be shown if you click Details', 'wp-sweep' ), number_format_i18n( WPSweep::get_instance()->limit_details ) ); ?>
562
  <button class="button button-primary btn-sweep-all"><?php _e( 'Sweep All', 'wp-sweep' ); ?></button>
563
  </p>
564
  </div>
565
+ </div>
class-command.php ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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' );
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === WP-Sweep ===
2
  Contributors: GamerZ
3
  Donate link: http://lesterchan.net/site/donation/
4
- Tags: sweep, clean, cleanup, clean up, optimize, orphan, unused, duplicated, posts, post meta, comments, comment meta, users, user meta, terms, term relationships, revisions, auto drafts, transient, database, tables, oembed
5
  Requires at least: 4.4
6
- Tested up to: 4.4
7
- Stable tag: 1.0.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -62,6 +62,13 @@ Following delete functions are used:
62
  I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
63
 
64
  == Changelog ==
 
 
 
 
 
 
 
65
  = 1.0.6 =
66
  * NEW: Delete 'languages' folder from the plugin
67
  * NEW: Use translate.wordpress.org to translate the plugin
1
  === WP-Sweep ===
2
  Contributors: GamerZ
3
  Donate link: http://lesterchan.net/site/donation/
4
+ Tags: sweep, clean, cleanup, clean up, optimize, orphan, unused, duplicated, posts, post meta, comments, comment meta, users, user meta, terms, term meta, term relationships, revisions, auto drafts, transient, database, tables, oembed
5
  Requires at least: 4.4
6
+ Tested up to: 4.6
7
+ Stable tag: 1.0.8
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
62
  I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
63
 
64
  == Changelog ==
65
+ = 1.0.8 =
66
+ * NEW: Added wp_sweep_excluded_taxonomies filter to allow more than just link_category taxonomy
67
+ * NEW: Support for WP-CLI `wp sweep`
68
+
69
+ = 1.0.7 =
70
+ * FIXES: Use custom query to delete Orphaned Term Relationship if wp_remove_object_terms() fails
71
+
72
  = 1.0.6 =
73
  * NEW: Delete 'languages' folder from the plugin
74
  * NEW: Use translate.wordpress.org to translate the plugin
wp-sweep.php CHANGED
@@ -3,14 +3,14 @@
3
  Plugin Name: WP-Sweep
4
  Plugin URI: http://lesterchan.net/portfolio/programming/php/
5
  Description: WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It cleans up revisions, auto drafts, unapproved comments, spam comments, trashed comments, orphan post meta, orphan comment meta, orphan user meta, orphan term relationships, unused terms, duplicated post meta, duplicated comment meta, duplicated user meta and transient options. It also optimizes your database tables.
6
- Version: 1.0.6
7
  Author: Lester 'GaMerZ' Chan
8
  Author URI: http://lesterchan.net
9
  Text Domain: wp-sweep
10
  License: GPL2
11
  */
12
 
13
- /* Copyright 2015 Lester Chan (email : lesterchan@gmail.com)
14
 
15
  This program is free software; you can redistribute it and/or modify
16
  it under the terms of the GNU General Public License, version 2, as
@@ -31,7 +31,7 @@ License: GPL2
31
  *
32
  * @since 1.0.0
33
  */
34
- define( 'WP_SWEEP_VERSION', '1.0.6' );
35
 
36
  /**
37
  * WP-Sweep class
@@ -101,7 +101,12 @@ class WPSweep {
101
  * @access public
102
  * @return void
103
  */
104
- public function init() {}
 
 
 
 
 
105
 
106
  /**
107
  * Adds all the plugin hooks
@@ -124,7 +129,7 @@ class WPSweep {
124
  * Enqueue JS/CSS files used for admin
125
  *
126
  * @since 1.0.3
127
- *
128
  * @access public
129
  * @param string $hook
130
  * @return void
@@ -153,7 +158,7 @@ class WPSweep {
153
  * Admin menu
154
  *
155
  * @since 1.0.3
156
- *
157
  * @access public
158
  * @return void
159
  */
@@ -346,7 +351,7 @@ class WPSweep {
346
  $count = $wpdb->get_var( "SELECT COUNT(meta_id) FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms)" );
347
  break;
348
  case 'orphan_term_relationships':
349
- $count = $wpdb->get_var( "SELECT COUNT(object_id) FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" );
350
  break;
351
  case 'unused_terms':
352
  $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(t.term_id) FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ")", 0 ) );
@@ -435,7 +440,7 @@ class WPSweep {
435
  $details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms) LIMIT %d", $this->limit_details ) );
436
  break;
437
  case 'orphan_term_relationships':
438
- $details = $wpdb->get_col( $wpdb->prepare( "SELECT tt.taxonomy FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts) LIMIT %d", $this->limit_details ) );
439
  break;
440
  case 'unused_terms':
441
  $details = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ") LIMIT %d", 0, $this->limit_details ) );
@@ -637,10 +642,13 @@ class WPSweep {
637
  }
638
  break;
639
  case 'orphan_term_relationships':
640
- $query = $wpdb->get_results( "SELECT tr.object_id, tt.term_id, tt.taxonomy FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" );
641
  if( $query ) {
642
  foreach ( $query as $tax ) {
643
- wp_remove_object_terms( intval( $tax->object_id ), intval( $tax->term_id ), $tax->taxonomy );
 
 
 
644
  }
645
 
646
  $message = sprintf( __( '%s Orphaned Term Relationships Processed', 'wp-sweep' ), number_format_i18n( sizeof( $query ) ) );
@@ -757,6 +765,21 @@ class WPSweep {
757
  return ( $total > 0 ? round( ( $current / $total ) * 100, 2 ) : 0 ) . '%';
758
  }
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
  /*
761
  * Get excluded term IDs
762
  *
@@ -832,10 +855,9 @@ class WPSweep {
832
  foreach ( $ms_sites as $ms_site ) {
833
  switch_to_blog( $ms_site['blog_id'] );
834
  $this->plugin_activated();
 
835
  }
836
  }
837
-
838
- restore_current_blog();
839
  } else {
840
  $this->plugin_activated();
841
  }
@@ -868,10 +890,9 @@ class WPSweep {
868
  foreach ( $ms_sites as $ms_site ) {
869
  switch_to_blog( $ms_site['blog_id'] );
870
  $this->plugin_deactivated();
 
871
  }
872
  }
873
-
874
- restore_current_blog();
875
  } else {
876
  $this->plugin_deactivated();
877
  }
3
  Plugin Name: WP-Sweep
4
  Plugin URI: http://lesterchan.net/portfolio/programming/php/
5
  Description: WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It cleans up revisions, auto drafts, unapproved comments, spam comments, trashed comments, orphan post meta, orphan comment meta, orphan user meta, orphan term relationships, unused terms, duplicated post meta, duplicated comment meta, duplicated user meta and transient options. It also optimizes your database tables.
6
+ Version: 1.0.8
7
  Author: Lester 'GaMerZ' Chan
8
  Author URI: http://lesterchan.net
9
  Text Domain: wp-sweep
10
  License: GPL2
11
  */
12
 
13
+ /* Copyright 2016 Lester Chan (email : lesterchan@gmail.com)
14
 
15
  This program is free software; you can redistribute it and/or modify
16
  it under the terms of the GNU General Public License, version 2, as
31
  *
32
  * @since 1.0.0
33
  */
34
+ define( 'WP_SWEEP_VERSION', '1.0.8' );
35
 
36
  /**
37
  * WP-Sweep class
101
  * @access public
102
  * @return void
103
  */
104
+ public function init() {
105
+ // include class for WP CLI command
106
+ if ( defined( 'WP_CLI' ) ) {
107
+ require __DIR__ . '/class-command.php';
108
+ }
109
+ }
110
 
111
  /**
112
  * Adds all the plugin hooks
129
  * Enqueue JS/CSS files used for admin
130
  *
131
  * @since 1.0.3
132
+ *
133
  * @access public
134
  * @param string $hook
135
  * @return void
158
  * Admin menu
159
  *
160
  * @since 1.0.3
161
+ *
162
  * @access public
163
  * @return void
164
  */
351
  $count = $wpdb->get_var( "SELECT COUNT(meta_id) FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms)" );
352
  break;
353
  case 'orphan_term_relationships':
354
+ $count = $wpdb->get_var( "SELECT COUNT(object_id) FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy NOT IN ('" . implode( '\',\'', $this->get_excluded_taxonomies() ) . "') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" );
355
  break;
356
  case 'unused_terms':
357
  $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(t.term_id) FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ")", 0 ) );
440
  $details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms) LIMIT %d", $this->limit_details ) );
441
  break;
442
  case 'orphan_term_relationships':
443
+ $details = $wpdb->get_col( $wpdb->prepare( "SELECT tt.taxonomy FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy NOT IN ('" . implode( '\',\'', $this->get_excluded_taxonomies() ) . "') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts) LIMIT %d", $this->limit_details ) );
444
  break;
445
  case 'unused_terms':
446
  $details = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ") LIMIT %d", 0, $this->limit_details ) );
642
  }
643
  break;
644
  case 'orphan_term_relationships':
645
+ $query = $wpdb->get_results( "SELECT tr.object_id, tr.term_taxonomy_id, tt.term_id, tt.taxonomy FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy NOT IN ('" . implode( '\',\'', $this->get_excluded_taxonomies() ) . "') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" );
646
  if( $query ) {
647
  foreach ( $query as $tax ) {
648
+ $wp_remove_object_terms = wp_remove_object_terms( intval( $tax->object_id ), intval( $tax->term_id ), $tax->taxonomy );
649
+ if( $wp_remove_object_terms !== true ) {
650
+ $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $tax->object_id, $tax->term_taxonomy_id ) );
651
+ }
652
  }
653
 
654
  $message = sprintf( __( '%s Orphaned Term Relationships Processed', 'wp-sweep' ), number_format_i18n( sizeof( $query ) ) );
765
  return ( $total > 0 ? round( ( $current / $total ) * 100, 2 ) : 0 ) . '%';
766
  }
767
 
768
+ /*
769
+ * Get excluded taxonomies
770
+ *
771
+ * @since 1.0.8
772
+ *
773
+ * @access private
774
+ * @return array Excluded taxonomies
775
+ */
776
+ private function get_excluded_taxonomies() {
777
+ $excluded_taxonomies = array();
778
+ $excluded_taxonomies[] = 'link_category';
779
+
780
+ return apply_filters( 'wp_sweep_excluded_taxonomies', $excluded_taxonomies );
781
+ }
782
+
783
  /*
784
  * Get excluded term IDs
785
  *
855
  foreach ( $ms_sites as $ms_site ) {
856
  switch_to_blog( $ms_site['blog_id'] );
857
  $this->plugin_activated();
858
+ restore_current_blog();
859
  }
860
  }
 
 
861
  } else {
862
  $this->plugin_activated();
863
  }
890
  foreach ( $ms_sites as $ms_site ) {
891
  switch_to_blog( $ms_site['blog_id'] );
892
  $this->plugin_deactivated();
893
+ restore_current_blog();
894
  }
895
  }
 
 
896
  } else {
897
  $this->plugin_deactivated();
898
  }