Skip to content

Commit c286ca1

Browse files
committed
Optimize the process of deleting AMP data during uninstallation (#6840)
1 parent 21d7d8a commit c286ca1

File tree

2 files changed

+608
-61
lines changed

2 files changed

+608
-61
lines changed

includes/uninstall-functions.php

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function delete_options() {
1818

1919
delete_option( 'amp-options' );
2020
delete_option( 'amp_css_transient_monitor_time_series' );
21-
delete_option( 'amp_customize_setting_modified_timestamps' );
2221
delete_option( 'amp_url_validation_queue' ); // See Validation\URLValidationCron::OPTION_KEY.
2322

2423
$theme_mod_name = 'amp_customize_setting_modified_timestamps';
@@ -57,35 +56,33 @@ function delete_user_metadata() {
5756
*/
5857
function delete_posts() {
5958

59+
/** @var \wpdb */
6060
global $wpdb;
6161

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
6564

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+
);
8278

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+
);
8684

87-
$current_page++;
88-
} while ( ! empty( $result ) );
85+
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
8986
}
9087

9188
/**
@@ -96,35 +93,68 @@ function delete_posts() {
9693
*/
9794
function delete_terms() {
9895

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+
}
100101

101-
$current_page = 0;
102-
$per_page = 1000;
103-
$taxonomy = 'amp_validation_error';
102+
/** @var \wpdb */
103+
global $wpdb;
104104

105-
do {
106-
$offset = $per_page * $current_page;
105+
$taxonomy = 'amp_validation_error';
106+
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
107107

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(
110111
$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
115120
)
116121
);
122+
}
117123

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+
);
121137

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+
);
125151

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
128158
}
129159

130160
/**
@@ -141,6 +171,7 @@ function delete_transients() {
141171
return;
142172
}
143173

174+
/** @var \wpdb */
144175
global $wpdb;
145176

146177
$transient_groups = [
@@ -200,5 +231,8 @@ function remove_plugin_data() {
200231
delete_posts();
201232
delete_terms();
202233
delete_transients();
234+
235+
// Clear any cached data that has been removed.
236+
wp_cache_flush();
203237
}
204238
}

0 commit comments

Comments
 (0)