Skip to content
wiki.fftac.org

wiki.fftac.org Core - Source Excerpt 05

Back to wiki.fftac.org Core

Summary

This source excerpt preserves a bounded section of Wiki.FFTAC.org/wp-content/plugins/wiki-fftac-core/wiki-fftac-core.php so readers can inspect the evidence without opening the full source file.

**Source path:** Wiki.FFTAC.org/wp-content/plugins/wiki-fftac-core/wiki-fftac-core.php

/**
 * Render inline markdown features used by wiki.fftac.org content pages.
 */
function aiwikis_core_render_inline_markdown( string $text ): string {
	$tokens = preg_split( '/(\[[^\]]+\]\([^)]+\))/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
	if ( ! is_array( $tokens ) ) {
		$tokens = array( $text );
	}

	$html = '';
	foreach ( $tokens as $token ) {
		if ( preg_match( '/^\[([^\]]+)\]\(([^)]+)\)$/', $token, $matches ) ) {
			$label = esc_html( $matches[1] );
			$url   = esc_url( aiwikis_core_normalize_content_link_url( $matches[2] ) );
			$html .= '<a href="' . $url . '">' . $label . '</a>';
			continue;
		}

		$escaped = esc_html( $token );
		$escaped = preg_replace( '/`([^`]+)`/', '<code>$1</code>', $escaped );
		$html   .= is_string( $escaped ) ? $escaped : esc_html( $token );
	}

	return $html;
}

/**
 * Render a markdown table.
 *
 * @param array<int,string> $rows Markdown table rows.
 */
function aiwikis_core_render_markdown_table( array $rows ): string {
	if ( count( $rows ) < 2 ) {
		return '';
	}

	$header = array_map( 'trim', explode( '|', trim( $rows[0], " \t|" ) ) );
	$body   = array_slice( $rows, 2 );
	$html   = '<table class="aiwikis-table"><thead><tr>';
	foreach ( $header as $cell ) {
		$html .= '<th>' . aiwikis_core_render_inline_markdown( $cell ) . '</th>';
	}
	$html .= '</tr></thead><tbody>';

	foreach ( $body as $row ) {
		$cells = array_map( 'trim', explode( '|', trim( $row, " \t|" ) ) );
		$html .= '<tr>';
		foreach ( $cells as $cell ) {
			$html .= '<td>' . aiwikis_core_render_inline_markdown( $cell ) . '</td>';
		}
		$html .= '</tr>';
	}

	return $html . '</tbody></table>';
}

/**
 * Convert reviewed content markdown into WordPress-safe HTML.
 */
function aiwikis_core_render_content_markdown( string $body, string $title ): string {
	$body           = aiwikis_core_strip_duplicate_h1( $body, $title );
	$lines          = preg_split( '/\R/', $body );
	$html           = array();
	$paragraph      = array();
	$list_items     = array();
	$list_type      = '';
	$table_rows     = array();
	$blockquote     = array();
	$code_lines     = array();
	$inside_code    = false;
	$code_fence     = '';

	$flush_paragraph = static function () use ( &$html, &$paragraph ): void {
		if ( empty( $paragraph ) ) {
			return;
		}
		$html[]    = '<p>' . aiwikis_core_render_inline_markdown( implode( ' ', $paragraph ) ) . '</p>';
		$paragraph = array();
	};

	$flush_list = static function () use ( &$html, &$list_items, &$list_type ): void {
		if ( empty( $list_items ) ) {
			return;
		}
		$tag  = 'ol' === $list_type ? 'ol' : 'ul';
		$out  = '<' . $tag . '>';
		foreach ( $list_items as $item ) {
			$out .= '<li>' . aiwikis_core_render_inline_markdown( $item ) . '</li>';
		}
		$out        .= '</' . $tag . '>';
		$html[]      = $out;
		$list_items  = array();
		$list_type   = '';
	};

	$flush_table = static function () use ( &$html, &$table_rows ): void {
		if ( empty( $table_rows ) ) {
			return;
		}
		$rendered = aiwikis_core_render_markdown_table( $table_rows );
		if ( '' !== $rendered ) {
			$html[] = $rendered;
		}
		$table_rows = array();
	};

	$flush_blockquote = static function () use ( &$html, &$blockquote ): void {
		if ( empty( $blockquote ) ) {
			return;
		}
		$html[]     = '<blockquote><p>' . aiwikis_core_render_inline_markdown( implode( ' ', $blockquote ) ) . '</p></blockquote>';
		$blockquote = array();
	};

	if ( ! is_array( $lines ) ) {
		$lines = array( $body );
	}

	foreach ( $lines as $line ) {
		$trimmed = trim( (string) $line );

		if ( preg_match( '/^(```|~~~)/', $trimmed, $fence_matches ) ) {
			$current_fence = $fence_matches[1];
			if ( $inside_code ) {
				if ( $current_fence === $code_fence ) {
					$html[]       = '<pre><code>' . esc_html( implode( "\n", $code_lines ) ) . '</code></pre>';
					$code_lines   = array();
					$inside_code  = false;
					$code_fence   = '';
				} else {
					$code_lines[] = (string) $line;
				}
			} else {
				$flush_paragraph();
				$flush_list();
				$flush_table();
				$flush_blockquote();
				$inside_code = true;
				$code_fence  = $current_fence;
			}
			continue;
		}

		if ( $inside_code ) {
			$code_lines[] = (string) $line;
			continue;
		}

		if ( '' === $trimmed ) {
			$flush_paragraph();
			$flush_list();
			$flush_table();
			$flush_blockquote();
			continue;
		}

		if ( preg_match( '/^\[[A-Za-z0-9_-]+(?:\s+[^\]]*)?\]$/', $trimmed ) ) {
			$flush_paragraph();
			$flush_list();
			$flush_table();
			$flush_blockquote();
			$html[] = do_shortcode( $trimmed );
			continue;
		}

		if ( str_starts_with( $trimmed, '|' ) && str_ends_with( $trimmed, '|' ) ) {
			$flush_paragraph();
			$flush_list();
			$flush_blockquote();
			$table_rows[] = $trimmed;
			continue;
		}

		$flush_table();

		if ( preg_match( '/^(#{1,6})\s+(.+)$/', $trimmed, $matches ) ) {
			$flush_paragraph();
			$flush_list();
			$flush_blockquote();
			$level  = min( 6, max( 2, strlen( $matches[1] ) ) );
			$html[] = '<h' . $level . '>' . aiwikis_core_render_inline_markdown( $matches[2] ) . '</h' . $level . '>';
			continue;
		}

		if ( preg_match( '/^-\s+(.+)$/', $trimmed, $matches ) ) {
			$flush_paragraph();
			$flush_blockquote();
			if ( 'ul' !== $list_type ) {
				$flush_list();
				$list_type = 'ul';
			}
			$list_items[] = $matches[1];
			continue;
		}

		if ( preg_match( '/^\d+\.\s+(.+)$/', $trimmed, $matches ) ) {
			$flush_paragraph();
			$flush_blockquote();
			if ( 'ol' !== $list_type ) {
				$flush_list();
				$list_type = 'ol';
			}
			$list_items[] = $matches[1];
			continue;
		}

		if ( preg_match( '/^>\s?(.*)$/', $trimmed, $matches ) ) {
			$flush_paragraph();
			$flush_list();
			$blockquote[] = $matches[1];
			continue;
		}

		$paragraph[] = $trimmed;
	}

	if ( $inside_code ) {
		$html[] = '<pre><code>' . esc_html( implode( "\n", $code_lines ) ) . '</code></pre>';
	}
	$flush_paragraph();
	$flush_list();
	$flush_table();
	$flush_blockquote();

	return wp_kses_post( implode( "\n\n", $html ) );
}

/**
 * Return file-backed content status.
 *
 * @return array{available:int,dir:string,routes:array<int,string>}
 */
function aiwikis_core_get_content_page_status(): array {
	$indexed_count = aiwikis_core_get_content_page_index_count();

	return array(
		'available' => $indexed_count > 0 ? $indexed_count : count( aiwikis_core_get_content_page_files() ),
		'dir'       => aiwikis_core_get_content_pages_dir(),
		'routes'    => aiwikis_core_get_public_content_routes(),
	);
}

/**
 * Return the Markdown-backed content slug for the current request.
 */
function aiwikis_core_get_request_content_slug(): string {
	$request_path = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) : '/';
	$path         = trim( $request_path, '/' );

	if ( '' === $path ) {
		return 'home';
	}

	if ( preg_match( '#^(wp-admin|wp-content|wp-includes|wp-json|feed|comments|xmlrpc\.php|wp-login\.php)(/|$)#', $path ) ) {
		return '';
	}

	if ( preg_match( '#\.[A-Za-z0-9]{2,8}$#', $path ) ) {
		return '';
	}

	return aiwikis_core_normalize_content_slug( $path );
}

