Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Term relationship detach #86

Open
wants to merge 4 commits into
base: term-relationship-detach-base
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions content/plugins/pof-importer/pof-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public function __construct() {
*/
public function importer_cleanup( $args = [], $assoc_args = [] ) : bool {
global $wpdb;
$posts_table = $wpdb->prefix . 'posts';
$postmeta_table = $wpdb->prefix . 'postmeta';
$options_table = $wpdb->prefix . 'options';
$posts_table = $wpdb->prefix . 'posts';
$postmeta_table = $wpdb->prefix . 'postmeta';
$term_relationships_table = $wpdb->prefix . 'term_relationships';

$this->wp_cli_msg( 'Deleting old imported data.' );

Expand All @@ -82,7 +84,7 @@ public function importer_cleanup( $args = [], $assoc_args = [] ) : bool {
$guids = array_keys( $this->flatten_tree( $tree['program'][0] ) );

// Collect all posts
$posts = $wpdb->get_results( 'SELECT ID,post_author,post_type,post_modified FROM ' . $posts_table . ' WHERE post_type IN ( "page", "nav_menu_item" )' );
$posts = $wpdb->get_results( sprintf( 'SELECT ID,post_author,post_type,post_modified FROM %s WHERE post_type IN ( "page", "nav_menu_item" )', $posts_table ) );

$progress = $this->wp_cli_progress( 'Fetching post data to check for deletion', count( $posts ) );

Expand Down Expand Up @@ -285,8 +287,9 @@ public function importer_cleanup( $args = [], $assoc_args = [] ) : bool {
$progress->finish();

// Check that we will still have a suitable amount of posts after the deletion
if ( count( $ids ) - count( $delete_ids ) < 70 ) {
$this->wp_cli_error( 'There would be less than a 1000 posts after cleanup so aborting just in case.' );
$delete_limit = 70;
if ( count( $ids ) - count( $delete_ids ) < $delete_limit ) {
$this->wp_cli_error( sprintf( 'There would be less than (%d) posts after cleanup so aborting just in case.', $delete_limit ) );
return false;
}

Expand All @@ -295,22 +298,26 @@ public function importer_cleanup( $args = [], $assoc_args = [] ) : bool {
array_key_exists( 'dry-run', $assoc_args ) &&
$assoc_args['dry-run']
) {
$this->wp_cli_success( 'Dry run complete, would delete (' . count( $delete_ids ) . ') posts.' );
$this->wp_cli_success( sprintf( 'Dry run complete, would delete (%d) posts.', count( $delete_ids ) ) );
}
else {
if ( ! empty( $delete_ids ) ) {
if ( empty( $delete_ids ) ) {
$this->wp_cli_msg( 'No posts to delete.' );
}
else {
$ids = wp_list_pluck( $delete_ids, 'id' );
$this->wp_cli_msg( 'Deleting old posts.' );
$wpdb->query( 'DELETE FROM ' . $posts_table . ' WHERE ID IN(' . implode( ',', $ids ) . ')' );
$this->wp_cli_msg( 'Deleting detached postmeta.' );
$wpdb->query( 'DELETE ' . $postmeta_table . '.* FROM ' . $postmeta_table . ' LEFT JOIN ' . $posts_table . ' ON ' . $postmeta_table . '.post_id = ' . $posts_table . '.ID WHERE ID IS NULL' );

$this->wp_cli_success( 'Run complete, deleted (' . count( $ids ) . ') posts.' );
$wpdb->query( sprintf( 'DELETE FROM %s WHERE ID IN(%s)', $posts_table, implode( ',', $ids ) ) );
$this->wp_cli_success( sprintf( 'Run complete, deleted (%d) posts.', count( $ids ) ) );
}

$this->wp_cli_msg( 'Deleting detached postmeta.' );
$wpdb->query( sprintf( 'DELETE pm FROM %s pm LEFT JOIN %s wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;', $postmeta_table, $posts_table ) );
$this->wp_cli_msg( 'Deleting detached term relationships.' );
$wpdb->query( sprintf( 'DELETE tr FROM %s tr LEFT JOIN %s wp ON wp.ID = tr.object_id WHERE wp.ID IS NULL;', $term_relationships_table, $posts_table ) );
$this->wp_cli_msg( 'Deleting unnecessary wpseo sitemap cache rows.' );
$wpdb->query( sprintf( 'DELETE FROM %s WHERE option_name like "wpseo_sitemap_%%_cache_validator";', $options_table ) );

$this->wp_cli_msg( 'Flushing cache & rewrite rules' );
wp_cache_flush();
flush_rewrite_rules();
Expand Down