Skip to content
wiki.fftac.org

Template Functions - Source Excerpt 01

Back to Template Functions

Summary

This source excerpt preserves a bounded section of 2IA.org/wp-content/themes/twoia-intelligence/inc/template-functions.php so readers can inspect the evidence without opening the full source file.

**Source path:** 2IA.org/wp-content/themes/twoia-intelligence/inc/template-functions.php

<?php
/**
 * Template helper functions.
 *
 * @package TwoIA
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Return a theme mod with a fallback.
 *
 * @param string $name    Theme mod name.
 * @param string $default Fallback value.
 * @return string
 */
function twoia_theme_mod( $name, $default = '' ) {
	$value = get_theme_mod( $name, $default );

	if ( '' === $value || null === $value ) {
		return $default;
	}

	return $value;
}

/**
 * Return the public-facing site name used in metadata.
 *
 * @return string
 */
function twoia_public_site_name() {
	return __( '2IA.org', 'two-identities-anonymous' );
}

/**
 * Keep frontend document titles aligned with 2IA branding.
 *
 * @param array $parts Title parts.
 * @return array
 */
function twoia_document_title_site_name( $parts ) {
	if ( ! is_admin() ) {
		if ( is_front_page() || is_home() ) {
			$parts['title'] = __( '2IA | Public-Interest Research on Surveillance, Intelligence Power, and Freedom', 'two-identities-anonymous' );
			unset( $parts['site'], $parts['tagline'] );
			return $parts;
		}

		$parts['site'] = twoia_public_site_name();
	}

	return $parts;
}
add_filter( 'document_title_parts', 'twoia_document_title_site_name', 20 );

/**
 * Prefix relative URLs with the site home URL.
 *
 * @param string $url Raw URL.
 * @return string
 */
function twoia_normalize_url( $url ) {
	$url = trim( (string) $url );

	if ( '' === $url ) {
		return home_url( '/' );
	}

	if ( 0 === strpos( $url, '/' ) ) {
		return home_url( $url );
	}

	return $url;
}

/**
 * Return the sitewide civil-liberties key phrase.
 *
 * @return string
 */
function twoia_civil_libertarian_key_phrase() {
	return __( 'Lawful public intelligence for human freedom.', 'two-identities-anonymous' );
}

/**
 * Get default briefing image URL.
 *
 * @return string
 */
function twoia_default_briefing_image_url() {
	return get_template_directory_uri() . '/assets/images/default-briefing.svg';
}

/**
 * Print featured image or safe SVG fallback.
 *
 * @param string $size Image size.
 */
function twoia_post_thumbnail_or_default( $size = 'briefing-card' ) {
	if ( has_post_thumbnail() ) {
		the_post_thumbnail(
			$size,
			array(
				'class'   => 'briefing-image',
				'loading' => 'lazy',
			)
		);
		return;
	}
	?>
	<img class="briefing-image briefing-image--default" src="<?php echo esc_url( twoia_default_briefing_image_url() ); ?>" alt="" loading="lazy" decoding="async">
	<?php
}

/**
 * Calculate reading time.
 *
 * @param int|null $post_id Post ID.
 * @return string
 */
function twoia_reading_time( $post_id = null ) {
	$post_id = $post_id ? absint( $post_id ) : get_the_ID();
	$content = get_post_field( 'post_content', $post_id );
	$words   = str_word_count( wp_strip_all_tags( strip_shortcodes( $content ) ) );
	$minutes = max( 1, (int) ceil( $words / 220 ) );

	return sprintf(
		/* translators: %d: Reading time in minutes. */
		esc_html( _n( '%d minute read', '%d minutes read', $minutes, 'two-identities-anonymous' ) ),
		$minutes
	);
}

/**
 * Display post categories as briefing chips.
 */
function twoia_post_categories() {
	$categories = get_the_category();

	if ( empty( $categories ) ) {
		return;
	}

	echo '<ul class="entry-categories" aria-label="' . esc_attr__( 'Categories', 'two-identities-anonymous' ) . '">';
	foreach ( $categories as $category ) {
		printf(
			'<li><a href="%1$s">%2$s</a></li>',
			esc_url( get_category_link( $category ) ),
			esc_html( $category->name )
		);
	}
	echo '</ul>';
}

/**
 * Return a readable label for the current editorial content type.
 *
 * @param int|null $post_id Post ID.
 * @return string
 */
function twoia_post_type_label( $post_id = null ) {
	$post_type = get_post_type( $post_id );

	if ( ! $post_type ) {
		return '';
	}

	$object = get_post_type_object( $post_type );

	if ( ! $object || empty( $object->labels->singular_name ) ) {
		return '';
	}

	return $object->labels->singular_name;
}

