Skip to content
wiki.fftac.org

Antichrist Message Board - Source Excerpt 04

Back to Antichrist Message Board

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/antichrist-message-board/antichrist-message-board.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/antichrist-message-board/antichrist-message-board.php

return array(
		'base_slug'   => $base_slug,
		'slug'        => acnmb_category_slug_for_base( $base_slug, $locale ),
		'name'        => isset( $localized['name'] ) ? (string) $localized['name'] : $base_slug,
		'description' => isset( $localized['description'] ) ? (string) $localized['description'] : '',
		'parent'      => isset( $record['parent'] ) ? (string) $record['parent'] : '',
	);
}

/**
 * Return a physical term slug for a locale-specific category.
 *
 * @param string $base_slug Base category slug.
 * @param string $locale Locale tag.
 * @return string
 */
function acnmb_category_slug_for_base( $base_slug, $locale ) {
	$base_slug = sanitize_title( $base_slug );
	$locale    = acnmb_normalize_locale_tag( $locale );

	return 'en-US' === $locale ? $base_slug : acnmb_locale_url_tag( $locale ) . '-' . $base_slug;
}

/**
 * Find the locale-specific term for a base category slug.
 *
 * @param string $base_slug Base category slug.
 * @param string $locale Locale tag.
 * @return WP_Term|null
 */
function acnmb_category_term_for_base_slug( $base_slug, $locale ) {
	$base_slug = sanitize_title( $base_slug );
	$locale    = acnmb_normalize_locale_tag( $locale );
	$term      = get_term_by( 'slug', acnmb_category_slug_for_base( $base_slug, $locale ), ACNMB_CATEGORY_TAXONOMY );

	if ( $term instanceof WP_Term ) {
		if ( '' === (string) get_term_meta( $term->term_id, '_acnmb_locale', true ) ) {
			update_term_meta( $term->term_id, '_acnmb_locale', $locale );
		}
		if ( '' === (string) get_term_meta( $term->term_id, '_acnmb_base_slug', true ) ) {
			update_term_meta( $term->term_id, '_acnmb_base_slug', $base_slug );
		}
		return $term;
	}

	$terms = get_terms(
		array(
			'taxonomy'   => ACNMB_CATEGORY_TAXONOMY,
			'hide_empty' => false,
			'number'     => 1,
			'meta_query' => array(
				array(
					'key'   => '_acnmb_locale',
					'value' => $locale,
				),
				array(
					'key'   => '_acnmb_base_slug',
					'value' => $base_slug,
				),
			),
		)
	);

	return ! is_wp_error( $terms ) && ! empty( $terms[0] ) && $terms[0] instanceof WP_Term ? $terms[0] : null;
}

/**
 * Return a board term's locale.
 *
 * @param WP_Term|int $term Term object or ID.
 * @return string
 */
function acnmb_term_locale( $term ) {
	$term = $term instanceof WP_Term ? $term : get_term( absint( $term ), ACNMB_CATEGORY_TAXONOMY );
	if ( ! $term instanceof WP_Term || is_wp_error( $term ) ) {
		return 'en-US';
	}

	$locale = (string) get_term_meta( $term->term_id, '_acnmb_locale', true );
	if ( '' !== trim( $locale ) ) {
		return acnmb_normalize_locale_tag( $locale );
	}

	$slug = (string) $term->slug;
	foreach ( acnmb_required_locale_tags() as $candidate ) {
		$prefix = acnmb_locale_url_tag( $candidate ) . '-';
		if ( 0 === strpos( $slug, $prefix ) ) {
			update_term_meta( $term->term_id, '_acnmb_locale', $candidate );
			return $candidate;
		}
	}

	update_term_meta( $term->term_id, '_acnmb_locale', 'en-US' );
	return 'en-US';
}

/**
 * Return a board term's stable base category slug.
 *
 * @param WP_Term|int $term Term object or ID.
 * @return string
 */
function acnmb_term_base_slug( $term ) {
	$term = $term instanceof WP_Term ? $term : get_term( absint( $term ), ACNMB_CATEGORY_TAXONOMY );
	if ( ! $term instanceof WP_Term || is_wp_error( $term ) ) {
		return '';
	}

	$base_slug = sanitize_title( (string) get_term_meta( $term->term_id, '_acnmb_base_slug', true ) );
	if ( '' !== $base_slug ) {
		return $base_slug;
	}

	$slug    = (string) $term->slug;
	$records = acnmb_category_records();
	if ( isset( $records[ $slug ] ) ) {
		update_term_meta( $term->term_id, '_acnmb_base_slug', $slug );
		return $slug;
	}

	foreach ( acnmb_required_locale_tags() as $locale ) {
		$prefix = acnmb_locale_url_tag( $locale ) . '-';
		if ( 0 === strpos( $slug, $prefix ) ) {
			$candidate = substr( $slug, strlen( $prefix ) );
			if ( isset( $records[ $candidate ] ) ) {
				update_term_meta( $term->term_id, '_acnmb_base_slug', $candidate );
				return $candidate;
			}
		}
	}

	return '';
}

/**
 * Resolve a submitted category ID, term slug, or base slug for a locale.
 *
 * @param mixed  $category Category value.
 * @param string $locale Locale tag.
 * @return WP_Term|null
 */
