lesterchan / wp-sweep

WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It also optimizes your database tables.
https://wordpress.org/plugins/wp-sweep/
153 stars 22 forks source link

Pluggable WP-Sweep / Dynamic admin page #39

Open szepeviktor opened 8 years ago

szepeviktor commented 8 years ago

For example

function render_sweep_admin( $group, $slug, $title, $count, $total ) {

            ?>
            <tr>
                <td>
                    <strong><?php esc_html( $title ); ?></strong>
                    <p class="sweep-details" style="display: none;"></p>
                </td>
                <td>
                    <span class="sweep-count"><?php echo esc_html( number_format_i18n( $count ) ); ?></span>
                </td>
                <td>
                    <span class="sweep-percentage"><?php echo esc_html( WPSweep::get_instance()->format_percentage( $count, $total ) ); ?></span>
                </td>
                <td>
                    <?php if ( empty( $count ) ) : ?>
                        <?php _e( 'N/A', 'wp-sweep' ); ?>
                    <?php else : ?>
                        <button data-action="sweep" data-sweep_name="<?php echo $slug; ?>"
                            data-sweep_type="posts" data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_sweep_' . $slug ) ); ?>"
                            class="button button-primary btn-sweep"><?php _e( 'Sweep', 'wp-sweep' ); ?></button>
                        <button data-action="sweep_details" data-sweep_name="<?php echo $slug; ?>"
                            data-sweep_type="posts" data-nonce="<?php echo wp_create_nonce( 'wp_sweep_details_' . $slug ); ?>"
                            class="button btn-sweep-details"><?php _e( 'Details', 'wp-sweep' ); ?></button>
                    <?php endif; ?>
                </td>
            </tr>
            <?php
}

render_sweep_admin( 'post', 'revisions', __( 'Revisions', 'wp-sweep' ), $revisions, $total_posts );
szepeviktor commented 8 years ago
function register_sweep_admin_group( $title, $group_slug, $status ) {

    ?>
    <h3><?php _e( 'Comment Sweep', 'wp-sweep' ); ?></h3>
    <p><?php esc_html( $status ); ?></p>
    <div class="sweep-message"></div>
    <table class="widefat table-sweep">
        <thead>
            <tr>
                <th class="col-sweep-details"><?php _e( 'Details', 'wp-sweep' ); ?></th>
                <th class="col-sweep-count"><?php _e( 'Count', 'wp-sweep' ); ?></th>
                <th class="col-sweep-percent"><?php _e( '% Of', 'wp-sweep' ); ?></th>
                <th class="col-sweep-action"><?php _e( 'Action', 'wp-sweep' ); ?></th>
            </tr>
        </thead>
        <tbody>
    <?php

    // loop of sweeps for this group

    ?>
        </tbody>
    </table>
    <?php do_action( 'wp_sweep_admin_' . $group . '_sweep' ); ?>
    <p>&nbsp;</p>
    <?php
}
register_sweep_admin_group();
register_sweep_admin();
render_sweep_admin();
render_sweep_admin_group();
lesterchan commented 8 years ago

How do we link render_sweep_admin into render_sweep_admin_group()? actions?

szepeviktor commented 8 years ago

The register functions would make up an array:

$sweep_groups = array(
  $group_slug => array(
    $sweep_details1,
    $sweep_details2,
  ),
  $group_slug2 => array(
    $sweep_details1,
    $sweep_details2,
  ),
);
lesterchan commented 8 years ago

Ah I see! Sounds good! PR please 😅

szepeviktor commented 6 years ago

@lesterchan I am starting to modularize WP-Sweep. WP-Sweep will be a tiny framework without any functionality. All sweeps will be implemented in separate classes. Could you help me list what should be possible per sweep?

What else?

szepeviktor commented 6 years ago

@lesterchan First try on WP Sweep 2.0! What are your thoughts?

<?php
/**
 * WP-Sweep sweep skeleton
 *
 * @package wp-sweep
 */

/**
 * Abstract class WPSweep_Sweep
 */
abstract class WPSweep_Sweep {
    /**
     * Sweep slug
     *
     * @access public
     * @var string
     */
    const SLUG = '';

    /**
     * Sweep name
     *
     * @access public
     * @var string
     */
    const NAME = self::SLUG;

    /**
     * The total the sweep pertains to
     *
     * @access public
     * @var string
     */
    const TOTAL = '';

    /**
     * "Processed" message in gettext format
     *
     * @access public
     * @var string
     */
    const PROCESSED_MESSAGE = "%s {self::NAME} Processed";

    /**
     * WPSweep instance
     *
     * @access private
     * @var WPSweep $wp_sweep
     */
    private $wp_sweep;

    /**
     * WPDB instance
     *
     * @access private
     * @var wpdb $wpdb
     */
    private $wp_db;

    /**
     * Initialize sweep
     *
     * @access public
     */
    public function __construct( $wp_sweep ) {
        global $wpdb;

        $this->wp_sweep = $wp_sweep;
        $this->wp_db = $wpdb;
    }

    /**
     * Return count of items to be swept
     *
     * @access public
     * @return int
     */
    public function count() {}

    /**
     * Return details about the sweep
     *
     * @access public
     * @return string
     */
    public function details() {}

    /**
     * Does the sweeping
     *
     * @access public
     * @return string Processed message
     */
    public function sweep() {}
}

<?php
/**
 * WP-Sweep unapproved comments sweep
 *
 * @package wp-sweep
 */

/**
 * Class WPSweep_Unapproved_Sweep_Comments
 */
class WPSweep_Sweep_Unapproved_Comments extends WPSweep_Sweep {

    const SLUG = 'unapproved_comments';

    const NAME = 'Unapproved Comments';

    const TOTAL = 'comments';

    const PROCESSED_MESSAGE = '%s Unapproved Comments Processed';

    public function count() {
        return $this->wp_db->get_var( $this->wp_db->prepare( "SELECT COUNT(comment_ID) FROM {$this->wp_db->comments} WHERE comment_approved = %s", '0' ) );
    }

    public function details() {
        return $this->wp_db->get_col( $this->wp_db->prepare( "SELECT comment_author FROM {$this->wp_db->comments} WHERE comment_approved = %s LIMIT %d", '0', $this->wp_sweep->
limit_details ) );
    }

    public function sweep() {
        $query = $this->wp_db->get_col( $this->wp_db->prepare( "SELECT comment_ID FROM {$this->wp_db->comments} WHERE comment_approved = %s", '0' ) );

        foreach ( $query as $id ) {
            wp_delete_comment( (int) $id, true );
        }

        // translators: %s is the Unapproved Comments count.
        return count( $query );
    }
}
lesterchan commented 6 years ago

Should the count(), details() and sweep() on the parent classes be abstract as well?

szepeviktor commented 6 years ago

Yes. This is just a theoretical plan. So it is probably not working. Running it in WordPress and running PHPStan on it will tell!

szepeviktor commented 6 years ago

@lesterchan What if I complete the rewrite with Unapproved Comments only, and you add the rest of the built-in sweeps? (14 sloc/sweep)

lesterchan commented 6 years ago

Sure if I have the time to do it! But it will not be that fast as I am quite busy with my day job.

I also created a new branch called modularize-admin, maybe we should merge it there?

szepeviktor commented 6 years ago

@lesterchan Now admin.php should fully become class WPSweep_Admin.