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

Release 1.1.1 #10

Merged
merged 7 commits into from
Oct 26, 2023
Merged
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
196 changes: 124 additions & 72 deletions api/list-all-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,130 +52,179 @@ function processRequest(\WP_REST_Request $request){
$offset = ($page - 1) * $page_size;

// Sorting
$sort_order = isset($sort_order) ? strtoupper($sort_order) : 'ASC';
$sort_by = self::mapTableColumns($sort_by, "t1", "t2");
$sort = isset($sort) ? strtoupper($sort) : 'ASC';
$sort_by = self::mapTableColumns($sort_by, "t1");

// Search
$search_text = self::filterNull($search_text);
$role = self::filterNull($role);

$sql_on = "
t1.ID = t2.user_id
AND (t2.meta_value LIKE '%$role%' AND t2.meta_key = 'wp_capabilities')
$sql_select = "
t1.ID,
t1.user_login,
t1.user_nicename,
t1.user_email,
t1.user_registered,
t1.user_status,
t1.display_name,
t2.meta_value AS first_name,
t3.meta_value AS last_name,
t4.meta_value AS roles,
t5.meta_value AS billing_company,
t6.meta_value AS billing_country,
t7.meta_value AS billing_phone
";

$sql_left_join = "
LEFT JOIN wp_usermeta AS t2 ON (
t1.ID = t2.user_id
AND t2.meta_key = 'first_name'
)
LEFT JOIN wp_usermeta AS t3 ON (
t1.ID = t3.user_id
AND t3.meta_key = 'last_name'
)
LEFT JOIN wp_usermeta AS t4 ON (
t1.ID = t4.user_id
AND t4.meta_key = 'wp_capabilities'
)
LEFT JOIN wp_usermeta AS t5 ON (
t1.ID = t5.user_id
AND t5.meta_key = 'billing_company'
)
LEFT JOIN wp_usermeta AS t6 ON (
t1.ID = t6.user_id
AND t6.meta_key = 'billing_country'
)
LEFT JOIN wp_usermeta AS t7 ON (
t1.ID = t7.user_id
AND t7.meta_key = 'billing_phone'
)
";

$sql_where = "
t1.user_login LIKE '%$search_text%'
(t1.user_login LIKE '%$search_text%'
OR t1.user_email LIKE '%$search_text%'
OR t1.user_nicename LIKE '%$search_text%'
OR t1.display_name LIKE '%$search_text%'
OR t2.meta_value LIKE '%$search_text%'
OR t3.meta_value LIKE '%$search_text%'
OR t5.meta_value LIKE '%$search_text%'
OR t6.meta_value LIKE '%$search_text%'
OR t7.meta_value LIKE '%$search_text%')
AND t4.meta_value LIKE '%$role%'
";

$sql = "
SELECT * FROM {$wpdb->users} as t1
INNER JOIN {$wpdb->usermeta} as t2
ON ($sql_on)
SELECT
$sql_select
FROM {$wpdb->users} as t1
$sql_left_join
WHERE
$sql_where
GROUP BY t2.user_id
ORDER BY $sort_by $sort_order
GROUP BY t1.ID
ORDER BY $sort_by $sort
LIMIT $offset, $page_size
";

$users = $wpdb->get_results($sql);

// Get total records
$sql_for_total_count = "
SELECT COUNT(*) FROM {$wpdb->users} as t1
INNER JOIN {$wpdb->usermeta} as t2
ON ($sql_on)
SELECT
COUNT(*)
FROM {$wpdb->users} as t1
$sql_left_join
WHERE
$sql_where
GROUP BY t2.user_id
GROUP BY t1.ID
";
$total_count_result = count($wpdb->get_results($sql_for_total_count));

$DBRecord['total'] = (int) $total_count_result;
$DBRecord['page'] = (int) $page;
$DBRecord['page_size'] = (int) $page_size;
$DBRecord['users'] = array();
$i=0;

foreach ( $users as $user )
// var_dump($users);
foreach ($users as $user)
{
// var_dump($user);
$record = array();
$record['roles'] = self::filterNull($user->roles);
$record['username'] = self::filterNull($user->user_login);
$record['id'] = self::filterNull($user->ID);
$record['user_registered'] = self::filterNull($user->user_registered);
$record['email'] = self::filterNull($user->user_email);

$UserData = get_user_meta( $user->ID );

// https://regex101.com/library/3q3RYF - smit
// a:1:{s:11:"contributor";b:1;} ==to==> ["contributor"]
$re = '/"([^"]+)"/';
preg_match_all($re, $user->meta_value, $matches, PREG_SET_ORDER, 0);
if ($matches) {
$record['roles'] = [];
foreach ($matches as $key => $value) {
array_push($record['roles'], $value[1]);
}
}

$record['first_name'] = self::filterNullFirst($UserData['first_name']);
$record['last_name'] = self::filterNullFirst($UserData['last_name']);
$record['billing_company'] = self::filterNullFirst($UserData['billing_company']);
$record['billing_address_1'] = self::filterNullFirst($UserData['billing_address_1']);
$record['billing_city'] = self::filterNullFirst($UserData['billing_city']);
$record['billing_state'] = self::filterNullFirst($UserData['billing_state']);
$record['billing_postcode'] = self::filterNullFirst($UserData['billing_postcode']);
$record['billing_country'] = self::filterNullFirst($UserData['billing_country']);
$record['billing_phone'] = self::filterNullFirst($UserData['billing_phone']);
$DBRecord['users'][$i] = $record;
$i++;
// Populate from wp_users & wp_usermeta table
$record['id'] = self::filterNull($user->ID);
$record['username'] = self::filterNull($user->user_login);
$record['user_registered'] = self::filterNull($user->user_registered);
$record['email'] = self::filterNull($user->user_email);
$record['roles'] = self::extract_roles_from_meta_value($user->roles);
$record['first_name'] = self::filterNull($user->first_name);
$record['last_name'] = self::filterNull($user->last_name);
$record['billing_company'] = self::filterNull($user->billing_company);
$record['billing_country'] = self::filterNull($user->billing_country);
$record['billing_phone'] = self::filterNull($user->billing_phone);

array_push($DBRecord['users'], $record);
}
return new \WP_REST_Response($DBRecord, 200);
}