function acnmb_resolve_category_term( $category, $locale ) {
	$locale = acnmb_normalize_locale_tag( $locale );

	if ( is_numeric( $category ) && (int) $category > 0 ) {
		$term = get_term( absint( $category ), ACNMB_CATEGORY_TAXONOMY );
		if ( $term instanceof WP_Term && ! is_wp_error( $term ) ) {
			if ( acnmb_term_locale( $term ) === $locale ) {
				return $term;
			}

			$base_slug = acnmb_term_base_slug( $term );
			if ( '' !== $base_slug ) {
				return acnmb_category_term_for_base_slug( $base_slug, $locale );
			}
		}
	}

	$slug = sanitize_title( (string) $category );
	if ( '' === $slug ) {
		return null;
	}

	$term = acnmb_category_term_for_base_slug( $slug, $locale );
	if ( $term instanceof WP_Term ) {
		return $term;
	}

	$term = get_term_by( 'slug', $slug, ACNMB_CATEGORY_TAXONOMY );
	if ( $term instanceof WP_Term ) {
		if ( acnmb_term_locale( $term ) === $locale ) {
			return $term;
		}

		$base_slug = acnmb_term_base_slug( $term );
		if ( '' !== $base_slug ) {
			return acnmb_category_term_for_base_slug( $base_slug, $locale );
		}
	}

	return null;
}

/**
 * Return ordered category terms for a locale and base parent.
 *
 * @param string $locale Locale tag.
 * @param string $parent_base_slug Parent base slug.
 * @return array
 */
function acnmb_category_terms_for_parent( $locale, $parent_base_slug = '' ) {
	$locale           = acnmb_normalize_locale_tag( $locale );
	$parent_base_slug = sanitize_title( $parent_base_slug );
	$terms            = array();

	foreach ( acnmb_category_records() as $base_slug => $record ) {
		$parent = isset( $record['parent'] ) ? sanitize_title( $record['parent'] ) : '';
		if ( $parent !== $parent_base_slug ) {
			continue;
		}

		$term = acnmb_category_term_for_base_slug( $base_slug, $locale );
		if ( $term instanceof WP_Term ) {
			$terms[] = $term;
		}
	}

	return $terms;
}

/**
 * Build locale-specific topic meta query.
 *
 * @param string $locale Locale tag.
 * @return array
 */
function acnmb_topic_locale_meta_query( $locale ) {
	$locale = acnmb_normalize_locale_tag( $locale );

	if ( 'en-US' === $locale ) {
		return array(
			'relation' => 'OR',
			array(
				'key'   => '_acnmb_locale',
				'value' => 'en-US',
			),
			array(
				'key'     => '_acnmb_locale',
				'compare' => 'NOT EXISTS',
			),
		);
	}

	return array(
		array(
			'key'   => '_acnmb_locale',
			'value' => $locale,
		),
	);
}

/**
 * Return a topic's locale, backfilling legacy topics as English.
 *
 * @param int $topic_id Topic ID.
 * @return string
 */
function acnmb_topic_locale( $topic_id ) {
	$topic_id = absint( $topic_id );
	$locale   = (string) get_post_meta( $topic_id, '_acnmb_locale', true );
	if ( '' !== trim( $locale ) ) {
		return acnmb_normalize_locale_tag( $locale );
	}

	$terms = wp_get_object_terms( $topic_id, ACNMB_CATEGORY_TAXONOMY );
	if ( ! is_wp_error( $terms ) && ! empty( $terms[0] ) && $terms[0] instanceof WP_Term ) {
		$locale = acnmb_term_locale( $terms[0] );
	} else {
		$locale = 'en-US';
	}

	update_post_meta( $topic_id, '_acnmb_locale', $locale );
	return $locale;
}

/**
 * Backfill locale metadata for legacy topics and replies.
 */
function acnmb_backfill_topic_locales() {
	$query = new WP_Query(
		array(
			'post_type'              => ACNMB_TOPIC_POST_TYPE,
			'post_status'            => 'any',
			'posts_per_page'         => -1,
			'fields'                 => 'ids',
			'no_found_rows'          => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		)
	);

	foreach ( $query->posts as $topic_id ) {
		$topic_id = absint( $topic_id );
		$locale   = acnmb_topic_locale( $topic_id );
		$terms    = wp_get_object_terms( $topic_id, ACNMB_CATEGORY_TAXONOMY );
		$term     = ! is_wp_error( $terms ) && ! empty( $terms[0] ) && $terms[0] instanceof WP_Term ? $terms[0] : null;

		if ( $term instanceof WP_Term ) {
			$base_slug = acnmb_term_base_slug( $term );
			if ( '' !== $base_slug ) {
				update_post_meta( $topic_id, '_acnmb_category_base_slug', $base_slug );
				$localized_term = acnmb_category_term_for_base_slug( $base_slug, $locale );
				if ( $localized_term instanceof WP_Term && (int) $localized_term->term_id !== (int) $term->term_id ) {
					wp_set_object_terms( $topic_id, array( (int) $localized_term->term_id ), ACNMB_CATEGORY_TAXONOMY );
				}
			}
		}

		$comments = get_comments(
			array(
				'post_id' => $topic_id,
				'status'  => 'all',
				'type'    => 'acn_board_reply',
				'fields'  => 'ids',
			)
		);

		foreach ( $comments as $comment_id ) {
			if ( '' === (string) get_comment_meta( $comment_id, '_acnmb_locale', true ) ) {
				update_comment_meta( $comment_id, '_acnmb_locale', $locale );
			}
		}
	}

	wp_reset_postdata();
}