Skip to content

Commit

Permalink
New. UsersChecker. Sorting users implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandergull committed Nov 29, 2024
1 parent 1fbc2b9 commit 9e41bcf
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 29 deletions.
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.

16 changes: 12 additions & 4 deletions css/src/cleantalk-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
}
/* Additional styles for admin pages */
.ct_meta_links{

}
.ct_translate_links{color: rgba(150, 150, 20, 1);}
.ct_support_links {color: rgba(150, 20, 20, 1);}
.ct_faq_links {color: rgba(20, 150, 20, 1);}
.ct_setting_links {color: rgba(20, 20, 150, 1);}

.ct_translate_links:hover{color: rgba(210, 210, 20, 1) !important;}
.ct_support_links:hover {color: rgba(250, 20, 20, 1) !important;}
.ct_faq_links:hover {color: rgba(20, 250, 20, 1) !important;}
.ct_setting_links:hover {color: rgba(20, 20, 250, 1) !important;}

.ct_link_new_tab img{
float: none !important;
margin: 0 2px;
Expand Down Expand Up @@ -137,4 +137,12 @@
border-radius: 4px;
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 0 0;;
}
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.

36 changes: 36 additions & 0 deletions js/src/cleantalk-users-checkspam.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,42 @@ jQuery(document).ready(function(){
jQuery('.ct_date').prop('checked', true).attr('disabled',false);
}
});

/**
* Usres ordering JS actions
*/
jQuery('#ct_users_ordering').on('click', function(event) {
event.preventDefault();
/*
* Handle the bulk action based on its value.
*/
const value = jQuery( '#ct_users_order_by' ).val();
const _expires = 'expires=' + new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toUTCString();
document.cookie = 'ct_users_order_by' +
'=' +
encodeURIComponent(value) +
'; ' +
_expires +
'path=/; samesite=lax';

const direction = jQuery( '#ct_users_order_direction' ).val();
document.cookie = 'ct_users_order_direction' +
'=' +
encodeURIComponent(direction) +
'; ' +
_expires +
'path=/; samesite=lax';

window.location.reload()
});

if (getCookie('ct_users_order_by') && jQuery('#ct_users_order_by')) {
jQuery('#ct_users_order_by').val(getCookie('ct_users_order_by'));
}

if (getCookie('ct_users_order_direction') && jQuery('#ct_users_order_direction')) {
jQuery('#ct_users_order_direction').val(getCookie('ct_users_order_direction'));
}
});

/**
Expand Down
7 changes: 4 additions & 3 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 @@ -1336,12 +1336,13 @@ protected function display_tablenav( $which ) {
<div class="tablenav <?php echo esc_attr( $which ); ?>">

<?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
endif;
$this->extra_tablenav( $which );
echo '<span class="spinner" style="float: left"></span>';
$this->pagination( $which );
?>

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
}
}
121 changes: 105 additions & 16 deletions lib/Cleantalk/ApbctWP/FindSpam/ListTable/UsersScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Cleantalk\ApbctWP\FindSpam\ListTable;

use Cleantalk\ApbctWP\Sanitize;
use Cleantalk\ApbctWP\Variables\Server;
use Cleantalk\Common\TT;

class UsersScan extends Users
{
Expand Down Expand Up @@ -50,31 +52,118 @@ public function prepare_items() // phpcs:ignore PSR1.Methods.CamelCapsMethodName

$this->items[] = $items;
}

$this->items = static::sortItems($this->items);
}

/**
* @param array $items
*
* @return array
*/
private static function sortItems($items)
{
try {
if (empty($items)) {
return $items;
}
if (isset($_COOKIE['ct_users_order_by'])) {
$order_by = Sanitize::cleanWord($_COOKIE['ct_users_order_by']);
if ( !in_array(
$order_by,
array('ct_id', 'ct_name', 'ct_email', 'ct_signed_up', 'ct_role', 'ct_posts', 'ct_orders')
) ) {
$order_by = 'ct_id';
}
} else {
$order_by = 'ct_id';
}

if (isset($_COOKIE['ct_users_order_direction'])) {
$order_direction = Sanitize::cleanWord($_COOKIE['ct_users_order_direction']);
$order_direction = $order_direction === 'desc' ? $order_direction : 'asc';
} else {
$order_direction = 'asc';
}

$order_direction = $order_direction === 'asc' ? 1 : -1;

usort($items, function ($a, $b) use ($order_by, $order_direction) {
return is_array($a) && is_array($b) && isset($a[$order_by], $b[$order_by])
? $order_direction * strcmp(TT::toString($a[$order_by]), TT::toString($b[$order_by]))
: 0;
});
} catch (\Exception $e) {
return $items;
}
return $items;
}

