created plugin custer, and removed shortcodes change_id and user_infos from cipf plugin
This commit is contained in:
182
plugins/custer/user_infos.php
Normal file
182
plugins/custer/user_infos.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?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_if_array_size_one($value) {
|
||||
if (is_array($value) && count($value) === 1)
|
||||
return reset($value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
function merge_two_arrays($array1, $array2) {
|
||||
$new_array = $array1;
|
||||
foreach ($array2 as $key2 => $value2) {
|
||||
$value = 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 = 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;
|
||||
}
|
||||
}
|
||||
return $new_array;
|
||||
}
|
||||
|
||||
|
||||
function output_list_front($array, $current_user, $user_id) {
|
||||
$output = '<ul>';
|
||||
foreach ($array as $key => $value) {
|
||||
if (str_starts_with($key, '_'))
|
||||
continue ;
|
||||
$value = format_user_info($key, $current_user, $user_id);
|
||||
$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 :
|
||||
* - [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
|
||||
* - [cipf_user_info user_email id='logged_in'] -> display the email of the connected user
|
||||
* - [cipf_user_info user_email id='author'] -> display the email of the creator of the page/post
|
||||
* - [cipf_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'
|
||||
*
|
||||
*/
|
||||
function current_user_infos($atts) {
|
||||
$is_important = false;
|
||||
|
||||
/*
|
||||
* choose the default id target :
|
||||
* - logged_in
|
||||
* - author
|
||||
*
|
||||
$id_is = 'logged_in';
|
||||
*/
|
||||
$id_is = 'author';
|
||||
|
||||
|
||||
/*
|
||||
* has parameter 'id' ?
|
||||
* has parameter important ?
|
||||
* if yes, 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 = get_author_id();
|
||||
}
|
||||
else {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if id is for 'author' && shortcode 'custer_change_id' is set
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
$current_user = get_user_by('id', $user_id);
|
||||
|
||||
|
||||
/*
|
||||
* 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);
|
||||
$user_infos = merge_two_arrays($user_metas, $user_properties);
|
||||
return output_list_front($user_infos, $current_user, $user_id);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 format_user_info($query, $current_user, $user_id);
|
||||
}
|
||||
add_shortcode('custer_user_info', __NAMESPACE__.'\current_user_infos');
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user