term_id)) ? self::values_for_term($term) : null;
}
return null;
}
/**
* What the fallback renderer outputs for a post (also the read endpoint's
* `seo.rendered` on sites without an SEO plugin).
*/
public static function values_for_post($post_id) {
// The theme's SEO fields (C-41) when it has them, else Visibility's own keys.
$a = Visibility_SEO::adapter();
if (!($a instanceof Visibility_SEO_Adapter_None)) $a = new Visibility_SEO_Adapter_None();
$v = $a->read($post_id);
$description = $v['metaDescription'];
if ($description === '') {
$post = get_post($post_id);
$excerpt = $post ? ($post->post_excerpt !== '' ? $post->post_excerpt : wp_trim_words(strip_shortcodes(wp_strip_all_tags(excerpt_remove_blocks($post->post_content))), 55, '')) : '';
$description = trim(wp_strip_all_tags((string) $excerpt));
}
$og_title = $v['ogTitle'] !== '' ? $v['ogTitle'] : ($v['metaTitle'] !== '' ? $v['metaTitle'] : get_the_title($post_id));
$og_desc = $v['ogDescription'] !== '' ? $v['ogDescription'] : $description;
$og_image = $v['ogImage'] !== '' ? $v['ogImage'] : (string) get_the_post_thumbnail_url($post_id, 'full');
$canonical = $v['canonical'];
return [
'title' => $v['metaTitle'],
'description' => self::clamp($description, 320),
'canonical' => $canonical !== '' ? $canonical : (string) get_permalink($post_id),
'canonicalSet' => $canonical !== '',
'robots' => ($v['robotsNoindex'] || $v['robotsNofollow']) ? implode(',', [$v['robotsNoindex'] ? 'noindex' : 'index', $v['robotsNofollow'] ? 'nofollow' : 'follow']) : '',
'ogTitle' => self::clamp($og_title, 200),
'ogDescription' => self::clamp($og_desc, 320),
'ogImage' => $og_image,
'ogType' => 'article',
'twitterTitle' => $v['twitterTitle'],
'twitterDescription' => $v['twitterDescription'],
'twitterImage' => $v['twitterImage'],
];
}
public static function values_for_term($term) {
$a = new Visibility_SEO_Adapter_None();
$v = $a->read_term($term->term_id, $term->taxonomy);
$description = $v['metaDescription'] !== '' ? $v['metaDescription'] : trim(wp_strip_all_tags(term_description($term)));
$canonical = $v['canonical'];
$link = get_term_link($term);
return [
'title' => $v['metaTitle'],
'description' => self::clamp($description, 320),
'canonical' => $canonical !== '' ? $canonical : (is_wp_error($link) ? '' : $link),
'canonicalSet' => $canonical !== '',
'robots' => ($v['robotsNoindex'] || $v['robotsNofollow']) ? implode(',', [$v['robotsNoindex'] ? 'noindex' : 'index', $v['robotsNofollow'] ? 'nofollow' : 'follow']) : '',
'ogTitle' => self::clamp($v['ogTitle'] !== '' ? $v['ogTitle'] : ($v['metaTitle'] !== '' ? $v['metaTitle'] : $term->name), 200),
'ogDescription' => self::clamp($v['ogDescription'] !== '' ? $v['ogDescription'] : $description, 320),
'ogImage' => $v['ogImage'],
'ogType' => 'website',
'twitterTitle' => '',
'twitterDescription' => '',
'twitterImage' => '',
];
}
public static function render() {
// An active SEO plugin owns the head. Never double-emit.
if (!self::enabled()) return;
$v = self::current_values();
if (!$v) return;
// Parts the theme prints from its own SEO fields (C-41) are left to it.
$theme = Visibility_SEO::theme_printed();
if (in_array('description', $theme, true)) $v['description'] = '';
if (in_array('canonical', $theme, true)) $v['canonicalSet'] = false;
if (in_array('og', $theme, true)) { $v['ogTitle'] = ''; $v['ogDescription'] = ''; $v['ogImage'] = ''; }
echo "\n\n";
if ($v['description'] !== '') {
printf("\n", esc_attr($v['description']));
}
if ($v['robots'] !== '' && !function_exists('wp_robots')) {
// WordPress before 5.7 has no wp_robots(); print our own tag there.
printf("\n", esc_attr($v['robots']));
}
if ($v['canonicalSet']) {
// Ours wins: core's rel_canonical runs later at priority 10, so remove it.
remove_action('wp_head', 'rel_canonical');
printf("\n", esc_url($v['canonical']));
}
// Open Graph — core emits none of this.
if ($v['ogTitle'] !== '') printf("\n", esc_attr($v['ogTitle']));
if ($v['ogDescription'] !== '') printf("\n", esc_attr($v['ogDescription']));
if ($v['ogImage'] !== '') printf("\n", esc_url($v['ogImage']));
if (!in_array('og', $theme, true)) {
printf("\n", esc_attr($v['ogType']));
if ($v['canonical'] !== '') printf("\n", esc_url($v['canonical']));
}
// Twitter reads og:* for anything not set here.
$tw_image = $v['twitterImage'] !== '' ? $v['twitterImage'] : $v['ogImage'];
printf("\n", $tw_image !== '' ? 'summary_large_image' : 'summary');
if ($v['twitterTitle'] !== '') printf("\n", esc_attr(self::clamp($v['twitterTitle'], 200)));
if ($v['twitterDescription'] !== '') printf("\n", esc_attr(self::clamp($v['twitterDescription'], 320)));
if ($v['twitterImage'] !== '') printf("\n", esc_url($v['twitterImage']));
// Structured data written for the post.
if (is_singular()) {
$post_id = get_queried_object_id();
$schema = class_exists('Visibility_Schema') ? Visibility_Schema::get($post_id) : null;
if (is_array($schema)) {
if (!isset($schema['@context']) && !isset($schema[0])) $schema = ['@context' => 'https://schema.org'] + $schema;
if (isset($schema[0])) $schema = ['@context' => 'https://schema.org', '@graph' => Visibility_Schema::nodes($post_id)];
printf("\n", wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG));
}
}
echo "\n";
}
private static function clamp($text, $max) {
$text = trim((string) preg_replace('/\s+/', ' ', (string) $text));
if (function_exists('mb_strlen') && mb_strlen($text) > $max) {
return rtrim(mb_substr($text, 0, $max - 1)) . '…';
}
if (strlen($text) > $max) return rtrim(substr($text, 0, $max - 1)) . '…';
return $text;
}
}