'', 'tldr_position' => 'top', // top | off (shortcode only) 'tldr_style' => 'box', // 2.63 — box | line | plain 'tldr_show_label' => 'on', // 2.63 — on | off ("In breve:" before the text) 'accent' => '', // 2.63 — hex colour; '' = the plugin violet 'faq_title' => '', 'faq_position' => 'bottom', // bottom | off 'faq_style' => 'accordion', // 2.63 — accordion | plain 'faq_existing' => 'merge', // 2.63 — merge into a FAQ the page already has | separate 'author_box' => 'on', // on | off ); $s = get_option(CITAMI_CONTENT_SETTINGS_OPT, array()); if (!is_array($s)) { return $d; } foreach ($d as $k => $v) { if (isset($s[$k]) && is_string($s[$k])) { $d[$k] = $s[$k]; } } return $d; } function citami_sanitize_content_settings($value) { $in = is_array($value) ? $value : array(); return array( 'tldr_label' => mb_substr(sanitize_text_field(isset($in['tldr_label']) ? (string) $in['tldr_label'] : ''), 0, 60), 'tldr_position' => (isset($in['tldr_position']) && $in['tldr_position'] === 'off') ? 'off' : 'top', 'faq_title' => mb_substr(sanitize_text_field(isset($in['faq_title']) ? (string) $in['faq_title'] : ''), 0, 80), 'faq_position' => (isset($in['faq_position']) && $in['faq_position'] === 'off') ? 'off' : 'bottom', 'author_box' => (isset($in['author_box']) && $in['author_box'] === 'off') ? 'off' : 'on', 'tldr_style' => (isset($in['tldr_style']) && in_array($in['tldr_style'], array('box', 'line', 'plain'), true)) ? $in['tldr_style'] : 'box', 'tldr_show_label' => (isset($in['tldr_show_label']) && $in['tldr_show_label'] === 'off') ? 'off' : 'on', 'accent' => (isset($in['accent']) && preg_match('/^#[0-9a-fA-F]{6}$/', (string) $in['accent'])) ? strtolower((string) $in['accent']) : '', 'faq_style' => (isset($in['faq_style']) && $in['faq_style'] === 'plain') ? 'plain' : 'accordion', 'faq_existing' => (isset($in['faq_existing']) && $in['faq_existing'] === 'separate') ? 'separate' : 'merge', ); } /** 2.63 — the accent colour of the blocks ('' setting = the plugin violet). */ function citami_content_accent() { $a = citami_content_settings()['accent']; return $a !== '' ? $a : '#7c3aed'; } add_action('admin_init', function () { register_setting('citami_seo', CITAMI_CONTENT_SETTINGS_OPT, array( 'type' => 'array', 'sanitize_callback' => 'citami_sanitize_content_settings', 'default' => array(), )); }); /* ---------------------------------------------------------------- */ /* Post data */ /* ---------------------------------------------------------------- */ function citami_content_tldr($post_id) { return trim((string) get_post_meta($post_id, CITAMI_META_TLDR, true)); } /** FAQ items as array of ['q' => ..., 'a' => ...] (plain text, tags stripped). */ function citami_content_faq($post_id) { $raw = get_post_meta($post_id, CITAMI_META_FAQ, true); $items = is_string($raw) ? json_decode($raw, true) : $raw; if (!is_array($items)) { return array(); } $out = array(); foreach ($items as $it) { if (!is_array($it)) { continue; } $q = trim((string) (isset($it['q']) ? $it['q'] : '')); $a = trim((string) (isset($it['a']) ? $it['a'] : '')); if ($q !== '' && $a !== '') { $out[] = array('q' => $q, 'a' => $a); } } return $out; } /** Stores TL;DR + FAQ. Answers keep basic formatting (wp_kses_post). */ function citami_content_save($post_id, $tldr, array $faq) { $tldr = mb_substr(trim(wp_strip_all_tags((string) $tldr)), 0, 600); update_post_meta($post_id, CITAMI_META_TLDR, $tldr); $clean = array(); foreach ($faq as $it) { $q = mb_substr(trim(wp_strip_all_tags((string) (isset($it['q']) ? $it['q'] : ''))), 0, 200); $a = mb_substr(trim(wp_kses_post((string) (isset($it['a']) ? $it['a'] : ''))), 0, 2000); if ($q !== '' && $a !== '') { $clean[] = array('q' => $q, 'a' => $a); } if (count($clean) >= 30) { break; } } if (empty($clean)) { delete_post_meta($post_id, CITAMI_META_FAQ); } else { update_post_meta($post_id, CITAMI_META_FAQ, wp_json_encode($clean, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG)); } return array('tldr' => $tldr, 'faq' => $clean); } /* ---------------------------------------------------------------- */ /* Rendering */ /* ---------------------------------------------------------------- */ function citami_content_render_tldr($post_id) { $tldr = citami_content_tldr($post_id); if ($tldr === '') { return ''; } $set = citami_content_settings(); $label = $set['tldr_label']; if ($label === '') { $label = citami_t('In breve', 'In short'); } // 2.63 — three looks: a tinted box, only the accent line, or plain text // that blends with the theme; the label ("In breve:") can be left out. $accent = citami_content_accent(); $styles = array( 'box' => 'border-left:4px solid ' . $accent . ';padding:12px 16px;margin:0 0 1.5em;background:' . $accent . '10', 'line' => 'border-left:4px solid ' . $accent . ';padding:2px 0 2px 16px;margin:0 0 1.5em', 'plain' => 'margin:0 0 1.5em', ); $style = isset($styles[$set['tldr_style']]) ? $set['tldr_style'] : 'box'; $prefix = $set['tldr_show_label'] === 'off' ? '' : '' . esc_html($label) . ': '; return '
' . $prefix . esc_html($tldr) . '
]*class="[^"]*schema-faq-answer[^"]*"[^>]*>(.*?)
)?#is', $html, $m, PREG_SET_ORDER)) { $out['kind'] = 'yoast'; foreach ($m as $x) $out['items'][] = array('q' => $clean($x[1]), 'a' => $clean(isset($x[2]) ? $x[2] : '')); return $out; } if (strpos($html, 'rank-math-question') !== false && preg_match_all('#]*>\s*<(?:strong|b)>[^<]*\?\s*(?:strong|b)>\s*
)#is', $section, -1, PREG_SPLIT_DELIM_CAPTURE); $items = array(); for ($i = 1; $i < count($parts); $i += 2) { $q = $clean($parts[$i]); if ($q === '') continue; $items[] = array('q' => $q, 'a' => $clean(isset($parts[$i + 1]) ? $parts[$i + 1] : '')); } if (!empty($items)) { return array('kind' => 'heading', 'items' => $items, 'level' => $level, 'end' => $end); } } return $out; } /** Index just before the that closes the' . wp_kses_post(wp_strip_all_tags($it['a']) === $it['a'] ? esc_html($it['a']) : $it['a']) . '
| FAQ | |
|---|---|