Skip to content
wiki.fftac.org

wiki.fftac.org Core - Source Excerpt 02

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

register_rest_route(
		'wiki.fftac.org/v1',
		'/manifest',
		array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => 'aiwikis_core_rest_manifest',
			'permission_callback' => '__return_true',
		)
	);

	register_rest_route(
		'wiki.fftac.org/v1',
		'/health',
		array(
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => 'aiwikis_core_rest_health',
			'permission_callback' => '__return_true',
		)
	);
}
add_action( 'rest_api_init', 'aiwikis_core_register_rest_routes' );

/**
 * Register wiki.fftac.org-named REST route aliases.
 */
function wiki_fftac_core_register_rest_routes(): void {
	$routes = array(
		'/catalog'         => 'aiwikis_core_rest_catalog',
		'/source-map'      => 'aiwikis_core_rest_source_map',
		'/source-packages' => 'aiwikis_core_rest_source_packages',
		'/llms'            => 'aiwikis_core_rest_llms',
		'/search-index'    => 'aiwikis_core_rest_search_index',
		'/manifest'        => 'aiwikis_core_rest_manifest',
		'/health'          => 'aiwikis_core_rest_health',
	);

	foreach ( $routes as $route => $callback ) {
		register_rest_route(
			'wiki-fftac/v1',
			$route,
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => $callback,
				'permission_callback' => '__return_true',
			)
		);
	}
}
add_action( 'rest_api_init', 'wiki_fftac_core_register_rest_routes' );

/**
 * Locate a source manifest file.
 */
function aiwikis_core_get_source_manifest_path(): string {
	$packaged = AIWIKIS_CORE_PLUGIN_DIR . 'data/SOURCE_MANIFEST.json';
	if ( file_exists( $packaged ) ) {
		return $packaged;
	}

	$source_tree = aiwikis_core_project_root() . '/sources/SOURCE_MANIFEST.json';
	if ( file_exists( $source_tree ) ) {
		return $source_tree;
	}

	return '';
}

/**
 * Decode JSON from disk.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_read_json_file( string $path ): array {
	if ( '' === $path || ! file_exists( $path ) || ! is_readable( $path ) ) {
		return array();
	}

	$raw = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	if ( false === $raw ) {
		return array();
	}

	$decoded = json_decode( $raw, true );
	return is_array( $decoded ) ? $decoded : array();
}

/**
 * Return source manifest data.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_get_source_manifest(): array {
	return aiwikis_core_read_json_file( aiwikis_core_get_source_manifest_path() );
}

/**
 * Locate the wiki.fftac.org source-package reference manifest.
 */
function aiwikis_core_get_source_package_manifest_path(): string {
	$packaged = AIWIKIS_CORE_PLUGIN_DIR . 'data/SOURCE_PACKAGES.json';
	if ( file_exists( $packaged ) ) {
		return $packaged;
	}

	$source_tree = aiwikis_core_project_root() . '/content/source-packages/SOURCE_PACKAGES.json';
	if ( file_exists( $source_tree ) ) {
		return $source_tree;
	}

	return '';
}

/**
 * Locate reviewed public Markdown pages.
 */
function aiwikis_core_get_content_pages_dir(): string {
	$packaged = AIWIKIS_CORE_PLUGIN_DIR . 'data/pages';
	if ( is_dir( $packaged ) ) {
		return $packaged;
	}

	$source_tree = aiwikis_core_project_root() . '/content/pages';
	if ( is_dir( $source_tree ) ) {
		return $source_tree;
	}

	return '';
}

/**
 * Return reviewed public Markdown page files in stable order.
 *
 * @return array<int,string>
 */
function aiwikis_core_get_content_page_files(): array {
	$dir = aiwikis_core_get_content_pages_dir();
	if ( '' === $dir ) {
		return array();
	}

	$files = glob( $dir . '/*.md' );
	if ( false === $files ) {
		return array();
	}

	sort( $files, SORT_NATURAL | SORT_FLAG_CASE );
	return $files;
}

/**
 * Locate the packaged content-page index.
 */
function aiwikis_core_get_content_pages_index_path(): string {
	$packaged = AIWIKIS_CORE_PLUGIN_DIR . 'data/PAGES_INDEX.json';
	if ( file_exists( $packaged ) ) {
		return $packaged;
	}

	$source_tree = aiwikis_core_project_root() . '/content/PAGES_INDEX.json';
	if ( file_exists( $source_tree ) ) {
		return $source_tree;
	}

	return '';
}

