196 lines
4.6 KiB
PHP
196 lines
4.6 KiB
PHP
<?php
|
|
namespace CUSTER;
|
|
|
|
/*
|
|
* 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_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_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;
|
|
}
|
|
}
|
|
uksort($new_array, 'strnatcasecmp');
|
|
return $new_array;
|
|
}
|
|
|
|
|
|
function output_list_front($array, $user_id, $if_empty) {
|
|
$output = '<ul>';
|
|
foreach ($array as $key => $value) {
|
|
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;
|
|
$output .= ' : ';
|
|
$output .= $value;
|
|
$output .= '</span>';
|
|
$output .= '</li>';
|
|
}
|
|
$output .= '</ul>';
|
|
return $output;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* shortcode to write user info of post author
|
|
* 0 or 1 argument, usage :
|
|
* - [custer_user_info] -> list of all availables infos
|
|
* - [custer_user_info user_email] -> display the email
|
|
* - [custer_user_info user_email user_login] -> display the email
|
|
* - [custer_user_info user_email id='logged_in'] -> display the email of the connected user
|
|
* - [custer_user_info user_email id='author'] -> display the email of the creator of the page/post
|
|
* - [custer_user_info user_email id='logged_in' important] -> display the email of the connected user, even if surrounded by shortcode 'custer_change_id'
|
|
* important keyword has no effect if id='author'
|
|
* - [custer_user_info user_email if_empty="value"] -> display 'value' if the user_email is empty or does not exist
|
|
*
|
|
*/
|
|
function current_user_infos($atts) {
|
|
$is_important = false;
|
|
|
|
|
|
/*
|
|
* choose the default id target :
|
|
* - logged_in
|
|
* - author
|
|
*
|
|
$id_is = 'logged_in';
|
|
*/
|
|
$id_is = 'author';
|
|
$if_empty = '';
|
|
|
|
|
|
/*
|
|
* has parameter 'id' ?
|
|
* has parameter important ?
|
|
* has parameter if_empty ?
|
|
* if yes, handle them and removes them from $atts
|
|
*
|
|
*/
|
|
if (is_array($atts)) {
|
|
if (isset($atts['id'])) {
|
|
$id_is = $atts['id'];
|
|
unset($atts['id']);
|
|
}
|
|
if ($id_is === 'logged_in') {
|
|
if (in_array('important', $atts)) {
|
|
$is_important = true;
|
|
$key = array_search('important', $atts);
|
|
unset($atts[$key]);
|
|
}
|
|
}
|
|
if (isset($atts['if_empty'])) {
|
|
$if_empty = $atts['if_empty'];
|
|
unset($atts['if_empty']);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* should output all or a specific parameter ?
|
|
*
|
|
*/
|
|
$output_all = false;
|
|
if (empty($atts))
|
|
$output_all = true;
|
|
else if (count($atts) === 0)
|
|
$output_all = true;
|
|
|
|
|
|
/*
|
|
* get id according to attributes,
|
|
* and the atts of the other shortcode 'custer_change_id' if in use
|
|
*
|
|
*/
|
|
if ($id_is === 'logged_in') {
|
|
if ($is_important) {
|
|
$user_id = Custer::get_current_user_backup();
|
|
if ($user_id === null)
|
|
$user_id = get_current_user_id();
|
|
}
|
|
else {
|
|
$user_id = get_current_user_id();
|
|
}
|
|
}
|
|
else if ($id_is === 'author') {
|
|
$user_id = \CUSTER\get_author_id();
|
|
}
|
|
else {
|
|
return '';
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
* output all the available parameters (for help)
|
|
*
|
|
*/
|
|
if ($output_all) {
|
|
$user_properties = (array) get_userdata($user_id)->data;
|
|
$user_metas = get_user_meta($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);
|
|
}
|
|
|
|
|
|
/*
|
|
* real purpose of this shortcode :
|
|
* only return the first argument
|
|
*
|
|
*/
|
|
if (is_array($atts))
|
|
$query = $atts[0];
|
|
else if (is_string($atts))
|
|
$query = $atts;
|
|
else
|
|
return '';
|
|
return \CUSTER\format_user_info($query, $user_id, $if_empty);
|
|
}
|
|
add_shortcode('custer_user_info', __NAMESPACE__.'\current_user_infos');
|
|
|
|
|
|
?>
|