diff --git a/content/plugins/pof-importer/pof-importer.php b/content/plugins/pof-importer/pof-importer.php index 2d3e6b5..596e334 100644 --- a/content/plugins/pof-importer/pof-importer.php +++ b/content/plugins/pof-importer/pof-importer.php @@ -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.' ); @@ -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 ) ); @@ -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; } @@ -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();