| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Gutenberg Blocks Manager |
| 5 |
* |
| 6 |
* Registers ThinkRank's editor blocks: enqueues their editor + front-end |
| 7 |
* assets and injects block-level structured data. |
| 8 |
* |
| 9 |
* @package ThinkRank |
| 10 |
* @subpackage Editor |
| 11 |
* @since 1.15.x |
| 12 |
*/ |
| 13 |
|
| 14 |
declare(strict_types=1); |
| 15 |
|
| 16 |
namespace ThinkRank\Editor; |
| 17 |
|
| 18 |
// Prevent direct access |
| 19 |
if (!defined('ABSPATH')) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Blocks Manager. |
| 25 |
* |
| 26 |
* @since 1.15.x |
| 27 |
*/ |
| 28 |
class Blocks_Manager { |
| 29 |
|
| 30 |
/** |
| 31 |
* FAQ block name. |
| 32 |
*/ |
| 33 |
private const FAQ_BLOCK = 'thinkrank/faq'; |
| 34 |
|
| 35 |
/** |
| 36 |
* HowTo block name. |
| 37 |
*/ |
| 38 |
private const HOWTO_BLOCK = 'thinkrank/howto'; |
| 39 |
|
| 40 |
/** |
| 41 |
* TOC block name. |
| 42 |
*/ |
| 43 |
private const TOC_BLOCK = 'thinkrank/toc'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Block name → webpack asset handle. Each entry builds |
| 47 |
* assets/{handle}.js / .css / .asset.php. |
| 48 |
* |
| 49 |
* @var array<string,string> |
| 50 |
*/ |
| 51 |
private const BLOCK_ASSETS = [ |
| 52 |
self::FAQ_BLOCK => 'faq-block', |
| 53 |
self::HOWTO_BLOCK => 'howto-block', |
| 54 |
self::TOC_BLOCK => 'toc-block', |
| 55 |
]; |
| 56 |
|
| 57 |
/** |
| 58 |
* Wire up hooks. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function init(): void { |
| 63 |
add_action('enqueue_block_editor_assets', [$this, 'enqueue_editor_assets']); |
| 64 |
add_action('enqueue_block_assets', [$this, 'enqueue_block_styles']); |
| 65 |
add_filter('render_block', [$this, 'inject_block_schema'], 10, 2); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue each block's editor script. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function enqueue_editor_assets(): void { |
| 74 |
foreach (self::BLOCK_ASSETS as $handle) { |
| 75 |
$asset_path = THINKRANK_PLUGIN_DIR . "assets/{$handle}.asset.php"; |
| 76 |
$asset = file_exists($asset_path) |
| 77 |
? include $asset_path |
| 78 |
: ['dependencies' => ['wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n'], 'version' => THINKRANK_VERSION]; |
| 79 |
|
| 80 |
wp_enqueue_script( |
| 81 |
"thinkrank-{$handle}", |
| 82 |
THINKRANK_PLUGIN_URL . "assets/{$handle}.js", |
| 83 |
$asset['dependencies'] ?? [], |
| 84 |
$asset['version'] ?? THINKRANK_VERSION, |
| 85 |
true |
| 86 |
); |
| 87 |
|
| 88 |
wp_set_script_translations("thinkrank-{$handle}", 'thinkrank'); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Enqueue block stylesheets where blocks render. |
| 94 |
* |
| 95 |
* Hooked to enqueue_block_assets — not enqueue_block_editor_assets — so |
| 96 |
* core mirrors them into the iframed editor canvas instead of only the |
| 97 |
* editor's outer document. On the front end each loads only when its |
| 98 |
* block is present. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function enqueue_block_styles(): void { |
| 103 |
foreach (self::BLOCK_ASSETS as $block_name => $handle) { |
| 104 |
if (!is_admin() && (!function_exists('has_block') || !has_block($block_name))) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
$css = THINKRANK_PLUGIN_DIR . "assets/{$handle}.css"; |
| 109 |
if (!file_exists($css)) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
$asset_path = THINKRANK_PLUGIN_DIR . "assets/{$handle}.asset.php"; |
| 114 |
$asset = file_exists($asset_path) ? include $asset_path : []; |
| 115 |
|
| 116 |
wp_enqueue_style( |
| 117 |
"thinkrank-{$handle}", |
| 118 |
THINKRANK_PLUGIN_URL . "assets/{$handle}.css", |
| 119 |
[], |
| 120 |
$asset['version'] ?? THINKRANK_VERSION |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Append block-level JSON-LD after a ThinkRank block's rendered output. |
| 127 |
* |
| 128 |
* Done server-side (not in the blocks' save output) so the schema is not |
| 129 |
* stripped by KSES for users without unfiltered_html. |
| 130 |
* |
| 131 |
* @param string $block_content Rendered block HTML. |
| 132 |
* @param array $block Parsed block (name + attrs). |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function inject_block_schema(string $block_content, array $block): string { |
| 136 |
$name = $block['blockName'] ?? ''; |
| 137 |
$attrs = $block['attrs'] ?? []; |
| 138 |
|
| 139 |
if (!isset(self::BLOCK_ASSETS[$name])) { |
| 140 |
return $block_content; |
| 141 |
} |
| 142 |
|
| 143 |
// Schema output is on by default; only skip when explicitly disabled. |
| 144 |
if (array_key_exists('outputSchema', $attrs) && false === $attrs['outputSchema']) { |
| 145 |
return $block_content; |
| 146 |
} |
| 147 |
|
| 148 |
switch ($name) { |
| 149 |
case self::FAQ_BLOCK: |
| 150 |
// The request's schema graph already merged this block's questions |
| 151 |
// into its single FAQPage, so emitting here would recreate the |
| 152 |
// duplicate FAQPage the graph exists to prevent (#355). |
| 153 |
if ($this->faq_absorbed_by_graph()) { |
| 154 |
return $block_content; |
| 155 |
} |
| 156 |
$schema = $this->build_faq_schema($attrs); |
| 157 |
break; |
| 158 |
case self::HOWTO_BLOCK: |
| 159 |
$schema = $this->build_howto_schema($attrs); |
| 160 |
break; |
| 161 |
case self::TOC_BLOCK: |
| 162 |
$schema = $this->build_toc_schema($attrs); |
| 163 |
break; |
| 164 |
default: |
| 165 |
$schema = null; |
| 166 |
} |
| 167 |
|
| 168 |
if (null === $schema) { |
| 169 |
return $block_content; |
| 170 |
} |
| 171 |
|
| 172 |
$json = wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); |
| 173 |
if (false === $json) { |
| 174 |
return $block_content; |
| 175 |
} |
| 176 |
|
| 177 |
return $block_content . "\n" . '<script type="application/ld+json">' . $json . '</script>'; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Whether the schema graph already absorbed this page's FAQ content. |
| 182 |
* |
| 183 |
* Falls back to false whenever the graph never ran, so the block keeps its |
| 184 |
* original standalone behaviour outside a normal front-end render. |
| 185 |
* |
| 186 |
* @since 1.32.0 |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
private function faq_absorbed_by_graph(): bool { |
| 190 |
if (!class_exists('ThinkRank\\Frontend\\Schema_Graph')) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
return \ThinkRank\Frontend\Schema_Graph::instance()->absorbed_content_faq(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* FAQPage schema from FAQ block attributes. |
| 199 |
* |
| 200 |
* @param array $attrs Block attributes. |
| 201 |
* @return array|null Schema array, or null when there is nothing to emit. |
| 202 |
*/ |
| 203 |
private function build_faq_schema(array $attrs): ?array { |
| 204 |
$faqs = $attrs['faqs'] ?? []; |
| 205 |
if (!is_array($faqs) || empty($faqs)) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
$entities = []; |
| 210 |
foreach ($faqs as $faq) { |
| 211 |
$question = isset($faq['question']) ? trim(wp_strip_all_tags((string) $faq['question'])) : ''; |
| 212 |
$answer = isset($faq['answer']) ? trim((string) $faq['answer']) : ''; |
| 213 |
if ($question === '' || $answer === '') { |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
$entities[] = [ |
| 218 |
'@type' => 'Question', |
| 219 |
'name' => $question, |
| 220 |
'acceptedAnswer' => [ |
| 221 |
'@type' => 'Answer', |
| 222 |
'text' => wp_kses_post($answer), |
| 223 |
], |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
if (empty($entities)) { |
| 228 |
return null; |
| 229 |
} |
| 230 |
|
| 231 |
return [ |
| 232 |
'@context' => 'https://schema.org', |
| 233 |
'@type' => 'FAQPage', |
| 234 |
'mainEntity' => $entities, |
| 235 |
]; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* HowTo schema from HowTo block attributes. |
| 240 |
* |
| 241 |
* Property shape follows Google's HowTo guidelines (and matches what |
| 242 |
* RankMath emits): name, description, totalTime as ISO 8601, and |
| 243 |
* HowToStep entries with name/text/image. |
| 244 |
* |
| 245 |
* @param array $attrs Block attributes. |
| 246 |
* @return array|null Schema array, or null when there is nothing to emit. |
| 247 |
*/ |
| 248 |
private function build_howto_schema(array $attrs): ?array { |
| 249 |
$steps = $attrs['steps'] ?? []; |
| 250 |
if (!is_array($steps) || empty($steps)) { |
| 251 |
return null; |
| 252 |
} |
| 253 |
|
| 254 |
$step_entities = []; |
| 255 |
foreach ($steps as $step) { |
| 256 |
$title = isset($step['title']) ? trim(wp_strip_all_tags((string) $step['title'])) : ''; |
| 257 |
$text = isset($step['text']) ? trim(wp_strip_all_tags((string) $step['text'])) : ''; |
| 258 |
if ($title === '' && $text === '') { |
| 259 |
continue; |
| 260 |
} |
| 261 |
|
| 262 |
$entity = ['@type' => 'HowToStep']; |
| 263 |
if ($title !== '' && $text !== '') { |
| 264 |
$entity['name'] = $title; |
| 265 |
$entity['text'] = $text; |
| 266 |
} else { |
| 267 |
// Google requires text; fall back to whichever field is set. |
| 268 |
$entity['text'] = $text !== '' ? $text : $title; |
| 269 |
} |
| 270 |
|
| 271 |
$image_url = isset($step['imageUrl']) ? esc_url_raw((string) $step['imageUrl']) : ''; |
| 272 |
if ($image_url !== '') { |
| 273 |
$entity['image'] = [ |
| 274 |
'@type' => 'ImageObject', |
| 275 |
'url' => $image_url, |
| 276 |
]; |
| 277 |
} |
| 278 |
|
| 279 |
$step_entities[] = $entity; |
| 280 |
} |
| 281 |
|
| 282 |
if (empty($step_entities)) { |
| 283 |
return null; |
| 284 |
} |
| 285 |
|
| 286 |
$heading = isset($attrs['heading']) ? trim(wp_strip_all_tags((string) $attrs['heading'])) : ''; |
| 287 |
$name = $heading !== '' ? $heading : get_the_title(); |
| 288 |
|
| 289 |
$schema = [ |
| 290 |
'@context' => 'https://schema.org', |
| 291 |
'@type' => 'HowTo', |
| 292 |
'name' => $name, |
| 293 |
'step' => $step_entities, |
| 294 |
]; |
| 295 |
|
| 296 |
$description = isset($attrs['description']) ? trim(wp_strip_all_tags((string) $attrs['description'])) : ''; |
| 297 |
if ($description !== '') { |
| 298 |
$schema['description'] = $description; |
| 299 |
} |
| 300 |
|
| 301 |
// ISO 8601 duration, e.g. P1DT2H30M — only when a duration was set. |
| 302 |
$days = max(0, (int) ($attrs['totalDays'] ?? 0)); |
| 303 |
$hours = max(0, (int) ($attrs['totalHours'] ?? 0)); |
| 304 |
$minutes = max(0, (int) ($attrs['totalMinutes'] ?? 0)); |
| 305 |
if ($days + $hours + $minutes > 0) { |
| 306 |
$schema['totalTime'] = sprintf('P%dDT%dH%dM', $days, $hours, $minutes); |
| 307 |
} |
| 308 |
|
| 309 |
return $schema; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* SiteNavigationElement schema from TOC block attributes (one element per |
| 314 |
* listed section — the shape RankMath's TOC block emits). |
| 315 |
* |
| 316 |
* @param array $attrs Block attributes. |
| 317 |
* @return array|null Schema array, or null when there is nothing to emit. |
| 318 |
*/ |
| 319 |
private function build_toc_schema(array $attrs): ?array { |
| 320 |
$headings = $attrs['headings'] ?? []; |
| 321 |
if (!is_array($headings) || empty($headings)) { |
| 322 |
return null; |
| 323 |
} |
| 324 |
|
| 325 |
$permalink = get_permalink(); |
| 326 |
if (!is_string($permalink)) { |
| 327 |
$permalink = ''; |
| 328 |
} |
| 329 |
|
| 330 |
$elements = []; |
| 331 |
foreach ($headings as $item) { |
| 332 |
$content = isset($item['content']) ? trim(wp_strip_all_tags((string) $item['content'])) : ''; |
| 333 |
$anchor = isset($item['anchor']) ? trim((string) $item['anchor']) : ''; |
| 334 |
if ($content === '' || $anchor === '') { |
| 335 |
continue; |
| 336 |
} |
| 337 |
|
| 338 |
$elements[] = [ |
| 339 |
'@type' => 'SiteNavigationElement', |
| 340 |
'name' => $content, |
| 341 |
'url' => $permalink . '#' . $anchor, |
| 342 |
]; |
| 343 |
} |
| 344 |
|
| 345 |
if (empty($elements)) { |
| 346 |
return null; |
| 347 |
} |
| 348 |
|
| 349 |
return [ |
| 350 |
'@context' => 'https://schema.org', |
| 351 |
'@graph' => $elements, |
| 352 |
]; |
| 353 |
} |
| 354 |
} |
| 355 |
|