' . esc_html($message) . ' ' . esc_html__('(Only people who can edit the site see this note.)', 'siteaudithub-case-studies') . '

'; } function sahcs_shortcode_grid($atts) { $atts = shortcode_atts(sahcs_grid_defaults(), $atts, 'sahcs_grid'); $atts['title'] = sanitize_text_field($atts['title']); $atts['button_text'] = sanitize_text_field($atts['button_text']); $atts['heading'] = $atts['heading'] === '' ? '' : sahcs_heading_tag($atts['heading'], 'h3'); $html = sahcs_grid($atts); if ($html === '') { return sahcs_editor_hint(__('Case studies: nothing to show. Check the category, ids or exclude values in this shortcode, and that the case studies are published.', 'siteaudithub-case-studies')); } return $html; } /** Can the current visitor see this case study? */ function sahcs_viewable_case($id) { // On a translated page, show the case study in that language. $post = get_post(function_exists('sahcs_localize_id') ? sahcs_localize_id($id) : (int) $id); if (!$post || $post->post_type !== SAHCS_CPT) { return null; } if ($post->post_status === 'publish' && $post->post_password === '') { return $post; } return current_user_can('read_post', $post->ID) ? $post : null; } /** One case study as a wide "featured" card. */ function sahcs_shortcode_card($atts) { $atts = shortcode_atts(['id' => 0, 'button' => '', 'image' => 'yes', 'heading' => 'h2'], $atts, 'sahcs_card'); $post = sahcs_viewable_case($atts['id']); if (!$post) { /* translators: %s: the id typed in the shortcode */ return sahcs_editor_hint(sprintf(__('Case study not found for id="%s". Use the id of a published case study (see Case Studies → All Case Studies).', 'siteaudithub-case-studies'), $atts['id'])); } sahcs_enqueue_front(); $url = get_permalink($post); $terms = sahcs_post_terms($post->ID); $hl = get_post_meta($post->ID, '_sahcs_highlight', true); $btn = $atts['button'] !== '' ? sanitize_text_field($atts['button']) : __('Read the case study', 'siteaudithub-case-studies'); $out = '
'; if (sahcs_bool($atts['image']) && has_post_thumbnail($post)) { $out .= '
' . get_the_post_thumbnail($post, 'large', ['loading' => 'lazy', 'alt' => '', 'sizes' => '(max-width: 780px) 100vw, 50vw', 'style' => 'width:100%;height:100%;max-width:100%']) . '
'; } $out .= '
'; if ($terms) { $out .= '

' . esc_html($terms[0]->name) . '

'; } $title_tag = sahcs_heading_tag($atts['heading'], 'h2'); $out .= '<' . $title_tag . ' class="sahcs-feature__title">' . esc_html(wp_strip_all_tags(get_the_title($post))) . ''; if (is_array($hl) && !empty($hl['value'])) { $out .= '

' . esc_html($hl['value']) . ' ' . esc_html(isset($hl['label']) ? $hl['label'] : '') . '

'; } $excerpt = sahcs_card_excerpt($post, 32); if ($excerpt !== '') { $out .= '

' . esc_html($excerpt) . '

'; } $out .= ''; $out .= '
'; return $out; } /** Section names people can use in [sahcs_section section="..."]. */ function sahcs_shortcode_section_map() { return [ 'hero' => ['block' => 'sahcs/hero'], 'glance' => ['block' => 'sahcs/glance'], 'at-a-glance' => ['block' => 'sahcs/glance'], 'facts' => ['block' => 'sahcs/facts'], 'key-facts' => ['block' => 'sahcs/facts'], 'results' => ['block' => 'sahcs/results'], 'stats' => ['block' => 'sahcs/results'], 'gallery' => ['block' => 'sahcs/gallery'], 'testimonial' => ['block' => 'sahcs/testimonial'], 'team' => ['block' => 'sahcs/team'], 'faq' => ['block' => 'sahcs/faq'], 'cta' => ['block' => 'sahcs/cta'], 'overview' => ['block' => 'sahcs/section', 'kind' => 'overview'], 'challenges' => ['block' => 'sahcs/section', 'kind' => 'challenges'], 'solutions' => ['block' => 'sahcs/section', 'kind' => 'solutions'], 'approach' => ['block' => 'sahcs/section', 'kind' => 'approach'], 'conclusion' => ['block' => 'sahcs/section', 'kind' => 'conclusion'], ]; } function sahcs_shortcode_section($atts) { $atts = shortcode_atts(['id' => 0, 'section' => 'results', 'link' => 'yes'], $atts, 'sahcs_section'); $post = sahcs_viewable_case($atts['id']); $map = sahcs_shortcode_section_map(); $key = sanitize_key($atts['section']); if (!$post) { /* translators: %s: the id typed in the shortcode */ return sahcs_editor_hint(sprintf(__('Case study not found for id="%s".', 'siteaudithub-case-studies'), $atts['id'])); } if (!isset($map[$key])) { /* translators: 1: section typed in the shortcode, 2: list of valid sections */ return sahcs_editor_hint(sprintf(__('Unknown section "%1$s". Use one of: %2$s.', 'siteaudithub-case-studies'), $atts['section'], implode(', ', array_keys($map)))); } $want = $map[$key]; $found = null; $walk = function ($blocks) use (&$walk, &$found, $want) { foreach ($blocks as $b) { if ($found) { return; } if ($b['blockName'] === $want['block']) { if (!isset($want['kind']) || (isset($b['attrs']['kind']) && $b['attrs']['kind'] === $want['kind'])) { $found = $b; return; } } if (!empty($b['innerBlocks'])) { $walk($b['innerBlocks']); } } }; $walk(parse_blocks($post->post_content)); if (!$found) { /* translators: 1: section name, 2: case study title */ return sahcs_editor_hint(sprintf(__('The case study "%2$s" has no "%1$s" section yet.', 'siteaudithub-case-studies'), $key, get_the_title($post))); } sahcs_enqueue_front(); // Render as the case study itself (so the hero shows its own title), // with section titles as H2 inside the page that embeds it. $GLOBALS['sahcs_embed'] = true; $previous_post = isset($GLOBALS['post']) ? $GLOBALS['post'] : null; $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- restored below setup_postdata($post); $block = new WP_Block($found, ['postId' => $post->ID, 'postType' => $post->post_type]); $html = $block->render(); $GLOBALS['post'] = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited if ($previous_post) { setup_postdata($previous_post); } $GLOBALS['sahcs_embed'] = false; if ($html === '') { /* translators: 1: section name, 2: case study title */ return sahcs_editor_hint(sprintf(__('The "%1$s" section in "%2$s" is empty. Fill it in the case study editor.', 'siteaudithub-case-studies'), $key, get_the_title($post))); } if (sahcs_bool($atts['link']) && $key !== 'hero') { $html .= ''; } return '
' . str_replace('', '', $html) . '
'; }