first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,582 @@
<?php
/**
* WP-Sweep admin.php
*
* @package wp-sweep
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Variables.
$current_page = admin_url( 'tools.php?page=' . plugin_basename( 'wp-sweep/admin.php' ) );
$message = '';
// Sweeping.
if ( ! empty( $_GET['sweep'] ) ) {
if ( check_admin_referer( 'wp_sweep_' . $_GET['sweep'] ) ) {
$message = WPSweep::get_instance()->sweep( $_GET['sweep'] );
}
}
// Database Table Status.
$total_posts = WPSweep::get_instance()->total_count( 'posts' );
$total_postmeta = WPSweep::get_instance()->total_count( 'postmeta' );
$total_comments = WPSweep::get_instance()->total_count( 'comments' );
$total_commentmeta = WPSweep::get_instance()->total_count( 'commentmeta' );
$total_users = WPSweep::get_instance()->total_count( 'users' );
$total_usermeta = WPSweep::get_instance()->total_count( 'usermeta' );
$total_term_relationships = WPSweep::get_instance()->total_count( 'term_relationships' );
$total_term_taxonomy = WPSweep::get_instance()->total_count( 'term_taxonomy' );
$total_terms = WPSweep::get_instance()->total_count( 'terms' );
$total_termmeta = WPSweep::get_instance()->total_count( 'termmeta' );
$total_options = WPSweep::get_instance()->total_count( 'options' );
$total_tables = WPSweep::get_instance()->total_count( 'tables' );
// Count.
$revisions = WPSweep::get_instance()->count( 'revisions' );
$auto_drafts = WPSweep::get_instance()->count( 'auto_drafts' );
$deleted_posts = WPSweep::get_instance()->count( 'deleted_posts' );
$orphan_postmeta = WPSweep::get_instance()->count( 'orphan_postmeta' );
$duplicated_postmeta = WPSweep::get_instance()->count( 'duplicated_postmeta' );
$oembed_postmeta = WPSweep::get_instance()->count( 'oembed_postmeta' );
$unapproved_comments = WPSweep::get_instance()->count( 'unapproved_comments' );
$spam_comments = WPSweep::get_instance()->count( 'spam_comments' );
$deleted_comments = WPSweep::get_instance()->count( 'deleted_comments' );
$orphan_commentmeta = WPSweep::get_instance()->count( 'orphan_commentmeta' );
$duplicated_commentmeta = WPSweep::get_instance()->count( 'duplicated_commentmeta' );
$orphan_usermeta = WPSweep::get_instance()->count( 'orphan_usermeta' );
$duplicated_usermeta = WPSweep::get_instance()->count( 'duplicated_usermeta' );
$orphan_term_relationships = WPSweep::get_instance()->count( 'orphan_term_relationships' );
$unused_terms = WPSweep::get_instance()->count( 'unused_terms' );
$orphan_termmeta = WPSweep::get_instance()->count( 'orphan_termmeta' );
$duplicated_termmeta = WPSweep::get_instance()->count( 'duplicated_termmeta' );
$transient_options = WPSweep::get_instance()->count( 'transient_options' );
?>
<style type="text/css">
.table-sweep thead th {
width: 12%;
}
.table-sweep thead th.col-sweep-details {
width: 56%;
}
.table-sweep thead th.col-sweep-action {
width: 20%;
}
</style>
<div class="wrap">
<h2><?php esc_html_e( 'WP-Sweep', 'wp-sweep' ); ?></h2>
<div class="notice notice-warning">
<p>
<?php /* translators: %1 WP-DBManager Plugin URL, %2 _blank to open new window */ ?>
<?php echo wp_kses_post( sprintf( __( 'Before you do any sweep, please <a href="%1$s" target="%2$s">backup your database</a> first because any sweep done is irreversible.', 'wp-sweep' ), 'https://wordpress.org/plugins/wp-dbmanager/', '_blank' ) ); ?>
</p>
</div>
<p>
<?php /* translators: %s maximum number of results */ ?>
<?php echo esc_html( sprintf( __( 'For performance reasons, only %s items will be shown if you click Details.', 'wp-sweep' ), number_format_i18n( WPSweep::get_instance()->limit_details ) ) ); ?>
</p>
<h3><?php esc_html_e( 'Post Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of posts, %2 is the number of post meta */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention"><span class="sweep-count-type-posts">%1$s</span> Posts</strong> and <strong class="attention"><span class="sweep-count-type-postmeta">%2$s</span> Post Meta</strong>.', 'wp-sweep' ), number_format_i18n( $total_posts ), number_format_i18n( $total_postmeta ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Revisions', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $revisions ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $revisions, $total_posts ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $revisions ) ) : ?>
<button data-action="sweep" data-sweep_name="revisions" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_revisions' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="revisions" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_revisions' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Auto Drafts', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $auto_drafts ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $auto_drafts, $total_posts ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $auto_drafts ) ) : ?>
<button data-action="sweep" data-sweep_name="auto_drafts" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_auto_drafts' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="auto_drafts" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_auto_drafts' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td>
<strong><?php esc_html_e( 'Deleted Posts', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $deleted_posts ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $deleted_posts, $total_posts ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $deleted_posts ) ) : ?>
<button data-action="sweep" data-sweep_name="deleted_posts" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_deleted_posts' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="deleted_posts" data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_deleted_posts' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Orphaned Post Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $orphan_postmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $orphan_postmeta, $total_postmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $orphan_postmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="orphan_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_orphan_postmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="orphan_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_orphan_postmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td>
<strong><?php esc_html_e( 'Duplicated Post Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $duplicated_postmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $duplicated_postmeta, $total_postmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $duplicated_postmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="duplicated_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_duplicated_postmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="duplicated_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_duplicated_postmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'oEmbed Caches In Post Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $oembed_postmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $oembed_postmeta, $total_postmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $oembed_postmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="oembed_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_oembed_postmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="oembed_postmeta" data-sweep_type="postmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_oembed_postmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_post_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'Comment Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of comments, %2 is the number of comment meta */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention"><span class="sweep-count-type-comments">%1$s</span> Comments</strong> and <strong class="attention"><span class="sweep-count-type-commentmeta">%2$s</span> Comment Meta</strong>.', 'wp-sweep' ), number_format_i18n( $total_comments ), number_format_i18n( $total_commentmeta ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Unapproved Comments', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $unapproved_comments ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $unapproved_comments, $total_comments ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $unapproved_comments ) ) : ?>
<button data-action="sweep" data-sweep_name="unapproved_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_unapproved_comments' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="unapproved_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_unapproved_comments' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Spammed Comments', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $spam_comments ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $spam_comments, $total_comments ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $spam_comments ) ) : ?>
<button data-action="sweep" data-sweep_name="spam_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_spam_comments' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="spam_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_spam_comments' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td>
<strong><?php esc_html_e( 'Deleted Comments', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $deleted_comments ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $deleted_comments, $total_comments ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $deleted_comments ) ) : ?>
<button data-action="sweep" data-sweep_name="deleted_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_deleted_comments' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="deleted_comments" data-sweep_type="comments" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_deleted_comments' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Orphaned Comment Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $orphan_commentmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $orphan_commentmeta, $total_commentmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $orphan_commentmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="orphan_commentmeta" data-sweep_type="commentmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_orphan_commentmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="orphan_commentmeta" data-sweep_type="commentmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_orphan_commentmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td>
<strong><?php esc_html_e( 'Duplicated Comment Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $duplicated_commentmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $duplicated_commentmeta, $total_commentmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $duplicated_commentmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="duplicated_commentmeta" data-sweep_type="commentmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_duplicated_commentmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="duplicated_commentmeta" data-sweep_type="commentmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_duplicated_commentmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_comment_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'User Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of users, %2 is the number of user meta */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention"><span class="sweep-count-type-users">%1$s</span> Users</strong> and <strong class="attention"><span class="sweep-count-type-usermeta">%2$s</span> User Meta</strong>.', 'wp-sweep' ), number_format_i18n( $total_users ), number_format_i18n( $total_usermeta ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Orphaned User Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $orphan_usermeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $orphan_usermeta, $total_usermeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $orphan_usermeta ) ) : ?>
<button data-action="sweep" data-sweep_name="orphan_usermeta" data-sweep_type="usermeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_orphan_usermeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="orphan_usermeta" data-sweep_type="usermeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_orphan_usermeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Duplicated User Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $duplicated_usermeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $duplicated_usermeta, $total_usermeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $duplicated_usermeta ) ) : ?>
<button data-action="sweep" data-sweep_name="duplicated_usermeta" data-sweep_type="usermeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_duplicated_usermeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="duplicated_usermeta" data-sweep_type="usermeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_duplicated_usermeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_user_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'Term Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of terms, %2 is the number of term meta */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention "><span class="sweep-count-type-terms">%1$s</span> Terms</strong>, <strong class="attention "><span class="sweep-count-type-termmeta">%2$s</span> Term Meta</strong>, <strong class="attention"><span class="sweep-count-type-term_taxonomy">%3$s</span> Term Taxonomy</strong> and <strong class="attention"><span class="sweep-count-type-term_relationships">%4$s</span> Term Relationships</strong>.', 'wp-sweep' ), number_format_i18n( $total_terms ), number_format_i18n( $total_termmeta ), number_format_i18n( $total_term_taxonomy ), number_format_i18n( $total_term_relationships ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Orphaned Term Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $orphan_termmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $orphan_termmeta, $total_termmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $orphan_termmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="orphan_termmeta" data-sweep_type="termmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_orphan_termmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="orphan_termmeta" data-sweep_type="termmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_orphan_termmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Duplicated Term Meta', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $duplicated_termmeta ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $duplicated_termmeta, $total_termmeta ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $duplicated_termmeta ) ) : ?>
<button data-action="sweep" data-sweep_name="duplicated_termmeta" data-sweep_type="termmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_duplicated_termmeta' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="duplicated_termmeta" data-sweep_type="termmeta" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_duplicated_termmeta' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr>
<td>
<strong><?php esc_html_e( 'Orphaned Term Relationship', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $orphan_term_relationships ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $orphan_term_relationships, $total_term_relationships ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $orphan_term_relationships ) ) : ?>
<button data-action="sweep" data-sweep_name="orphan_term_relationships" data-sweep_type="term_relationships" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_orphan_term_relationships' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="orphan_term_relationships" data-sweep_type="term_relationships" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_orphan_term_relationships' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
<tr class="alternate">
<td>
<strong><?php esc_html_e( 'Unused Terms', 'wp-sweep' ); ?></strong>
<p><?php esc_html_e( 'Note that some unused terms might belong to draft posts that have not been published yet. Only sweep this when you do not have any draft posts.', 'wp-sweep' ); ?></p>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $unused_terms ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $unused_terms, $total_terms ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $unused_terms ) ) : ?>
<button data-action="sweep" data-sweep_name="unused_terms" data-sweep_type="terms" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_unused_terms' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="unused_terms" data-sweep_type="terms" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_unused_terms' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_term_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'Option Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of options */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention"><span class="sweep-count-type-options">%s</span> Options</strong>.', 'wp-sweep' ), number_format_i18n( $total_options ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Transient Options', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $transient_options ) ); ?></span>
</td>
<td>
<span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $transient_options, $total_options ) ); ?></span>
</td>
<td>
<?php if ( ! empty( $transient_options ) ) : ?>
<button data-action="sweep" data-sweep_name="transient_options" data-sweep_type="options" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_transient_options' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="transient_options" data-sweep_type="options" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_transient_options' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_option_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'Database Sweep', 'wp-sweep' ); ?></h3>
<?php /* translators: %1 is the number of database tables */ ?>
<p><?php echo wp_kses_post( sprintf( __( 'There are a total of <strong class="attention"><span class="sweep-count-type-tables">%s</span> Tables</strong>.', 'wp-sweep' ), number_format_i18n( $total_tables ) ) ); ?></p>
<div class="sweep-message"></div>
<table class="widefat table-sweep">
<thead>
<tr>
<th class="col-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></th>
<th class="col-sweep-count"><?php esc_html_e( 'Count', 'wp-sweep' ); ?></th>
<th class="col-sweep-percent"><?php esc_html_e( '% Of', 'wp-sweep' ); ?></th>
<th class="col-sweep-action"><?php esc_html_e( 'Action', 'wp-sweep' ); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong><?php esc_html_e( 'Optimize Tables', 'wp-sweep' ); ?></strong>
<p class="sweep-details" style="display: none;"></p>
</td>
<td>
<span class="sweep-count"><?php echo esc_html( number_format_i18n( $total_tables ) ); ?></span>
</td>
<td>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
</td>
<td>
<?php if ( ! empty( $total_tables ) ) : ?>
<button data-action="sweep" data-sweep_name="optimize_database" data-sweep_type="tables" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_optimize_database' ) ); ?>" class="button button-primary btn-sweep"><?php esc_html_e( 'Sweep', 'wp-sweep' ); ?></button>
<button data-action="sweep_details" data-sweep_name="optimize_database" data-sweep_type="tables" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_details_optimize_database' ) ); ?>" class="button btn-sweep-details"><?php esc_html_e( 'Details', 'wp-sweep' ); ?></button>
<?php else : ?>
<?php esc_html_e( 'N/A', 'wp-sweep' ); ?>
<?php endif; ?>
</td>
</tr>
</tbody>
</table>
<?php do_action( 'wp_sweep_admin_database_sweep' ); ?>
<p>&nbsp;</p>
<h3><?php esc_html_e( 'Sweep All', 'wp-sweep' ); ?></h3>
<p><?php esc_html_e( 'Note that some unused terms might belong to draft posts that have not been published yet. Only sweep all when you do not have any draft posts.', 'wp-sweep' ); ?></p>
<div class="sweep-all">
<p style="text-align: center;">
<button class="button button-primary btn-sweep-all"><?php esc_html_e( 'Sweep All', 'wp-sweep' ); ?></button>
</p>
</div>
</div>