/**
 * Render internal memory navigation for the reviewed wiki pages.
 */
function aiwikis_core_render_memory_navigation(): void {
	$links = array(
		'Home'                   => '/',
		'Start Here'             => '/start-here/',
		'Search'                 => '/search/',
		'Crawl Map'              => '/memory/crawl-map/',
		'All Memory Sources'     => '/memory/sources/',
		'Structure'              => '/wp-fftac-memory-and-site-structure/',
		'For AI Agents'          => '/for-ai-agents/',
		'2IA.org Memory'         => '/2ia-org/',
		'FFTAC Memory'           => '/fftac-org/',
		'Anti-Christ.net Memory' => '/anti-christ-net/',
		'Antichrist.net Memory'  => '/antichrist-net/',
		'Anti-Christ.org Memory' => '/anti-christ-org/',
		'Spiralist Memory'       => '/spiralist-org/',
		'Transfer Log'           => '/transfer-log/',
	);
	?>
	<nav class="aiwikis-memory-nav" aria-label="<?php echo esc_attr__( 'Memory navigation', 'aiwikis-core' ); ?>">
		<?php foreach ( $links as $label => $url ) : ?>
			<a href="<?php echo esc_url( home_url( $url ) ); ?>"><?php echo esc_html( $label ); ?></a>
		<?php endforeach; ?>
	</nav>
	<?php
}

/**
 * Render breadcrumbs for a file-backed content page.
 *
 * @param array<string,mixed> $page Content page record.
 */
function aiwikis_core_render_breadcrumbs( array $page ): void {
	$route = isset( $page['route'] ) ? (string) $page['route'] : '/';
	$route = '/' . trim( $route, '/' ) . '/';
	if ( '//' === $route ) {
		$route = '/';
	}