Seometadata - Source Excerpt 01
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
<?php
namespace UAIXLocaleRouter;
use UAIXLocaleRouter\Support\Path;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Localized SEO metadata for locale-prefixed public pages.
*/
final class SeoMetadata {
/**
* Register hooks.
*
* @return void
*/
public static function register() {
add_filter( 'pre_get_document_title', array( __CLASS__, 'filter_document_title' ), 20 );
add_action( 'wp_head', array( __CLASS__, 'render_head_metadata' ), 2 );
}
/**
* Provide a localized document title when dictionaries or site code supply one.
*
* @param string $title Existing precomputed title.
* @return string
*/
public static function filter_document_title( $title ) {
if ( self::should_bypass() ) {
return $title;
}
$context = self::request_context();
$fallback = self::default_title( $context );
$override = self::resolve_localized_value( 'title', $context, $fallback );
return '' !== $override ? $override : $title;
}
/**
* Output localized meta, Open Graph, Twitter, and JSON-LD tags.
*
* @return void
*/
public static function render_head_metadata() {
if ( self::should_bypass() ) {
return;
}
$metadata = self::build_metadata();
if ( ! empty( $metadata['description'] ) && apply_filters( 'uaixlr_render_meta_description', true, $metadata ) ) {
self::render_named_meta( 'description', $metadata['description'] );
}
self::render_property_meta( 'og:locale', $metadata['og_locale'] );
foreach ( $metadata['alternate_og_locales'] as $alternate_locale ) {
self::render_property_meta( 'og:locale:alternate', $alternate_locale );
}
self::render_property_meta( 'og:type', $metadata['og_type'] );
self::render_property_meta( 'og:url', $metadata['url'] );
self::render_property_meta( 'og:title', $metadata['title'] );
self::render_property_meta( 'og:description', $metadata['description'] );
self::render_property_meta( 'og:site_name', $metadata['site_name'] );
if ( '' !== $metadata['image'] ) {
self::render_property_meta( 'og:image', $metadata['image'] );
}
self::render_named_meta( 'twitter:card', '' !== $metadata['image'] ? 'summary_large_image' : 'summary' );
self::render_named_meta( 'twitter:title', $metadata['title'] );
self::render_named_meta( 'twitter:description', $metadata['description'] );
if ( '' !== $metadata['image'] ) {
self::render_named_meta( 'twitter:image', $metadata['image'] );
}
if ( ! empty( $metadata['schema'] ) && is_array( $metadata['schema'] ) ) {
echo '<script type="application/ld+json">' . wp_json_encode( $metadata['schema'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "</script>\n";
}
}
/**
* Build the full metadata payload.
*
* @return array
*/
public static function build_metadata() {
$context = self::request_context();
$current_url = self::current_localized_url( $context );
$title = self::resolve_title( $context );
$description = self::resolve_description( $context );
$site_name = self::resolve_site_name();
$image = self::resolve_image( $context );
$alternates = self::alternate_urls( $context );
$metadata = array(
'title' => $title,
'description' => $description,
'site_name' => $site_name,
'url' => $current_url,
'locale' => $context['locale'],
'url_tag' => $context['url_tag'],
'og_locale' => self::open_graph_locale( $context['locale'] ),
'alternate_og_locales' => self::alternate_open_graph_locales( $context['locale'] ),
'og_type' => self::open_graph_type(),
'image' => $image,
'alternates' => $alternates,
'schema' => array(),
);
$metadata['schema'] = self::build_schema( $metadata, $context );
return apply_filters( 'uaixlr_seo_metadata', $metadata, $context );
}
/**
* Resolve localized page title.
*
* @param array $context Request context.
* @return string
*/
private static function resolve_title( array $context ) {
$fallback = self::default_title( $context );
$title = self::resolve_localized_value( 'title', $context, $fallback );
return '' !== $title ? $title : $fallback;
}
/**
* Resolve localized page description.
*
* @param array $context Request context.
* @return string
*/
private static function resolve_description( array $context ) {
$fallback = self::default_description( $context );
$description = self::resolve_localized_value( 'description', $context, $fallback );
return '' !== $description ? $description : $fallback;
}
/**
* Resolve a dictionary, meta, or filter supplied SEO value.
*
* @param string $field Metadata field.
* @param array $context Request context.
* @param string $fallback Fallback value.
* @return string
*/
private static function resolve_localized_value( $field, array $context, $fallback = '' ) {
$value = self::post_meta_value( $field, $context );
if ( '' === $value ) {
foreach ( self::translation_keys( $field, $context ) as $key ) {
$value = DictionaryRepository::translate( $key, '' );
if ( $value === $key || self::is_unresolved_seo_key( $value ) ) {
$value = '';
continue;
}
if ( '' !== $value ) {
break;
}
}
}
$value = self::clean_text( $value );
$value = apply_filters( 'uaixlr_seo_' . $field, $value, $context, $fallback );
$value = self::clean_text( $value );
return self::is_unresolved_seo_key( $value ) ? '' : $value;
}
/**
* Build dictionary keys from the current route and queried object.
*
* @param string $field Metadata field.
* @param array $context Request context.
* @return array
*/
private static function translation_keys( $field, array $context ) {
$keys = array();
if ( ! empty( $context['is_front_page'] ) ) {
$keys[] = 'seo.home.' . $field;
}
if ( $context['post_id'] > 0 ) {
$keys[] = 'seo.post.' . $context['post_id'] . '.' . $field;
if ( '' !== $context['post_type'] && '' !== $context['post_slug'] ) {
$keys[] = 'seo.' . $context['post_type'] . '.' . $context['post_slug'] . '.' . $field;
}
}
if ( '' !== $context['archive_key'] ) {
$keys[] = 'seo.archive.' . $context['archive_key'] . '.' . $field;
}
if ( '' !== $context['path_key'] ) {
$keys[] = 'seo.path.' . $context['path_key'] . '.' . $field;
}
$keys[] = 'seo.site.' . $field;
return array_values( array_unique( $keys ) );
}
/**
* Read locale-specific post metadata when present.
*
* @param string $field Metadata field.
* @param array $context Request context.
* @return string
*/
private static function post_meta_value( $field, array $context ) {
if ( $context['post_id'] <= 0 || ! function_exists( 'get_post_meta' ) ) {
return '';
}
$suffixes = array(
$context['url_tag'],
strtolower( str_replace( '-', '_', $context['locale'] ) ),
str_replace( '-', '_', $context['locale'] ),
);
foreach ( array_unique( $suffixes ) as $suffix ) {
$value = get_post_meta( $context['post_id'], '_uaixlr_seo_' . $field . '_' . $suffix, true );
if ( '' !== self::clean_text( $value ) ) {
return (string) $value;
}
}
return (string) get_post_meta( $context['post_id'], '_uaixlr_seo_' . $field, true );
}
/**
* Build JSON-LD graph metadata.
*
* @param array $metadata Metadata payload.
* @param array $context Request context.
* @return array
*/
private static function build_schema( array $metadata, array $context ) {
$home_url = function_exists( 'home_url' ) ? home_url( '/' ) : Path::build_front_url( '/' );
$website_id = trailingslashit( $home_url ) . '#website';
$webpage_id = $metadata['url'] . '#webpage';
$search_target = add_query_arg( array( 's' => 'uaixlr_search_term_placeholder' ), $home_url );
$search_target = str_replace( 'uaixlr_search_term_placeholder', '{search_term_string}', $search_target );
$site_schema = array(
'@type' => 'WebSite',
'@id' => $website_id,
'url' => $home_url,
'name' => $metadata['site_name'],
'inLanguage' => LocaleRepository::get_default_locale(),
'availableLanguage' => self::enabled_language_tags(),
'potentialAction' => array(
'@type' => 'SearchAction',
'target' => $search_target,
'query-input' => 'required name=search_term_string',
),
);
$page_schema = array(
'@type' => self::schema_type(),
'@id' => $webpage_id,
'url' => $metadata['url'],
'name' => $metadata['title'],
'inLanguage' => $context['locale'],
'isPartOf' => array( '@id' => $website_id ),
);
if ( '' !== $metadata['description'] ) {
$page_schema['description'] = $metadata['description'];
}
if ( '' !== $metadata['image'] ) {
$page_schema['image'] = $metadata['image'];
}