View File

@@ -0,0 +1,26 @@
{
"name": "lesterchan/wp-sweep",
"description": "WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It also optimizes your database tables.",
"keywords": ["wordpress", "plugin", "wp-sweep", "sweep"],
"type": "wordpress-plugin",
"homepage": "https://wordpress.org/plugins/wp-sweep/",
"authors": [
{
"name": "Lester Chan",
"email": "lesterchan@gmail.com"
}
],
"license" : "GPL-2.0+",
"repositories": [
{
"type": "vcs",
"url": "git@github.com:lesterchan/WordPress-Coding-Standards.git"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"wp-coding-standards/wpcs": "dev-develop"
}
}

136
wp-content/plugins/wp-sweep/composer.lock generated Normal file
View File

@@ -0,0 +1,136 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9000cd3a445ab002c25944897a08aff5",
"packages": [],
"packages-dev": [
{
"name": "squizlabs/php_codesniffer",
"version": "3.5.4",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "dceec07328401de6211037abbb18bda423677e26"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/dceec07328401de6211037abbb18bda423677e26",
"reference": "dceec07328401de6211037abbb18bda423677e26",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcs",
"bin/phpcbf"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "https://github.com/squizlabs/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2020-01-30T22:20:29+00:00"
},
{
"name": "wp-coding-standards/wpcs",
"version": "dev-develop",
"source": {
"type": "git",
"url": "https://github.com/lesterchan/WordPress-Coding-Standards.git",
"reference": "3c1967b0b56e4ea0f25beee6d0650b977edd3f5a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lesterchan/WordPress-Coding-Standards/zipball/3c1967b0b56e4ea0f25beee6d0650b977edd3f5a",
"reference": "3c1967b0b56e4ea0f25beee6d0650b977edd3f5a",
"shasum": ""
},
"require": {
"php": ">=5.4",
"squizlabs/php_codesniffer": "^3.3.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"phpcompatibility/php-compatibility": "^9.0",
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically."
},
"type": "phpcodesniffer-standard",
"scripts": {
"check-cs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs"
],
"fix-cs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
],
"install-codestandards": [
"Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
],
"run-tests": [
"@php ./vendor/phpunit/phpunit/phpunit --filter WordPress --bootstrap=\"./vendor/squizlabs/php_codesniffer/tests/bootstrap.php\" ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
]
},
"license": [
"MIT"
],
"authors": [
{
"name": "Contributors",
"homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors"
}
],
"description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions",
"keywords": [
"WordPress",
"phpcs",
"standards"
],
"support": {
"issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues",
"wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki",
"source": "https://github.com/WordPress/WordPress-Coding-Standards"
},
"time": "2020-01-16T07:14:21+00:00"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"wp-coding-standards/wpcs": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=5.4.0"
},
"platform-dev": []
}

