Antichrist Content System Implementation - Source Excerpt 08
Back to Antichrist Content System Implementation
Summary
This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/antichrist-content-system/includes/antichrist-content-system-implementation.php so readers can inspect the evidence without opening the full source file.
**Source path:** Antichrist.net/wp-content/plugins/antichrist-content-system/includes/antichrist-content-system-implementation.php
/**
* Resolve a translated string pair for a locale, falling back to English.
*
* @param array $pair Translation pair.
* @param string $locale Locale tag.
* @return string
*/
function acn_cs_translation_value_for_locale( $pair, $locale ) {
$language_key = acn_cs_translation_language_key( $locale );
if ( isset( $pair[ $language_key ] ) && '' !== trim( (string) $pair[ $language_key ] ) ) {
return (string) $pair[ $language_key ];
}
return isset( $pair['en'] ) ? (string) $pair['en'] : '';
}
/**
* Write a generated dictionary file, replacing stale generated copies when needed.
*
* @param string $file Dictionary path.
* @param array $data Dictionary payload.
*/
function acn_cs_write_dictionary_file( $file, array $data ) {
$json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
if ( false !== @file_put_contents( $file, $json ) ) {
return;
}
if ( file_exists( $file ) ) {
@chmod( $file, 0666 );
@unlink( $file );
}
@file_put_contents( $file, $json );
}
/**
* Merge site UI strings into the locale-router runtime dictionaries.
*/
function acn_cs_write_ns12_dictionaries() {
$upload_dir = wp_upload_dir();
if ( empty( $upload_dir['basedir'] ) ) {
return;
}
$strings = acn_cs_translation_strings();
$locale_records = acn_cs_dictionary_locale_records();
$payloads = array();
foreach ( $locale_records as $record ) {
$tag = isset( $record['tag'] ) ? (string) $record['tag'] : '';
if ( '' === $tag ) {
continue;
}
$payloads[ $tag ] = array();
}
foreach ( $strings as $key => $pair ) {
foreach ( array_keys( $payloads ) as $locale ) {
$payloads[ $locale ][ $key ] = acn_cs_translation_value_for_locale( $pair, $locale );
}
}
foreach ( $locale_records as $option_record ) {
$option_tag = isset( $option_record['tag'] ) ? (string) $option_record['tag'] : '';
if ( '' === $option_tag ) {
continue;
}
$label = isset( $option_record['nativeName'] ) && '' !== trim( (string) $option_record['nativeName'] )
? (string) $option_record['nativeName']
: $option_tag;
foreach ( array_keys( $payloads ) as $locale ) {
$key = 'locale.name.' . $option_tag;
if ( empty( $payloads[ $locale ][ $key ] ) ) {
$payloads[ $locale ][ $key ] = $label;
}
}
}
$dictionary_dirs = array(
trailingslashit( $upload_dir['basedir'] ) . 'uaix-locale-router/dictionaries',
trailingslashit( $upload_dir['basedir'] ) . 'ns12-locale-router/dictionaries',
);
foreach ( $dictionary_dirs as $dir ) {
wp_mkdir_p( $dir );
if ( ! is_dir( $dir ) || ! is_writable( $dir ) ) {
continue;
}
foreach ( $payloads as $locale => $locale_strings ) {
$file = trailingslashit( $dir ) . $locale . '.json';
$data = array(
'meta' => array(
'locale' => $locale,
'version' => 1,
'fallback' => 'en-US',
'generatedFrom' => 'antichrist-content-system',
'keyMode' => 'runtime-key',
'sourceLocale' => 'en-US',
),
'strings' => array(),
);
if ( file_exists( $file ) ) {
$existing = json_decode( (string) file_get_contents( $file ), true );
if ( is_array( $existing ) ) {
$data = wp_parse_args( $existing, $data );
if ( empty( $data['strings'] ) || ! is_array( $data['strings'] ) ) {
$data['strings'] = array();
}
if ( empty( $data['meta'] ) || ! is_array( $data['meta'] ) ) {
$data['meta'] = array();
}
}
}
$data['meta']['locale'] = $locale;
$data['meta']['keyMode'] = 'runtime-key';
$data['meta']['sourceLocale'] = 'en-US';
$data['strings'] = array_merge( $data['strings'], $locale_strings );
acn_cs_write_dictionary_file( $file, $data );
}
}
$dictionary_repository = acn_cs_dictionary_repository_class();
if ( '' !== $dictionary_repository ) {
foreach ( array_keys( $payloads ) as $locale ) {
$dictionary_repository::clear_cache( $locale );
}
}
}
/**
* Merge Antichrist.net runtime labels into dictionaries even when uploads are read-only.
*
* @param array $dictionary Runtime dictionary.
* @param string $locale Locale tag.
* @return array
*/
function acn_cs_merge_runtime_dictionary_strings( $dictionary, $locale ) {
if ( ! is_array( $dictionary ) ) {
$dictionary = array();
}
if ( empty( $dictionary['meta'] ) || ! is_array( $dictionary['meta'] ) ) {
$dictionary['meta'] = array();
}
if ( empty( $dictionary['strings'] ) || ! is_array( $dictionary['strings'] ) ) {
$dictionary['strings'] = array();
}
foreach ( acn_cs_translation_strings() as $key => $pair ) {
$dictionary['strings'][ $key ] = acn_cs_translation_value_for_locale( $pair, $locale );
}
foreach ( acn_cs_dictionary_locale_records() as $option_record ) {
$option_tag = isset( $option_record['tag'] ) ? (string) $option_record['tag'] : '';
if ( '' === $option_tag ) {
continue;
}
$label = isset( $option_record['nativeName'] ) && '' !== trim( (string) $option_record['nativeName'] )
? (string) $option_record['nativeName']
: $option_tag;
$dictionary['strings'][ 'locale.name.' . $option_tag ] = $label;
}
$dictionary['meta']['locale'] = (string) $locale;
$dictionary['meta']['keyMode'] = 'runtime-key';
$dictionary['meta']['sourceLocale'] = 'en-US';
return $dictionary;
}
/**
* Detect missing locale-router runtime dictionary keys.
*
* @return bool
*/
function acn_cs_dictionaries_need_rewrite() {
$dictionary_repository = acn_cs_dictionary_repository_class();
if ( '' === $dictionary_repository || ! method_exists( $dictionary_repository, 'load_locale_dictionary' ) ) {
return false;
}
$strings = acn_cs_translation_strings();
$locale_records = acn_cs_dictionary_locale_records();
foreach ( $locale_records as $record ) {
$tag = isset( $record['tag'] ) ? (string) $record['tag'] : '';
if ( '' === $tag ) {
continue;
}
$dictionary = $dictionary_repository::load_locale_dictionary( $tag );
$stored = isset( $dictionary['strings'] ) && is_array( $dictionary['strings'] ) ? $dictionary['strings'] : array();
foreach ( array_keys( $strings ) as $key ) {
if ( empty( $stored[ $key ] ) || '' === trim( (string) $stored[ $key ] ) ) {
return true;
}
}
foreach ( $locale_records as $option_record ) {
$option_tag = isset( $option_record['tag'] ) ? (string) $option_record['tag'] : '';
if ( '' === $option_tag ) {
continue;
}
$key = 'locale.name.' . $option_tag;
if ( empty( $stored[ $key ] ) || '' === trim( (string) $stored[ $key ] ) ) {
return true;
}
}
}
return false;
}
/**
* Return whether the self-repair health check ran recently.
*/
function acn_cs_recent_health_check( $option_name ) {
$last_checked = (int) get_option( $option_name, 0 );
$interval = defined( 'HOUR_IN_SECONDS' ) ? HOUR_IN_SECONDS : 3600;
return $last_checked > 0 && ( time() - $last_checked ) < $interval;
}
/**
* Mark a self-repair health check as complete.
*/
function acn_cs_mark_health_check( $option_name ) {
update_option( $option_name, time(), false );
}
/**
* Seed pages and menus once per content-system version.
*/
function acn_cs_seed_content_system() {
$pages = acn_cs_pages();
$is_current = get_option( 'acn_content_system_version' ) === ACN_CONTENT_SYSTEM_VERSION;
if ( $is_current ) {
$pages_need_reseed = acn_cs_pages_need_reseed( $pages );
$menus_need_rebuild = acn_cs_menus_need_rebuild();
$dictionaries_need_rewrite = acn_cs_dictionaries_need_rewrite();
$recent_health_check = acn_cs_recent_health_check( 'acn_content_system_health_checked_at' );
if ( ! $pages_need_reseed && ! $menus_need_rebuild ) {
if ( $dictionaries_need_rewrite || ! $recent_health_check ) {
acn_cs_write_ns12_dictionaries();
}
acn_cs_mark_health_check( 'acn_content_system_health_checked_at' );
return;
}
}
acn_cs_migrate_public_library_slug();
$ids = array();
foreach ( $pages as $path => $page ) {
$ids[ $path ] = acn_cs_upsert_page( $path, $page, $ids );
}
acn_cs_rebuild_menus();
acn_cs_write_ns12_dictionaries();
flush_rewrite_rules( false );
update_option( 'acn_content_system_version', ACN_CONTENT_SYSTEM_VERSION, false );
acn_cs_mark_health_check( 'acn_content_system_health_checked_at' );
}
/**
* Detect missing or unpublished seeded pages even when the version option is current.
*
* @param array $pages Page definitions keyed by page path.
* @return bool
*/
function acn_cs_pages_need_reseed( $pages ) {
foreach ( $pages as $path => $page ) {
$expected_status = isset( $page['status'] ) ? (string) $page['status'] : 'publish';
$post = get_page_by_path( $path, OBJECT, 'page' );
if ( ! $post instanceof WP_Post || $expected_status !== $post->post_status || acn_cs_seeded_page_needs_repair( $post, $page ) ) {
return true;
}
}
return false;
}