Site * Name"), which is what Yoast emits by default too. * * @param string $seo_title Explicit _abds_seo_title (may be ''). * @param string $post_title The post/page title. * @param string $site_name Blog name. * @param string $separator Title separator (e.g. "-"). * @return string Resolved title, never empty when a post title exists. */ public static function resolve_title( string $seo_title, string $post_title, string $site_name, string $separator = '-' ): string { $seo_title = trim( $seo_title ); if ( '' !== $seo_title ) { return $seo_title; } $parts = array_filter( array( trim( $post_title ), trim( $site_name ) ), fn( $part ) => '' !== $part ); return implode( ' ' . trim( $separator ) . ' ', $parts ); } /** * Resolves the meta description: the explicit one when set, otherwise * a trimmed excerpt (or, failing that, the plain content) capped at * DESCRIPTION_FALLBACK_MAX characters on a word boundary. * * @param string $seo_description Explicit _abds_seo_description (may be ''). * @param string $excerpt Post excerpt (may be ''). * @param string $plain_content Tag-stripped post content (may be ''). * @return string Resolved description (may be '' when nothing to derive from). */ public static function resolve_description( string $seo_description, string $excerpt, string $plain_content ): string { $seo_description = trim( $seo_description ); if ( '' !== $seo_description ) { return $seo_description; } $source = '' !== trim( $excerpt ) ? $excerpt : $plain_content; $source = trim( (string) preg_replace( '/\s+/u', ' ', $source ) ); if ( '' === $source ) { return ''; } return self::truncate_on_word_boundary( $source, self::DESCRIPTION_FALLBACK_MAX ); } /** * Builds the Open Graph tag map (property => content) from already * resolved values. Only non-empty tags are included, so an imageless * post simply omits og:image rather than emitting an empty one. * * @param array $context Resolved page values: title, description, url, * site_name, type, image (each '' if unset). * @return array Property => content, empties dropped. */ public static function build_open_graph( array $context ): array { $tags = array( 'og:title' => (string) ( $context['title'] ?? '' ), 'og:description' => (string) ( $context['description'] ?? '' ), 'og:url' => (string) ( $context['url'] ?? '' ), 'og:site_name' => (string) ( $context['site_name'] ?? '' ), 'og:type' => (string) ( $context['type'] ?? 'website' ), 'og:image' => (string) ( $context['image'] ?? '' ), ); return array_filter( $tags, fn( $value ) => '' !== $value ); } /** * Builds the Twitter Card tag map (name => content). Uses * summary_large_image when an image exists, plain summary otherwise. * * @param array $context Same shape as build_open_graph()'s $context. * @return array name => content, empties dropped. */ public static function build_twitter_card( array $context ): array { $image = (string) ( $context['image'] ?? '' ); $tags = array( 'twitter:card' => '' !== $image ? 'summary_large_image' : 'summary', 'twitter:title' => (string) ( $context['title'] ?? '' ), 'twitter:description' => (string) ( $context['description'] ?? '' ), 'twitter:image' => $image, ); return array_filter( $tags, fn( $value ) => '' !== $value ); } /** * Builds an Article JSON-LD node for a normal post/page from resolved * values. Emits only fields that have a value, so a node never carries * empty headline/image keys. * * @param array $context Resolved values: title (headline), description, * url, image, author, published, modified, * site_name (each '' if unset). * @return array The Article node. */ public static function build_article_schema( array $context ): array { $node = array( '@context' => 'https://schema.org', '@type' => 'Article', 'headline' => (string) ( $context['title'] ?? '' ), ); $optional = array( 'description' => (string) ( $context['description'] ?? '' ), 'datePublished' => (string) ( $context['published'] ?? '' ), 'dateModified' => (string) ( $context['modified'] ?? '' ), ); foreach ( $optional as $key => $value ) { if ( '' !== $value ) { $node[ $key ] = $value; } } $url = (string) ( $context['url'] ?? '' ); if ( '' !== $url ) { $node['mainEntityOfPage'] = array( '@type' => 'WebPage', '@id' => $url, ); } $image = (string) ( $context['image'] ?? '' ); if ( '' !== $image ) { $node['image'] = $image; } $author = (string) ( $context['author'] ?? '' ); if ( '' !== $author ) { $node['author'] = array( '@type' => 'Person', 'name' => $author, ); } $site_name = (string) ( $context['site_name'] ?? '' ); if ( '' !== $site_name ) { $node['publisher'] = array( '@type' => 'Organization', 'name' => $site_name, ); } return $node; } /** * Builds a LocalBusiness JSON-LD node from a reference-NAP array (the * one ABDS_SEO_Local_SEO_Scanner already stores). Returns null when * the NAP has no name — a LocalBusiness node without a name is invalid * (see ABDS_SEO_LocalBusiness_Validator), so there's nothing to emit. * * @param array $nap Reference NAP: name, address (single line), phone. * @param string $url Site URL for the node. * @return array|null LocalBusiness node, or null when not emittable. */ public static function build_local_business_schema( array $nap, string $url = '' ): ?array { $name = trim( (string) ( $nap['name'] ?? '' ) ); if ( '' === $name ) { return null; } $node = array( '@context' => 'https://schema.org', '@type' => 'LocalBusiness', 'name' => $name, ); $phone = trim( (string) ( $nap['phone'] ?? '' ) ); if ( '' !== $phone ) { $node['telephone'] = $phone; } $address = trim( (string) ( $nap['address'] ?? '' ) ); if ( '' !== $address ) { $node['address'] = array( '@type' => 'PostalAddress', 'streetAddress' => $address, ); } if ( '' !== trim( $url ) ) { $node['url'] = trim( $url ); } return $node; } /** * Truncates text to a character budget on a word boundary (when one * exists in the final stretch), with a trailing ellipsis. Multibyte-safe. * * @param string $text Text to truncate. * @param int $max Maximum characters including the ellipsis. * @return string */ protected static function truncate_on_word_boundary( string $text, int $max ): string { if ( mb_strlen( $text ) <= $max ) { return $text; } $cut = mb_substr( $text, 0, $max - 1 ); $space = mb_strrpos( $cut, ' ' ); if ( false !== $space && $space > (int) ( $max * 0.6 ) ) { $cut = mb_substr( $cut, 0, $space ); } return rtrim( $cut, ' ,.;:—-' ) . '…'; } }