Skip to content

Commit 1dece18

Browse files
committed
Update database queries to delete data during uninstalltion
1 parent 25e2337 commit 1dece18

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

includes/uninstall-functions.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ function delete_posts() {
5959

6060
global $wpdb;
6161

62+
$post_type = 'amp_validated_url';
6263
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
6364

65+
// Delete all post meta data related to "amp_validated_url" post_type.
66+
$wpdb->query(
67+
$wpdb->prepare(
68+
"DELETE meta FROM $wpdb->postmeta AS meta INNER JOIN $wpdb->posts AS posts ON posts.ID = meta.post_id WHERE posts.post_type = %s;",
69+
$post_type
70+
)
71+
);
72+
6473
$wpdb->delete(
6574
$wpdb->posts,
66-
[
67-
'post_type' => 'amp_validated_url',
68-
]
75+
compact( 'post_type' )
6976
);
7077

71-
// Delete orphan post meta data.
72-
$wpdb->query( "DELETE meta FROM $wpdb->postmeta AS meta LEFT JOIN $wpdb->posts AS posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" );
73-
7478
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
7579
}
7680

@@ -84,24 +88,38 @@ function delete_terms() {
8488

8589
global $wpdb;
8690

91+
$taxonomy = 'amp_validation_error';
8792
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
8893

89-
$wpdb->delete(
90-
$wpdb->term_taxonomy,
91-
[
92-
'taxonomy' => 'amp_validation_error',
93-
]
94+
// Delete term meta.
95+
$wpdb->query(
96+
$wpdb->prepare(
97+
"DELETE tm from $wpdb->termmeta AS tm INNER JOIN $wpdb->term_taxonomy AS tt ON tm.term_id = tt.term_id WHERE tt.taxonomy = %s;",
98+
$taxonomy
99+
)
94100
);
95101

96-
// Delete orphan relationships.
97-
$wpdb->query( "DELETE tr FROM $wpdb->term_relationships AS tr LEFT JOIN $wpdb->posts AS posts ON posts.ID = tr.object_id WHERE posts.ID IS NULL;" );
98-
99-
// Delete orphan terms.
100-
$wpdb->query( "DELETE t FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.term_id IS NULL;" );
102+
// Delete term relationship.
103+
$wpdb->query(
104+
$wpdb->prepare(
105+
"DELETE tr 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 = %s;",
106+
$taxonomy
107+
)
108+
);
101109

102-
// Delete orphan term meta.
103-
$wpdb->query( "DELETE tm FROM $wpdb->termmeta AS tm LEFT JOIN $wpdb->term_taxonomy AS tt ON tm.term_id = tt.term_id WHERE tt.term_id IS NULL;" );
110+
// Delete terms.
111+
$wpdb->query(
112+
$wpdb->prepare(
113+
"DELETE terms from $wpdb->terms AS terms INNER JOIN $wpdb->term_taxonomy AS tt ON terms.term_id = tt.term_id WHERE tt.taxonomy = %s;",
114+
$taxonomy
115+
)
116+
);
104117

118+
// Delete term taxonomy.
119+
$wpdb->delete(
120+
$wpdb->term_taxonomy,
121+
compact( 'taxonomy' )
122+
);
105123
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
106124
}
107125

0 commit comments

Comments
 (0)