170 lines
2.7 KiB
PHP
170 lines
2.7 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_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, $current_user, $user_id, $if_empty = '') {
|
|
$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_url__') {
|
|
$output = get_author_posts_url($user_id);
|
|
return \CUSTER\return_result($output);
|
|
}
|
|
|
|
|
|
/*
|
|
* if is special query __user_post_url__
|
|
* return author page url
|
|
*
|
|
*/
|
|
if ($query === '__user_post_url__') {
|
|
$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);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* check options to format the result
|
|
*
|
|
*/
|
|
return \CUSTER\return_result($output, $if_empty);
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* 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;
|
|
}
|
|
|
|
|
|
|
|
?>
|