Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pluggable WP-Sweep / Dynamic admin page #39

Open
szepeviktor opened this issue Sep 1, 2016 · 11 comments
Open

Pluggable WP-Sweep / Dynamic admin page #39

szepeviktor opened this issue Sep 1, 2016 · 11 comments

Comments

@szepeviktor
Copy link
Collaborator

szepeviktor commented Sep 1, 2016

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
Copy link
Collaborator Author

szepeviktor commented Sep 1, 2016

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
Copy link
Owner

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

@szepeviktor
Copy link
Collaborator Author

szepeviktor commented Sep 1, 2016

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
Copy link
Owner

Ah I see! Sounds good! PR please 😅

@szepeviktor szepeviktor changed the title Dynamic admin page Pluggable WP-Sweep / Dynamic admin page Oct 7, 2018
@szepeviktor
Copy link
Collaborator Author

szepeviktor commented Oct 7, 2018

@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?

  • total_count
  • count
  • details
  • the actual sweep action (same as CLI action)
  • displaying a <tr>

What else?

@szepeviktor
Copy link
Collaborator Author

szepeviktor commented Oct 7, 2018

@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
Copy link
Owner

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

@szepeviktor
Copy link
Collaborator Author

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
Copy link
Collaborator Author

@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
Copy link
Owner

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
Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants