<?php
|
if (!defined('ABSPATH')) {
|
exit; // Exit if accessed directly
|
}
|
/**
|
* Directory List Block Render
|
*
|
* @package Edmonton_Ink
|
*/
|
|
function jvbRenderListBlock(array $attributes):string
|
{
|
// Get the selected list type
|
$selected_type_slug = $attributes['listType'] ?? BASE.'artist';
|
$refresh_cache = $attributes['refreshCache'] ?? false;
|
$types = jvbGlobalDirectoryInfo();
|
$umami = new JVBase\managers\UmamiTracker();
|
|
// Find the selected type in the types array
|
$selected_type = null;
|
foreach ($types as $type) {
|
if ($type['slug'] === $selected_type_slug) {
|
$selected_type = $type;
|
break;
|
}
|
}
|
|
// If no valid type was found, return an error message
|
if (!$selected_type) {
|
return '<p>Error: Selected list type not found.</p>';
|
}
|
// Buffer output
|
ob_start();
|
|
// Add the "More Lists" button
|
echo '<a class="button" href="#directory-list">'.
|
jvbIcon('plus', ['title'=>'More Lists']).' <span>More Lists</span></a>';
|
|
// Get the list data
|
$list = get_option(BASE.$selected_type['slug'].'_list');
|
$list = false;
|
// If refresh cache is enabled or list doesn't exist, regenerate it
|
if ($refresh_cache || $list === false) {
|
$list = array();
|
if ($selected_type['type'] == 'post') {
|
$get = new WP_Query(array(
|
'post_type' => $selected_type['slug'],
|
'posts_per_page' => -1,
|
'orderby' => 'title',
|
'order' => 'ASC'
|
));
|
if ($get->have_posts()) {
|
while ($get->have_posts()) {
|
$get->the_post();
|
$extra = false;
|
if (!empty($selected_type['links'])) {
|
$extra = [];
|
foreach ($selected_type['links'] as $item) {
|
$terms = get_the_terms(get_the_ID(), $item);
|
if ($terms && !is_wp_error($terms)) {
|
$term = $terms[0];
|
$extra[] = [
|
'name' => html_entity_decode($term->name),
|
'url' => get_term_link($term->term_id, $item),
|
'id' => $term->term_id,
|
'type' => $item,
|
];
|
}
|
}
|
}
|
$list = jvbAlphabetizeMe(
|
$list,
|
get_the_title(),
|
get_the_permalink(),
|
get_the_ID(),
|
$extra
|
);
|
}
|
}
|
wp_reset_postdata();
|
} elseif ($selected_type['type'] == 'term') {
|
$terms = get_terms(array(
|
'taxonomy' => $selected_type['slug'],
|
'hide_empty' => true,
|
'orderby' => 'name',
|
'order' => 'ASC',
|
));
|
if ($terms) {
|
foreach ($terms as $term) {
|
$extra = false;
|
$list = jvbAlphabetizeMe(
|
$list,
|
html_entity_decode($term->name),
|
get_term_link($term->term_id, $selected_type['slug']),
|
$term->term_id,
|
$extra
|
);
|
}
|
}
|
}
|
update_option(BASE.$selected_type['slug'].'_list', $list);
|
}
|
|
// Check if this is a hierarchical type
|
if (array_key_exists('hierarchical', $selected_type) && $selected_type['hierarchical']) {
|
echo jvbRenderThemesHierarchical();
|
return ob_get_clean();
|
}
|
// Build the HTML output
|
$out = '<section class="directory-list artists">';
|
if (empty($list)) {
|
$out .= '<h2>Nothing here.</h2><p>We don\'t have anything to show here yet.</p>';
|
} else {
|
$out .= '<nav class="letters on-this-page"><ul>';
|
$nav = jvbLetters();
|
$letters = array_keys($list);
|
foreach ($nav as $l) {
|
$aOpen = $aClose = $class = '';
|
if (in_array($l, $letters)) {
|
$aOpen = '<a href="#starts-with-'.$l.'">';
|
$aClose = '</a>';
|
$class = ' class="has"';
|
}
|
$out .= '<li'.$class.'>'.$aOpen.strtoupper($l).$aClose.'</li>';
|
}
|
$out .= '</ul></nav><ul class="list-none">';
|
foreach ($list as $letter => $artists) {
|
$out .= '<li id="starts-with-'.$letter.'"><h3>'.strtoupper($letter).'</h3><ul>';
|
foreach ($artists as $a) {
|
$extra = '';
|
if ($a['extra'] !== false) {
|
$extra = '<span>';
|
foreach ($a['extra'] as $ext) {
|
$umamiType = 'click_taxonomy';
|
if ($ext['type'] == BASE.'shop') {
|
$umamiType = 'click_shop';
|
}
|
$extra .= '<a href="'.$ext['url'].'" '.
|
$umami->attributesToString(
|
$umami->buildAttributes(
|
$umamiType,
|
$selected_type['slug'],
|
['source_type' => 'directory']
|
)
|
).'>'.$ext['name'].'</a>';
|
}
|
$extra .= '</span>';
|
}
|
$umamiType = 'click_profile';
|
if ($selected_type['slug'] == BASE.'shop') {
|
$umamiType = 'click_shop';
|
} elseif ($selected_type['type'] == 'taxonomy') {
|
$umamiType = 'click_taxonomy';
|
}
|
|
$out .= '<li><a href="'.$a['url'].'" title="More about '.$a['name'].'" '.
|
$umami->attributesToString(
|
$umami->buildAttributes(
|
$umamiType,
|
$selected_type['slug'],
|
['source_type' => 'directory']
|
)
|
).'>'.$a['name'].'</a>'.$extra.'</li>';
|
}
|
$out .= '</ul></li>';
|
}
|
$out .= '</ul>';
|
}
|
$out .= '</section>';
|
|
echo $out;
|
echo JVB()->directories()?->renderNavigation();
|
|
return ob_get_clean();
|
}
|