Skip to content

Commit

Permalink
Update database queries to delete data during uninstalltion
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaval-parekh committed Jan 24, 2022
1 parent 36aa673 commit cf26652
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions includes/uninstall-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,22 @@ function delete_posts() {

global $wpdb;

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

// Delete all post meta data related to "amp_validated_url" post_type.
$wpdb->query(
$wpdb->prepare(
"DELETE meta FROM $wpdb->postmeta AS meta INNER JOIN $wpdb->posts AS posts ON posts.ID = meta.post_id WHERE posts.post_type = %s;",
$post_type
)
);

$wpdb->delete(
$wpdb->posts,
[
'post_type' => 'amp_validated_url',
]
compact( 'post_type' )
);

// Delete orphan post meta data.
$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;" );

// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}

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

global $wpdb;

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

$wpdb->delete(
$wpdb->term_taxonomy,
[
'taxonomy' => 'amp_validation_error',
]
// Delete term meta.
$wpdb->query(
$wpdb->prepare(
"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;",
$taxonomy
)
);

// Delete orphan relationships.
$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;" );

// Delete orphan terms.
$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;" );
// Delete term relationship.
$wpdb->query(
$wpdb->prepare(
"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;",
$taxonomy
)
);

// Delete orphan term meta.
$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;" );
// Delete terms.
$wpdb->query(
$wpdb->prepare(
"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;",
$taxonomy
)
);

// Delete term taxonomy.
$wpdb->delete(
$wpdb->term_taxonomy,
compact( 'taxonomy' )
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}

Expand Down

0 comments on commit cf26652

Please sign in to comment.