Skip to content
wiki.fftac.org

Functions - Source Excerpt 04

Back to Functions

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/themes/antichrist-net/functions.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/themes/antichrist-net/functions.php

$segments = array_values( array_filter( explode( '/', trim( $path, '/' ) ), 'strlen' ) );
	if ( ! empty( $segments ) && in_array( strtolower( $segments[0] ), acn_public_home_locale_tags(), true ) ) {
		array_shift( $segments );
	}

	return empty( $segments ) ? '/' : trailingslashit( '/' . implode( '/', $segments ) );
}

/**
 * Determine whether a header navigation URL matches the current request.
 *
 * @param string $url URL.
 * @return bool
 */
function acn_navigation_url_is_current( $url ) {
	$target_path = acn_navigation_path_key( $url );
	if ( '' === $target_path || 0 === strpos( $target_path, 'external:' ) ) {
		return false;
	}

	$current_path = acn_navigation_path_key( home_url( acn_current_public_request_path() ) );

	return $target_path === $current_path;
}

/**
 * Default primary navigation items for fresh or repaired installs.
 *
 * @return array
 */
function acn_default_primary_navigation_items() {
	$defaults = array(
		array( '', 'acn.nav.home', 'Home' ),
		array( 'archive', 'acn.nav.archive', 'Archive' ),
		array( 'research-agenda', 'acn.nav.research_agenda', 'Research Agenda' ),
		array( 'research-library', 'acn.nav.library', 'Library' ),
		array( 'timeline', 'acn.nav.timeline', 'Timeline' ),
		array( 'symbols', 'acn.nav.symbols', 'Symbols' ),
		array( 'apocalyptic-ai', 'acn.nav.ai', 'Apocalyptic AI' ),
		array( 'state-and-religion', 'acn.nav.state_religion', 'State & Religion' ),
		array( 'surveillance', 'acn.nav.surveillance', 'Surveillance' ),
		array( 'community-baseline', 'acn.nav.community_baseline', 'Community Baseline' ),
		array( 'about', 'acn.nav.about', 'About' ),
	);

	$items = array();
	foreach ( $defaults as $default ) {
		$url     = acn_page_url( $default[0] );
		$current = acn_navigation_url_is_current( $url );
		$items[] = array(
			'label'   => acn_ui_t( $default[1], $default[2] ),
			'url'     => $url,
			'current' => $current,
			'classes' => $current ? array( 'menu-item', 'current-menu-item' ) : array( 'menu-item' ),
		);
	}

	return $items;
}

/**
 * Translate a navigation label from its public path when a saved WordPress menu is used.
 *
 * @param string $url URL.
 * @param string $fallback Existing menu label.
 * @return string
 */
function acn_translated_navigation_label_for_url( $url, $fallback ) {
	$path_key = trim( acn_navigation_path_key( $url ), '/' );
	$map      = array(
		''                   => array( 'acn.nav.home', 'Home' ),
		'archive'            => array( 'acn.nav.archive', 'Archive' ),
		'research-agenda'    => array( 'acn.nav.research_agenda', 'Research Agenda' ),
		'research-library'   => array( 'acn.nav.library', 'Library' ),
		'timeline'           => array( 'acn.nav.timeline', 'Timeline' ),
		'symbols'            => array( 'acn.nav.symbols', 'Symbols' ),
		'apocalyptic-ai'     => array( 'acn.nav.ai', 'Apocalyptic AI' ),
		'state-and-religion' => array( 'acn.nav.state_religion', 'State & Religion' ),
		'surveillance'       => array( 'acn.nav.surveillance', 'Surveillance' ),
		'community-baseline' => array( 'acn.nav.community_baseline', 'Community Baseline' ),
		'about'              => array( 'acn.nav.about', 'About' ),
		'message-board'      => array( 'acn.nav.board', 'Board' ),
		'members'            => array( 'acn.nav.members', 'Members' ),
	);

	if ( ! isset( $map[ $path_key ] ) ) {
		return $fallback;
	}

	return acn_ui_t( $map[ $path_key ][0], $map[ $path_key ][1] );
}

/**
 * Convert a WordPress menu item to the compact header navigation shape.
 *
 * @param WP_Post $menu_item Menu item.
 * @param object  $args Menu args for WordPress filters.
 * @return array|null
 */
function acn_primary_navigation_item_from_menu_item( $menu_item, $args ) {
	if ( ! $menu_item instanceof WP_Post || ! empty( $menu_item->menu_item_parent ) ) {
		return null;
	}

	$title = apply_filters( 'nav_menu_item_title', $menu_item->title, $menu_item, $args, 0 );
	$title = trim( wp_strip_all_tags( (string) $title ) );
	$url   = isset( $menu_item->url ) ? trim( (string) $menu_item->url ) : '';

	if ( '' === $title || '' === $url || '#' === $url ) {
		return null;
	}

	if ( 'post_type' === $menu_item->type && 'page' === $menu_item->object && ! empty( $menu_item->object_id ) ) {
		$page = get_post( (int) $menu_item->object_id );
		if ( $page instanceof WP_Post ) {
			$url = acn_page_url( get_page_uri( $page ) );
		}
	} elseif ( '/' === acn_navigation_path_key( $url ) ) {
		$url = acn_page_url( '' );
	} else {
		$url = acn_localized_url( $url );
	}

	$title   = acn_translated_navigation_label_for_url( $url, $title );
	$current = acn_navigation_url_is_current( $url );
	$classes = array_filter( array_map( 'sanitize_html_class', (array) $menu_item->classes ) );
	$classes[] = 'menu-item';
	$classes[] = 'menu-item-' . (int) $menu_item->ID;
	if ( $current ) {
		$classes[] = 'current-menu-item';
	}

	return array(
		'label'   => $title,
		'url'     => $url,
		'current' => $current,
		'classes' => array_values( array_unique( array_filter( $classes ) ) ),
	);
}

