Skip to content

Commit

Permalink
Merge pull request #6840 from ampproject/bug/6826-delete-data-while-u…
Browse files Browse the repository at this point in the history
…ninstalling

Optimize the process of deleting AMP data during uninstallation
  • Loading branch information
westonruter authored Jan 25, 2022
2 parents a32862f + ab388a3 commit d78bac5
Show file tree
Hide file tree
Showing 3 changed files with 611 additions and 64 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"civicrm/composer-downloads-plugin": true,
"ampproject/php-css-parser-install-plugin": true,
"cweagans/composer-patches": true
"civicrm/composer-downloads-plugin": true,
"cweagans/composer-patches": true,
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "5.6.20"
Expand Down
124 changes: 79 additions & 45 deletions includes/uninstall-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function delete_options() {

delete_option( 'amp-options' );
delete_option( 'amp_css_transient_monitor_time_series' );
delete_option( 'amp_customize_setting_modified_timestamps' );
delete_option( 'amp_url_validation_queue' ); // See Validation\URLValidationCron::OPTION_KEY.

$theme_mod_name = 'amp_customize_setting_modified_timestamps';
Expand Down Expand Up @@ -57,35 +56,33 @@ function delete_user_metadata() {
*/
function delete_posts() {

/** @var \wpdb */
global $wpdb;

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

do {
$offset = $per_page * $current_page;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Cannot cache result since we're deleting the records.
$post_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_type = %s LIMIT %d OFFSET %d;",
$post_type,
$per_page,
$offset
)
);

if ( empty( $post_ids ) || ! is_array( $post_ids ) ) {
break;
}
// 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
)
);

foreach ( $post_ids as $post_id ) {
wp_delete_post( $post_id );
}
// Delete all amp_validated_url posts.
$wpdb->delete(
$wpdb->posts,
compact( 'post_type' )
);

$current_page++;
} while ( ! empty( $result ) );
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}

/**
Expand All @@ -96,35 +93,68 @@ function delete_posts() {
*/
function delete_terms() {

global $wpdb;
// Abort if term splitting has not been done. This is done by WooCommerce so it's
// it's also done here for good measure, even though we require WP 4.9+.
if ( version_compare( get_bloginfo( 'version' ), '4.2', '<' ) ) {
return;
}

$current_page = 0;
$per_page = 1000;
$taxonomy = 'amp_validation_error';
/** @var \wpdb */
global $wpdb;

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

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Cannot cache result since we're deleting the records.
$term_ids = $wpdb->get_col(
// Delete term meta (added in WP 4.4).
if ( ! empty( $wpdb->termmeta ) ) {
$wpdb->query(
$wpdb->prepare(
"SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s LIMIT %d OFFSET %d;",
$taxonomy,
$per_page,
$offset
"
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
)
);
}

if ( empty( $term_ids ) || ! is_array( $term_ids ) ) {
break;
}
// 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
)
);

foreach ( $term_ids as $term_id ) {
wp_delete_term( $term_id, $taxonomy );
}
// 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
)
);

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

/**
Expand All @@ -141,6 +171,7 @@ function delete_transients() {
return;
}

/** @var \wpdb */
global $wpdb;

$transient_groups = [
Expand Down Expand Up @@ -200,5 +231,8 @@ function remove_plugin_data() {
delete_posts();
delete_terms();
delete_transients();

// Clear any cached data that has been removed.
wp_cache_flush();
}
}
Loading

0 comments on commit d78bac5

Please sign in to comment.