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

merge cleanup option and comment count performtouch improving #58

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion assets/css/wp-bulk-delete-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ html:not([data-scroll="0"]) .wpbd-header {
}

.wpbd-card>.header svg.wpbd-caret.rotated {
transform: rotate(90deg)
transform: rotate(360deg)
}

.wpbd-card>.header svg.wpbd-close {
Expand Down Expand Up @@ -1738,3 +1738,9 @@ html:not([data-scroll="0"]) .wpbd-header {
color: #424242 !important;
font-family: unset !important;
}

.cleanups_section{
display: flex;
flex-direction: row;
align-items: center;
}
189 changes: 53 additions & 136 deletions includes/class-delete-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,88 +295,10 @@ public function get_post_count( $counttype = '' ) {
case 'trash':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status = %s", 'trash' ) );
break;
case 'orphan_postmeta':
$count = $wpdb->get_var( "SELECT COUNT(meta_id) FROM $wpdb->postmeta WHERE post_id NOT IN (SELECT ID FROM $wpdb->posts)" );
break;
case 'orphan_commentmeta':
$count = $wpdb->get_var( "SELECT COUNT(meta_id) FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)" );
break;
case 'orphan_usermeta':
$count = $wpdb->get_var( "SELECT COUNT(umeta_id) FROM $wpdb->usermeta WHERE user_id NOT IN (SELECT ID FROM $wpdb->users)" );
break;
case 'orphan_termmeta':
$count = $wpdb->get_var( "SELECT COUNT(meta_id) FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms)" );
break;

case 'duplicated_postmeta':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT(meta_id) AS count FROM $wpdb->postmeta GROUP BY post_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( is_array( $query ) ) {
$count = array_sum( array_map( 'intval', $query ) );
}
break;
case 'duplicated_commentmeta':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT(meta_id) AS count FROM $wpdb->commentmeta GROUP BY comment_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( is_array( $query ) ) {
$count = array_sum( array_map( 'intval', $query ) );
}
break;
case 'duplicated_usermeta':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT(umeta_id) AS count FROM $wpdb->usermeta GROUP BY user_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( is_array( $query ) ) {
$count = array_sum( array_map( 'intval', $query ) );
}
break;
case 'duplicated_termmeta':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT(meta_id) AS count FROM $wpdb->termmeta GROUP BY term_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( is_array( $query ) ) {
$count = array_sum( array_map( 'intval', $query ) );
}
break;

}
return $count;
}

/**
* Get Comment Count by status
*
* @access public
* @since 1.0
* @param array $status status
* @return int | posts count.
*/
public function get_comment_count( $status = '' ) {
global $wpdb;

$count = 0;

switch( $status ) {

case 'pending':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = %s", '0' ) );
break;

case 'spam':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = %s", 'spam' ) );
break;

case 'trash':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE (comment_approved = %s OR comment_approved = %s)", 'trash', 'post-trashed' ) );
break;

case 'approved':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = %s", '1' ) );
break;

default:
$count = 0;
break;
}

return $count;
}