public function extra_tablenav($which) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
{
if (empty($this->items)) {
return;
}
echo static::getExtraTableNav();
}

public static function getExtraTableNav()
{
//prepare sorting elements
$users = new UsersScan();
$columns = $users->get_columns();
unset($columns['cb']);
unset($columns['ct_id']);
unset($columns['ct_username']);

$options = '';
foreach ($columns as $key => $value) {
$options .= '<option value="' . htmlspecialchars($key) . '">' . htmlspecialchars($value) . '</option>';
}

$out = '';

$out .= '<div class="alignleft actions bulkactions apbct-table-actions-wrapper">';
$out .= '<span class="displaying-num">' . __('Order users by', 'cleantalk-spam-protect') . '</span>';
$out .= '<select id="ct_users_order_by" style="float: none; margin: 0 3px 0 3px;">';
$out .= $options;
$out .= '</select>';

$out .= '<select id="ct_users_order_direction" style="float: none; margin: 0 3px 0 0;">';
$out .= '<option value="desc">Descend</option>';
$out .= '<option value="asc">Ascend</option>';
$out .= '</select>';
$out .= '<button type="button" id="ct_users_ordering" class="button">';
$out .= __('Apply', 'cleantalk-spam-protect');
$out .= '</button>';
$out .= '</div>';

$out .= '<div class="alignleft actions bulkactions apbct-table-actions-wrapper">';
$out .= '<button type="button" id="ct_delete_all_users" class="button action ct_delete_all_users" style="margin: 0 2px;">';
$out .= __('Delete all users from list', 'cleantalk-spam-protect');
$out .= '</button>';

$out .= '<button type="button" id="ct_get_csv_file" class="button action ct_get_csv_file" style="margin: 0 2px;">';
$out .= __('Download results in CSV', 'cleantalk-spam-protect');
$out .= '</button>';
$out .= '</div>';
return $out;
}

public static function getExtraTableNavInsertDeleteUsers()
{
$out = '';
if (
isset($_SERVER['SERVER_ADDR']) &&
$_SERVER['SERVER_ADDR'] === '127.0.0.1' &&
in_array(Server::getDomain(), array('lc', 'loc', 'lh'))
) {
?>
<button type="button" class="button action ct_insert_users">Insert users</button>
<button type="button" class="button action ct_insert_users__delete">Delete inserted</button>
<?php
}
if ( ! $this->has_items() ) {
return;
$out .= '<div class="ctlk---red bar" style="padding: 10px">';
$out .= '<span>These actions available only for test purpose and buttons are visible only in local env:</span>';
$out .= '<button type="button" class="button button-small action ct_insert_users" style="margin:0 5px">Insert 500 users</button>';
$out .= '<button type="button" class="button button-small action ct_insert_users__delete" style="margin:0 5px">Delete inserted users</button>';
$out .= '</div>';
}
?>
<div class="alignleft actions bulkactions">
<button type="button" id="ct_delete_all_users" class="button action ct_delete_all_users"><?php
esc_html_e('Delete all users from list', 'cleantalk-spam-protect'); ?></button>
<button type="button" id="ct_get_csv_file" class="button action ct_get_csv_file"><?php
esc_html_e('Download results in CSV', 'cleantalk-spam-protect') ?></button>
<span class="spinner"></span>
</div>
<?php
return $out;
}
}
5 changes: 4 additions & 1 deletion lib/Cleantalk/ApbctWP/FindSpam/UsersChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public function getCurrentScanPage()
$this->list_table = new UsersScan();

$this->getCurrentScanPanel($this);
echo UsersScan::getExtraTableNavInsertDeleteUsers();
echo '<form action="" method="POST">';
$this->list_table->display();
echo '</form>';
Expand Down Expand Up @@ -577,8 +578,10 @@ public static function ctAjaxInsertUsers()

// TEST INSERTION
$to_insert = 500;
$query = 'SELECT network FROM `' . APBCT_TBL_FIREWALL_DATA . '` LIMIT ' . $to_insert . ';';

$result = $wpdb->get_results(
'SELECT network FROM `' . APBCT_TBL_FIREWALL_DATA . '` LIMIT ' . $to_insert . ';',
$query,
ARRAY_A
);

Expand Down

0 comments on commit 9e41bcf

Please sign in to comment.