Skip to content
wiki.fftac.org

Antichrist Message Board - Source Excerpt 05

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

/**
 * Seed the public board page.
 */
function acnmb_seed_board_page() {
	$page = get_page_by_path( 'message-board' );
	$data = array(
		'post_title'   => 'Message Board',
		'post_name'    => 'message-board',
		'post_content' => '[acn_message_board]',
		'post_status'  => 'publish',
		'post_type'    => 'page',
	);

	if ( $page instanceof WP_Post ) {
		$data['ID'] = $page->ID;
		$page_id    = wp_update_post( wp_slash( $data ), true );
	} else {
		$page_id = wp_insert_post( wp_slash( $data ), true );
	}

	if ( ! is_wp_error( $page_id ) && $page_id ) {
		update_post_meta( $page_id, '_acnmb_board_page', ACNMB_VERSION );
		update_post_meta( $page_id, '_wp_page_template', 'templates/template-full-width.php' );
	}
}

/**
 * Add board links to the active primary and footer menus.
 */
function acnmb_seed_menu_links() {
	$targets = array(
		'Antichrist.net Primary Sections' => 'Board',
		'Antichrist.net Footer Sections'  => 'Message Board',
	);

	$page = get_page_by_path( 'message-board' );
	if ( ! $page instanceof WP_Post ) {
		return;
	}

	foreach ( $targets as $menu_name => $label ) {
		$menu = wp_get_nav_menu_object( $menu_name );
		if ( ! $menu ) {
			continue;
		}

		$items  = wp_get_nav_menu_items( (int) $menu->term_id );
		$exists = false;
		if ( $items ) {
			foreach ( $items as $item ) {
				if ( (int) $item->object_id === (int) $page->ID ) {
					$exists = true;
					break;
				}
			}
		}

		if ( $exists ) {
			continue;
		}

		wp_update_nav_menu_item(
			(int) $menu->term_id,
			0,
			array(
				'menu-item-title'     => $label,
				'menu-item-object'    => 'page',
				'menu-item-object-id' => (int) $page->ID,
				'menu-item-type'      => 'post_type',
				'menu-item-status'    => 'publish',
			)
		);
	}
}

/**
 * Enqueue board CSS.
 */
function acnmb_enqueue_assets() {
	wp_enqueue_style( 'acn-message-board', ACNMB_PLUGIN_URL . 'assets/message-board.css', array(), ACNMB_VERSION );
}

/**
 * Render the message board shortcode.
 *
 * @return string
 */
function acnmb_render_board_shortcode() {
	$locale         = acnmb_current_locale_tag();
	$selected_slug  = isset( $_GET['board_category'] ) ? sanitize_title( wp_unslash( $_GET['board_category'] ) ) : '';
	$search         = isset( $_GET['board_search'] ) ? sanitize_text_field( wp_unslash( $_GET['board_search'] ) ) : '';
	$paged          = isset( $_GET['board_page'] ) ? max( 1, absint( $_GET['board_page'] ) ) : 1;
	$selected_term  = $selected_slug ? acnmb_resolve_category_term( $selected_slug, $locale ) : null;
	$selected_slug  = $selected_term instanceof WP_Term ? acnmb_term_base_slug( $selected_term ) : '';

	$query = acnmb_topic_query( $selected_term instanceof WP_Term ? $selected_term : null, $search, $paged );

	ob_start();
	?>
	<section class="acnmb-board" aria-labelledby="acnmb-board-title">
		<div class="acnmb-board-head">
			<div>
				<p class="kicker"><?php echo esc_html( acnmb_t( 'acnmb.board.kicker', 'Community Research' ) ); ?></p>
				<h1 id="acnmb-board-title"><?php echo esc_html( acnmb_t( 'acnmb.board.title', 'Message Board' ) ); ?></h1>
				<p><?php echo esc_html( acnmb_t( 'acnmb.board.intro', 'Read the board publicly. Log in to start topics or reply to ongoing discussions.' ) ); ?></p>
			</div>
			<?php if ( shortcode_exists( 'uaix_locale_switcher' ) || shortcode_exists( 'ns12_locale_switcher' ) ) : ?>
				<div class="acnmb-locale-switcher"><?php echo do_shortcode( shortcode_exists( 'uaix_locale_switcher' ) ? '[uaix_locale_switcher]' : '[ns12_locale_switcher]' ); ?></div>
			<?php endif; ?>
		</div>

		<?php echo wp_kses_post( acnmb_status_notice() ); ?>

		<div class="acnmb-layout">
			<aside class="acnmb-sidebar" aria-label="<?php echo esc_attr( acnmb_t( 'acnmb.board.categories_label', 'Board categories' ) ); ?>">
				<h2><?php echo esc_html( acnmb_t( 'acnmb.board.categories', 'Categories' ) ); ?></h2>
				<?php echo wp_kses_post( acnmb_render_category_tree( $selected_slug ) ); ?>
			</aside>

			<div class="acnmb-main">
				<form class="acnmb-search" method="get" action="<?php echo esc_url( acnmb_board_url() ); ?>">
					<?php if ( $selected_slug ) : ?>
						<input type="hidden" name="board_category" value="<?php echo esc_attr( $selected_slug ); ?>" />
					<?php endif; ?>
					<label class="screen-reader-text" for="acnmb-board-search"><?php echo esc_html( acnmb_t( 'acnmb.board.search_label', 'Search topics' ) ); ?></label>
					<input id="acnmb-board-search" type="search" name="board_search" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php echo esc_attr( acnmb_t( 'acnmb.board.search_placeholder', 'Search the board' ) ); ?>" />
					<button type="submit"><?php echo esc_html( acnmb_t( 'acnmb.board.search_button', 'Search' ) ); ?></button>
				</form>

				<?php echo acnmb_render_topic_list( $query ); ?>
				<?php echo acnmb_render_pagination( $query, $selected_slug, $search, $paged ); ?>
				<?php echo acnmb_render_topic_form( $selected_term instanceof WP_Term ? $selected_term : null ); ?>
			</div>
		</div>
	</section>
	<?php
	wp_reset_postdata();
	return ob_get_clean();
}

