$taxonomy->name,
'label' => $taxonomy->labels->name,
);
}
wp_localize_script($script_handle, 'AnsicoLNTaxonomilisteData', array('taxonomies' => $taxonomies));
register_block_type('ansico-ln/taxonomiliste', array(
'editor_script' => $script_handle,
'render_callback' => array($this, 'render_taxonomiliste_block'),
'attributes' => array(
'taxonomy' => array('type' => 'string', 'default' => ''),
'displayType' => array('type' => 'string', 'default' => 'list'),
'showCount' => array('type' => 'boolean', 'default' => false),
'showAll' => array('type' => 'boolean', 'default' => true),
'limit' => array('type' => 'number', 'default' => 20),
'sortBy' => array('type' => 'string', 'default' => 'alphabetical'),
),
));
}
public function render_taxonomiliste_block($attributes) {
$taxonomy = isset($attributes['taxonomy']) ? sanitize_key($attributes['taxonomy']) : '';
if (!$taxonomy || !taxonomy_exists($taxonomy)) {
return '';
}
$show_count = !empty($attributes['showCount']);
$show_all = !empty($attributes['showAll']);
$limit = !empty($attributes['limit']) ? max(1, absint($attributes['limit'])) : 20;
$sort_by = isset($attributes['sortBy']) && $attributes['sortBy'] === 'count' ? 'count' : 'alphabetical';
$display_type = isset($attributes['displayType']) && $attributes['displayType'] === 'cloud' ? 'cloud' : 'list';
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'number' => $show_all ? 0 : $limit,
'orderby' => $sort_by === 'count' ? 'count' : 'name',
'order' => $sort_by === 'count' ? 'DESC' : 'ASC',
));
if (is_wp_error($terms) || empty($terms)) {
return '';
}
ob_start();
if ($display_type === 'cloud') {
echo '
';
} else {
echo '';
foreach ($terms as $term) {
echo '- ' . esc_html($term->name) . '';
if ($show_count) {
echo ' (' . intval($term->count) . ')';
}
echo '
';
}
echo '
';
}
self::$toc_assets_needed = true;
return trim(ob_get_clean());
}
public function include_post_types_in_search($query) {
if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
return;
}
$query->set('post_type', self::get_searchable_post_types());
}
public function template_include($template) {
if (self::is_block_theme_active()) {
return $template;
}
if (is_search()) {
$custom_template = plugin_dir_path(__FILE__) . 'templates/search.php';
if (file_exists($custom_template)) return $custom_template;
}
if (is_post_type_archive() && self::is_enabled_post_type_archive(self::get_current_archive_post_type())) {
$custom_template = plugin_dir_path(__FILE__) . 'templates/archive-post-type.php';
if (file_exists($custom_template)) return $custom_template;
}
if ((is_tax() || is_category() || is_tag()) && self::is_enabled_taxonomy_archive(self::get_current_taxonomy_name())) {
$custom_template = plugin_dir_path(__FILE__) . 'templates/archive-taxonomy.php';
if (file_exists($custom_template)) return $custom_template;
}
return $template;
}
public function render_block_content($block_content, $block) {
if (is_admin() || !self::is_block_theme_active() || empty($block['blockName'])) {
return $block_content;
}
if (in_array($block['blockName'], array('core/post-author', 'core/post-author-name', 'core/post-date'), true)) {
$post_id = 0;
if (!empty($block['attrs']['postId'])) {
$post_id = absint($block['attrs']['postId']);
} elseif (get_the_ID()) {
$post_id = get_the_ID();
}
if ($post_id) {
$post_type = get_post_type($post_id);
if (in_array($block['blockName'], array('core/post-author', 'core/post-author-name'), true) && self::should_hide_author_for_post_type($post_type)) {
return '';
}
if ($block['blockName'] === 'core/post-date' && self::should_hide_date_for_post_type($post_type)) {
return '';
}
}
}
if (!self::should_replace_block_results()) {
self::$block_replaced = false;
return $block_content;
}
if (
(is_post_type_archive() || is_tax() || is_category() || is_tag()) &&
in_array($block['blockName'], array('core/query-title', 'core/archive-title', 'core/term-description'), true)
) {
return '';
}
if (!self::$block_replaced && $block['blockName'] === 'core/post-template') {
self::$block_replaced = true;
return self::get_block_theme_markup();
}
if (self::$block_replaced && in_array($block['blockName'], array('core/query-pagination', 'core/query-no-results'), true)) {
return '';
}
return $block_content;
}
public function print_meta_visibility_css() {
if (is_admin() || !is_singular()) {
return;
}
$post_type = self::get_context_post_type();
if (!$post_type) {
return;
}
$css = '';
if (self::should_hide_author_for_post_type($post_type)) {
$css .= '.author,.byline,.entry-author,.post-author,.wp-block-post-author,.wp-block-post-author-name,[class*="author-meta"],[class*="post-author"],[class*="byline"],[rel="author"],.wp-block-post-author__name,.wp-block-post-author__content{display:none !important;}';
$css .= '.wp-block-post-author:has(a),.wp-block-post-author:has([rel="author"]),.byline:has(a),.entry-meta:has([rel="author"]),[class*="author"]:has(a[href*="/author/"]){display:none !important;}';
$css .= '.meta-separator,.entry-meta .separator,.post-meta .separator,.wp-block-post-date + .separator,.wp-block-post-date + .meta-separator,.wp-block-post-date + span.separator,.wp-block-post-date + span.meta-separator,.wp-block-post-date + p,.wp-block-post-date + .wp-block-post-author::before,.wp-block-post-date + .wp-block-post-author-name::before{display:none !important;}';
}
if (self::should_hide_date_for_post_type($post_type)) {
$css .= '.posted-on,.entry-date,.post-date,.wp-block-post-date,time.entry-date,time.published,[class*="post-date"],[class*="entry-date"]{display:none !important;}';
}
if ($css !== '') {
echo '';
}
}
public function print_meta_visibility_script() {
if (is_admin() || !is_singular()) {
return;
}
$post_type = self::get_context_post_type();
if (!$post_type || !self::should_hide_author_for_post_type($post_type)) {
return;
}
$author_archive = '';
$custom_url = trim((string) self::get_setting('author_custom_url', ''));
$author_id = get_post_field('post_author', get_the_ID());
if ($author_id) {
$author_archive = get_author_posts_url($author_id);
}
?>
$post_type_object) {
if (!empty($input['hidden_post_types']) && in_array($post_type_name, (array) $input['hidden_post_types'], true)) {
$sanitized['hidden_post_types'][] = $post_type_name;
}
if (!empty($input['archive_post_types']) && in_array($post_type_name, (array) $input['archive_post_types'], true)) {
$sanitized['archive_post_types'][] = $post_type_name;
}
if (!empty($input['toc_post_types']) && in_array($post_type_name, (array) $input['toc_post_types'], true)) {
$sanitized['toc_post_types'][] = $post_type_name;
}
if (!empty($input['hide_author_post_types']) && in_array($post_type_name, (array) $input['hide_author_post_types'], true)) {
$sanitized['hide_author_post_types'][] = $post_type_name;
}
if (!empty($input['hide_date_post_types']) && in_array($post_type_name, (array) $input['hide_date_post_types'], true)) {
$sanitized['hide_date_post_types'][] = $post_type_name;
}
if (!empty($input['modified_date_post_types']) && in_array($post_type_name, (array) $input['modified_date_post_types'], true)) {
$sanitized['modified_date_post_types'][] = $post_type_name;
}
if (!empty($input['citation_post_types']) && in_array($post_type_name, (array) $input['citation_post_types'], true)) {
$sanitized['citation_post_types'][] = $post_type_name;
}
$order = isset($input['post_type_order'][$post_type_name]) ? absint($input['post_type_order'][$post_type_name]) : 9999;
$sanitized['post_type_order'][$post_type_name] = $order;
}
foreach (self::get_available_taxonomies_for_settings() as $taxonomy_name => $taxonomy_object) {
if (!empty($input['archive_taxonomies']) && in_array($taxonomy_name, (array) $input['archive_taxonomies'], true)) {
$sanitized['archive_taxonomies'][] = $taxonomy_name;
}
}
return $sanitized;
}
public function render_settings_page() {
if (!current_user_can('manage_options')) return;
$settings = self::get_settings();
$post_types = self::get_ordered_available_post_types();
$taxonomies = self::get_available_taxonomies_for_settings();
?>
post_type, $settings['toc_post_types'], true)) {
return $content;
}
$headings = self::extract_headings($content);
if (count($headings) < (int) $settings['toc_min_headings']) {
return $content;
}
$content_with_ids = self::add_heading_ids($content, $headings);
$toc_markup = self::build_toc_markup($headings, $settings);
if (!$toc_markup) {
return $content;
}
self::$toc_assets_needed = true;
$first_id = $headings[0]['id'];
$pattern = '/(]*id="' . preg_quote($first_id, '/') . '"[^>]*>)/i';
return preg_replace($pattern, $toc_markup . '$1', $content_with_ids, 1);
}
public static function extract_headings($content) {
$headings = array();
if (!preg_match_all('/]*)>(.*?)<\/h\1>/is', $content, $matches, PREG_SET_ORDER)) {
return $headings;
}
$used_ids = array();
foreach ($matches as $index => $match) {
$level = (int) $match[1];
$text = trim(wp_strip_all_tags($match[3]));
if ($text === '') continue;
$base_id = sanitize_title($text);
if ($base_id === '') $base_id = 'overskrift-' . ($index + 1);
$id = $base_id;
$suffix = 2;
while (isset($used_ids[$id])) {
$id = $base_id . '-' . $suffix;
$suffix++;
}
$used_ids[$id] = true;
$headings[] = array('level' => $level, 'text' => $text, 'id' => $id);
}
return $headings;
}
public static function add_heading_ids($content, $headings) {
$i = 0;
return preg_replace_callback('/]*)>(.*?)<\/h\1>/is', function ($match) use ($headings, &$i) {
if (!isset($headings[$i])) return $match[0];
$heading = $headings[$i];
$attrs = $match[2];
if (preg_match('/\sid=("|\")(.*?)(\1)/i', $attrs)) {
$i++;
return $match[0];
}
$i++;
return '' . $match[3] . '';
}, $content);
}
public static function build_toc_markup($headings, $settings) {
if (empty($headings)) return '';
$title = !empty($settings['toc_title']) ? $settings['toc_title'] : 'INDHOLD';
$is_numbered = isset($settings['toc_list_type']) && $settings['toc_list_type'] === 'numbers';
$collapsible = !empty($settings['toc_collapsible']);
$scrollspy = !empty($settings['toc_scrollspy']);
$items = array();
$counters = array(2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
foreach ($headings as $heading) {
$level = (int) $heading['level'];
$counters[$level]++;
for ($reset = $level + 1; $reset <= 6; $reset++) {
$counters[$reset] = 0;
}
$number = '';
if ($is_numbered) {
$parts = array();
for ($l = 2; $l <= $level; $l++) {
if ($counters[$l] > 0) $parts[] = $counters[$l];
}
$number = implode('.', $parts);
}
$items[] = array_merge($heading, array('number' => $number));
}
ob_start();
echo '