custer v0.2.2 includes queries in all output

This commit is contained in:
asus
2024-04-02 18:50:56 +02:00
parent 1f5fff26d6
commit ccb800c203
7 changed files with 134 additions and 89 deletions

View File

@@ -12,42 +12,53 @@ if (!defined('ABSPATH')) {
function extract_if_array_size_one($value) {
if (is_array($value) && count($value) === 1)
return reset($value);
function extract_smallest_entity($value) {
$size = 1;
while (is_array($value) && $size === 1) {
// https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php#answer-20177855
$tmp_array = array_filter($value);
$size = count($tmp_array);
if ($size === 1) {
$value = reset($tmp_array);
}
}
return $value;
}
function merge_two_arrays($array1, $array2) {
$new_array = $array1;
foreach ($array2 as $key2 => $value2) {
$value = \CUSTER\extract_if_array_size_one($value2);
// if key was not in first array, add the new element to it
if (!isset($new_array[$key2])) {
$new_array[$key2] = $value2;
continue;
}
// if key was in first array, add both in an array
$value1 = \CUSTER\extract_if_array_size_one($new_array[$key2]);
if (empty($value1))
$new_array[$key2] = $value2;
else if (empty($value2))
$new_array[$key2] = $value1;
else {
$new_value = array($value1, $value2);
$new_array[$key] = $new_value;
function merge_arrays(...$arrays) {
// extract first element
$new_array = array_shift($arrays);
// then loop through the next arrays
foreach ($arrays as $array) {
foreach ($array as $key => $value) {
$value = \CUSTER\extract_smallest_entity($value);
// if key does not already exist, simply add it and the value to new array
if (!isset($new_array[$key])) {
$new_array[$key] = $value;
continue;
}
// if key already exist, add a new key with a (number) suffix
$i = 1;
while (isset($new_array[$key.'('.$i.')'])) {
++$i;
}
$new_array[$key.'('.$i.')'] = $value;
}
}
ksort($new_array, 'strnatcasecmp');
return $new_array;
}
function output_list_front($array, $current_user, $user_id) {
function output_list_front($array, $user_id, $if_empty) {
$output = '<ul>';
foreach ($array as $key => $value) {
if (str_starts_with($key, '_'))
continue ;
$value = \CUSTER\format_user_info($key, $current_user, $user_id);
if (str_starts_with($key, '_')) {
if (!str_starts_with($key, '__')) {
continue ;
}
}
$value = \CUSTER\format_user_info($key, $user_id, $if_empty);
$output .= '<li>';
$output .= '<span>';
$output .= $key;
@@ -150,13 +161,6 @@ function current_user_infos($atts) {
}
/*
* if id is for 'author' && shortcode 'custer_change_id' is set
*
*/
$current_user = get_user_by('id', $user_id);
/*
@@ -166,8 +170,9 @@ function current_user_infos($atts) {
if ($output_all) {
$user_properties = (array) get_userdata($user_id)->data;
$user_metas = get_user_meta($user_id);
$user_infos = \CUSTER\merge_two_arrays($user_metas, $user_properties);
return \CUSTER\output_list_front($user_infos, $current_user, $user_id);
$queries = \CUSTER\get_queries($user_id, $if_empty);
$user_infos = \CUSTER\merge_arrays($user_metas, $user_properties, $queries);
return \CUSTER\output_list_front($user_infos, $user_id, $if_empty);
}
@@ -182,7 +187,7 @@ function current_user_infos($atts) {
$query = $atts;
else
return '';
return \CUSTER\format_user_info($query, $current_user, $user_id, $if_empty);
return \CUSTER\format_user_info($query, $user_id, $if_empty);
}
add_shortcode('custer_user_info', __NAMESPACE__.'\current_user_infos');