/**
 * Display tag list.
 */
function twoia_post_tags() {
	$tags = get_the_tags();

	if ( empty( $tags ) || is_wp_error( $tags ) ) {
		return;
	}

	echo '<ul class="entry-tags" aria-label="' . esc_attr__( 'Tags', 'two-identities-anonymous' ) . '">';
	foreach ( $tags as $tag ) {
		printf(
			'<li><a href="%1$s">#%2$s</a></li>',
			esc_url( get_tag_link( $tag ) ),
			esc_html( $tag->name )
		);
	}
	echo '</ul>';
}

/**
 * Output byline metadata.
 */
function twoia_post_meta() {
	?>
	<div class="entry-meta metadata-grid metadata-grid--inline">
		<span>
			<strong><?php esc_html_e( 'Filed', 'two-identities-anonymous' ); ?></strong>
			<time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>"><?php echo esc_html( get_the_date() ); ?></time>
		</span>
		<?php if ( twoia_post_type_label() ) : ?>
			<span>
				<strong><?php esc_html_e( 'Format', 'two-identities-anonymous' ); ?></strong>
				<span><?php echo esc_html( twoia_post_type_label() ); ?></span>
			</span>
		<?php endif; ?>
		<span>
			<strong><?php esc_html_e( 'Analyst', 'two-identities-anonymous' ); ?></strong>
			<span><?php echo esc_html( get_the_author() ); ?></span>
		</span>
		<span>
			<strong><?php esc_html_e( 'Length', 'two-identities-anonymous' ); ?></strong>
			<span><?php echo esc_html( twoia_reading_time() ); ?></span>
		</span>
	</div>
	<?php
}

/**
 * Get a trimmed excerpt for cards and SEO.
 *
 * @param int $words Number of words.
 * @return string
 */
function twoia_get_the_excerpt( $words = 28 ) {
	if ( has_excerpt() ) {
		$excerpt = get_the_excerpt();
	} else {
		$excerpt = get_the_content( null, false );
	}

	return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), absint( $words ), '…' );
}

/**
 * Detect the default WordPress placeholder post.
 *
 * @param WP_Post|null $post Post object.
 * @return bool
 */
function twoia_is_default_placeholder_post( $post = null ) {
	$post = get_post( $post );

	if ( ! $post instanceof WP_Post ) {
		return false;
	}

	return 'hello-world' === $post->post_name && 'Hello world!' === wp_strip_all_tags( get_the_title( $post ) );
}

/**
 * Pagination wrapper.
 */
function twoia_posts_pagination() {
	$links = paginate_links(
		array(
			'type'      => 'list',
			'prev_text' => esc_html__( 'Previous', 'two-identities-anonymous' ),
			'next_text' => esc_html__( 'Next', 'two-identities-anonymous' ),
		)
	);

	if ( $links ) {
		echo '<nav class="pagination pagination--archive" aria-label="' . esc_attr__( 'Posts pagination', 'two-identities-anonymous' ) . '">';
		echo wp_kses_post( $links );
		echo '</nav>';
	}
}

/**
 * Primary menu fallback.
 */
function twoia_primary_menu_fallback() {
	?>
	<ul id="primary-menu" class="menu primary-menu fallback-menu">
		<li><a href="<?php echo esc_url( home_url( '/start-here/' ) ); ?>"><?php esc_html_e( 'Start Here', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/issues/' ) ); ?>"><?php esc_html_e( 'Issues', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/research/' ) ); ?>"><?php esc_html_e( 'Research', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/resources/' ) ); ?>"><?php esc_html_e( 'Resources', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/organizations/' ) ); ?>"><?php esc_html_e( 'Organizations', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/methodology/' ) ); ?>"><?php esc_html_e( 'Methodology', 'two-identities-anonymous' ); ?></a></li>
		<li><a href="<?php echo esc_url( home_url( '/about/' ) ); ?>"><?php esc_html_e( 'About', 'two-identities-anonymous' ); ?></a></li>
		<li class="menu-item--support"><a href="<?php echo esc_url( home_url( '/support/' ) ); ?>"><?php esc_html_e( 'Support', 'two-identities-anonymous' ); ?></a></li>
	</ul>
	<?php
}

/**
 * Keep the primary navigation source-controlled when an admin menu is stale.
 *
 * @param string $items Rendered menu items.
 * @param object $args  Menu arguments.
 * @return string
 */
function twoia_primary_menu_items( $items, $args ) {
	if ( empty( $args->theme_location ) || 'primary' !== $args->theme_location ) {
		return $items;
	}