Skip to content
wiki.fftac.org

Functions - Source Excerpt 02

Back to Functions

Summary

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

**Source path:** Antichrist.net/wp-content/themes/antichrist-net/functions.php

/**
 * Current public URL without tracking query strings.
 *
 * @return string
 */
function acn_seo_current_url() {
	foreach (
		array(
			array( '\UAIXLocaleRouter\Router', '\UAIXLocaleRouter\Support\Path' ),
			array( '\Ns12LocaleRouter\Router', '\Ns12LocaleRouter\Support\Path' ),
		) as $classes
	) {
		list( $router_class, $path_class ) = $classes;
		if ( class_exists( $router_class ) && class_exists( $path_class ) && ! $router_class::is_excluded_path( $router_class::get_original_request_path() ) ) {
			return $path_class::build_front_url(
				$path_class::localize_path(
					$router_class::get_stripped_request_path(),
					$router_class::get_current_url_tag()
				),
				array()
			);
		}
	}

	$path = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) : '/';
	if ( '' === $path ) {
		$path = '/';
	}

	return home_url( $path );
}

/**
 * Return the current locale in Open Graph format.
 *
 * @return string
 */
function acn_seo_current_og_locale() {
	foreach (
		array(
			array( '\UAIXLocaleRouter\Router', '\UAIXLocaleRouter\LocaleRepository' ),
			array( '\Ns12LocaleRouter\Router', '\Ns12LocaleRouter\LocaleRepository' ),
		) as $classes
	) {
		list( $router_class, $locale_repository ) = $classes;
		if ( class_exists( $router_class ) && class_exists( $locale_repository ) ) {
			$current_url_tag = $router_class::get_current_url_tag();
			foreach ( $locale_repository::get_enabled_locales() as $locale ) {
				$url_tag = isset( $locale['urlTag'] ) ? strtolower( (string) $locale['urlTag'] ) : '';
				$tag     = isset( $locale['tag'] ) ? (string) $locale['tag'] : '';
				if ( $url_tag && $tag && strtolower( $current_url_tag ) === $url_tag ) {
					return str_replace( '-', '_', $tag );
				}
			}
		}
	}

	$path = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) : '/';
	if ( preg_match( '#^/es-us(?:/|$)#i', $path ) ) {
		return 'es_US';
	}

	return 'en_US';
}

/**
 * Social preview image data.
 *
 * @return array
 */
function acn_seo_image() {
	if ( is_singular() ) {
		$post = get_queried_object();
		if ( $post instanceof WP_Post && has_post_thumbnail( $post ) ) {
			$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post ), 'full' );
			if ( $image ) {
				return array(
					'url'    => $image[0],
					'width'  => isset( $image[1] ) ? (int) $image[1] : 0,
					'height' => isset( $image[2] ) ? (int) $image[2] : 0,
				);
			}
		}
	}

	$path = get_template_directory() . '/assets/img/foundation-seal-1024.jpg';
	$size = is_readable( $path ) ? getimagesize( $path ) : false;

	return array(
		'url'    => get_template_directory_uri() . '/assets/img/foundation-seal-1024.jpg',
		'width'  => $size ? (int) $size[0] : 1024,
		'height' => $size ? (int) $size[1] : 1024,
	);
}

/**
 * Robots directives for duplicate or faceted public views.
 *
 * @return string
 */
function acn_seo_robots_content() {
	if ( is_search() ) {
		return 'noindex,follow';
	}

	foreach ( array( 'board_search', 'board_category', 'board_page' ) as $param ) {
		if ( ! isset( $_GET[ $param ] ) ) {
			continue;
		}
		$value = wp_unslash( $_GET[ $param ] );
		if ( is_array( $value ) || '' !== (string) $value ) {
			return 'noindex,follow';
		}
	}

	return '';
}

/**
 * Structured data for the current request.
 *
 * @param string $title Page title.
 * @param string $description Page description.
 * @param string $url Canonical-like current URL.
 * @param array  $image Image data.
 * @return array
 */
