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

Ref. UsersChecker. Sorting results refactored. #537

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion css/cleantalk-admin.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions css/src/cleantalk-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,21 @@
z-index: 99999;
opacity: 1;
}

.apbct-table-actions-wrapper {
background: #FСFСFС;
border-radius: 2px;
padding: 5px !important;
border: 1px solid lightgray;
margin: 0 5px 5px 0 !important;
}

@media screen and (max-width: 1120px) {
.apbct-tablenav {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
height: 100%;
max-width: 50%;
}
}
2 changes: 1 addition & 1 deletion js/cleantalk-users-checkspam.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/cleantalk-users-checkspam.min.js.map

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions js/src/cleantalk-users-checkspam.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ function ct_delete_all_users( e ){
};

jQuery('.' + e.target.id).addClass('disabled');
jQuery('.spinner').css('visibility', 'visible');
jQuery.ajax({
type: "POST",
url: ajaxurl,
Expand All @@ -362,7 +361,6 @@ function ct_delete_all_users( e ){
ct_delete_all_users( e, data );
}else{
jQuery('.' + e.target.id).removeClass('disabled');
jQuery('.spinner').css('visibility', 'hidden');
location.href='users.php?page=ct_check_users';
}
},
Expand Down Expand Up @@ -453,7 +451,6 @@ jQuery(document).ready(function(){
//Approve button
jQuery(".cleantalk_delete_from_list_button").click(function(){
ct_id = jQuery(this).attr("data-id");

// Approving
var data = {
'action': 'ajax_ct_approve_user',
Expand Down Expand Up @@ -503,6 +500,14 @@ jQuery(document).ready(function(){

});

function blinkElement(id) {
jQuery("#" + id).fadeTo(500, 0.25).fadeTo(500, 1);
}

function stopBlinkElement(id) {
jQuery("#" + id).fadeTo(1)
}

// Request to Download CSV file.
jQuery(".ct_get_csv_file").click(function( e ){
var data = {
Expand All @@ -512,7 +517,7 @@ jQuery(document).ready(function(){
'no_cache': Math.random()
};
jQuery('.' + e.target.id).addClass('disabled');
jQuery('.spinner').css('visibility', 'visible');
blinkElement('ct_get_csv_file');
jQuery.ajax({
type: "POST",
url: ajaxurl,
Expand All @@ -531,7 +536,7 @@ jQuery(document).ready(function(){
dummy.click();
}
jQuery('.' + e.target.id).removeClass('disabled');
jQuery('.spinner').css('visibility', 'hidden');
stopBlinkElement('ct_get_csv_file');
}
});
});
Expand All @@ -551,7 +556,7 @@ jQuery(document).ready(function(){

if ( ! confirm( ctUsersCheck.ct_confirm_deletion_all ) )
return false;

blinkElement('ct_delete_all_users');
ct_delete_all_users( e );

});
Expand Down
16 changes: 11 additions & 5 deletions lib/Cleantalk/ApbctWP/CleantalkListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,9 @@ protected function pagination( $which ) {
$output .= "\n<span class='$pagination_links_class'>" . implode( "\n", $page_links ) . '</span>';

if ( $total_pages ) {
$page_class = $total_pages < 2 ? ' one-page' : '';
$page_class = $total_pages < 2 ? ' one-page apbct-table-actions-wrapper' : ' apbct-table-actions-wrapper';
} else {
$page_class = ' no-pages';
$page_class = ' no-pages apbct-table-actions-wrapper';
}
$this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";

Expand Down Expand Up @@ -1249,7 +1249,13 @@ public function print_column_headers( $with_id = true ) {
}

$column_display_name = sprintf(
'<a href="%s"><span>%s</span><span class="sorting-indicator"></span></a>',
'<a href="%s">
<span>%s</span>
<span class="sorting-indicators">
<span class="sorting-indicator asc"></span>
<span class="sorting-indicator desc"></span>
</span>
</a>',
esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ),
$column_display_name
);
Expand Down Expand Up @@ -1333,10 +1339,10 @@ protected function display_tablenav( $which ) {
wp_nonce_field( 'bulk-' . $this->_args['plural'] );
}
?>
<div class="tablenav <?php echo esc_attr( $which ); ?>">
<div class="tablenav <?php echo esc_attr( $which ); ?> apbct-tablenav">

<?php if ( $this->has_items() ) : ?>
<div class="alignleft actions bulkactions">
<div class="alignleft actions bulkactions apbct-table-actions-wrapper">
<?php $this->bulk_actions( $which ); ?>
</div>
<?php
Expand Down
4 changes: 4 additions & 0 deletions lib/Cleantalk/ApbctWP/FindSpam/ListTable/BadUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class BadUsers extends Users
*/
public function prepare_items() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
/**
* This code chunk disables sorting, to make it sortable, remove it.
* Do not forget adapt getBadUsers() method like Users->getSpamNow() to handle sorting.
**/
$columns = $this->get_columns();
$this->_column_headers = array($columns, array(), array());

Expand Down
4 changes: 2 additions & 2 deletions lib/Cleantalk/ApbctWP/FindSpam/ListTable/CommentsScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public function extra_tablenav($which) // phpcs:ignore PSR1.Methods.CamelCapsMet
$button_id_spam = ($which) ? "ct_spam_all_$which" : "ct_spam_all";
$button_id_trash = ($which) ? "ct_trash_all_$which" : "ct_trash_all";
?>
<div class="alignleft actions bulkactions">
<div class="alignleft actions bulkactions apbct-table-actions-wrapper">
<button type="button" id="<?php
echo $button_id_spam; ?>" class="button action ct_spam_all"><?php
esc_html_e('Mark as spam all comments from the list', 'cleantalk-spam-protect'); ?></button>
<button type="button" id="<?php
echo $button_id_trash; ?>" class="button action ct_trash_all"><?php
esc_html_e('Move to trash all comments from the list', 'cleantalk-spam-protect'); ?></button>
<span class="spinner"></span>
</div>
<span class="spinner" style="float: left"></span>
<?php
}
}
200 changes: 166 additions & 34 deletions lib/Cleantalk/ApbctWP/FindSpam/ListTable/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function __construct()
{
parent::__construct(array(
'singular' => 'spam',
'plural' => 'spam'
'plural' => 'spam',
'screen' => str_replace('users_page_', '', current_action())
));

$this->bulk_actions_handler();
Expand Down Expand Up @@ -52,6 +53,27 @@ public function get_columns() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.N
return $columns;
}

/**
* @return array|array[]
*/
protected function get_sortable_columns() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
$columns = array(
'ct_username' => array('ct_username'),
'ct_email' => array('ct_email'),
'ct_name' => array('ct_name'),
'ct_signed_up' => array('ct_signed_up','desc'),
'ct_role' => array('ct_role'),
'ct_posts' => array('ct_posts'),
);

if ( $this->wc_active ) {
$columns['ct_orders'] = array('ct_orders');
}

return $columns;
}

/**
* CheckBox column
*
Expand Down Expand Up @@ -306,19 +328,152 @@ public function getCheckedNow()
}

/**
* @return \WP_User_Query
* Get spam users.
* @param integer|null $per_page if null - then just count users
* @param integer|null $current_page
* @param string $orderby
* @param string $order
* @return array
*/
public function getSpamNow($per_page, $current_page)
public function getSpamNow($per_page, $current_page, $orderby = 'ct_signed_up', $order = 'ASC')
{
$params_spam = array(
'number' => $per_page,
'offset' => ( $current_page - 1 ) * $per_page,
'fields' => 'ID',
'meta_key' => 'ct_marked_as_spam',
'count_total' => true,
);
global $wpdb;
//table names
$wc_orders_table = $wpdb->prefix . 'wc_orders';
$wp_usermeta_table = $wpdb->usermeta;
$wp_posts_table = $wpdb->posts;
$wp_users = $wpdb->users;
$wp_capabilities = $wpdb->prefix . 'capabilities';

$wc_exists = $this->wc_active && $wpdb->get_row("SHOW TABLES LIKE '$wc_orders_table'");

// ordering
$orderby = !empty($orderby) ? $orderby : 'ct_signed_up';
$order = !empty($order) ? $order : 'ASC';

// chunks

//woo commerce orders
$wc_sql_chunk_count = $wc_exists ? " COUNT( DISTINCT $wc_orders_table.ID ) AS ct_orders, " : '';
$wc_sql_chunk_join = $wc_exists
? " LEFT JOIN $wc_orders_table ON
users.ID = $wc_orders_table.customer_id
AND $wc_orders_table.status LIKE '%wc_completed%' "
: '';

if (!isset($current_page)) {
$current_page = 1;
}

return new \WP_User_Query($params_spam);
if (is_null($per_page)) { // if null - just count users
//limit is no limit
$limit_sql_chunk = '';
// global selector is count only
$selectors_sql_chunk = ' COUNT(*) as cnt ';
//ordering and group chunks empty
$group_by_chunk = '';
$order_by_chunk = '';
// drop wc_orders_join on count
$wc_sql_chunk_join = '';
} else { // else - get users select with limit, group and order
//limit
$limit_sql_chunk = "LIMIT " . ($current_page - 1) * $per_page . ", " . $per_page;

// global selector
$selectors_sql_chunk = ' user_login AS ct_username,
user_nicename AS ct_name,
user_email AS ct_email,
user_registered AS ct_signed_up,
users.ID AS user_id,
' . $wc_sql_chunk_count . '
( SELECT meta_table.meta_value
FROM ' . $wp_usermeta_table . ' as meta_table
WHERE meta_table.meta_key LIKE \'' . $wp_capabilities . '\'
AND meta_table.user_id = users.ID
LIMIT 1
) AS ct_role,
( SELECT COUNT( posts_table.ID )
FROM ' . $wp_posts_table . ' as posts_table
WHERE posts_table.post_author = users.ID
AND posts_table.post_type = \'post\'
AND posts_table.post_status = \'publish\'
) AS ct_posts';

//ordering chunks
$group_by_chunk = ' GROUP BY users.ID ';
$order_by_chunk = " ORDER BY $orderby $order ";
}

$the_final_query = "
SELECT
$selectors_sql_chunk
FROM
$wp_users AS users
$wc_sql_chunk_join
LEFT JOIN
$wp_usermeta_table ON users.ID = $wp_usermeta_table.user_id
LEFT JOIN $wp_posts_table ON users.ID = $wp_posts_table.post_author
WHERE $wp_usermeta_table.meta_key LIKE '%ct_marked_as_spam%'
$group_by_chunk
$order_by_chunk
$limit_sql_chunk;
";

/** The final common SQL query looks LIKE
* SELECT
* user_login AS ct_username,
* user_nicename AS ct_name,
* user_email AS ct_email,
* user_registered AS ct_signed_up,
* users.ID AS user_id,
* COUNT( DISTINCT wp_wc_orders.ID ) AS ct_orders,
* ( SELECT meta_table.meta_value
* FROM wp_usermeta as meta_table
* WHERE meta_table.meta_key LIKE \'wp_capabilities\'
* AND meta_table.user_id = users.ID
* LIMIT 1
* ) AS ct_role,
* ( SELECT COUNT( posts_table.ID )
* FROM wp_posts as posts_table
* WHERE posts_table.post_author = users.ID
* AND posts_table.post_type = \'post\'
* AND posts_table.post_status = \'publish\'
* ) AS ct_posts
* FROM
* wp_users AS users
* LEFT JOIN wp_wc_orders ON
* users.ID = wp_wc_orders.customer_id
* AND wp_wc_orders.status LIKE \'%wc_completed%\'
* LEFT JOIN wp_usermeta ON
* users.ID = wp_usermeta.user_id
* LEFT JOIN wp_posts ON
* users.ID = wp_posts.post_author
* WHERE wp_usermeta.meta_key LIKE \'%ct_marked_as_spam%\'
* GROUP BY users.ID
* ORDER BY ct_posts desc
* LIMIT 0, 10;
*/

/**
* The final count SQL looks like
* SELECT
* COUNT(*) as cnt
* FROM
* wp_users AS users
* LEFT JOIN wp_usermeta ON
* users.ID = wp_usermeta.user_id
* LEFT JOIN wp_posts ON
* users.ID = wp_posts.post_author
* WHERE wp_usermeta.meta_key LIKE \'%ct_marked_as_spam%\'
*/

$result = $wpdb->get_results($the_final_query);

if ( !is_array($result) ) {
$result = array();
}

return $result;
}

/**
Expand Down Expand Up @@ -366,27 +521,4 @@ protected function removeLogs($ids)
ID IN ($ids_string)"
);
}

/**
* @param int $user_id
*
* @return string
*/
protected function getWcOrdersCount($user_id)
{
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
);

$description = '';
if ( $count = count(get_posts($args)) ) {
$description = esc_html__('Do "accurate check" to skip checking this user', 'cleantalk-spam-protect');
}

return '<p>' . $count . '</p><i>' . $description . '</i>';
}
}
Loading
Loading