@@ -18,7 +18,6 @@ function delete_options() {
18
18
19
19
delete_option ( 'amp-options ' );
20
20
delete_option ( 'amp_css_transient_monitor_time_series ' );
21
- delete_option ( 'amp_customize_setting_modified_timestamps ' );
22
21
delete_option ( 'amp_url_validation_queue ' ); // See Validation\URLValidationCron::OPTION_KEY.
23
22
24
23
$ theme_mod_name = 'amp_customize_setting_modified_timestamps ' ;
@@ -57,35 +56,33 @@ function delete_user_metadata() {
57
56
*/
58
57
function delete_posts () {
59
58
59
+ /** @var \wpdb */
60
60
global $ wpdb ;
61
61
62
- $ current_page = 0 ;
63
- $ per_page = 1000 ;
64
- $ post_type = 'amp_validated_url ' ;
62
+ $ post_type = 'amp_validated_url ' ;
63
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
65
64
66
- do {
67
- $ offset = $ per_page * $ current_page ;
68
-
69
- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Cannot cache result since we're deleting the records.
70
- $ post_ids = $ wpdb ->get_col (
71
- $ wpdb ->prepare (
72
- "SELECT ID FROM $ wpdb ->posts WHERE post_type = %s LIMIT %d OFFSET %d; " ,
73
- $ post_type ,
74
- $ per_page ,
75
- $ offset
76
- )
77
- );
78
-
79
- if ( empty ( $ post_ids ) || ! is_array ( $ post_ids ) ) {
80
- break ;
81
- }
65
+ // Delete all post meta data related to "amp_validated_url" post_type.
66
+ $ wpdb ->query (
67
+ $ wpdb ->prepare (
68
+ "
69
+ DELETE meta
70
+ FROM $ wpdb ->postmeta AS meta
71
+ INNER JOIN $ wpdb ->posts AS posts
72
+ ON posts.ID = meta.post_id
73
+ WHERE posts.post_type = %s;
74
+ " ,
75
+ $ post_type
76
+ )
77
+ );
82
78
83
- foreach ( $ post_ids as $ post_id ) {
84
- wp_delete_post ( $ post_id );
85
- }
79
+ // Delete all amp_validated_url posts.
80
+ $ wpdb ->delete (
81
+ $ wpdb ->posts ,
82
+ compact ( 'post_type ' )
83
+ );
86
84
87
- $ current_page ++;
88
- } while ( ! empty ( $ result ) );
85
+ // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
89
86
}
90
87
91
88
/**
@@ -96,35 +93,68 @@ function delete_posts() {
96
93
*/
97
94
function delete_terms () {
98
95
99
- global $ wpdb ;
96
+ // Abort if term splitting has not been done. This is done by WooCommerce so it's
97
+ // it's also done here for good measure, even though we require WP 4.9+.
98
+ if ( version_compare ( get_bloginfo ( 'version ' ), '4.2 ' , '< ' ) ) {
99
+ return ;
100
+ }
100
101
101
- $ current_page = 0 ;
102
- $ per_page = 1000 ;
103
- $ taxonomy = 'amp_validation_error ' ;
102
+ /** @var \wpdb */
103
+ global $ wpdb ;
104
104
105
- do {
106
- $ offset = $ per_page * $ current_page ;
105
+ $ taxonomy = ' amp_validation_error ' ;
106
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
107
107
108
- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Cannot cache result since we're deleting the records.
109
- $ term_ids = $ wpdb ->get_col (
108
+ // Delete term meta (added in WP 4.4).
109
+ if ( ! empty ( $ wpdb ->termmeta ) ) {
110
+ $ wpdb ->query (
110
111
$ wpdb ->prepare (
111
- "SELECT term_id FROM $ wpdb ->term_taxonomy WHERE taxonomy = %s LIMIT %d OFFSET %d; " ,
112
- $ taxonomy ,
113
- $ per_page ,
114
- $ offset
112
+ "
113
+ DELETE tm
114
+ FROM $ wpdb ->termmeta AS tm
115
+ INNER JOIN $ wpdb ->term_taxonomy AS tt
116
+ ON tm.term_id = tt.term_id
117
+ WHERE tt.taxonomy = %s;
118
+ " ,
119
+ $ taxonomy
115
120
)
116
121
);
122
+ }
117
123
118
- if ( empty ( $ term_ids ) || ! is_array ( $ term_ids ) ) {
119
- break ;
120
- }
124
+ // Delete term relationship.
125
+ $ wpdb ->query (
126
+ $ wpdb ->prepare (
127
+ "
128
+ DELETE tr
129
+ FROM $ wpdb ->term_relationships AS tr
130
+ INNER JOIN $ wpdb ->term_taxonomy AS tt
131
+ ON tr.term_taxonomy_id = tt.term_taxonomy_id
132
+ WHERE tt.taxonomy = %s;
133
+ " ,
134
+ $ taxonomy
135
+ )
136
+ );
121
137
122
- foreach ( $ term_ids as $ term_id ) {
123
- wp_delete_term ( $ term_id , $ taxonomy );
124
- }
138
+ // Delete terms.
139
+ $ wpdb ->query (
140
+ $ wpdb ->prepare (
141
+ "
142
+ DELETE terms
143
+ FROM $ wpdb ->terms AS terms
144
+ INNER JOIN $ wpdb ->term_taxonomy AS tt
145
+ ON terms.term_id = tt.term_id
146
+ WHERE tt.taxonomy = %s;
147
+ " ,
148
+ $ taxonomy
149
+ )
150
+ );
125
151
126
- $ current_page ++;
127
- } while ( ! empty ( $ result ) );
152
+ // Delete term taxonomy.
153
+ $ wpdb ->delete (
154
+ $ wpdb ->term_taxonomy ,
155
+ compact ( 'taxonomy ' )
156
+ );
157
+ // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
128
158
}
129
159
130
160
/**
@@ -141,6 +171,7 @@ function delete_transients() {
141
171
return ;
142
172
}
143
173
174
+ /** @var \wpdb */
144
175
global $ wpdb ;
145
176
146
177
$ transient_groups = [
@@ -200,5 +231,8 @@ function remove_plugin_data() {
200
231
delete_posts ();
201
232
delete_terms ();
202
233
delete_transients ();
234
+
235
+ // Clear any cached data that has been removed.
236
+ wp_cache_flush ();
203
237
}
204
238
}
0 commit comments