function acn_seo_schema( $title, $description, $url, $image ) {
	$language = str_replace( '_', '-', acn_seo_current_og_locale() );
	$page_type = 'WebPage';
	if ( is_singular( 'acn_board_topic' ) ) {
		$page_type = 'DiscussionForumPosting';
	} elseif ( is_singular( 'post' ) ) {
		$page_type = 'Article';
	} elseif ( is_tax( 'acn_board_category' ) ) {
		$page_type = 'CollectionPage';
	}

	$graph    = array(
		array(
			'@type'           => 'WebSite',
			'@id'             => home_url( '/#website' ),
			'url'             => home_url( '/' ),
			'name'            => get_bloginfo( 'name' ),
			'description'     => acn_seo_default_description(),
			'inLanguage'      => $language,
			'potentialAction' => array(
				'@type'       => 'SearchAction',
				'target'      => home_url( '/?s={search_term_string}' ),
				'query-input' => 'required name=search_term_string',
			),
		),
		array(
			'@type'             => $page_type,
			'@id'               => $url . '#webpage',
			'url'               => $url,
			'name'              => $title,
			'description'       => $description,
			'isPartOf'          => array( '@id' => home_url( '/#website' ) ),
			'inLanguage'        => $language,
			'primaryImageOfPage' => array(
				'@type' => 'ImageObject',
				'url'   => $image['url'],
			),
		),
	);

	if ( is_singular() ) {
		$post = get_queried_object();
		if ( $post instanceof WP_Post ) {
			$graph[1]['datePublished'] = get_the_date( DATE_W3C, $post );
			$graph[1]['dateModified']  = get_the_modified_date( DATE_W3C, $post );
			$graph[1]['author']        = array(
				'@type' => 'Person',
				'name'  => get_the_author_meta( 'display_name', (int) $post->post_author ),
			);

			if ( 'acn_board_topic' === $post->post_type ) {
				$graph[1]['headline']    = $title;
				$graph[1]['articleBody'] = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post->post_content ) ), 140, '' );

				if ( function_exists( 'acnmb_reply_count' ) ) {
					$graph[1]['commentCount'] = (int) acnmb_reply_count( $post->ID );
				}

				$terms = wp_get_post_terms( $post->ID, 'acn_board_category' );
				if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
					$names = wp_list_pluck( $terms, 'name' );
					$graph[1]['keywords'] = implode( ', ', $names );
					$graph[1]['about']    = array_map(
						static function ( $term ) {
							return array(
								'@type'       => 'Thing',
								'name'        => $term->name,
								'description' => $term->description,
							);
						},
						$terms
					);
				}

				if ( function_exists( 'acnmb_get_replies' ) ) {
					$replies = array_slice( acnmb_get_replies( $post->ID ), -5 );
					if ( ! empty( $replies ) ) {
						$graph[1]['comment'] = array();
						foreach ( $replies as $reply ) {
							if ( ! $reply instanceof WP_Comment ) {
								continue;
							}
							$graph[1]['comment'][] = array(
								'@type'       => 'Comment',
								'text'        => wp_trim_words( wp_strip_all_tags( $reply->comment_content ), 80, '' ),
								'dateCreated' => mysql2date( DATE_W3C, $reply->comment_date_gmt, false ),
								'author'      => array(
									'@type' => 'Person',
									'name'  => $reply->comment_author,
								),
							);
						}
					}
				}
			}
		}
	}

	if ( is_tax( 'acn_board_category' ) ) {
		$term = get_queried_object();
		if ( $term instanceof WP_Term ) {
			$graph[1]['about'] = array(
				'@type'       => 'Thing',
				'name'        => $term->name,
				'description' => $term->description,
			);
		}
	}

	return array(
		'@context' => 'https://schema.org',
		'@graph'   => $graph,
	);
}

/**
 * Render metadata that WordPress core and NS12 do not already provide.
 */
function acn_render_seo_metadata() {
	if ( is_admin() || is_feed() || is_trackback() || ( function_exists( 'is_robots' ) && is_robots() ) ) {
		return;
	}

	$title       = wp_get_document_title();
	$description = acn_seo_description();
	$url         = acn_seo_current_url();
	$image       = acn_seo_image();
	$og_locale   = acn_seo_current_og_locale();
	$og_type     = is_singular( 'post' ) || is_singular( 'acn_board_topic' ) ? 'article' : 'website';
	$robots      = acn_seo_robots_content();