Skip to content
wiki.fftac.org

Site Integrity

**Site relevance:** 2IA.org

**Memory type:** theme source memory

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

**Size:** 7.9 KB

Summary

Route integrity, privacy, and low-value archive controls.

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php
/**
 * Route integrity, privacy, and low-value archive controls.
 *
 * @package TwoIA
 */

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

/**
 * Normalize the current request path without query string.
 *
 * @return string
 */
function twoia_current_request_path() {
	$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
	$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( $path, $home_path ) ) {
		$path = trim( substr( $path, strlen( $home_path ) ), '/' );
	}

	return $path;
}

/**
 * Redirect launch scaffolding and low-value public taxonomy routes.
 */
function twoia_redirect_scaffold_and_taxonomy_routes() {
	if ( is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
		return;
	}

	$path = twoia_current_request_path();

	$redirects = array(
		'hello-world'                                 => home_url( '/research-archive/' ),
		'category/uncategorized'                      => home_url( '/research-archive/' ),
		'category/surveillance-architecture'          => home_url( '/surveillance-systems/' ),
		'category/keyword-monitoring'                 => home_url( '/keyword-monitoring/' ),
		'category/metadata-and-identity'              => home_url( '/metadata-and-identity/' ),
		'category/ai-and-sentiment-analysis'          => home_url( '/ai-surveillance/' ),
		'category/civil-liberties'                    => home_url( '/ethics-and-civil-liberties/' ),
		'category/ethical-information-operations'     => home_url( '/psychological-warfare/' ),
		'public-records-and-foia/contracts-show-policy' => home_url( '/public-records-and-foia/request-governance-not-just-purchase-orders/' ),
		'public-records-and-foia/public-records-are-leverage' => home_url( '/public-records-and-foia/' ),
		'false-positives/errors-travel-faster-than-corrections' => home_url( '/false-positives/trace-the-error-chain/' ),
		'surveillance-systems/data-brokers-are-shadow-infrastructure' => home_url( '/surveillance-systems/follow-the-data-flow/' ),
		'ai-surveillance/inference-is-not-proof'      => home_url( '/ai-surveillance/do-not-let-ai-launder-uncertainty/' ),
		'two-identities/pseudonymity-protects-dissent' => home_url( '/two-identities/pseudonymity-can-be-civic-infrastructure/' ),
		'start-here/if-you-want-something-useful-now' => home_url( '/start-here/' ),
	);

	if ( isset( $redirects[ $path ] ) ) {
		wp_safe_redirect( $redirects[ $path ], 301 );
		exit;
	}
}
add_action( 'template_redirect', 'twoia_redirect_scaffold_and_taxonomy_routes', -20 );

/**
 * Unpublish the default WordPress placeholder post when an admin session loads.
 */
function twoia_unpublish_default_placeholder_post() {
	$post = get_page_by_path( 'hello-world', OBJECT, 'post' );

	if ( ! $post instanceof WP_Post || 'publish' !== get_post_status( $post ) ) {
		return;
	}

	wp_update_post(
		array(
			'ID'          => $post->ID,
			'post_status' => 'draft',
		)
	);
}
add_action( 'admin_init', 'twoia_unpublish_default_placeholder_post' );

/**
 * Return placeholder post IDs that must stay out of public listings.
 *
 * @return int[]
 */
function twoia_placeholder_post_ids() {
	$post = get_page_by_path( 'hello-world', OBJECT, 'post' );

	if ( $post instanceof WP_Post && function_exists( 'twoia_is_default_placeholder_post' ) && twoia_is_default_placeholder_post( $post ) ) {
		return array( (int) $post->ID );
	}

	return array();
}

/**
 * Remove launch placeholders from main public queries and feeds.
 *
 * @param WP_Query $query Query object.
 */
function twoia_exclude_placeholder_posts_from_public_queries( $query ) {
	if ( is_admin() || ! $query instanceof WP_Query ) {
		return;
	}

	if ( ! $query->is_main_query() && ! $query->is_feed() ) {
		return;
	}

	$placeholder_ids = twoia_placeholder_post_ids();
	if ( empty( $placeholder_ids ) ) {
		return;
	}

	$excluded = (array) $query->get( 'post__not_in' );
	$query->set( 'post__not_in', array_values( array_unique( array_merge( $excluded, $placeholder_ids ) ) ) );
}
add_action( 'pre_get_posts', 'twoia_exclude_placeholder_posts_from_public_queries' );

