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

Enhance data cleanup logic in cleaner.php #2032

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Changes from 2 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
113 changes: 68 additions & 45 deletions import/cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,50 +39,73 @@ public function __construct() {
$this->urlBuilder = new Brizy_Editor_UrlBuilder( $this->project );
}

/**
* @throws Exception
*/
public function clean() {
$this->deleteFiles();
$this->cleanTables();
}

private function deleteFiles() {

$ids = $this->wpdb->get_results( "SELECT p.ID FROM {$this->wpdb->posts} p WHERE p.post_type = 'attachment'", ARRAY_N );

foreach ( $ids as $id ) {
wp_delete_attachment( $id, true );
}

$this->fileSystem->delete( $this->urlBuilder->brizy_upload_path(), true );
}
/**
* @throws Exception
*/
public function clean() {
$brzPostTypes = $this->getPostTypes();
$placeholders = implode( ', ', array_fill( 0, count( $brzPostTypes ), '%s' ) );

$this->deletePostRelationships( $brzPostTypes, $placeholders );
$this->deletePostsComments( $brzPostTypes, $placeholders );
$this->deleteMedia();

$this->project->setDataAsJson( json_encode( new stdClass() ) )->saveStorage();
$this->project->cleanClassCache();
}

private function getPostTypes() {
return [
Brizy_Admin_Blocks_Main::CP_GLOBAL,
Brizy_Admin_Blocks_Main::CP_SAVED,
Brizy_Admin_Fonts_Main::CP_FONT,
BrizyPro_Admin_Membership_Membership::CP_ROLE,
Brizy_Admin_Layouts_Main::CP_LAYOUT,
Brizy_Admin_Stories_Main::CP_STORY,
Brizy_Admin_Popups_Main::CP_POPUP,
Brizy_Admin_Templates::CP_TEMPLATE,
Brizy_Admin_FormEntries::CP_FORM_ENTRY,

// not deleted
// Brizy_Editor_Project::BRIZY_PROJECT,
];
}

private function deletePostRelationships( $brzPostTypes, $placeholders ) {
$this->wpdb->query(
KytHub marked this conversation as resolved.
Show resolved Hide resolved
$this->wpdb->prepare(
"DELETE p, m, tr, tt, t, tm
FROM {$this->wpdb->posts} p
LEFT JOIN {$this->wpdb->term_relationships} tr ON p.ID = tr.object_id
LEFT JOIN {$this->wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
LEFT JOIN {$this->wpdb->terms} t ON t.term_id = tt.term_id
LEFT JOIN {$this->wpdb->termmeta} tm ON tm.term_id = t.term_id
LEFT JOIN {$this->wpdb->postmeta} m ON p.ID = m.post_id
WHERE p.post_type IN ($placeholders)",
...$brzPostTypes
)
);
}

private function deletePostsComments( $brzPostTypes, $placeholders ) {
$this->wpdb->query(
$this->wpdb->prepare(
"DELETE c, cm
FROM {$this->wpdb->comments} c
INNER JOIN {$this->wpdb->posts} p ON c.comment_post_ID = p.ID
LEFT JOIN {$this->wpdb->commentmeta} cm ON c.comment_ID = cm.comment_id
WHERE p.post_type IN ($placeholders)",
...$brzPostTypes
)
);
}

private function deleteMedia() {
$media_ids = $this->wpdb->get_col( "SELECT ID FROM {$this->wpdb->posts} WHERE post_type = 'attachment'" );

foreach ( $media_ids as $media_id ) {
wp_delete_attachment( $media_id, true );
}
}

/**
* @throws Exception
*/
private function cleanTables() {

$this->wpdb->query(
$this->wpdb->prepare(
"DELETE p, m, tr, tt, t, tm
FROM {$this->wpdb->posts} p
LEFT JOIN {$this->wpdb->term_relationships} tr ON p.ID = tr.object_id
LEFT JOIN {$this->wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
LEFT JOIN {$this->wpdb->terms} t ON t.term_id = tt.term_id
LEFT JOIN {$this->wpdb->termmeta} tm ON tm.term_id = t.term_id
LEFT JOIN {$this->wpdb->postmeta} m ON p.ID = m.post_id
WHERE p.post_type <> %s AND p.post_type <> %s",
Brizy_Editor_Project::BRIZY_PROJECT,
'wp_global_styles'
)
);

$this->wpdb->query( "DELETE FROM {$this->wpdb->commentmeta}" );
$this->wpdb->query( "DELETE FROM {$this->wpdb->comments}" );

$this->project->setDataAsJson( json_encode( new stdClass() ) )->saveStorage();

$this->project->cleanClassCache();
}
}