/**
* Filter null values
* Check if meta_value is array or not and extract roles from it
*
* @param mixed $val
* @return string ""
* @param string $meta_value
* @return string[] $roles
*/
protected function filterNull($val){
if($val===NULL) {
return "";
protected function extract_roles_from_meta_value($meta_value) {
$roles = [];

if (!is_array($meta_value)) {
$role = self::match_role($meta_value);
if (!empty($role)) {
array_push($roles, $role);
}
} else {
return $val;
foreach ($meta_value as $value) {
$role = self::match_role($value);
if (!empty($role)) {
array_push($roles, $role);
}
}
}

return $roles;
}

/**
* Filter null values
* Extract role from meta_value
*
* @param mixed $val
* @return string or NULL
* @param string $meta_value
* @return string ""
*/
protected function filterIsSetNull($val){
if(isset($val)) {
return $val;
} else {
return NULL;
protected function match_role($meta_value) {
// https://regex101.com/library/3q3RYF - smit
// a:1:{s:11:"contributor";b:1;} ==to==> ["contributor"]
$re = '/"([^"]+)"/';
$matches = NULL;
preg_match_all($re, $meta_value, $matches, PREG_SET_ORDER, 0);
if (!empty($matches) && is_array($matches) && !is_null($matches)) {
foreach ($matches as $value) {
return $value[1];
}
}
}

/**
* Filter null values and return first value
*
* Filter null values
*
* @param mixed $val
* @return string or ""
* @return string ""
*/
protected function filterNullFirst($val){
if(!isset($val) || $val===NULL || !isset($val[0]) || $val[0]===NULL) {
protected function filterNull($val){
if($val===NULL) {
return "";
} else {
return $val[0];
return $val;
}
}

Expand All @@ -184,22 +233,25 @@ protected function filterNullFirst($val){
*
* @param string $column_name
* @param string $users_table
* @param string $usermeta_table
* @return string
*/
protected function mapTableColumns($column_name, $users_table, $usermeta_table) {
protected function mapTableColumns($column_name, $users_table) {
$sort = strtolower(self::filterNull($column_name));

$sort_map = [
'username' => "$users_table.user_login",
'email' => "$users_table.user_email",
'first_name' => "$users_table.user_nicename",
'last_name' => "$users_table.user_login",
'first_name' => "first_name",
'last_name' => "last_name",
'roles' => "roles",
'company_name' => "billing_company",
'phone' => "billing_phone",
'created_at' => "$users_table.user_registered",
];

if (isset($sort_map[$sort])) {
return $sort_map[$sort];
}
return "$usermeta_table.meta_value";
return "$users_table.user_registered";
}
}
1 change: 1 addition & 0 deletions constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private function define_rus_constants(){
define('RUS_DIRECTORY', __DIR__);
define('RUS_WP_CURRENT_VERSION', $wp_version);
define('RUS_CAPABILITY', 'robust_user_search');
define('RUS_ADMIN_CAPABILITY', 'manage_options');
define('RUS_MENU_ICON_URL', plugins_url('/assets/robust_teal.svg', __FILE__));
define('RUS_FAVICON_URL', plugins_url('/dist/favicon.ico', __FILE__));

Expand Down
2 changes: 1 addition & 1 deletion controller/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function indexOutput() {
echo wp_kses('
<div class="flex flex-wrap" style="width:100% !important;">
<div class="w-full flex flex-wrap mt-2">
<div id="vueApp" class="w-full"/>
<div id="rus-vue-app" class="w-full"/>
</div>
</div>
<style>
Expand Down
7 changes: 6 additions & 1 deletion controller/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function init(){
*/
private function register() {
//add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position = null )
add_submenu_page('rus', 'Settings', 'Settings', RUS_CAPABILITY, 'rus-settings', [$this, 'settingsOutput']);
add_submenu_page('rus', 'Settings', 'Settings', RUS_ADMIN_CAPABILITY, 'rus-settings', [$this, 'settingsOutput']);
}

/**
Expand Down Expand Up @@ -117,6 +117,11 @@ public function settingsOutput(){
<a href="#roles">#</a>
<span>Allowed Roles</span>
</div>
<div class="w-full text-teal-700 text-sm">
<span>
Only account with &quot;admin&quot; role can access settings page, even if you allow other roles to access the main page.
</span>
</div>
<div class="rus-settings-checkboxes">
<?php
foreach($editable_roles as $key => $role){
Expand Down
2 changes: 1 addition & 1 deletion dist/assets/index.css

Large diffs are not rendered by default.

Loading