94 lines
2.3 KiB
PHP
94 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* it means someone outside wp is accessing the file, in this case kill it.
|
|
*/
|
|
if (!defined('ABSPATH')) {
|
|
die('You can not access this file!');
|
|
}
|
|
|
|
function extract_if_array_size_one_CIPF($value) {
|
|
if (is_array($value) && count($value) === 1)
|
|
return reset($value);
|
|
return $value;
|
|
}
|
|
|
|
function merge_two_arrays_CIPF($array1, $array2) {
|
|
$new_array = $array1;
|
|
foreach ($array2 as $key2 => $value2) {
|
|
$value = extract_if_array_size_one_CIPF($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 = extract_if_array_size_one_CIPF($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;
|
|
}
|
|
}
|
|
return $new_array;
|
|
}
|
|
|
|
|
|
function output_list_front_CIPF($array) {
|
|
$output = '<ul>';
|
|
foreach ($array as $key => $value) {
|
|
if (str_starts_with($key, '_'))
|
|
continue ;
|
|
$output .= '<li>';
|
|
$output .= '<span>';
|
|
$output .= $key;
|
|
$output .= ' : ';
|
|
if (is_array($value) && count($value) === 1)
|
|
$output .= json_encode($value[0]);
|
|
else
|
|
$output .= json_encode($value);
|
|
$output .= '</span>';
|
|
$output .= '</li>';
|
|
}
|
|
$output .= '</ul>';
|
|
return $output;
|
|
}
|
|
|
|
|
|
/*
|
|
* shortcode to write user info of the logged in user
|
|
* 0 or 1 argument, usage :
|
|
* - [cipf_user_info] -> list of all availables infos
|
|
* - [cipf_user_info user_email] -> display the email
|
|
* - [cipf_user_info user_email user_login] -> display the email
|
|
*/
|
|
function current_user_infos_CIPF($atts) {
|
|
if (!is_user_logged_in())
|
|
return ;
|
|
error_log("----");
|
|
|
|
$current_user = wp_get_current_user();
|
|
$current_user_infos = $current_user->data;
|
|
|
|
if (empty($atts)) {
|
|
$user_properties = (array) get_userdata($current_user->ID)->data;
|
|
$user_metas = get_user_meta($current_user->ID);
|
|
$user_infos = merge_two_arrays_CIPF($user_metas, $user_properties);
|
|
return output_list_front_CIPF($user_infos);
|
|
}
|
|
// only return the first argument
|
|
$query = $atts[0];
|
|
$output = $current_user->$query;
|
|
if (is_string($output))
|
|
return $output;
|
|
else
|
|
return json_encode($output);
|
|
}
|
|
add_shortcode('cipf_user_info', 'current_user_infos_CIPF');
|
|
|
|
|
|
?>
|