'application/ld+json', 'class' => 'sahcs-schema'] ); } /** Plain-text description of a case study for meta tags and schema. */ function sahcs_description($post) { $text = sahcs_card_excerpt($post, 30); return sahcs_trim(preg_replace('/\s+/u', ' ', $text)); } /** People from the case study's Project Team sections, as schema.org Person. */ function sahcs_team_schema($post) { $people = []; $walk = function ($blocks) use (&$walk, &$people) { foreach ($blocks as $b) { if ($b['blockName'] === 'sahcs/team' && !empty($b['attrs']['items'])) { foreach (sahcs_team_items($b['attrs']['items']) as $m) { $person = ['@type' => 'Person', 'name' => wp_strip_all_tags($m['name'])]; if ($m['role'] !== '') { $person['jobTitle'] = wp_strip_all_tags($m['role']); } if ($m['link'] !== '') { $person['url'] = $m['link']; } $people[] = $person; } } if (!empty($b['innerBlocks'])) { $walk($b['innerBlocks']); } } }; $walk(parse_blocks($post->post_content)); return $people; } /** The case study's author as schema.org Person (when the author line is on). */ function sahcs_author_schema($post) { $id = (int) $post->post_author; if (!$id || !sahcs_get_settings('show_author')) { return null; } $name = get_the_author_meta('display_name', $id); return $name !== '' ? ['@type' => 'Person', 'name' => $name, 'url' => get_author_posts_url($id)] : null; } /* * With Rank Math or Yoast active, their Article data is kept and only the * project team is added to it as "contributor". */ function sahcs_add_contributors(&$entity, $people) { if (!is_array($entity) || !$people) { return; } $types = isset($entity['@type']) ? (array) $entity['@type'] : []; if (array_intersect($types, ['Article', 'BlogPosting', 'NewsArticle', 'TechArticle', 'CreativeWork'])) { $entity['contributor'] = $people; } } add_filter('rank_math/json_ld', function ($data) { if (!is_singular(SAHCS_CPT) || !is_array($data)) { return $data; } $people = sahcs_team_schema(get_queried_object()); foreach ($data as $key => $entity) { sahcs_add_contributors($entity, $people); $data[$key] = $entity; } return $data; }, 99); add_filter('wpseo_schema_article', function ($data) { if (is_singular(SAHCS_CPT)) { sahcs_add_contributors($data, sahcs_team_schema(get_queried_object())); } return $data; }); add_action('wp_head', function () { if (!is_singular(SAHCS_CPT) || sahcs_seo_plugin_active()) { return; } $post = get_queried_object(); if (!($post instanceof WP_Post)) { return; } $desc = sahcs_description($post); if ($desc !== '') { echo '' . "\n"; } }, 2); add_action('wp_footer', function () { if (!is_singular(SAHCS_CPT)) { return; } $post = get_queried_object(); if (!($post instanceof WP_Post) || post_password_required($post)) { return; } // FAQ - always, built from the saved FAQ sections. $faqs = []; $walk = function ($blocks) use (&$walk, &$faqs) { foreach ($blocks as $b) { if ($b['blockName'] === 'sahcs/faq' && !empty($b['attrs']['items'])) { foreach ($b['attrs']['items'] as $i) { if (!empty($i['q']) && !empty($i['a'])) { $faqs[] = [ '@type' => 'Question', 'name' => wp_strip_all_tags($i['q']), 'acceptedAnswer' => ['@type' => 'Answer', 'text' => wp_strip_all_tags($i['a'])], ]; } } } if (!empty($b['innerBlocks'])) { $walk($b['innerBlocks']); } } }; $walk(parse_blocks($post->post_content)); if ($faqs) { sahcs_json_ld(['@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => $faqs]); } if (sahcs_seo_plugin_active()) { return; } // Article $article = [ '@context' => 'https://schema.org', '@type' => 'Article', 'headline' => wp_strip_all_tags(get_the_title($post)), 'datePublished' => get_post_time('c', true, $post), 'dateModified' => get_post_modified_time('c', true, $post), 'mainEntityOfPage' => get_permalink($post), 'author' => ['@type' => 'Organization', 'name' => get_bloginfo('name'), 'url' => home_url('/')], 'publisher' => ['@type' => 'Organization', 'name' => get_bloginfo('name'), 'url' => home_url('/')], ]; $author = sahcs_author_schema($post); if ($author) { $article['author'] = $author; } $team = sahcs_team_schema($post); if ($team) { $article['contributor'] = $team; } $desc = sahcs_description($post); if ($desc !== '') { $article['description'] = $desc; } $img = get_the_post_thumbnail_url($post, 'full'); if ($img) { $article['image'] = $img; } $logo_id = get_theme_mod('custom_logo'); if ($logo_id) { $logo = wp_get_attachment_image_url($logo_id, 'full'); if ($logo) { $article['publisher']['logo'] = ['@type' => 'ImageObject', 'url' => $logo]; } } sahcs_json_ld($article); // Breadcrumb: Home › Case Studies › Category › Title $crumbs = [['name' => get_bloginfo('name'), 'item' => home_url('/')]]; $title = sahcs_get_settings('archive_title'); $crumbs[] = ['name' => $title !== '' ? $title : __('Case Studies', 'siteaudithub-case-studies'), 'item' => get_post_type_archive_link(SAHCS_CPT)]; $terms = sahcs_post_terms($post->ID); if ($terms) { $link = get_term_link($terms[0]); if (!is_wp_error($link)) { $crumbs[] = ['name' => $terms[0]->name, 'item' => $link]; } } $crumbs[] = ['name' => wp_strip_all_tags(get_the_title($post)), 'item' => get_permalink($post)]; $list = []; foreach ($crumbs as $n => $c) { $list[] = ['@type' => 'ListItem', 'position' => $n + 1, 'name' => $c['name'], 'item' => $c['item']]; } sahcs_json_ld(['@context' => 'https://schema.org', '@type' => 'BreadcrumbList', 'itemListElement' => $list]); });