/**
 * Read the packaged content-page index.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_read_content_pages_index(): array {
	static $index = null;

	if ( null !== $index ) {
		return $index;
	}

	$path = aiwikis_core_get_content_pages_index_path();
	if ( '' === $path || ! is_readable( $path ) ) {
		$index = array();
		return $index;
	}

	$raw = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	if ( false === $raw ) {
		$index = array();
		return $index;
	}

	$decoded = json_decode( $raw, true );
	$index   = is_array( $decoded ) && isset( $decoded['records'] ) && is_array( $decoded['records'] ) ? $decoded : array();
	return $index;
}

/**
 * Return packaged index records without reparsing every Markdown page.
 *
 * @return array<int,array<string,mixed>>
 */
function aiwikis_core_get_content_page_index_records(): array {
	$index = aiwikis_core_read_content_pages_index();
	return isset( $index['records'] ) && is_array( $index['records'] ) ? $index['records'] : array();
}

/**
 * Return an indexed record by its packaged Markdown filename.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_get_content_page_index_record_by_file_name( string $file_name ): array {
	static $records_by_file = null;

	if ( null === $records_by_file ) {
		$records_by_file = array();
		foreach ( aiwikis_core_get_content_page_index_records() as $record ) {
			if ( isset( $record['file'] ) ) {
				$records_by_file[ (string) $record['file'] ] = $record;
			}
		}
	}

	return isset( $records_by_file[ $file_name ] ) && is_array( $records_by_file[ $file_name ] ) ? $records_by_file[ $file_name ] : array();
}

/**
 * Return the indexed content page count.
 */
function aiwikis_core_get_content_page_index_count(): int {
	$index = aiwikis_core_read_content_pages_index();
	if ( isset( $index['page_count'] ) && is_numeric( $index['page_count'] ) ) {
		return (int) $index['page_count'];
	}

	return count( aiwikis_core_get_content_page_index_records() );
}

/**
 * Return a content-page index record by slug.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_get_content_page_index_record_by_slug( string $slug ): array {
	$slug  = aiwikis_core_normalize_content_slug( '' === trim( $slug ) ? 'home' : $slug );
	$index = aiwikis_core_read_content_pages_index();

	if ( isset( $index['slug_map'] ) && is_array( $index['slug_map'] ) && isset( $index['slug_map'][ $slug ] ) ) {
		$file = (string) $index['slug_map'][ $slug ];
		$record = aiwikis_core_get_content_page_index_record_by_file_name( $file );
		if ( ! empty( $record ) ) {
			return $record;
		}
	}

	foreach ( aiwikis_core_get_content_page_index_records() as $record ) {
		if ( isset( $record['slug'] ) && $slug === aiwikis_core_normalize_content_slug( (string) $record['slug'] ) ) {
			return $record;
		}
	}

	return array();
}

/**
 * Return a content-page index record by route alias.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_get_content_page_index_record_by_alias( string $slug ): array {
	$slug  = aiwikis_core_normalize_content_slug( $slug );
	$index = aiwikis_core_read_content_pages_index();

	if ( isset( $index['alias_map'] ) && is_array( $index['alias_map'] ) && isset( $index['alias_map'][ $slug ] ) ) {
		$file = (string) $index['alias_map'][ $slug ];
		$record = aiwikis_core_get_content_page_index_record_by_file_name( $file );
		if ( ! empty( $record ) ) {
			return $record;
		}
	}

	foreach ( aiwikis_core_get_content_page_index_records() as $record ) {
		$aliases = isset( $record['route_aliases'] ) && is_array( $record['route_aliases'] ) ? $record['route_aliases'] : array();
		if ( in_array( $slug, $aliases, true ) ) {
			return $record;
		}
	}

	return array();
}

/**
 * Resolve an indexed Markdown filename to its public route.
 */
function aiwikis_core_get_content_page_index_route_by_file( string $file_name ): string {
	$index = aiwikis_core_read_content_pages_index();

	if ( isset( $index['file_route_map'] ) && is_array( $index['file_route_map'] ) && isset( $index['file_route_map'][ $file_name ] ) ) {
		return (string) $index['file_route_map'][ $file_name ];
	}

	foreach ( aiwikis_core_get_content_page_index_records() as $record ) {
		if ( isset( $record['file'], $record['route'] ) && $file_name === (string) $record['file'] ) {
			return (string) $record['route'];
		}
	}

	return '';
}

/**
 * Resolve a packaged Markdown path for an index record.
 */
function aiwikis_core_get_content_page_file_from_index_record( array $record ): string {
	if ( empty( $record['file'] ) ) {
		return '';
	}

	$file = basename( (string) $record['file'] );
	$path = trailingslashit( aiwikis_core_get_content_pages_dir() ) . $file;
	return file_exists( $path ) ? $path : '';
}