turned custer into a repo
This commit is contained in:
145
format_user_infos.php
Normal file
145
format_user_infos.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?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!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* return the result
|
||||
*
|
||||
*/
|
||||
function return_result($output, $if_empty = '') {
|
||||
|
||||
/*
|
||||
* if empty, apply options, or default empty string ''
|
||||
*
|
||||
*/
|
||||
if (empty($output)) {
|
||||
$output = $if_empty;
|
||||
}
|
||||
|
||||
/*
|
||||
* if number, output a string
|
||||
*
|
||||
*/
|
||||
if (is_numeric($output)) {
|
||||
$output = (string)$output;
|
||||
}
|
||||
|
||||
/*
|
||||
* if not scalar, stringify output
|
||||
*
|
||||
*/
|
||||
if (!is_scalar($output)) {
|
||||
$output = json_encode($output, JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
return esc_html($output);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* format the output
|
||||
* if is acf, use acf default format
|
||||
*/
|
||||
function format_user_info($query, $user_id, $if_empty = '') {
|
||||
$output_date_format = Custer::USER_INFO_DATE_FORMAT;
|
||||
$special_queries = Custer::QUERIES;
|
||||
|
||||
$is_acf = false;
|
||||
|
||||
|
||||
/*
|
||||
* if is special query
|
||||
*
|
||||
*/
|
||||
if (in_array($query, $special_queries, true)) {
|
||||
$trimmed_query = trim($query, '_');
|
||||
$function_name = __NAMESPACE__.'\find_'.$trimmed_query;
|
||||
$output = $function_name($user_id);
|
||||
return \CUSTER\return_result($output, $if_empty);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* for the rest, we need the user
|
||||
*
|
||||
*/
|
||||
$current_user = get_user_by('id', $user_id);
|
||||
if (empty($current_user)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* check if it's an acf field
|
||||
*
|
||||
*/
|
||||
$acf_id = 'user_'.$user_id;
|
||||
$acf_object = get_field_object($query, $acf_id);
|
||||
if ($acf_object !== false) {
|
||||
$is_acf = true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if is acf, use the acf return format
|
||||
* otherwise, use the default wordpress value
|
||||
*
|
||||
*/
|
||||
if ($is_acf) {
|
||||
$output = get_field($query, $acf_id);
|
||||
}
|
||||
else {
|
||||
$output = $current_user->$query;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* try to extract a single entity
|
||||
* for example : ["string"] -> "string"
|
||||
* or : ["", "string"] -> "string"
|
||||
*
|
||||
*/
|
||||
$output = \CUSTER\extract_smallest_entity($output);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* if is not acf
|
||||
* check if is a date
|
||||
* and format it
|
||||
* to create a DateTime from a time stamp : https://stackoverflow.com/a/12039058/9497573
|
||||
*
|
||||
*/
|
||||
if (!$is_acf) {
|
||||
$timestamp = false;
|
||||
if (is_string($output)) {
|
||||
$timestamp = strtotime($output);
|
||||
}
|
||||
if ($timestamp !== false) {
|
||||
//$date = new \DateTime('@' . $timestamp);
|
||||
$date = date_create('@' . $timestamp);
|
||||
$output = $date->format($output_date_format);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* check options to format the result
|
||||
*
|
||||
*/
|
||||
return \CUSTER\return_result($output, $if_empty);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user