Skip to content
wiki.fftac.org

Seometadata - Source Excerpt 02

Back to Seometadata

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/SeoMetadata.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/SeoMetadata.php

if ( $context['post_id'] > 0 ) {
			$published = self::post_date_iso8601( $context['post_id'], 'post_date_gmt' );
			$modified  = self::post_date_iso8601( $context['post_id'], 'post_modified_gmt' );

			if ( '' !== $published ) {
				$page_schema['datePublished'] = $published;
			}

			if ( '' !== $modified ) {
				$page_schema['dateModified'] = $modified;
			}
		}

		if ( count( $metadata['alternates'] ) > 1 ) {
			$page_schema['workTranslation'] = array();

			foreach ( $metadata['alternates'] as $alternate ) {
				if ( $alternate['locale'] === $context['locale'] ) {
					continue;
				}

				$page_schema['workTranslation'][] = array(
					'@type'      => self::schema_type(),
					'url'        => $alternate['url'],
					'inLanguage' => $alternate['locale'],
					'name'       => $metadata['title'],
				);
			}
		}

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

		return apply_filters( 'uaixlr_seo_schema', $schema, $metadata, $context );
	}

	/**
	 * Build current request context.
	 *
	 * @return array
	 */
	private static function request_context() {
		$url_tag = Router::get_current_url_tag();
		$locale  = LocaleRepository::get_enabled_locale_by_url_tag( $url_tag );
		$tag     = ! empty( $locale['tag'] ) ? $locale['tag'] : LocaleRepository::get_default_locale();
		$path    = Router::get_stripped_request_path();
		$post_id = self::queried_post_id();

		return array(
			'locale'        => $tag,
			'url_tag'       => $url_tag,
			'path'          => $path,
			'path_key'      => self::path_key( $path ),
			'query_args'    => Path::current_query_args(),
			'post_id'       => $post_id,
			'post_type'     => self::post_type( $post_id ),
			'post_slug'     => self::post_slug( $post_id ),
			'archive_key'   => self::archive_key(),
			'is_front_page' => function_exists( 'is_front_page' ) && is_front_page(),
		);
	}

	/**
	 * Build the current localized front-end URL.
	 *
	 * @param array $context Request context.
	 * @return string
	 */
	private static function current_localized_url( array $context ) {
		return Path::build_front_url( Path::localize_path( $context['path'], $context['url_tag'] ), $context['query_args'] );
	}

	/**
	 * Build alternate URLs for all enabled locales.
	 *
	 * @param array $context Request context.
	 * @return array
	 */
	private static function alternate_urls( array $context ) {
		$alternates = array();

		foreach ( LocaleRepository::get_enabled_locales() as $locale ) {
			$alternates[] = array(
				'locale' => $locale['tag'],
				'urlTag' => $locale['urlTag'],
				'url'    => Path::build_front_url( Path::localize_path( $context['path'], $locale['urlTag'] ), $context['query_args'] ),
			);
		}

		return $alternates;
	}

	/**
	 * Resolve site name with dictionary support.
	 *
	 * @return string
	 */
	private static function resolve_site_name() {
		$fallback = function_exists( 'get_bloginfo' ) ? (string) get_bloginfo( 'name' ) : '';
		$value    = DictionaryRepository::translate( 'seo.site.name', '' );

		return '' !== self::clean_text( $value ) ? self::clean_text( $value ) : self::clean_text( $fallback );
	}

	/**
	 * Determine a fallback page title.
	 *
	 * @param array $context Request context.
	 * @return string
	 */
	private static function default_title( array $context ) {
		if ( function_exists( 'is_front_page' ) && is_front_page() ) {
			return self::clean_text( function_exists( 'get_bloginfo' ) ? get_bloginfo( 'name' ) : '' );
		}

		if ( function_exists( 'is_singular' ) && is_singular() && $context['post_id'] > 0 && function_exists( 'get_the_title' ) ) {
			return self::clean_text( get_the_title( $context['post_id'] ) );
		}

		if ( function_exists( 'is_search' ) && is_search() && function_exists( 'get_search_query' ) ) {
			return self::clean_text( sprintf( __( 'Search results for "%s"', 'uaix-locale-router' ), get_search_query() ) );
		}

		if ( function_exists( 'is_404' ) && is_404() ) {
			return self::clean_text( __( 'Page not found', 'uaix-locale-router' ) );
		}

		foreach ( array( 'single_post_title', 'post_type_archive_title', 'single_term_title', 'get_the_author' ) as $function_name ) {
			if ( function_exists( $function_name ) ) {
				$value = call_user_func( $function_name, '', false );

				if ( '' !== self::clean_text( $value ) ) {
					return self::clean_text( $value );
				}
			}
		}

		return self::clean_text( function_exists( 'get_bloginfo' ) ? get_bloginfo( 'name' ) : '' );
	}

	/**
	 * Determine a fallback page description.
	 *
	 * @param array $context Request context.
	 * @return string
	 */
	private static function default_description( array $context ) {
		if ( function_exists( 'is_singular' ) && is_singular() && $context['post_id'] > 0 ) {
			$excerpt = function_exists( 'get_the_excerpt' ) ? get_the_excerpt( $context['post_id'] ) : '';

			if ( '' !== self::clean_text( $excerpt ) ) {
				return self::clean_text( $excerpt );
			}
		}

		if ( function_exists( 'term_description' ) ) {
			$term_description = term_description();

			if ( '' !== self::clean_text( $term_description ) ) {
				return self::clean_text( $term_description );
			}
		}

		return self::clean_text( function_exists( 'get_bloginfo' ) ? get_bloginfo( 'description' ) : '' );
	}

	/**
	 * Resolve social image URL.
	 *
	 * @param array $context Request context.
	 * @return string
	 */
	private static function resolve_image( array $context ) {
		$image = '';

		if ( $context['post_id'] > 0 && function_exists( 'get_the_post_thumbnail_url' ) ) {
			$image = (string) get_the_post_thumbnail_url( $context['post_id'], 'full' );
		}

		if ( '' === $image && function_exists( 'get_site_icon_url' ) ) {
			$image = (string) get_site_icon_url( 512 );
		}

		$image = apply_filters( 'uaixlr_seo_image', $image, $context );

		return esc_url( $image );
	}

	/**
	 * Query object ID when available.
	 *
	 * @return int
	 */
	private static function queried_post_id() {
		if ( function_exists( 'get_queried_object_id' ) ) {
			return absint( get_queried_object_id() );
		}

		return 0;
	}

	/**
	 * Post type for the queried object.
	 *
	 * @param int $post_id Post ID.
	 * @return string
	 */
	private static function post_type( $post_id ) {
		if ( $post_id > 0 && function_exists( 'get_post_type' ) ) {
			return sanitize_key( (string) get_post_type( $post_id ) );
		}

		return '';
	}

	/**
	 * Post slug for the queried object.
	 *
	 * @param int $post_id Post ID.
	 * @return string
	 */
	private static function post_slug( $post_id ) {
		if ( $post_id > 0 && function_exists( 'get_post_field' ) ) {
			return sanitize_key( (string) get_post_field( 'post_name', $post_id ) );
		}

		return '';
	}

	/**
	 * Archive key for dictionary lookups.
	 *
	 * @return string
	 */
	private static function archive_key() {
		if ( function_exists( 'is_search' ) && is_search() ) {
			return 'search';
		}

		if ( function_exists( 'is_404' ) && is_404() ) {
			return '404';
		}

		if ( function_exists( 'is_author' ) && is_author() ) {
			return 'author';
		}

		if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() && function_exists( 'get_query_var' ) ) {
			return 'post_type.' . sanitize_key( (string) get_query_var( 'post_type' ) );
		}

		if ( function_exists( 'is_category' ) && is_category() ) {
			return 'category';
		}

		if ( function_exists( 'is_tag' ) && is_tag() ) {
			return 'tag';
		}

		if ( function_exists( 'is_tax' ) && is_tax() ) {
			return 'taxonomy';
		}

		return '';
	}

	/**
	 * Normalize path to a stable dictionary key segment.
	 *
	 * @param string $path Request path.
	 * @return string
	 */
	private static function path_key( $path ) {
		$path = trim( Path::normalize_path( $path ), '/' );

		if ( '' === $path ) {
			return 'home';
		}

		$key = preg_replace( '/[^A-Za-z0-9]+/', '.', $path );

		return trim( strtolower( (string) $key ), '.' );
	}

	/**
	 * JSON-LD page type.
	 *
	 * @return string
	 */
	private static function schema_type() {
		if ( function_exists( 'is_singular' ) && is_singular( 'post' ) ) {
			return 'Article';
		}

		if ( function_exists( 'is_search' ) && is_search() ) {
			return 'SearchResultsPage';
		}

		if ( function_exists( 'is_archive' ) && is_archive() ) {
			return 'CollectionPage';
		}

		return 'WebPage';
	}

	/**
	 * Open Graph type.
	 *
	 * @return string
	 */
	private static function open_graph_type() {
		return function_exists( 'is_singular' ) && is_singular( 'post' ) ? 'article' : 'website';
	}

	/**
	 * Enabled locale tags for schema.
	 *
	 * @return array
	 */
	private static function enabled_language_tags() {
		$tags = array();

		foreach ( LocaleRepository::get_enabled_locales() as $locale ) {
			$tags[] = $locale['tag'];
		}

		return $tags;
	}

	/**
	 * Open Graph locale value.
	 *
	 * @param string $locale Locale tag.
	 * @return string
	 */
	private static function open_graph_locale( $locale ) {
		return str_replace( '-', '_', LocaleValidator::canonicalize_tag( $locale ) );
	}