custer v0.2.2 includes queries in all output

This commit is contained in:
asus
2024-04-02 18:50:56 +02:00
parent 1f5fff26d6
commit ccb800c203
7 changed files with 134 additions and 89 deletions

View File

@@ -48,33 +48,26 @@ function return_result($output, $if_empty = '') {
* format the output
* if is acf, use acf default format
*/
function format_user_info($query, $current_user, $user_id, $if_empty = '') {
function format_user_info($query, $user_id, $if_empty = '') {
$output_date_format = Custer::USER_INFO_DATE_FORMAT;
$special_queries = Custer::QUERIES;
$current_user = get_user_by('id', $user_id);
$is_acf = false;
/*
* if is special query __author_page__
* return author page url
* if is special query
*
*/
if ($query === '__author_page_url__') {
$output = get_author_posts_url($user_id);
return \CUSTER\return_result($output);
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);
}
/*
* 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
@@ -101,12 +94,12 @@ function format_user_info($query, $current_user, $user_id, $if_empty = '') {
/*
* try to extract a string
* try to extract a single entity
* for example : ["string"] -> "string"
* or : ["", "string"] -> "string"
*
*/
while (is_array($output) && count($output) === 1) {
$output = reset($output);
}
$output = \CUSTER\extract_smallest_entity($output);
@@ -139,31 +132,5 @@ function format_user_info($query, $current_user, $user_id, $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;
}
?>