wiki.fftac.org Core - Source Excerpt 04
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
if ( null !== $memoized_records ) {
return $memoized_records;
}
$records = array();
$index_records = aiwikis_core_get_content_page_index_records();
foreach ( ( ! empty( $index_records ) ? $index_records : aiwikis_core_get_content_page_records() ) as $record ) {
if ( ! empty( $index_records ) ) {
$record = aiwikis_core_get_content_page_record_from_index( $record, false );
}
$frontmatter = isset( $record['frontmatter'] ) && is_array( $record['frontmatter'] ) ? $record['frontmatter'] : array();
$tags = isset( $frontmatter['tags'] ) && is_array( $frontmatter['tags'] ) ? array_values( array_filter( array_map( 'strval', $frontmatter['tags'] ) ) ) : array();
$page_type = aiwikis_core_frontmatter_string( $frontmatter, 'page_type' );
$category = aiwikis_core_frontmatter_string( $frontmatter, 'primary_category' );
$records[] = array(
'title' => (string) ( $record['title'] ?? '' ),
'description' => aiwikis_core_content_page_description( $frontmatter, (string) ( $record['body'] ?? '' ) ),
'body_preview' => wp_trim_words( wp_strip_all_tags( (string) ( $record['body'] ?? '' ) ), 48, '...' ),
'route' => (string) ( $record['route'] ?? '' ),
'canonical_url' => aiwikis_core_frontmatter_string( $frontmatter, 'canonical_url' ),
'page_type' => $page_type,
'primary_category' => $category,
'source_site' => aiwikis_core_frontmatter_string( $frontmatter, 'source_site' ),
'source_status' => aiwikis_core_frontmatter_string( $frontmatter, 'source_status' ),
'trust_level' => aiwikis_core_frontmatter_string( $frontmatter, 'trust_level' ),
'schema_type' => aiwikis_core_frontmatter_string( $frontmatter, 'schema_type' ),
'noindex' => aiwikis_core_frontmatter_bool( $frontmatter, 'noindex' ),
'route_aliases' => isset( $record['route_aliases'] ) && is_array( $record['route_aliases'] ) ? $record['route_aliases'] : array(),
'tags' => $tags,
);
}
usort(
$records,
static function ( array $a, array $b ): int {
return strcasecmp( (string) $a['route'], (string) $b['route'] );
}
);
$memoized_records = $records;
return $memoized_records;
}
/**
* Search public route records by title, description, body preview, route, source status, and tags.
*
* @return array<int,array<string,mixed>>
*/
function aiwikis_core_search_public_records( string $query, int $limit = 40 ): array {
$query = strtolower( trim( preg_replace( '/\s+/', ' ', $query ) ) );
if ( '' === $query ) {
return array_slice( aiwikis_core_get_public_search_records(), 0, $limit );
}
$terms = array_values( array_filter( preg_split( '/\s+/', $query ) ?: array() ) );
$scored = array();
$records = aiwikis_core_get_public_search_records();
foreach ( $records as $record ) {
$haystack = strtolower(
implode(
' ',
array(
(string) ( $record['title'] ?? '' ),
(string) ( $record['description'] ?? '' ),
(string) ( $record['body_preview'] ?? '' ),
(string) ( $record['route'] ?? '' ),
(string) ( $record['page_type'] ?? '' ),
(string) ( $record['primary_category'] ?? '' ),
(string) ( $record['source_site'] ?? '' ),
(string) ( $record['source_status'] ?? '' ),
implode( ' ', isset( $record['tags'] ) && is_array( $record['tags'] ) ? $record['tags'] : array() ),
)
)
);
$score = 0;
if ( str_contains( $haystack, $query ) ) {
$score += 10;
}
foreach ( $terms as $term ) {
if ( strlen( $term ) >= 2 && str_contains( $haystack, $term ) ) {
$score += 2;
}
}
if ( $score > 0 ) {
$record['_score'] = $score;
$scored[] = $record;
}
}
usort(
$scored,
static function ( array $a, array $b ): int {
$score_compare = (int) ( $b['_score'] ?? 0 ) <=> (int) ( $a['_score'] ?? 0 );
if ( 0 !== $score_compare ) {
return $score_compare;
}
return strcasecmp( (string) ( $a['route'] ?? '' ), (string) ( $b['route'] ?? '' ) );
}
);
return array_slice( $scored, 0, $limit );
}
/**
* Return the public search query from the reviewed-memory search page.
*/
function aiwikis_core_get_search_query(): string {
$query = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
if ( '' === $query && isset( $_GET['s'] ) ) {
$query = sanitize_text_field( wp_unslash( $_GET['s'] ) );
}
return trim( $query );
}
/**
* Return a public search record by route.
*
* @return array<string,mixed>
*/
function aiwikis_core_get_public_search_record_by_route( string $route ): array {
$route = '/' . trim( $route, '/' ) . '/';
if ( '//' === $route ) {
$route = '/';
}
foreach ( aiwikis_core_get_public_search_records() as $record ) {
if ( isset( $record['route'] ) && $route === (string) $record['route'] ) {
return $record;
}
}
return array();
}
/**
* Turn a route segment into readable breadcrumb text.
*/
function aiwikis_core_route_label( string $route ): string {
$route = trim( $route, '/' );
if ( '' === $route ) {
return 'Home';
}
$segment = basename( $route );
$segment = preg_replace( '/[-_]+/', ' ', $segment );
return ucwords( trim( (string) $segment ) );
}
/**
* Find a public content page by slug.
*
* @return array<string,mixed>
*/
function aiwikis_core_get_content_page_by_slug( string $slug ): array {
$slug = aiwikis_core_normalize_content_slug( '' === trim( $slug ) ? 'home' : $slug );
$indexed = aiwikis_core_get_content_page_index_record_by_slug( $slug );
if ( ! empty( $indexed ) ) {
return aiwikis_core_get_content_page_record_from_index( $indexed, true );
}
foreach ( aiwikis_core_get_content_page_records() as $record ) {
if ( isset( $record['slug'] ) && $slug === $record['slug'] ) {
return $record;
}
}
return array();
}
/**
* Find a public content page by a legacy route alias.
*
* @return array<string,mixed>
*/
function aiwikis_core_get_content_page_by_route_alias( string $slug ): array {
$slug = aiwikis_core_normalize_content_slug( $slug );
$indexed = aiwikis_core_get_content_page_index_record_by_alias( $slug );
if ( ! empty( $indexed ) ) {
return aiwikis_core_get_content_page_record_from_index( $indexed, true );
}
foreach ( aiwikis_core_get_content_page_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();
}
/**
* Return the first markdown heading.
*/
function aiwikis_core_get_first_markdown_heading( string $body, string $fallback ): string {
foreach ( preg_split( '/\R/', $body ) as $line ) {
if ( preg_match( '/^#\s+(.+)$/', (string) $line, $matches ) ) {
return trim( $matches[1] );
}
}
return $fallback;
}
/**
* Strip the first H1 when it duplicates the WordPress page title.
*/
function aiwikis_core_strip_duplicate_h1( string $body, string $title ): string {
$lines = preg_split( '/\R/', $body );
if ( ! is_array( $lines ) ) {
return $body;
}
for ( $i = 0; $i < count( $lines ); $i++ ) {
if ( '' === trim( $lines[ $i ] ) ) {
continue;
}
if ( preg_match( '/^#\s+(.+)$/', $lines[ $i ], $matches ) && trim( $matches[1] ) === $title ) {
unset( $lines[ $i ] );
}
break;
}
return trim( implode( "\n", $lines ) );
}
/**
* Convert a markdown relative URL into a public page URL when possible.
*/
function aiwikis_core_normalize_content_link_url( string $url ): string {
$url = trim( html_entity_decode( $url, ENT_QUOTES, 'UTF-8' ) );
if ( preg_match( '/^(https?:|mailto:|#|\/)/', $url ) ) {
return $url;
}
$clean = preg_replace( '/^\.\//', '', $url );
if ( is_string( $clean ) && preg_match( '/\.md(?:#.*)?$/', $clean ) ) {
$fragment = '';
if ( str_contains( $clean, '#' ) ) {
list( $clean, $fragment ) = explode( '#', $clean, 2 );
$fragment = '#' . sanitize_title( $fragment );
}
$route = aiwikis_core_get_content_page_route_by_file( basename( $clean ) );
if ( '' !== $route ) {
return home_url( $route . $fragment );
}
$slug = preg_replace( '/^\d+-/', '', pathinfo( $clean, PATHINFO_FILENAME ) );
return home_url( '/' . aiwikis_core_normalize_content_slug( (string) $slug ) . '/' . $fragment );
}
return $url;
}
/**
* Resolve a Markdown filename to its public route when it is a reviewed page.
*/
function aiwikis_core_get_content_page_route_by_file( string $file_name ): string {
$indexed_route = aiwikis_core_get_content_page_index_route_by_file( $file_name );
if ( '' !== $indexed_route ) {
return $indexed_route;
}
foreach ( aiwikis_core_get_content_page_records() as $record ) {
if ( isset( $record['file'], $record['route'] ) && $file_name === $record['file'] ) {
return (string) $record['route'];
}
}
return '';
}