/**
 * Return deduplicated primary navigation items for the header.
 *
 * @return array
 */
function acn_primary_navigation_items() {
	$locations = get_nav_menu_locations();
	$menu_id   = ! empty( $locations['primary'] ) ? (int) $locations['primary'] : 0;
	$items     = array();
	$seen      = array();

	if ( $menu_id ) {
		$args       = (object) array( 'theme_location' => 'primary' );
		$menu_items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) );

		if ( is_array( $menu_items ) ) {
			foreach ( $menu_items as $menu_item ) {
				$item = acn_primary_navigation_item_from_menu_item( $menu_item, $args );
				if ( null === $item ) {
					continue;
				}

				$key = acn_navigation_path_key( $item['url'] );
				if ( '' === $key ) {
					$key = 'label:' . sanitize_title( $item['label'] );
				}

				if ( isset( $seen[ $key ] ) ) {
					continue;
				}

				$seen[ $key ] = true;
				$items[] = $item;
			}
		}
	}

	if ( empty( $items ) ) {
		$items = acn_default_primary_navigation_items();
	}

	return apply_filters( 'acn_primary_navigation_items', $items );
}

/**
 * Render one header navigation link.
 *
 * @param array $item Header navigation item.
 */
function acn_render_primary_navigation_link( $item ) {
	$classes = ! empty( $item['classes'] ) && is_array( $item['classes'] ) ? $item['classes'] : array( 'menu-item' );
	$classes = array_values( array_unique( array_filter( array_map( 'sanitize_html_class', $classes ) ) ) );
	$current = ! empty( $item['current'] ) ? ' aria-current="page"' : '';

	printf(
		'<li class="%1$s"><a href="%2$s"%3$s>%4$s</a></li>',
		esc_attr( implode( ' ', $classes ) ),
		esc_url( $item['url'] ),
		$current,
		esc_html( $item['label'] )
	);
}

/**
 * Render the compact primary navigation used by the site header.
 */
function acn_render_primary_navigation() {
	$items = acn_primary_navigation_items();
	if ( empty( $items ) ) {
		acn_fallback_menu();
		return;
	}

	$direct_count = (int) apply_filters( 'acn_primary_navigation_direct_count', 7 );
	$direct_count = max( 1, min( count( $items ), $direct_count ) );
	$direct_items = array_slice( $items, 0, $direct_count );
	$more_items   = array_slice( $items, $direct_count );
	$more_current = false;

	foreach ( $more_items as $item ) {
		if ( ! empty( $item['current'] ) ) {
			$more_current = true;
			break;
		}
	}

	echo '<ul id="primary-menu" class="menu primary-menu">';
	foreach ( $direct_items as $item ) {
		acn_render_primary_navigation_link( $item );
	}

	if ( ! empty( $more_items ) ) {
		$more_classes = array( 'menu-item', 'nav-more-item' );
		if ( $more_current ) {
			$more_classes[] = 'current-menu-ancestor';
		}

		printf(
			'<li class="%1$s"><details class="nav-more"><summary>%2$s</summary><ul class="nav-more-menu" aria-label="%3$s">',
			esc_attr( implode( ' ', $more_classes ) ),
			esc_html( acn_ui_t( 'acn.nav.more', 'More' ) ),
			esc_attr__( 'More primary navigation', 'antichrist-net' )
		);
		foreach ( $more_items as $item ) {
			acn_render_primary_navigation_link( $item );
		}
		echo '</ul></details></li>';
	}

	echo '</ul>';
}

/**
 * Render a compact locale switcher in the header.
 */
function acn_locale_switcher() {
	$shortcode = '';

	if ( shortcode_exists( 'uaix_locale_switcher' ) ) {
		$shortcode = 'uaix_locale_switcher';
	} elseif ( shortcode_exists( 'ns12_locale_switcher' ) ) {
		$shortcode = 'ns12_locale_switcher';
	}

	if ( '' === $shortcode ) {
		return;
	}

	echo '<div class="header-locale">';
	echo acn_compact_locale_switcher_markup( do_shortcode( '[' . $shortcode . ']' ) );
	echo '</div>';
}

/**
 * Shorten native locale names inside the tight header control.
 *
 * @param string $markup Switcher markup.
 * @return string
 */
function acn_compact_locale_switcher_markup( $markup ) {
	$markup = (string) $markup;