View File

@@ -0,0 +1,194 @@
<?php
/**
* WP-Sweep WP-API
*
* @package wp-sweep
*/
/**
* Class WPSweep_Api
*/
class WPSweep_Api {
/**
* WP-Sweep WP Rest API namespace
*
* @var string
*/
private $namespace = 'sweep/v1';
/**
* List of sweeps
*
* @var array
*/
private $sweeps = array(
'revisions',
'auto_drafts',
'deleted_posts',
'unapproved_comments',
'spam_comments',
'deleted_comments',
'transient_options',
'orphan_postmeta',
'orphan_commentmeta',
'orphan_usermeta',
'orphan_termmeta',
'orphan_term_relationships',
'unused_terms',
'duplicated_postmeta',
'duplicated_commentmeta',
'duplicated_usermeta',
'duplicated_termmeta',
'optimize_database',
'oembed_postmeta',
);
/**
* Register WP-Sweep API Routes
*
* @since 1.1.0
*
* @access public
* @return void
*/
public function __construct() {
add_action(
'rest_api_init', function() {
register_rest_route(
$this->namespace, 'count/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'count' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
register_rest_route(
$this->namespace, 'details/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'details' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
register_rest_route(
$this->namespace, 'sweep/(?P<name>\w+)', array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'sweep' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
}
);
}
/**
* Sweep item count
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function count( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$count = (int) $sweep->count( $params['name'] );
return new WP_REST_Response(
array(
'name' => $params['name'],
'count' => $count,
)
);
}
/**
* Sweep details
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function details( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$details = $sweep->details( $params['name'] );
return new WP_REST_Response(
array(
'name' => $params['name'],
'count' => count( $details ),
'data' => $details,
)
);
}
/**
* Lets do the sweeping
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function sweep( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$results = $sweep->sweep( $params['name'] );
return new WP_REST_Response(
array(
'success' => ! empty( $results ),
'name' => $params['name'],
'message' => empty( $results ) ? __( 'No items left to sweep.', 'wp-sweep' ) : $results,
)
);
}
/**
* Check whether a sweep is valid
*
* @since 1.1.0
*
* @access public
* @param string $name Sweep name.
* @return bool Is the sweep name valid?
*/
public function is_sweep_name_valid( $name ) {
return in_array( $name, $this->sweeps, true );
}
/**
* Check whether the function is allowed to be run. Must have either capabilities to enact action, or a valid nonce.
*
* @since 1.1.0
*
* @access public
* @return bool Does the user has access to sweep?
*/
public function permission_check() {
return current_user_can( 'activate_plugins' );
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* WP-Sweep WP-CLI
*
* @package wp-sweep
*/
/**
* Class WPSweep_Command
*/
class WPSweep_Command extends WP_CLI_Command {
/**
* Clean up unused, orphaned and duplicated data in your WordPress
*
* ## OPTIONS
*
* [--all]
* Sweep all the orphaned data at once.
*
* Name of the items selected individually
* Available Items =
* revisions
* auto_drafts
* deleted_posts
* unapproved_comments
* spam_comments
* deleted_comments
* transient_options
* orphan_postmeta
* orphan_commentmeta
* orphan_usermeta
* orphan_termmeta
* orphan_term_relationships
* unused_terms
* duplicated_postmeta
* duplicated_commentmeta
* duplicated_usermeta
* duplicated_termmeta
* optimize_database
* oembed_postmeta
*
* ## EXAMPLES
*
* 1. wp sweep --all
* - Run Sweep for all the items.
* 2. wp sweep revisions
* - Sweep only Revision
* 3. wp sweep revisions auto_drafts deleted_posts unapproved_comments spam_comments deleted_comments transient_options orphan_postmeta orphan_commentmeta orphan_usermeta orphan_termmeta orphan_term_relationships unused_terms duplicated_postmeta duplicated_commentmeta duplicated_usermeta duplicated_termmeta optimize_database oembed_postmet
* - Sweep the selected items
*
* @since 1.0.8
*
* @access public
*
* @param array $args array Arguments passed to command. Generally unused.
* @param array $assoc_args Parameters passed to command to be passed to callback.
* @return void
*/
public function __invoke( $args, $assoc_args ) {
$items = array();
$default_items = array(
'0' => 'revisions',
'1' => 'auto_drafts',
'2' => 'deleted_posts',
'3' => 'unapproved_comments',
'4' => 'spam_comments',
'5' => 'deleted_comments',
'6' => 'transient_options',
'7' => 'orphan_postmeta',
'8' => 'orphan_commentmeta',
'9' => 'orphan_usermeta',
'10' => 'orphan_termmeta',
'11' => 'orphan_term_relationships',
'12' => 'unused_terms',
'13' => 'duplicated_postmeta',
'14' => 'duplicated_commentmeta',
'15' => 'duplicated_usermeta',
'16' => 'duplicated_termmeta',
'17' => 'optimize_database',
'18' => 'oembed_postmeta',
);
if ( isset( $assoc_args['all'] ) && true === $assoc_args['all'] ) {
$this->run_sweep( $default_items );
WP_CLI::success( 'Sweep Complete' );
return;
} else {
foreach ( $default_items as $key => $item ) {
if ( in_array( $item, $args, true ) ) {
array_push( $items, $item );
}
}
$this->run_sweep( $items );
WP_CLI::success( 'Sweep Complete!' );
return;
}
}
/**
* Run WP-Sweep
*
* @since 1.0.8
*
* @access public
*
* @param array $items Sweep items.
* @return void
*/
public function run_sweep( $items ) {
$sweep = new WPSweep();
foreach ( $items as $key => $value ) {
$count = $sweep->count( $value );
if ( 0 !== $count && '0' !== $count ) {
$message = $sweep->sweep( $value );
WP_CLI::success( $message );
}
}
}
}

View File

@@ -0,0 +1,8 @@
<?php
/**
* WP-Sweep index.php
*
* @package wp-sweep
*/
// Silence is golden.

View File

@@ -0,0 +1,95 @@
(function ($) {
'use strict';
$(function () {
var $body = $( 'body' ),
sweep = function($node) {
var $row = $node.parents( 'tr' );
// Add Active
$body.addClass( 'sweep-active' );
// Add Disabled
$node.prop( 'disabled', true ).text( wp_sweep.text_sweeping );
return $.get(ajaxurl, { action: $node.data( 'action' ), sweep_name: $node.data( 'sweep_name' ), sweep_type: $node.data( 'sweep_type' ), '_wpnonce': $node.data( 'nonce' ) }, function(data) {
if (data.success) {
var count = parseInt( data.data.count, 10 );
// Count Col
$( '.sweep-count', $row ).text( count.toLocaleString() );
// % Of Col
$( '.sweep-percentage', $row ).text( data.data.percentage );
// Action Col
if (count === 0) {
$node.parent( 'td' ).html( wp_sweep.text_na );
}
// Stats
$.each(data.data.stats, function(key, value) {
$( '.sweep-count-type-' + key ).text( parseInt( value, 10 ).toLocaleString() );
});
// Message
$row.parents( '.table-sweep' ).prev( '.sweep-message' ).html( '<div class="updated"><p>' + data.data.sweep + '</p></div>' );
// Hide Sweep Details
$( '.sweep-details', $row ).html( '' ).hide();
// Remove Active
$body.removeClass( 'sweep-active' );
// Remove Disabled
$node.prop( 'disabled', false ).text( wp_sweep.text_sweep );
}
});
};
$( '.btn-sweep' ).click(function(evt) {
evt.preventDefault();
sweep( $( this ) );
});
$( '.btn-sweep-details' ).click(function(evt) {
evt.preventDefault();
var $node = $( this );
$.get(ajaxurl, { action: $node.data( 'action' ), sweep_name: $node.data( 'sweep_name' ), sweep_type: $node.data( 'sweep_type' ), '_wpnonce': $node.data( 'nonce' ) }, function(data) {
if (data.success) {
if (data.data.length > 0) {
var html = '';
$.each(data.data, function(i, n) {
html += '<li>' + n + '</li>';
});
$( '.sweep-details', $node.parents( 'tr' ) ).html( '<ol>' + html + '</ol>' ).show();
}
}
});
});
$( '.btn-sweep-all' ).click(function(evt) {
evt.preventDefault();
var $node = $( this ), $btn_sweep = $( '.btn-sweep' ), sweep_all;
$node.prop( 'disabled', true ).text( wp_sweep.text_sweeping );
sweep_all = $btn_sweep.toArray().reduce(function(current, next) {
return current.then(function() {
return sweep( $( next ) );
});
}, $().promise());
sweep_all.done(function() {
// Remove Active
$body.removeClass( 'sweep-active' );
// Remove Disabled
$node.prop( 'disabled', false ).text( wp_sweep.text_sweep_all );
});
});
/*
Page closing confirmation
https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/beforeunload
*/
$( window ).on('beforeunload', function (e) {
if ($body.hasClass( 'sweep-active' )) {
(e || window.event).returnValue = wp_sweep.text_close_warning; // Gecko and Trident
return wp_sweep.text_close_warning; // Gecko and WebKit
}
});
});
})(jQuery);

View File

@@ -0,0 +1,4 @@
(function(b){b(function(){var f=b("body"),g=function(a){var c=a.parents("tr");f.addClass("sweep-active");a.prop("disabled",!0).text(wp_sweep.text_sweeping);return b.get(ajaxurl,{action:a.data("action"),sweep_name:a.data("sweep_name"),sweep_type:a.data("sweep_type"),_wpnonce:a.data("nonce")},function(e){if(e.success){var d=parseInt(e.data.count,10);b(".sweep-count",c).text(d.toLocaleString());b(".sweep-percentage",c).text(e.data.percentage);0===d&&a.parent("td").html(wp_sweep.text_na);b.each(e.data.stats,
function(a,c){b(".sweep-count-type-"+a).text(parseInt(c,10).toLocaleString())});c.parents(".table-sweep").prev(".sweep-message").html('<div class="updated"><p>'+e.data.sweep+"</p></div>");b(".sweep-details",c).html("").hide();f.removeClass("sweep-active");a.prop("disabled",!1).text(wp_sweep.text_sweep)}})};b(".btn-sweep").click(function(a){a.preventDefault();g(b(this))});b(".btn-sweep-details").click(function(a){a.preventDefault();var c=b(this);b.get(ajaxurl,{action:c.data("action"),sweep_name:c.data("sweep_name"),
sweep_type:c.data("sweep_type"),_wpnonce:c.data("nonce")},function(a){if(a.success&&0<a.data.length){var d="";b.each(a.data,function(a,b){d+="<li>"+b+"</li>"});b(".sweep-details",c.parents("tr")).html("<ol>"+d+"</ol>").show()}})});b(".btn-sweep-all").click(function(a){a.preventDefault();var c=b(this);a=b(".btn-sweep");c.prop("disabled",!0).text(wp_sweep.text_sweeping);a.toArray().reduce(function(a,c){return a.then(function(){return g(b(c))})},b().promise()).done(function(){f.removeClass("sweep-active");
c.prop("disabled",!1).text(wp_sweep.text_sweep_all)})});b(window).on("beforeunload",function(a){if(f.hasClass("sweep-active"))return(a||window.event).returnValue=wp_sweep.text_close_warning})})})(jQuery);

View File

@@ -0,0 +1,193 @@
# WP-Sweep
Contributors: GamerZ
Donate link: https://lesterchan.net/site/donation/
Tags: sweep, clean, cleanup, clean up, optimize, orphan, unused, duplicated, posts, post meta, comments, comment meta, users, user meta, terms, term meta, term relationships, revisions, auto drafts, transient, database, tables, oembed
Requires at least: 4.6
Tested up to: 5.8
Stable tag: 1.1.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It also optimizes your database tables.
## Description
This plugin cleans up:
* Revisions
* Auto drafts
* Deleted comments
* Unapproved comments
* Spammed comments
* Deleted comments
* Orphaned post meta
* Orphaned comment meta
* Orphaned user meta
* Orphaned term meta
* Orphan term relationships
* Unused terms
* Duplicated post meta
* Duplicated comment meta
* Duplicated user meta
* Duplicated term meta
* Transient options
* Optimizes database tables
* oEmbed caches in post meta
This plugin uses proper WordPress delete functions as much as possible instead of running direct delete MySQL queries.
Following delete functions are used:
* wp_delete_post_revision()
* wp_delete_post()
* wp_delete_comment()
* delete_post_meta()
* delete_comment_meta()
* delete_user_meta()
* delete_term_meta()
* wp_remove_object_terms()
* wp_delete_term()
* delete_transient()
* delete_site_transient()
WP-Sweep WP REST API Endpoints
* `GET /wp-json/sweep/v1/count/<Name>`. Get the number of items that we will be sweeping.
* `GET /wp-json/sweep/v1/details/<Name>`. Get the details of the items that we will be sweeping.
* `DELETE /wp-json/sweep/v1/sweep/<Name>`. Runs sweep for that particular item.
WP-Sweep WP-CLI Commands
* `wp sweep --all`. Runs sweep for all items.
* `wp sweep <Name>`. Runs sweep for that particular item.
* `wp sweep <Name1> <Name2>`. Run sweep for the selected items.
WP-Sweep Available Items:
* revisions
* auto_drafts
* deleted_posts
* unapproved_comments
* spam_comments
* deleted_comments
* transient_options
* orphan_postmeta
* orphan_commentmeta
* orphan_usermeta
* orphan_termmeta
* orphan_term_relationships
* unused_terms
* duplicated_postmeta
* duplicated_commentmeta
* duplicated_usermeta
* duplicated_termmeta
* optimize_database
* oembed_postmeta
WP-Sweep is not compatible with the following plugins:
* [Asgaros Forum](https://wordpress.org/plugins/asgaros-forum/)
* [Custom Fonts](https://wordpress.org/plugins/custom-fonts/)
* [Elementor Popup Builder](https://elementor.com/features/popup-builder/)
* [MailPress](https://wordpress.org/plugins/mailpress/)
* [Meta Slider](https://wordpress.org/support/plugin/ml-slider/)
* [Polylang](https://wordpress.org/plugins/polylang/)
* [Slider Revolution](https://revolution.themepunch.com/)
* [Viba Portfolio](https://codecanyon.net/item/viba-portfolio-wordpress-plugin/9561599)
* [WPML](https://wpml.org/)
### Build Status
[![Build Status](https://travis-ci.org/lesterchan/wp-sweep.svg?branch=master)](https://travis-ci.org/lesterchan/wp-sweep) [![Code Climate](https://codeclimate.com/github/lesterchan/wp-sweep/badges/gpa.svg)](https://codeclimate.com/github/lesterchan/wp-sweep) [![Issue Count](https://codeclimate.com/github/lesterchan/wp-sweep/badges/issue_count.svg)](https://codeclimate.com/github/lesterchan/wp-sweep)
### Development
* [https://github.com/lesterchan/wp-sweep](https://github.com/lesterchan/wp-sweep "https://github.com/lesterchan/wp-sweep")
### Credits
* Plugin icon by [Freepik](http://www.freepik.com) from [Flaticon](http://www.flaticon.com)
### Donations
I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
## Changelog
### 1.1.15
* NEW: Bump to WordPress 5.8
### 1.1.4
* FIXED: Replaced %\_transient\_% with %\\\_transient\\\_%. Escape _ in MySQL if not it is being used as a wildcard character. Props @janrenn.
### 1.1.3
* FIXED: Changed permissions check to `activate_plugins` because `update_plugins` will return false when DISALLOW_FILE_MODS=true.
### 1.1.2
* NEW: Changed permission check to `update_plugins` for better MultiSite compatibility.
* NEW: Bump min PHP version to 5.6.
### 1.1.1
* NEW: `wp_sweep_excluded_termids` filter.
### 1.1.0
* NEW: Added WP Rest API Endpoint support, `sweep/v1/count/<Name>`, `sweep/v1/details/<Name>`, and `sweep/v1/sweep/<Name>`
* FIXED: Follow as close as possible to WordPress Coding Standards
### 1.0.12
* NEW: Bump to WordPress 4.9
* NEW: Update README to incompatible plugins
### 1.0.10
* FIXED: Invalid plugin head 'This plugin has an invalid header.'
### 1.0.9
* NEW: Support for Codeclimate
* FIXES: Uses `get_sites()` on WordPress 4.6. This should fix deprecated notices.
* FIXES: Fixes translation placeholder count. Props @pedro-mendonca.
* FIXES: Use `manage_options` capability as it conflicts with Admin Menu Editor on multisite installs. Props @EusebiuOprinoiu.
### 1.0.8
* NEW: Added wp_sweep_excluded_taxonomies filter to allow more than just link_category taxonomy
* NEW: Support for WP-CLI `wp sweep`
### 1.0.7
* FIXES: Use custom query to delete Orphaned Term Relationship if wp_remove_object_terms() fails
### 1.0.6
* NEW: Delete 'languages' folder from the plugin
* NEW: Use translate.wordpress.org to translate the plugin
* FIXED: Works only with WordPress 4.4 because of new term meta
### 1.0.5
* FIXED: apply_filters() wrong arguments
### 1.0.4
* NEW: oEmbed caches in post meta Sweep
* NEW: Add POT file for translators
### 1.0.3
* NEW: AJAX Sweep All
* NEW: AJAX Sweeping
* NEW: View details of sweep
* NEW: Optimize DB sweep
* NEW: User hint and confirmation. Props @SiamKreative
* FIXED: Division by zero. Pros @barisunver
### 1.0.2
* FIXED: Use term_id for wp_remove_object_terms()
* FIXED: number_format_i18n() issues after sweeping
### 1.0.1
* NEW: Moved plugin location to WP-Admin -> Tools -> Sweep
* NEW: Add Deleted Post Sweep
* FIXED: Use forced_delete for wp_delete_post() and wp_delete_comment();
* FIXED: If orphaned meta has an object id of 0, use SQL query to delete
### 1.0.0
* Initial release
## Installation
1. Upload `wp-sweep` folder to the `/wp-content/plugins/` directory
2. Activate the `WP-Sweep` plugin through the 'Plugins' menu in WordPress
3. You can access `WP-Sweep` via `WP-Admin -> Tools -> Sweep`
## Screenshots
1. WP-Sweep Administrator Page (Before Sweeping)
2. WP-Sweep Administrator Page (Swept)
## Frequently Asked Questions
Coming soon ...
## Upgrade Notice
N/A

View File

@@ -0,0 +1,34 @@
<?php
/**
* WP-Sweep uninstall.php
*
* @package wp-sweep
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
if ( is_multisite() ) {
$ms_sites = get_sites();
if ( 0 < count( $ms_sites ) ) {
foreach ( $ms_sites as $ms_site ) {
switch_to_blog( $ms_site->blog_id );
plugin_uninstalled();
}
}
restore_current_blog();
} else {
plugin_uninstalled();
}
/**
* Delete plugin table when uninstalled
*
* @access public
* @return void
*/
function plugin_uninstalled() {
}

View File

@@ -0,0 +1,981 @@
<?php
/**
* WP-Sweep wp-sweep.php
*
* @package wp-sweep
*/
/*
Plugin Name: WP-Sweep
Plugin URI: https://lesterchan.net/portfolio/programming/php/
Description: WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It cleans up revisions, auto drafts, unapproved comments, spam comments, trashed comments, orphan post meta, orphan comment meta, orphan user meta, orphan term relationships, unused terms, duplicated post meta, duplicated comment meta, duplicated user meta and transient options. It also optimizes your database tables.
Version: 1.1.5
Author: Lester 'GaMerZ' Chan
Author URI: https://lesterchan.net
Text Domain: wp-sweep
License: GPL2
*/
/*
Copyright 2021 Lester Chan (email : lesterchan@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* WP-Sweep version
*
* @since 1.0.0
*/
define( 'WP_SWEEP_VERSION', '1.1.5' );
/**
* WP Rest API
*/
require __DIR__ . '/inc/class-wpsweep-api.php';
new WPSweep_Api();
/**
* WP-Sweep class
*
* @since 1.0.0
*/
class WPSweep {
/**
* Limit the number of items to show for sweep details
*
* @since 1.0.3
*
* @access public
* @var int
*/
public $limit_details = 500;
/**
* Static instance
*
* @since 1.0.0
*
* @access private
* @var WPSweep $instance
*/
private static $instance;
/**
* Constructor method
*
* @since 1.0.0
*
* @access public
*/
public function __construct() {
// Add Plugin Hooks.
add_action( 'plugins_loaded', array( $this, 'add_hooks' ) );
// Load Translation.
load_plugin_textdomain( 'wp-sweep' );
// Plugin Activation/Deactivation.
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'plugin_deactivation' ) );
}
/**
* Initializes the plugin object and returns its instance
*
* @since 1.0.0
*
* @access public
* @return object The plugin object instance
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Init this plugin
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function init() {
// include class for WP CLI command.
if ( defined( 'WP_CLI' ) ) {
require __DIR__ . '/inc/class-wpsweep-command.php';
WP_CLI::add_command( 'sweep', 'WPSweep_Command' );
}
}
/**
* Adds all the plugin hooks
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function add_hooks() {
// Actions.
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_ajax_sweep_details', array( $this, 'ajax_sweep_details' ) );
add_action( 'wp_ajax_sweep', array( $this, 'ajax_sweep' ) );
}
/**
* Enqueue JS/CSS files used for admin
*
* @since 1.0.3
*
* @access public
* @param string $hook Page hook.
* @return void
*/
public function admin_enqueue_scripts( $hook ) {
if ( 'wp-sweep/admin.php' !== $hook ) {
return;
}
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
wp_enqueue_script( 'wp-sweep', plugins_url( 'wp-sweep/js/wp-sweep.js' ), array( 'jquery' ), WP_SWEEP_VERSION, true );
} else {
wp_enqueue_script( 'wp-sweep', plugins_url( 'wp-sweep/js/wp-sweep.min.js' ), array( 'jquery' ), WP_SWEEP_VERSION, true );
}
wp_localize_script(
'wp-sweep', 'wp_sweep', array(
'text_close_warning' => __( 'Sweeping is in progress. If you leave now, the process won\'t be completed.', 'wp-sweep' ),
'text_sweep' => __( 'Sweep', 'wp-sweep' ),
'text_sweep_all' => __( 'Sweep All', 'wp-sweep' ),
'text_sweeping' => __( 'Sweeping...', 'wp-sweep' ),
'text_na' => __( 'N/A', 'wp-sweep' ),
)
);
}
/**
* Admin menu
*
* @since 1.0.3
*
* @access public
* @return void
*/
public function admin_menu() {
add_management_page( _x( 'Sweep', 'Page title', 'wp-sweep' ), _x( 'Sweep', 'Menu title', 'wp-sweep' ), 'activate_plugins', 'wp-sweep/admin.php' );
}
/**
* Sweep Details loaded via AJAX
*
* @since 1.0.3
*
* @access public
* @return void
*/
public function ajax_sweep_details() {
if ( ! empty( $_GET['action'] )
&& ! empty( $_GET['sweep_name'] )
&& ! empty( $_GET['sweep_type'] )
) {
// Verify Referer.
if ( ! check_admin_referer( 'wp_sweep_details_' . $_GET['sweep_name'] ) ) {
wp_send_json_error(
array(
'error' => __( 'Failed to verify referrer.', 'wp-sweep' ),
)
);
} elseif ( 'sweep_details' === $_GET['action'] ) {
wp_send_json_success( $this->details( $_GET['sweep_name'] ) );
}
}
}
/**
* Sweep via AJAX
*
* @since 1.0.3
*
* @access public
* @return void
*/
public function ajax_sweep() {
if ( ! empty( $_GET['action'] )
&& ! empty( $_GET['sweep_name'] )
&& ! empty( $_GET['sweep_type'] )
) {
// Verify Referer.
if ( ! check_admin_referer( 'wp_sweep_' . $_GET['sweep_name'] ) ) {
wp_send_json_error(
array(
'error' => __( 'Failed to verify referrer.', 'wp-sweep' ),
)
);
} elseif ( 'sweep' === $_GET['action'] ) {
$sweep = $this->sweep( $_GET['sweep_name'] );
$count = $this->count( $_GET['sweep_name'] );
$total_count = $this->total_count( $_GET['sweep_type'] );
$total_stats = array();
switch ( $_GET['sweep_type'] ) {
case 'posts':
case 'postmeta':
$total_stats = array(
'posts' => $this->total_count( 'posts' ),
'postmeta' => $this->total_count( 'postmeta' ),
);
break;
case 'comments':
case 'commentmeta':
$total_stats = array(
'comments' => $this->total_count( 'comments' ),
'commentmeta' => $this->total_count( 'commentmeta' ),
);
break;
case 'users':
case 'usermeta':
$total_stats = array(
'users' => $this->total_count( 'users' ),
'usermeta' => $this->total_count( 'usermeta' ),
);
break;
case 'term_relationships':
case 'term_taxonomy':
case 'terms':
case 'termmeta':
$total_stats = array(
'term_relationships' => $this->total_count( 'term_relationships' ),
'term_taxonomy' => $this->total_count( 'term_taxonomy' ),
'terms' => $this->total_count( 'terms' ),
'termmeta' => $this->total_count( 'termmeta' ),
);
break;
case 'options':
$total_stats = array( 'options' => $this->total_count( 'options' ) );
break;
case 'tables':
$total_stats = array( 'tables' => $this->total_count( 'tables' ) );
break;
}
wp_send_json_success(
array(
'sweep' => $sweep,
'count' => $count,
'total' => $total_count,
'percentage' => $this->format_percentage( $count, $total_count ),
'stats' => $total_stats,
)
);
}
}
}
/**
* Count the number of total items belonging to each sweep
*
* @since 1.0.0
*
* @access public
* @param string $name Sweep name.
* @return int Number of items belonging to each sweep
*/
public function total_count( $name ) {
global $wpdb;
$count = 0;
switch ( $name ) {
case 'posts':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts" );
break;
case 'postmeta':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->postmeta" );
break;
case 'comments':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments" );
break;
case 'commentmeta':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->commentmeta" );
break;
case 'users':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
break;
case 'usermeta':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta" );
break;
case 'term_relationships':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_relationships" );
break;
case 'term_taxonomy':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->term_taxonomy" );
break;
case 'terms':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->terms" );
break;
case 'termmeta':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->termmeta" );
break;
case 'options':
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->options" );
break;
case 'tables':
$count = count( $wpdb->get_col( 'SHOW TABLES' ) );
break;
}
return apply_filters( 'wp_sweep_total_count', $count, $name );
}
/**
* Count the number of items belonging to each sweep
*
* @since 1.0.0
*
* @access public
* @param string $name Sweep name.
* @return int Number of items belonging to each sweep
*/
public function count( $name ) {
global $wpdb;
$count = 0;
switch ( $name ) {
case 'revisions':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s", 'revision' ) );
break;
case 'auto_drafts':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status = %s", 'auto-draft' ) );
break;
case 'deleted_posts':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status = %s", 'trash' ) );
break;
case 'unapproved_comments':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = %s", '0' ) );
break;
case 'spam_comments':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = %s", 'spam' ) );
break;
case 'deleted_comments':
$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 'transient_options':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(option_id) FROM $wpdb->options WHERE option_name LIKE(%s)", '%\_transient\_%' ) );
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 'orphan_term_relationships':
$orphan_term_relationships_sql = implode( "','", array_map( 'esc_sql', $this->get_excluded_taxonomies() ) );
$count = $wpdb->get_var( "SELECT COUNT(object_id) 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 NOT IN ('$orphan_term_relationships_sql') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" ); // phpcs:ignore
break;
case 'unused_terms':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(t.term_id) FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ')', 0 ) ); // phpcs:ignore
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;
case 'optimize_database':
$count = count( $wpdb->get_col( 'SHOW TABLES' ) );
break;
case 'oembed_postmeta':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_id) FROM $wpdb->postmeta WHERE meta_key LIKE(%s)", '%_oembed_%' ) );
break;
}
return apply_filters( 'wp_sweep_count', $count, $name );
}
/**
* Return more details about a sweep
*
* @since 1.0.3
*
* @access public
* @param string $name Sweep name.
* @return array Details of items belonging to each sweep
*/
public function details( $name ) {
global $wpdb;
$details = array();
switch ( $name ) {
case 'revisions':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE post_type = %s LIMIT %d", 'revision', $this->limit_details ) );
break;
case 'auto_drafts':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE post_status = %s LIMIT %d", 'auto-draft', $this->limit_details ) );
break;
case 'deleted_posts':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE post_status = %s LIMIT %d", 'trash', $this->limit_details ) );
break;
case 'unapproved_comments':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT comment_author FROM $wpdb->comments WHERE comment_approved = %s LIMIT %d", '0', $this->limit_details ) );
break;
case 'spam_comments':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT comment_author FROM $wpdb->comments WHERE comment_approved = %s LIMIT %d", 'spam', $this->limit_details ) );
break;
case 'deleted_comments':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT comment_author FROM $wpdb->comments WHERE (comment_approved = %s OR comment_approved = %s) LIMIT %d", 'trash', 'post-trashed', $this->limit_details ) );
break;
case 'transient_options':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE(%s) LIMIT %d", '%\_transient\_%', $this->limit_details ) );
break;
case 'orphan_postmeta':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE post_id NOT IN (SELECT ID FROM $wpdb->posts) LIMIT %d", $this->limit_details ) );
break;
case 'orphan_commentmeta':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_ID FROM $wpdb->comments) LIMIT %d", $this->limit_details ) );
break;
case 'orphan_usermeta':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE user_id NOT IN (SELECT ID FROM $wpdb->users) LIMIT %d", $this->limit_details ) );
break;
case 'orphan_termmeta':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->termmeta WHERE term_id NOT IN (SELECT term_id FROM $wpdb->terms) LIMIT %d", $this->limit_details ) );
break;
case 'orphan_term_relationships':
$orphan_term_relationships_sql = implode( "','", array_map( 'esc_sql', $this->get_excluded_taxonomies() ) );
$details = $wpdb->get_col( $wpdb->prepare( "SELECT tt.taxonomy 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 NOT IN ('$orphan_term_relationships_sql') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts) LIMIT %d", $this->limit_details ) ); // phpcs:ignore
break;
case 'unused_terms':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ') LIMIT %d', 0, $this->limit_details ) ); // phpcs:ignore
break;
case 'duplicated_postmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(meta_id) AS count, meta_key FROM $wpdb->postmeta GROUP BY post_id, meta_key, meta_value HAVING count > %d LIMIT %d", 1, $this->limit_details ) );
$details = array();
if ( $query ) {
foreach ( $query as $meta ) {
$details[] = $meta->meta_key;
}
}
break;
case 'duplicated_commentmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(meta_id) AS count, meta_key FROM $wpdb->commentmeta GROUP BY comment_id, meta_key, meta_value HAVING count > %d LIMIT %d", 1, $this->limit_details ) );
$details = array();
if ( $query ) {
foreach ( $query as $meta ) {
$details[] = $meta->meta_key;
}
}
break;
case 'duplicated_usermeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(umeta_id) AS count, meta_key FROM $wpdb->usermeta GROUP BY user_id, meta_key, meta_value HAVING count > %d LIMIT %d", 1, $this->limit_details ) );
$details = array();
if ( $query ) {
foreach ( $query as $meta ) {
$details[] = $meta->meta_key;
}
}
break;
case 'duplicated_termmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(meta_id) AS count, meta_key FROM $wpdb->termmeta GROUP BY term_id, meta_key, meta_value HAVING count > %d LIMIT %d", 1, $this->limit_details ) );
$details = array();
if ( $query ) {
foreach ( $query as $meta ) {
$details[] = $meta->meta_key;
}
}
break;
case 'optimize_database':
$details = $wpdb->get_col( 'SHOW TABLES' );
break;
case 'oembed_postmeta':
$details = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM $wpdb->postmeta WHERE meta_key LIKE(%s) LIMIT %d", '%_oembed_%', $this->limit_details ) );
break;
}
return apply_filters( 'wp_sweep_details', $details, $name );
}
/**
* Does the sweeping/cleaning up
*
* @since 1.0.0
*
* @access public
* @param string $name Sweep name.
* @return string Processed message
*/
public function sweep( $name ) {
global $wpdb;
$message = '';
switch ( $name ) {
case 'revisions':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s", 'revision' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_post_revision( (int) $id );
}
// translators: %s is Revisions count.
$message = sprintf( __( '%s Revisions Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'auto_drafts':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = %s", 'auto-draft' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_post( (int) $id, true );
}
// translators: %s is the Auto Drafts count.
$message = sprintf( __( '%s Auto Drafts Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'deleted_posts':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = %s", 'trash' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_post( $id, true );
}
// translators: %s is the Deleted Posts count.
$message = sprintf( __( '%s Deleted Posts Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'unapproved_comments':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = %s", '0' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_comment( (int) $id, true );
}
// translators: %s is the Unapproved Comments count.
$message = sprintf( __( '%s Unapproved Comments Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'spam_comments':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = %s", 'spam' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_comment( (int) $id, true );
}
// translators: %s is the Spam Comments count.
$message = sprintf( __( '%s Spam Comments Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'deleted_comments':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE (comment_approved = %s OR comment_approved = %s)", 'trash', 'post-trashed' ) );
if ( $query ) {
foreach ( $query as $id ) {
wp_delete_comment( (int) $id, true );
}
// translators: %s is the Trash Comments count.
$message = sprintf( __( '%s Trash Comments Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'transient_options':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE(%s)", '%\_transient\_%' ) );
if ( $query ) {
foreach ( $query as $option_name ) {
if ( strpos( $option_name, '_site_transient_' ) !== false ) {
delete_site_transient( str_replace( '_site_transient_', '', $option_name ) );
} else {
delete_transient( str_replace( '_transient_', '', $option_name ) );
}
}
// translators: %s is the Transient Options count.
$message = sprintf( __( '%s Transient Options Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$post_id = (int) $meta->post_id;
if ( 0 === $post_id ) {
$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 );
}
}
// translators: %s is the Orphaned Post Meta count.
$message = sprintf( __( '%s Orphaned Post Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$comment_id = (int) $meta->comment_id;
if ( 0 === $comment_id ) {
$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 );
}
}
// translators: %s is the Orphaned Comment Meta count.
$message = sprintf( __( '%s Orphaned Comment Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$user_id = (int) $meta->user_id;
if ( 0 === $user_id ) {
$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 );
}
}
// translators: %s is the Orphaned User Meta count.
$message = sprintf( __( '%s Orphaned User Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$term_id = (int) $meta->term_id;
if ( 0 === $term_id ) {
$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 );
}
}
// translators: %s is the Orphaned Term Meta count.
$message = sprintf( __( '%s Orphaned Term Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'orphan_term_relationships':
$query = $wpdb->get_results( "SELECT tr.object_id, tr.term_taxonomy_id, tt.term_id, tt.taxonomy 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 NOT IN ('" . implode( '\',\'', $this->get_excluded_taxonomies() ) . "') AND tr.object_id NOT IN (SELECT ID FROM $wpdb->posts)" ); // phpcs:ignore
if ( $query ) {
foreach ( $query as $tax ) {
$wp_remove_object_terms = wp_remove_object_terms( (int) $tax->object_id, (int) $tax->term_id, $tax->taxonomy );
if ( true !== $wp_remove_object_terms ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $tax->object_id, $tax->term_taxonomy_id ) );
}
}
// translators: %s is the Orphaned Term Relationships count.
$message = sprintf( __( '%s Orphaned Term Relationships Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'unused_terms':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT tt.term_taxonomy_id, t.term_id, tt.taxonomy FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode( ',', $this->get_excluded_termids() ) . ')', 0 ) ); // phpcs:ignore
if ( $query ) {
$check_wp_terms = false;
foreach ( $query as $tax ) {
if ( taxonomy_exists( $tax->taxonomy ) ) {
wp_delete_term( (int) $tax->term_id, $tax->taxonomy );
} else {
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", (int) $tax->term_taxonomy_id ) );
$check_wp_terms = true;
}
}
// We need this for invalid taxonomies.
if ( $check_wp_terms ) {
$wpdb->get_results( "DELETE FROM $wpdb->terms WHERE term_id NOT IN (SELECT term_id FROM $wpdb->term_taxonomy)" );
}
// translators: %s is the Unused Terms count.
$message = sprintf( __( '%s Unused Terms Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$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', (int) $meta->post_id ) ); // phpcs:ignore
}
// translators: %s is the Duplicated Post Meta count.
$message = sprintf( __( '%s Duplicated Post Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$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', (int) $meta->comment_id ) ); // phpcs:ignore
}
// translators: %s is the Duplicated Comment Meta count.
$message = sprintf( __( '%s Duplicated Comment Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$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', (int) $meta->user_id ) ); // phpcs:ignore
}
// translators: %s is the Duplicated User Meta count.
$message = sprintf( __( '%s Duplicated User Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
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 ) {
$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', (int) $meta->term_id ) ); // phpcs:ignore
}
// translators: %s is the Duplicated Term Meta count.
$message = sprintf( __( '%s Duplicated Term Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'optimize_database':
$query = $wpdb->get_col( 'SHOW TABLES' );
if ( $query ) {
$tables = implode( ',', $query );
$wpdb->query( "OPTIMIZE TABLE $tables" ); // phpcs:ignore
// translators: %s is the Tables count.
$message = sprintf( __( '%s Tables Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
case 'oembed_postmeta':
$query = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_key FROM $wpdb->postmeta WHERE meta_key LIKE(%s)", '%_oembed_%' ) );
if ( $query ) {
foreach ( $query as $meta ) {
$post_id = (int) $meta->post_id;
if ( 0 === $post_id ) {
$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 );
}
}
// translators: %s is the oEmbed Caches count.
$message = sprintf( __( '%s oEmbed Caches In Post Meta Processed', 'wp-sweep' ), number_format_i18n( count( $query ) ) );
}
break;
}
return apply_filters( 'wp_sweep_sweep', $message, $name );
}
/**
* Format number to percentage, taking care of division by 0.
* Props @barisunver https://github.com/barisunver
*
* @since 1.0.2
*
* @access public
* @param int $current Current number.
* @param int $total Total number.
* @return string Number in percentage
*/
public function format_percentage( $current, $total ) {
return ( $total > 0 ? round( ( $current / $total ) * 100, 2 ) : 0 ) . '%';
}
/**
* Get excluded taxonomies
*
* @since 1.0.8
*
* @access private
* @return array Excluded taxonomies
*/
private function get_excluded_taxonomies() {
$excluded_taxonomies = array();
$excluded_taxonomies[] = 'link_category';
return apply_filters( 'wp_sweep_excluded_taxonomies', $excluded_taxonomies );
}
/**
* Get excluded term IDs
*
* @since 1.0.3
*
* @access private
* @return array Excluded term IDs
*/
private function get_excluded_termids() {
$default_term_ids = $this->get_default_taxonomy_termids();
if ( ! is_array( $default_term_ids ) ) {
$default_term_ids = array();
}
$parent_term_ids = $this->get_parent_termids();
if ( ! is_array( $parent_term_ids ) ) {
$parent_term_ids = array();
}
$excluded_termids = array_merge( $default_term_ids, $parent_term_ids );
return apply_filters( 'wp_sweep_excluded_termids', $excluded_termids );
}
/**
* Get all default taxonomy term IDs
*
* @since 1.0.3
*
* @access private
* @return array Default taxonomy term IDs
*/
private function get_default_taxonomy_termids() {
$taxonomies = get_taxonomies();
$default_term_ids = array();
if ( $taxonomies ) {
$tax = array_keys( $taxonomies );
if ( $tax ) {
foreach ( $tax as $t ) {
$term_id = (int) get_option( 'default_' . $t );
if ( $term_id > 0 ) {
$default_term_ids[] = $term_id;
}
}
}
}
return $default_term_ids;
}
/**
* Get terms that has a parent term
*
* @since 1.0.3
*
* @access private
* @return array Parent term IDs
*/
private function get_parent_termids() {
global $wpdb;
return $wpdb->get_col( $wpdb->prepare( "SELECT tt.parent FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.parent > %d", 0 ) );
}
/**
* What to do when the plugin is being deactivated
*
* @since 1.0.0
*
* @access public
* @param boolean $network_wide Is network wide.
* @return void
*/
public function plugin_activation( $network_wide ) {
if ( is_multisite() && $network_wide ) {
$ms_sites = (array) get_sites();
if ( 0 < count( $ms_sites ) ) {
foreach ( $ms_sites as $ms_site ) {
switch_to_blog( $ms_site->blog_id );
$this->plugin_activated();
restore_current_blog();
}
}
} else {
$this->plugin_activated();
}
}
/**
* Perform plugin activation tasks
*
* @since 1.0.0
*
* @access private
* @return void
*/
private function plugin_activated() {
}
/**
* What to do when the plugin is being activated
*
* @since 1.0.0
*
* @access public
* @param boolean $network_wide Is network wide.
* @return void
*/
public function plugin_deactivation( $network_wide ) {
if ( is_multisite() && $network_wide ) {
$ms_sites = (array) get_sites();
if ( 0 < count( $ms_sites ) ) {
foreach ( $ms_sites as $ms_site ) {
switch_to_blog( $ms_site->blog_id );
$this->plugin_deactivated();
restore_current_blog();
}
}
} else {
$this->plugin_deactivated();
}
}
/**
* Perform plugin deactivation tasks
*
* @since 1.0.0
*
* @access private
* @return void
*/
private function plugin_deactivated() {
}
}
/**
* Init WP-Sweep
*/
WPSweep::get_instance();