/**
* Run Cleanup
*
Expand Down Expand Up @@ -421,114 +343,109 @@ public function run_cleanup( $cleanuptype = '' ){
}
break;

case 'orphan_postmeta':
$query = $wpdb->get_results( "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE post_id NOT IN (SELECT ID FROM $wpdb->posts)" );
if( $query ) {
foreach ( $query as $meta ) {
//Delete all orphan and duplicate
case 'all_orphan_duplicate':
$dp = $ocm = $oum = $otm = $dpm = $dcm = $dum = $dtm = 0;
$query1 = $wpdb->get_results( "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE post_id NOT IN (SELECT ID FROM $wpdb->posts)" );
if( $query1 ) {
foreach ( $query1 as $meta ) {
$post_id = intval( $meta->post_id );
if( $post_id === 0 ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta->meta_key ) );
} else {
delete_post_meta( $post_id, $meta->meta_key );
}
}

$message = sprintf( __( '%s Orphaned Post Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$dp = number_format_i18n( sizeof( $query1 ) );
}
break;
case 'orphan_commentmeta':
$query = $wpdb->get_results( "SELECT comment_id, meta_key FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)" );
if( $query ) {
foreach ( $query as $meta ) {

//Orphan Comment Meta
$query2 = $wpdb->get_results( "SELECT comment_id, meta_key FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments)" );
if( $query2 ) {
foreach ( $query2 as $meta ) {
$comment_id = intval( $meta->comment_id );
if( $comment_id === 0 ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->commentmeta WHERE comment_id = %d AND meta_key = %s", $comment_id, $meta->meta_key ) );
} else {
delete_comment_meta( $comment_id, $meta->meta_key );
}
}

$message = sprintf( __( '%s Orphaned Comment Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$ocm = number_format_i18n( sizeof( $query2 ) );
}
break;
case 'orphan_usermeta':
$query = $wpdb->get_results( "SELECT user_id, meta_key FROM $wpdb->usermeta WHERE user_id NOT IN (SELECT ID FROM $wpdb->users)" );
if( $query ) {
foreach ( $query as $meta ) {

//Orphan User Meta
$query3 = $wpdb->get_results( "SELECT user_id, meta_key FROM $wpdb->usermeta WHERE user_id NOT IN (SELECT ID FROM $wpdb->users)" );
if( $query3 ) {
foreach ( $query3 as $meta ) {
$user_id = intval( $meta->user_id );
if( $user_id === 0 ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta->meta_key ) );
} else {
delete_user_meta( $user_id, $meta->meta_key );
}
}

$message = sprintf( __( '%s Orphaned User Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$oum = number_format_i18n( sizeof( $query3 ) );
}
break;
case 'orphan_termmeta':
$query = $wpdb->get_results( "SELECT term_id, meta_key FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms)" );
if( $query ) {
foreach ( $query as $meta ) {

//Orphan Term Meta
$query4 = $wpdb->get_results( "SELECT term_id, meta_key FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms)" );
if( $query4 ) {
foreach ( $query4 as $meta ) {
$term_id = intval( $meta->term_id );
if( $term_id === 0 ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->termmeta WHERE term_id = %d AND meta_key = %s", $term_id, $meta->meta_key ) );
} else {
delete_term_meta( $term_id, $meta->meta_key );
}
}

$message = sprintf( __( '%s Orphaned Term Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$otm = number_format_i18n( sizeof( $query4 ) );
}
break;

case 'duplicated_postmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, post_id, COUNT(*) AS count FROM $wpdb->postmeta GROUP BY post_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query ) {
foreach ( $query as $meta ) {

//Duplicate Post Meta
$query5 = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, post_id, COUNT(*) AS count FROM $wpdb->postmeta GROUP BY post_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query5 ) {
foreach ( $query5 as $meta ) {
$ids = array_map( 'intval', explode( ',', $meta->ids ) );
array_pop( $ids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_id IN (" . implode( ',', $ids ) . ") AND post_id = %d", intval( $meta->post_id ) ) );
}

$message = sprintf( __( '%s Duplicated Post Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$dpm = number_format_i18n( sizeof( $query5 ) );
}
break;
case 'duplicated_commentmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, comment_id, COUNT(*) AS count FROM $wpdb->commentmeta GROUP BY comment_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query ) {
foreach ( $query as $meta ) {

//Duplicate Comment Meta
$query6 = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, comment_id, COUNT(*) AS count FROM $wpdb->commentmeta GROUP BY comment_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query6 ) {
foreach ( $query6 as $meta ) {
$ids = array_map( 'intval', explode( ',', $meta->ids ) );
array_pop( $ids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->commentmeta WHERE meta_id IN (" . implode( ',', $ids ) . ") AND comment_id = %d", intval( $meta->comment_id ) ) );
}

$message = sprintf( __( '%s Duplicated Comment Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$dcm = number_format_i18n( sizeof( $query6 ) );
}
break;
case 'duplicated_usermeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(umeta_id ORDER BY umeta_id DESC) AS ids, user_id, COUNT(*) AS count FROM $wpdb->usermeta GROUP BY user_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query ) {
foreach ( $query as $meta ) {

//Duplicate user Meta
$query7 = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(umeta_id ORDER BY umeta_id DESC) AS ids, user_id, COUNT(*) AS count FROM $wpdb->usermeta GROUP BY user_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query7 ) {
foreach ( $query7 as $meta ) {
$ids = array_map( 'intval', explode( ',', $meta->ids ) );
array_pop( $ids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE umeta_id IN (" . implode( ',', $ids ) . ") AND user_id = %d", intval( $meta->user_id ) ) );
}

$message = sprintf( __( '%s Duplicated User Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$dum = number_format_i18n( sizeof( $query7 ) );
}
break;
case 'duplicated_termmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, term_id, COUNT(*) AS count FROM $wpdb->termmeta GROUP BY term_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query ) {
foreach ( $query as $meta ) {

//Duplicate term Meta
$query8 = $wpdb->get_results( $wpdb->prepare( "SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, term_id, COUNT(*) AS count FROM $wpdb->termmeta GROUP BY term_id, meta_key, meta_value HAVING count > %d", 1 ) );
if( $query8 ) {
foreach ( $query8 as $meta ) {
$ids = array_map( 'intval', explode( ',', $meta->ids ) );
array_pop( $ids );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->termmeta WHERE meta_id IN (" . implode( ',', $ids ) . ") AND term_id = %d", intval( $meta->term_id ) ) );
}

$message = sprintf( __( '%s Duplicated Term Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( sizeof( $query ) ) );
$dtm = number_format_i18n( sizeof( $query7 ) );
}
$odsum = $dp + $ocm + $oum + $otm + $dpm + $dcm + $dum + $dtm;
$message = sprintf( __( '%s Orphan and Duplicate Meta Cleaned up', 'wp-bulk-delete' ), number_format_i18n( $odsum ) );
break;

}
Expand Down Expand Up @@ -814,7 +731,7 @@ public function get_delete_comment_count( $data = array() ){
foreach ( $delete_comment_status as $comment_status ) {

switch( $comment_status ) {
case 'pending':
case 'moderated':
$temp_delete_query[] = "comment_approved = '0'";
break;

Expand Down Expand Up @@ -896,7 +813,7 @@ public function do_delete_comments( $data = array() ) {
foreach ( $delete_comment_status as $comment_status ) {

switch( $comment_status ) {
case 'pending':
case 'moderated':
$temp_delete_query[] = "comment_approved = '0'";
break;

Expand Down
33 changes: 27 additions & 6 deletions includes/common-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ function wpbd_get_wc_order_count(){
global $wpdb;
//Get WC order type with count
$results = array();
$query = "SELECT COUNT(`id`) as `count`, `type`, REPLACE(`type`, 'shop_', '') as `type_name` FROM `{$wpdb->prefix}wc_orders` WHERE `type` = 'shop_order' ";
$results = $wpdb->get_results($query);
$results = (array)reset($results);
$results['type_name'] = ucfirst( $results['type_name'] ). '';

return $results;
$table_name = $wpdb->prefix . 'wc_orders';
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$table_name}'");

if ( $table_exists ) {
$query = "SELECT COUNT(`id`) as `count`, `type`, REPLACE(`type`, 'shop_', '') as `type_name` FROM `{$wpdb->prefix}wc_orders` WHERE `type` = 'shop_order' ";
$results = $wpdb->get_results($query);
$results = (array)reset($results);
$results['type_name'] = ucfirst( $results['type_name'] ). '';
return $results;
}else{
return ;
}
}

/**
Expand Down Expand Up @@ -386,4 +392,19 @@ function wpdb_render_common_header( $page_title ){
</div>
<?php

}

/**
* Get WP Post Status
*
* @since 1.3.1
* @return array
*/
function get_wp_post_status(){

$get_post_status = get_post_stati();
$get_post_status = array_filter( $get_post_status, function($key) { return strpos( $key, 'wc-' ) !== 0 && strpos( $key, 'request-' ) !== 0; }, ARRAY_FILTER_USE_KEY );
$wpbd_post_status = array_diff_key( $get_post_status, array_flip( ['inherit', 'auto-draft'] ) );

return $wpbd_post_status;
}
9 changes: 6 additions & 3 deletions includes/delele-comments-form-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function xt_delete_comments_form_process( $data ) {
*/
function wpdb_render_delete_comments_status(){
$comment_status = array(
'pending' => __( 'Pending Comments', 'wp-bulk-delete'),
'moderated' => __( 'Pending Comments', 'wp-bulk-delete'),
'spam' => __( 'Spam Comments', 'wp-bulk-delete'),
'trash' => __( 'Trash Comments', 'wp-bulk-delete'),
'approved' => __( 'Approved Comments', 'wp-bulk-delete'),
Expand Down Expand Up @@ -126,13 +126,16 @@ function wpdb_render_delete_comments_status(){
</div>
<div class="wpbd-inner-section-2">
<?php
//get comments counts
$get_counts = wp_count_comments();

if( ! empty( $comment_status ) ){
foreach ($comment_status as $comment_status_value => $comment_status_name ) {
?>
<div>
<input name="delete_comment_status[]" class="delete_comment_status" id="comment_status_<?php echo $comment_status_value; ?>" type="checkbox" value="<?php echo $comment_status_value; ?>" >
<span for="comment_status_<?php echo $comment_status_value; ?>">
<?php echo $comment_status_name . ' ' . sprintf( __( '( %s Comment(s) )', 'wp-bulk-delete' ), wpbulkdelete()->api->get_comment_count( $comment_status_value ) ); ?>
<?php echo $comment_status_name . ' ' . sprintf( __( '( %s Comment(s) )', 'wp-bulk-delete' ), $get_counts->$comment_status_value ); ?>
</span>
</div>
<?php
Expand Down Expand Up @@ -165,7 +168,7 @@ function wpdb_render_delete_comments_date_interval(){
<div class="header-extra" ></div>
</div>
<svg viewBox="0 0 24 24" fill="none"
xmlns="http://www.w3.org/2000/svg" class="wpbd-caret rotated">
xmlns="http://www.w3.org/2000/svg" class="wpbd-caret">
<path d="M16.59 8.29492L12 12.8749L7.41 8.29492L6 9.70492L12 15.7049L18 9.70492L16.59 8.29492Z" fill="currentColor"></path>
</svg>
</div>
Expand Down
Loading