Files
2024_WEBSITE_fipf/plugins/custer/format_user_infos.php

146 lines
2.4 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!');
}
/*
* return the result
*
*/
function return_result($output) {
if (is_array($output) && count($output) === 0) {
$output = '';
}
if (is_numeric($output)) {
$output = (string)$output;
}
if (!is_string($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, &$current_user, $user_id) {
$output_date_format = Custer::USER_INFO_DATE_FORMAT;
$is_acf = false;
/*
* if is special query __author_page__
* return author page url
*
*/
if ($query === '__author_page__') {
$output = get_author_posts_url($user_id);
return \CUSTER\return_result($output);
}
/*
* if is special query __user_post__
* return author page url
*
*/
if ($query === '__user_post__') {
$output = \CUSTER\find_user_post_url($user_id);
return \CUSTER\return_result($output);
}
/*
* 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 string
*
*/
while (is_array($output) && count($output) === 1)
$output = reset($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);
}
}
return \CUSTER\return_result($output);
}
/*
* find the posts of the user and return the first one
*
*/
function find_user_post_url($user_id) {
$user_post_url = '';
$args = array(
'post_type' => 'post',
'author' => $user_id,
'posts_per_page' => 1,
);
$posts = get_posts($args);
if (empty($posts)) {
$user_post_url = '';
}
else {
$query = reset($posts);
$user_post_url = get_permalink($query->ID);
}
return $user_post_url;
}
?>