/**
 * Build the topic query.
 *
 * @param WP_Term|null $term Selected category.
 * @param string       $search Search string.
 * @param int          $paged Page number.
 * @return WP_Query
 */
function acnmb_topic_query( $term, $search, $paged ) {
	$locale = acnmb_current_locale_tag();
	$args = array(
		'post_type'           => ACNMB_TOPIC_POST_TYPE,
		'post_status'         => 'publish',
		'posts_per_page'      => 12,
		'paged'               => max( 1, (int) $paged ),
		'ignore_sticky_posts' => true,
		'meta_key'            => '_acnmb_pinned',
		'meta_query'          => acnmb_topic_locale_meta_query( $locale ),
		'orderby'             => array(
			'meta_value_num' => 'DESC',
			'modified'       => 'DESC',
		),
	);

	if ( $term instanceof WP_Term ) {
		$args['tax_query'] = array(
			array(
				'taxonomy'         => ACNMB_CATEGORY_TAXONOMY,
				'field'            => 'term_id',
				'terms'            => array( (int) $term->term_id ),
				'include_children' => true,
			),
		);
	}

	if ( '' !== $search ) {
		$args['s'] = $search;
	}

	return new WP_Query( $args );
}

/**
 * Render categories recursively.
 *
 * @param string $selected_slug Selected slug.
 * @param int    $parent Parent term.
 * @return string
 */
function acnmb_render_category_tree( $selected_slug = '', $parent_base_slug = '' ) {
	$locale = acnmb_current_locale_tag();
	$terms  = acnmb_category_terms_for_parent( $locale, $parent_base_slug );
	$html   = '' === $parent_base_slug ? '<ul class="acnmb-category-tree"><li><a class="' . ( '' === $selected_slug ? 'is-active' : '' ) . '" href="' . esc_url( acnmb_board_url() ) . '">' . esc_html( acnmb_t( 'acnmb.board.all_topics', 'All topics' ) ) . '</a></li>' : '<ul>';

	foreach ( $terms as $term ) {
		$base_slug = acnmb_term_base_slug( $term );
		$url       = add_query_arg( 'board_category', $base_slug, acnmb_board_url() );
		$class     = $selected_slug === $base_slug ? ' class="is-active"' : '';
		$html     .= '<li><a' . $class . ' href="' . esc_url( $url ) . '">' . esc_html( $term->name ) . ' <span>' . absint( $term->count ) . '</span></a>';
		$html     .= acnmb_render_category_tree( $selected_slug, $base_slug );
		$html     .= '</li>';
	}

	$html .= '</ul>';
	return $html;
}

/**
 * Render locale-specific category select options.
 *
 * @param WP_Term|null $selected_term Selected category.
 * @param string       $parent_base_slug Parent base slug.
 * @param int          $depth Nesting depth.
 * @return string
 */
function acnmb_render_category_select_options( $selected_term = null, $parent_base_slug = '', $depth = 0 ) {
	$locale      = acnmb_current_locale_tag();
	$selected_id = $selected_term instanceof WP_Term ? (int) $selected_term->term_id : 0;
	$html        = '';

	foreach ( acnmb_category_terms_for_parent( $locale, $parent_base_slug ) as $term ) {
		$prefix = str_repeat( '— ', max( 0, (int) $depth ) );
		$html  .= '<option value="' . esc_attr( $term->term_id ) . '"' . selected( $selected_id, (int) $term->term_id, false ) . '>' . $prefix . esc_html( $term->name ) . '</option>';
		$html  .= acnmb_render_category_select_options( $selected_term, acnmb_term_base_slug( $term ), (int) $depth + 1 );
	}

	return $html;
}

/**
 * Render topic list.
 *
 * @param WP_Query $query Query object.
 * @return string
 */
function acnmb_render_topic_list( WP_Query $query ) {
	if ( ! $query->have_posts() ) {
		return '<div class="acnmb-empty"><h2>' . esc_html( acnmb_t( 'acnmb.board.no_topics_title', 'No topics yet' ) ) . '</h2><p>' . esc_html( acnmb_t( 'acnmb.board.no_topics_copy', 'Start a logged-in discussion to open this category.' ) ) . '</p></div>';
	}

	$html = '<div class="acnmb-topic-list">';
	while ( $query->have_posts() ) {
		$query->the_post();
		$html .= acnmb_topic_card( get_post() );
	}
	$html .= '</div>';

	return $html;
}