Antichrist Content System Implementation - Source Excerpt 10
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
/**
* Keep the custom root sitemap ahead of WordPress core's /sitemap.xml redirect.
*
* @param array $rules Rewrite rules.
* @return array
*/
function acn_cs_prepend_sitemap_rewrite_rules( $rules ) {
$custom = array(
'sitemap\.xml$' => 'index.php?acn_sitemap=xml&acn_sitemap_locale=root',
'sitemap\.html$' => 'index.php?acn_sitemap=html&acn_sitemap_locale=root',
'([A-Za-z]{2,8}(?:-[A-Za-z0-9]{2,8})*)/sitemap\.xml$' => 'index.php?acn_sitemap=xml&acn_sitemap_locale=$matches[1]',
'([A-Za-z]{2,8}(?:-[A-Za-z0-9]{2,8})*)/sitemap\.html$' => 'index.php?acn_sitemap=html&acn_sitemap_locale=$matches[1]',
);
return $custom + $rules;
}
/**
* Prevent WordPress core from redirecting /sitemap.xml to /wp-sitemap.xml.
*
* @param string|false $redirect_url Redirect URL.
* @param string $requested_url Requested URL.
* @return string|false
*/
function acn_cs_disable_sitemap_canonical_redirect( $redirect_url, $requested_url ) {
$path = (string) wp_parse_url( $requested_url, PHP_URL_PATH );
if ( preg_match( '#/(?:[A-Za-z]{2,8}(?:-[A-Za-z0-9]{2,8})*/)?sitemap\.(?:xml|html)/?$#', $path ) ) {
return false;
}
return $redirect_url;
}
/**
* Render sitemap requests before WordPress falls through to a 404 template.
*/
function acn_cs_render_sitemap_request() {
if ( is_admin() ) {
return;
}
$request = acn_cs_current_sitemap_request();
if ( ! $request ) {
return;
}
if ( ! acn_cs_sitemap_locale_allowed( $request['locale'] ) ) {
status_header( 404 );
nocache_headers();
echo 'Sitemap locale not found.';
exit;
}
if ( 'xml' === $request['type'] ) {
acn_cs_render_xml_sitemap( $request['locale'] );
}
acn_cs_render_html_sitemap( $request['locale'] );
}
/**
* Detect sitemap type and locale from query vars or the request path.
*
* @return array|null
*/
function acn_cs_current_sitemap_request() {
$type = get_query_var( 'acn_sitemap' );
$locale = get_query_var( 'acn_sitemap_locale' );
if ( $type ) {
return array(
'type' => strtolower( (string) $type ),
'locale' => acn_cs_normalize_sitemap_locale( $locale ? (string) $locale : 'root' ),
);
}
$path = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) : '';
$path = '/' . trim( $path, '/' );
if ( preg_match( '#^/(?:([A-Za-z]{2,8}(?:-[A-Za-z0-9]{2,8})*)/)?sitemap\.(xml|html)$#', $path, $matches ) ) {
return array(
'type' => strtolower( $matches[2] ),
'locale' => acn_cs_normalize_sitemap_locale( ! empty( $matches[1] ) ? $matches[1] : 'root' ),
);
}
return null;
}
/**
* Normalize a sitemap locale marker.
*
* @param string $locale Locale or URL tag.
* @return string
*/
function acn_cs_normalize_sitemap_locale( $locale ) {
$locale = strtolower( trim( (string) $locale ) );
if ( '' === $locale || 'root' === $locale ) {
return 'root';
}
return str_replace( '_', '-', $locale );
}
/**
* Return enabled locale records for sitemap rendering.
*
* @return array
*/
function acn_cs_sitemap_locales() {
$locale_repository = acn_cs_locale_repository_class();
if ( '' !== $locale_repository ) {
$records = $locale_repository::get_enabled_locales();
if ( is_array( $records ) && $records ) {
return array_values( $records );
}
}
return array(
array(
'tag' => 'en-US',
'urlTag' => 'en-us',
'nativeName' => 'English (United States)',
),
array(
'tag' => 'es-US',
'urlTag' => 'es-us',
'nativeName' => 'Espanol (Estados Unidos)',
),
array(
'tag' => 'fr-FR',
'urlTag' => 'fr-fr',
'nativeName' => 'Francais (France)',
),
array(
'tag' => 'zh-CN',
'urlTag' => 'zh-cn',
'nativeName' => '简体中文(中国)',
),
);
}
/**
* Check whether a sitemap locale can be rendered.
*
* @param string $locale Locale marker.
* @return bool
*/
function acn_cs_sitemap_locale_allowed( $locale ) {
if ( 'root' === $locale ) {
return true;
}
foreach ( acn_cs_sitemap_locales() as $record ) {
$url_tag = isset( $record['urlTag'] ) ? acn_cs_normalize_sitemap_locale( $record['urlTag'] ) : '';
if ( $url_tag === $locale ) {
return true;
}
}
return false;
}
/**
* Return the default sitemap URL tag.
*
* @return string
*/
function acn_cs_default_sitemap_url_tag() {
$locale_repository = acn_cs_locale_repository_class();
if ( '' !== $locale_repository ) {
$tag = $locale_repository::get_default_url_tag();
if ( $tag ) {
return acn_cs_normalize_sitemap_locale( $tag );
}
}
return 'en-us';
}
/**
* Last modified timestamp for site-level sitemap entries.
*
* @return string
*/
function acn_cs_sitemap_site_lastmod() {
$latest = get_posts(
array(
'post_type' => array( 'page', 'post', 'acn_board_topic' ),
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC',
'fields' => 'ids',
)
);
if ( $latest ) {
$post = get_post( (int) $latest[0] );
if ( $post instanceof WP_Post ) {
return mysql2date( 'c', $post->post_modified_gmt ? $post->post_modified_gmt : $post->post_date_gmt, false );
}
}
return gmdate( 'c' );
}
/**
* Build sitemap entries for pages, board topics, and board categories.
*
* @param string $locale Locale marker.
* @return array
*/
function acn_cs_sitemap_entries( $locale ) {
$site_lastmod = acn_cs_sitemap_site_lastmod();
$entries = array(
array(
'title' => get_bloginfo( 'name' ),
'loc' => acn_cs_sitemap_url( '', $locale ),
'lastmod' => $site_lastmod,
'type' => 'Home',
),
);
$posts = get_posts(
array(
'post_type' => array( 'page', 'acn_board_topic' ),
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => array(
'menu_order' => 'ASC',
'title' => 'ASC',
),
)
);
foreach ( $posts as $post ) {
$path = 'page' === $post->post_type ? get_page_uri( $post ) : 'message-board/topic/' . $post->post_name;
if ( ! $path ) {
continue;
}
$entries[] = array(
'title' => get_the_title( $post ),
'loc' => acn_cs_sitemap_url( $path, $locale ),
'lastmod' => mysql2date( 'c', $post->post_modified_gmt ? $post->post_modified_gmt : $post->post_date_gmt, false ),
'type' => 'page' === $post->post_type ? 'Page' : 'Board Topic',
);
}
if ( taxonomy_exists( 'acn_board_category' ) ) {
$terms = get_terms(
array(
'taxonomy' => 'acn_board_category',
'hide_empty' => false,
)
);
if ( ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$entries[] = array(
'title' => $term->name,
'loc' => acn_cs_sitemap_url( 'message-board/category/' . acn_cs_term_path( $term ), $locale ),
'lastmod' => $site_lastmod,
'type' => 'Board Category',
);
}
}
}
usort(
$entries,
function ( $a, $b ) {
if ( $a['loc'] === $b['loc'] ) {
return 0;
}
return $a['loc'] < $b['loc'] ? -1 : 1;
}
);
return $entries;
}
/**
* Build a localized absolute URL without relying on front-end link rewriting.
*
* @param string $path Relative path.
* @param string $locale Locale marker.
* @return string
*/
function acn_cs_sitemap_url( $path, $locale ) {
$base = untrailingslashit( (string) get_option( 'home' ) );
$path = trim( (string) $path, '/' );
$prefix = 'root' === $locale ? '' : trim( $locale, '/' );
$parts = array_filter( array( $prefix, $path ), 'strlen' );
$suffix = preg_match( '#\.(?:xml|html|txt)$#i', $path ) ? '' : '/';
return $base . '/' . ( $parts ? implode( '/', $parts ) . $suffix : '' );
}
/**
* Strip the current sitemap locale from a loc value before alternate-link building.
*
* @param string $loc Absolute sitemap URL.
* @param string $locale Current sitemap locale marker.
* @return string
*/
function acn_cs_sitemap_relative_path( $loc, $locale ) {
$base = untrailingslashit( (string) get_option( 'home' ) );
$relative = trim( str_replace( $base, '', (string) $loc ), '/' );
$locale = acn_cs_normalize_sitemap_locale( $locale );
if ( 'root' !== $locale ) {
if ( $relative === $locale ) {
return '';
}
if ( 0 === strpos( $relative, $locale . '/' ) ) {
return trim( substr( $relative, strlen( $locale ) ), '/' );
}
}
return $relative;
}
/**
* Return a hierarchical term path.
*
* @param WP_Term $term Term.
* @return string
*/
function acn_cs_term_path( $term ) {
$parts = array( $term->slug );
$parents = get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' );
foreach ( array_reverse( $parents ) as $parent_id ) {
$parent = get_term( $parent_id, $term->taxonomy );
if ( $parent instanceof WP_Term && ! is_wp_error( $parent ) ) {
array_unshift( $parts, $parent->slug );
}
}
return implode( '/', $parts );
}