/**
 * Last-line filtering for placeholder posts in secondary loops.
 *
 * @param WP_Post[] $posts Posts.
 * @return WP_Post[]
 */
function twoia_filter_placeholder_posts_from_results( $posts ) {
	if ( is_admin() || empty( $posts ) || ! function_exists( 'twoia_is_default_placeholder_post' ) ) {
		return $posts;
	}

	return array_values(
		array_filter(
			$posts,
			static function ( $post ) {
				return ! twoia_is_default_placeholder_post( $post );
			}
		)
	);
}
add_filter( 'the_posts', 'twoia_filter_placeholder_posts_from_results' );

/**
 * Keep placeholder posts out of WordPress core sitemaps.
 *
 * @param array  $args      Query args.
 * @param string $post_type Post type.
 * @return array
 */
function twoia_sitemap_exclude_placeholder_posts( $args, $post_type ) {
	if ( 'post' !== $post_type ) {
		return $args;
	}

	$placeholder_ids = twoia_placeholder_post_ids();
	if ( empty( $placeholder_ids ) ) {
		return $args;
	}

	$args['post__not_in'] = array_values( array_unique( array_merge( (array) ( $args['post__not_in'] ?? array() ), $placeholder_ids ) ) );
	return $args;
}
add_filter( 'wp_sitemaps_posts_query_args', 'twoia_sitemap_exclude_placeholder_posts', 10, 2 );

/**
 * Hide thin default taxonomy sitemap surfaces.
 *
 * @param array  $args     Query args.
 * @param string $taxonomy Taxonomy.
 * @return array
 */
function twoia_sitemap_hide_default_taxonomy_terms( $args, $taxonomy ) {
	if ( 'category' !== $taxonomy && 'post_tag' !== $taxonomy ) {
		return $args;
	}

	$args['hide_empty'] = true;
	$args['exclude']    = array_filter(
		array_map(
			static function ( $slug ) use ( $taxonomy ) {
				$term = get_term_by( 'slug', $slug, $taxonomy );
				return $term && ! is_wp_error( $term ) ? (int) $term->term_id : 0;
			},
			array(
				'uncategorized',
				'surveillance-architecture',
				'keyword-monitoring',
				'metadata-and-identity',
				'ai-and-sentiment-analysis',
				'civil-liberties',
				'ethical-information-operations',
			)
		)
	);

	return $args;
}
add_filter( 'wp_sitemaps_taxonomies_query_args', 'twoia_sitemap_hide_default_taxonomy_terms', 10, 2 );

/**
 * Noindex low-value or duplicate surfaces while preserving followable links.
 *
 * @param array $robots Robots directives.
 * @return array
 */
function twoia_public_robots_directives( $robots ) {
	$noindex = is_search() || is_date() || is_author() || is_category() || is_tag();

	if ( function_exists( 'twoia_get_requested_virtual_page' ) ) {
		$page = twoia_get_requested_virtual_page();
		if ( $page && ! empty( $page['slug'] ) && in_array( $page['slug'], array( 'research', 'metadata-is-identity' ), true ) ) {
			$noindex = true;
		}
	}

	if ( $noindex ) {
		unset( $robots['index'] );
		$robots['noindex'] = true;
		$robots['follow']  = true;
	}

	return $robots;
}
add_filter( 'wp_robots', 'twoia_public_robots_directives' );

/**
 * Send conservative frontend security and privacy headers.
 */
function twoia_send_frontend_security_headers() {
	if ( is_admin() || wp_doing_ajax() || headers_sent() ) {
		return;
	}

	header( 'Referrer-Policy: strict-origin-when-cross-origin' );
	header( 'X-Frame-Options: SAMEORIGIN' );
	header( 'Permissions-Policy: accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=(), interest-cohort=()' );
	$csp = "default-src 'self'; img-src 'self' data:; font-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'";

	if ( is_ssl() ) {
		$csp .= '; upgrade-insecure-requests';
		header( 'Strict-Transport-Security: max-age=31536000; includeSubDomains; preload' );
	}

	header( 'Content-Security-Policy: ' . $csp );
}
add_action( 'send_headers', 'twoia_send_frontend_security_headers' );

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );