Skip to content
wiki.fftac.org

Virtual Pages - Source Excerpt 50

Back to Virtual Pages

Summary

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

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

/**
 * Redirect the old archive label to the current research route.
 */
function twoia_redirect_research_archive_to_research() {
	if ( is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
		return;
	}

	$request_uri  = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
	$request_path = trim( (string) parse_url( $request_uri, PHP_URL_PATH ), '/' );
	$home_path    = trim( (string) parse_url( home_url( '/' ), PHP_URL_PATH ), '/' );

	if ( '' !== $home_path && 0 === strpos( $request_path, $home_path ) ) {
		$request_path = trim( substr( $request_path, strlen( $home_path ) ), '/' );
	}

	if ( 'research-archive' === $request_path ) {
		wp_safe_redirect( home_url( '/research/' ), 301 );
		exit;
	}
}
add_action( 'template_redirect', 'twoia_redirect_research_archive_to_research', -10 );

/**
 * Improve body classes for virtual routes.
 *
 * @param array $classes Body classes.
 * @return array
 */
function twoia_virtual_page_body_classes( $classes ) {
	$page = twoia_get_requested_virtual_page();
	$route_path = $page && ! empty( $page['route_path'] ) ? $page['route_path'] : ( $page['slug'] ?? '' );

	if ( ! $page || ! twoia_should_render_virtual_page( $route_path ) ) {
		return $classes;
	}

	$classes   = array_diff( $classes, array( 'error404' ) );
	$classes[] = 'page';
	$classes[] = 'twoia-virtual-page';
	$classes[] = 'twoia-virtual-page-' . sanitize_html_class( str_replace( '/', '-', $route_path ) );

	if ( ! empty( $page['route_type'] ) && 'brief' === $page['route_type'] ) {
		$classes[] = 'twoia-virtual-brief-page';
	}

	return $classes;
}
add_filter( 'body_class', 'twoia_virtual_page_body_classes' );

/**
 * Determine whether an editor-owned page needs the source-controlled backbone.
 *
 * @param array $page    Virtual page definition for the same slug.
 * @param int   $post_id Page post ID.
 * @return bool
 */
function twoia_page_needs_source_supplement( $page, $post_id ) {
	$content = wp_strip_all_tags( strip_shortcodes( get_post_field( 'post_content', $post_id ) ) );

	if ( strlen( $content ) < 2500 ) {
		return true;
	}

	$required_phrases = array(
		'Proudly Civil-Libertarian',
		twoia_civil_libertarian_key_phrase(),
	);

	foreach ( $required_phrases as $phrase ) {
		if ( false === stripos( $content, $phrase ) ) {
			return true;
		}
	}

	return false;
}

/**
 * Render source-controlled route content under thin editor-owned pages.
 */
function twoia_render_current_page_source_supplement() {
	$post_id = get_the_ID();

	if ( ! $post_id ) {
		return;
	}

	$slug = get_post_field( 'post_name', $post_id );
	$page = twoia_get_virtual_page( $slug );

	if ( ! $page || ! twoia_page_needs_source_supplement( $page, $post_id ) ) {
		return;
	}
	?>
	<section class="briefing-panel briefing-panel--warning page-source-supplement" aria-labelledby="twoia-source-backbone-<?php echo esc_attr( $page['slug'] ); ?>">
		<p class="eyebrow"><?php esc_html_e( 'Source-controlled research backbone', 'two-identities-anonymous' ); ?></p>
		<h2 id="twoia-source-backbone-<?php echo esc_attr( $page['slug'] ); ?>"><?php esc_html_e( 'Proudly Civil-Libertarian', 'two-identities-anonymous' ); ?></h2>
		<p><?php echo esc_html( twoia_civil_libertarian_key_phrase() ); ?> <?php esc_html_e( 'This supplement keeps the report-backed route dossier visible when a published WordPress page is too thin to carry the site mission.', 'two-identities-anonymous' ); ?></p>
		<div class="twoia-dossier-grid">
			<?php foreach ( $page['sections'] as $index => $section ) : ?>
				<?php twoia_render_virtual_page_section_card( $page, $section, $index, 3 ); ?>
			<?php endforeach; ?>
		</div>
	</section>
	<?php
}

/**
 * Return a stable in-page section ID for a virtual brief card.
 *
 * @param array $section Section definition.
 * @param int   $index   Zero-based section index.
 * @return string
 */
function twoia_virtual_page_section_id( $section, $index ) {
	return 'dossier-' . twoia_virtual_page_section_slug( $section, $index );
}

/**
 * Return the crawlable URL for a virtual dossier card.
 *
 * @param array $page    Page definition.
 * @param array $section Section definition.
 * @param int   $index   Zero-based section index.
 * @return string
 */
function twoia_virtual_page_section_url( $page, $section, $index ) {
	if ( ! empty( $section['url'] ) ) {
		return $section['url'];
	}

	$page_slug    = isset( $page['slug'] ) ? trim( $page['slug'], '/' ) : '';
	$page_path    = '' === $page_slug ? '/' : '/' . $page_slug . '/';
	$section_slug = twoia_virtual_page_section_slug( $section, $index );

	return home_url( $page_path . $section_slug . '/' );
}

/**
 * Render one virtual page dossier card as a plain HTML link.
 *
 * @param array $page          Page definition.
 * @param array $section       Section definition.
 * @param int   $index         Zero-based section index.
 * @param int   $heading_level Heading level for the card title.
 */
function twoia_render_virtual_page_section_card( $page, $section, $index, $heading_level = 2 ) {
	$section_url  = twoia_virtual_page_section_url( $page, $section, $index );
	$heading_tag  = 3 === (int) $heading_level ? 'h3' : 'h2';
	$heading_text = isset( $section['title'] ) ? $section['title'] : '';
	$body_text    = isset( $section['body'] ) ? $section['body'] : '';
	?>
	<a class="briefing-panel briefing-panel--compact twoia-dossier-card twoia-dossier-card--link" href="<?php echo esc_url( $section_url ); ?>">
		<span class="metadata-label"><?php echo esc_html( sprintf( __( 'Dossier %02d', 'two-identities-anonymous' ), $index + 1 ) ); ?></span>
		<<?php echo tag_escape( $heading_tag ); ?>><?php echo esc_html( $heading_text ); ?></<?php echo tag_escape( $heading_tag ); ?>>
		<p><?php echo esc_html( $body_text ); ?></p>
	</a>
	<?php
}

/**
 * Return the public argument that makes a route useful to carry forward.
 *
 * @param array $page    Virtual page definition.
 * @param array $section Optional section-backed child route.
 * @param array $dossier Optional dossier payload for a child route.
 * @return array
 */
function twoia_get_virtual_share_payload( $page, $section = null, $dossier = array() ) {
	$route_slug = isset( $page['parent_slug'] ) ? sanitize_title( $page['parent_slug'] ) : sanitize_title( $page['slug'] ?? '' );
	$title      = isset( $page['title'] ) ? wp_strip_all_tags( $page['title'] ) : '';
	$url        = home_url( '/' . trim( $page['slug'] ?? $route_slug, '/' ) . '/' );