changed plugin name to cipf
This commit is contained in:
221
plugins/cipf_plugin/php/user_infos.php
Normal file
221
plugins/cipf_plugin/php/user_infos.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
|
||||
function format_user_info_CIPF($output, $query, &$current_user, $user_id) {
|
||||
$output_date_format = PLGNTLS_class::USER_INFO_DATE_FORMAT;
|
||||
|
||||
$is_acf = false;
|
||||
$is_date = false;
|
||||
|
||||
/*
|
||||
* check if it's an acf field
|
||||
* first method : check if _field exist
|
||||
*
|
||||
$_acf_field = '_'.$query;
|
||||
$acf_field = $current_user->$_acf_field;
|
||||
if (!empty($acf_field))
|
||||
$is_acf = true;
|
||||
*/
|
||||
|
||||
/*
|
||||
* check if it's an acf field, and a date
|
||||
* second method : check what get_field_object() returns
|
||||
*
|
||||
*/
|
||||
$acf_id = 'user_'.$user_id;
|
||||
$acf_object = get_field_object($query, $acf_id);
|
||||
if ($acf_object !== false)
|
||||
$is_acf = true;
|
||||
|
||||
/*
|
||||
* check if is date
|
||||
*
|
||||
*/
|
||||
if ($is_acf) {
|
||||
$acf_type = $acf_object['type'];
|
||||
if ($acf_type && $acf_type === "date_picker")
|
||||
$is_date = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* if is date, transform format
|
||||
*
|
||||
*/
|
||||
if ($is_date) {
|
||||
$acf_date_string = $acf_object['value'];
|
||||
$acf_date_format = $acf_object['return_format'];
|
||||
$date = date_create_from_format($acf_date_format, $acf_date_string);
|
||||
$output = $date->format($output_date_format);
|
||||
}
|
||||
|
||||
/*
|
||||
* return the result
|
||||
*
|
||||
*/
|
||||
if (is_string($output))
|
||||
return $output;
|
||||
else
|
||||
return json_encode($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 author='logged_in'] -> display the email of the connected user
|
||||
* - [cipf_user_info user_email author='post_creator'] -> display the email of the creator of the page/post
|
||||
*
|
||||
*/
|
||||
function current_user_infos_CIPF($atts) {
|
||||
if (!is_user_logged_in())
|
||||
return ;
|
||||
|
||||
|
||||
/*
|
||||
* choose the default id target : logged in user, or post creator ?
|
||||
*
|
||||
$author_is = 'logged_in'; // logged in user
|
||||
*/
|
||||
$author_is = 'post_creator'; // creator of post (also for author pages)
|
||||
|
||||
|
||||
/*
|
||||
* has parameter 'author' ?
|
||||
* if yes, removes it from $atts
|
||||
*
|
||||
*/
|
||||
if (is_array($atts)) {
|
||||
if (isset($atts['author'])) {
|
||||
$author_is = $atts['author'];
|
||||
unset($atts['author']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 author id outside loop and outside singular page : https://wordpress.stackexchange.com/q/65548
|
||||
*
|
||||
*/
|
||||
if ($author_is === 'logged_in') {
|
||||
$current_user = wp_get_current_user();
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
else {
|
||||
if (is_author()) {
|
||||
$user_id = get_queried_object_id();
|
||||
}
|
||||
else if (in_the_loop()) {
|
||||
$user_id = get_the_author_meta('ID');
|
||||
}
|
||||
else if (is_singular()) {
|
||||
$user_id = get_queried_object()->post_author;
|
||||
}
|
||||
else {
|
||||
global $wp_query;
|
||||
if (!empty($wp_query->posts))
|
||||
$user_id = $wp_query->posts[0]->post_author;
|
||||
}
|
||||
//$current_user = new WP_User($user_id);
|
||||
$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_CIPF($user_metas, $user_properties);
|
||||
return output_list_front_CIPF($user_infos);
|
||||
}
|
||||
|
||||
/*
|
||||
* real purpose of this shortcode :
|
||||
* only return the first argument (that is not 'author')
|
||||
*
|
||||
*/
|
||||
if (is_array($atts))
|
||||
$query = reset($atts);
|
||||
else if (is_string($atts))
|
||||
$query = $atts;
|
||||
else
|
||||
return '';
|
||||
$output = $current_user->$query;
|
||||
return format_user_info_CIPF($output, $query, $current_user, $user_id);
|
||||
}
|
||||
add_shortcode('cipf_user_info', 'current_user_infos_CIPF');
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user