| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * The design guide ability, which hands agents that work on the site through 4 * the Abilities API what Ipsum is built with and how it is meant to be used. 5 * 6 * @package Ipsum 7 * @since Ipsum 1.0 8 */ 9 10 if ( ! function_exists( 'ipsum_register_abilities' ) ) : 11 /** 12 * Registers the design guide ability, so agents that work on the site 13 * through the Abilities API, rather than this repository, can learn how 14 * Ipsum is meant to be built with. 15 * 16 * It needs `edit_theme_options`, the capability behind the Site Editor and 17 * Global Styles, since the guide is for whoever builds the site's design. 18 * 19 * @since Ipsum 1.0 20 * 21 * @return void 22 */ 23 function ipsum_register_abilities() { 24 $output_schema = ipsum_get_design_guide_schema(); 25 26 wp_register_ability( 27 'ipsum/get-design-guide', 28 array( 29 'label' => __( 'Get Design Guide', 'ipsum' ), 30 'description' => __( 'Returns the design guide of the Ipsum theme, read from the site as it is now. By default returns the basic design sections: the active theme, whether the palette is light or dark, the color palette and what each color is for, gradients, duotone, typography, spacing, layout widths, and guidelines. Pass fields to request specific sections, including larger lists such as style variations, block styles, patterns, templates, template parts, and template part areas. Read it before building pages, templates or styles.', 'ipsum' ), 31 'category' => 'site', 32 'input_schema' => array( 33 'type' => 'object', 34 'properties' => array( 35 'fields' => array( 36 'type' => 'array', 37 'title' => __( 'Fields', 'ipsum' ), 38 'description' => __( 'Optional. Sections to return. When omitted or empty, returns the basic design sections (theme, colorScheme, colors, gradients, duotone, typography, spacing, layout, guidelines). When set, returns only the listed sections. Every section name in the enum is valid, including larger lists such as patterns, templates, template parts, template part areas, block styles, and style variations.', 'ipsum' ), 39 'items' => array( 40 'type' => 'string', 41 'enum' => array_keys( $output_schema['properties'] ), 42 ), 43 ), 44 ), 45 'additionalProperties' => false, 46 'default' => array(), 47 ), 48 'output_schema' => $output_schema, 49 'execute_callback' => 'ipsum_get_design_guide', 50 'required_capabilities' => array( 'edit_theme_options' ), 51 'meta' => array( 52 'annotations' => array( 53 'readonly' => true, 54 'destructive' => false, 55 'idempotent' => true, 56 ), 57 'public' => true, 58 ), 59 ) 60 ); 61 } 62 endif; 63 add_action( 'wp_abilities_api_init', 'ipsum_register_abilities' ); 64 65 if ( ! function_exists( 'ipsum_get_color_roles' ) ) : 66 /** 67 * Returns what each palette color is for, keyed by its slug. 68 * 69 * Every color variation redefines the same slugs, so the roles hold under 70 * whichever variation is active. 71 * 72 * @since Ipsum 1.0 73 * 74 * @return array<string, string> Role descriptions, keyed by palette slug. 75 */ 76 function ipsum_get_color_roles() { 77 return array( 78 'base' => __( 'The page background.', 'ipsum' ), 79 'contrast' => __( 'Body text, headings, links and buttons, on Base. It can be translucent, so on any other background use Strong Contrast.', 'ipsum' ), 80 'accent-1' => __( 'An accent for small highlights. The theme uses it only for the arrival bar. Links and buttons use Contrast, not this. Use it sparingly.', 'ipsum' ), 81 'accent-3' => __( 'The strongest contrast with the background, not an accent: hover states, and the background of inverted sections such as the navigation overlay, with Base for the text.', 'ipsum' ), 82 'accent-5' => __( 'A subtle, translucent wash behind quotes, tags and table headers. It works on any background; keep Contrast text on it.', 'ipsum' ), 83 'accent-6' => __( 'Faint, translucent lines, such as borders, list markers and table rules. Not for text.', 'ipsum' ), 84 ); 85 } 86 endif; 87 88 if ( ! function_exists( 'ipsum_get_design_guide_default_fields' ) ) : 89 /** 90 * Returns the design guide sections included when fields is omitted or empty. 91 * 92 * Larger lists (style variations, block styles, patterns, templates, 93 * template parts, and template part areas) are left out of the default 94 * response and must be requested through fields. 95 * 96 * @since Ipsum 1.0 97 * 98 * @return string[] Section names. 99 */ 100 function ipsum_get_design_guide_default_fields() { 101 return array( 102 'theme', 103 'colorScheme', 104 'colors', 105 'gradients', 106 'duotone', 107 'typography', 108 'spacing', 109 'layout', 110 'guidelines', 111 ); 112 } 113 endif; 114 115 if ( ! function_exists( 'ipsum_get_design_guide_schema' ) ) : 116 /** 117 * Returns the JSON Schema of the design guide, with one property per section. 118 * 119 * Top-level sections stay optional so callers can request a subset. Nested 120 * objects with a fixed set of fields mark guaranteed properties as required 121 * and set additionalProperties to false. 122 * 123 * @since Ipsum 1.0 124 * 125 * @return array<string, mixed> The output schema of the design guide ability. 126 * @phpstan-return array{ 127 * type: 'object', 128 * additionalProperties: false, 129 * properties: array<string, array<string, mixed>> 130 * } 131 */ 132 function ipsum_get_design_guide_schema() { 133 $string = array( 'type' => 'string' ); 134 $nullable = array( 135 'type' => array( 'string', 'null' ), 136 'description' => __( 'A font family preset slug, a literal font-family value, or null when unset.', 'ipsum' ), 137 ); 138 $strings = array( 139 'type' => 'array', 140 'items' => $string, 141 ); 142 $scheme = array( 143 'type' => 'string', 144 'title' => __( 'Color Scheme', 'ipsum' ), 145 'description' => __( 'Whether the palette is light or dark.', 'ipsum' ), 146 'enum' => array( 'light', 'dark' ), 147 ); 148 149 $object_list = static function ( $title, $description, $properties, $required = array() ) { 150 $items = array( 151 'type' => 'object', 152 'properties' => $properties, 153 'additionalProperties' => false, 154 ); 155 if ( ! empty( $required ) ) { 156 $items['required'] = $required; 157 } 158 return array( 159 'type' => 'array', 160 'title' => $title, 161 'description' => $description, 162 'items' => $items, 163 ); 164 }; 165 166 $preset_list = static function ( $title, $description, $value_key, $value_schema, $required, $extra_properties = array() ) use ( $object_list ) { 167 $name_description = in_array( 'name', $required, true ) 168 ? __( 'The human-readable preset name.', 'ipsum' ) 169 : __( 'The human-readable preset name. Omitted when the preset does not set one.', 'ipsum' ); 170 $properties = array_merge( 171 array( 172 'slug' => array( 173 'type' => 'string', 174 'title' => __( 'Slug', 'ipsum' ), 175 'description' => __( 'The preset slug.', 'ipsum' ), 176 ), 177 'name' => array( 178 'type' => 'string', 179 'title' => __( 'Name', 'ipsum' ), 180 'description' => $name_description, 181 ), 182 $value_key => $value_schema, 183 ), 184 $extra_properties 185 ); 186 return $object_list( $title, $description, $properties, $required ); 187 }; 188 189 $colors = $preset_list( 190 __( 'Colors', 'ipsum' ), 191 __( 'The color palette from the theme and any colors added on the site, with what each theme color is for. Every style variation redefines the same slugs. The role field is omitted for colors that have no known role.', 'ipsum' ), 192 'color', 193 array( 194 'type' => 'string', 195 'title' => __( 'Color', 'ipsum' ), 196 'description' => __( 'The CSS color value.', 'ipsum' ), 197 ), 198 array( 'slug', 'name', 'color' ), 199 array( 200 'role' => array( 201 'type' => 'string', 202 'title' => __( 'Role', 'ipsum' ), 203 'description' => __( 'What the color is for. Present only for theme palette colors with a known role.', 'ipsum' ), 204 ), 205 ) 206 ); 207 208 $font_value = array( 209 'type' => array( 'string', 'null' ), 210 'description' => __( 'A font family preset slug, a literal font-family value, or null when unset. headingFont falls back to bodyFont when headings do not set their own family.', 'ipsum' ), 211 ); 212 213 return array( 214 'type' => 'object', 215 'additionalProperties' => false, 216 'properties' => array( 217 'theme' => array( 218 'type' => 'object', 219 'title' => __( 'Theme', 'ipsum' ), 220 'description' => __( 'The active theme. Its stylesheet is the one to use in REST routes such as /wp/v2/global-styles/themes/{stylesheet}.', 'ipsum' ), 221 'properties' => array( 222 'name' => array( 223 'type' => 'string', 224 'title' => __( 'Name', 'ipsum' ), 225 'description' => __( 'The active theme name.', 'ipsum' ), 226 ), 227 'stylesheet' => array( 228 'type' => 'string', 229 'title' => __( 'Stylesheet', 'ipsum' ), 230 'description' => __( 'The theme stylesheet slug.', 'ipsum' ), 231 ), 232 ), 233 'required' => array( 'name', 'stylesheet' ), 234 'additionalProperties' => false, 235 ), 236 'colorScheme' => array_merge( 237 $scheme, 238 array( 'description' => __( 'Whether the active palette is light or dark.', 'ipsum' ) ) 239 ), 240 'colors' => $colors, 241 'gradients' => $preset_list( 242 __( 'Gradients', 'ipsum' ), 243 __( 'The gradient presets from the theme and any added on the site.', 'ipsum' ), 244 'gradient', 245 array( 246 'type' => 'string', 247 'title' => __( 'Gradient', 'ipsum' ), 248 'description' => __( 'The CSS gradient value.', 'ipsum' ), 249 ), 250 array( 'slug', 'name', 'gradient' ) 251 ), 252 'duotone' => $preset_list( 253 __( 'Duotone', 'ipsum' ), 254 __( 'The duotone filter presets from the theme and any added on the site.', 'ipsum' ), 255 'colors', 256 array_merge( 257 $strings, 258 array( 259 'title' => __( 'Colors', 'ipsum' ), 260 'description' => __( 'The two colors in the duotone filter.', 'ipsum' ), 261 ) 262 ), 263 array( 'slug', 'name', 'colors' ) 264 ), 265 'typography' => array( 266 'type' => 'object', 267 'title' => __( 'Typography', 'ipsum' ), 268 'description' => __( 'The font families of body text and headings, the blocks that use another family, and the font family and font size presets. Font values are preset slugs, literal font-family values, or null. headingFont falls back to bodyFont when headings do not set their own family. Font size presets list theme sizes first, then any custom sizes; they are not sorted by size.', 'ipsum' ), 269 'properties' => array( 270 'bodyFont' => array_merge( 271 $font_value, 272 array( 'title' => __( 'Body Font', 'ipsum' ) ) 273 ), 274 'headingFont' => array_merge( 275 $font_value, 276 array( 'title' => __( 'Heading Font', 'ipsum' ) ) 277 ), 278 'blockFonts' => array( 279 'type' => 'object', 280 'title' => __( 'Block Fonts', 'ipsum' ), 281 'description' => __( 'The blocks set in another font than body text, such as buttons, navigation and post meta, grouped by font family slug or literal value.', 'ipsum' ), 282 'additionalProperties' => $strings, 283 ), 284 'fontFamilies' => $preset_list( 285 __( 'Font Families', 'ipsum' ), 286 __( 'The font family presets from the theme and any added on the site. The name is omitted when a preset does not set one.', 'ipsum' ), 287 'fontFamily', 288 array( 289 'type' => 'string', 290 'title' => __( 'Font Family', 'ipsum' ), 291 'description' => __( 'The CSS font-family value.', 'ipsum' ), 292 ), 293 array( 'slug', 'fontFamily' ) 294 ), 295 'fontSizes' => $preset_list( 296 __( 'Font Sizes', 'ipsum' ), 297 __( 'The font size presets from the theme, then any custom sizes. Not sorted by size. The name is omitted when a preset does not set one.', 'ipsum' ), 298 'size', 299 array( 300 'type' => 'string', 301 'title' => __( 'Size', 'ipsum' ), 302 'description' => __( 'The CSS font-size value.', 'ipsum' ), 303 ), 304 array( 'slug', 'size' ) 305 ), 306 ), 307 'required' => array( 'bodyFont', 'headingFont', 'blockFonts', 'fontFamilies', 'fontSizes' ), 308 'additionalProperties' => false, 309 ), 310 'spacing' => $preset_list( 311 __( 'Spacing', 'ipsum' ), 312 __( 'The spacing scale from the theme, then any custom sizes. Not sorted by size. The name is omitted when a preset does not set one.', 'ipsum' ), 313 'size', 314 array( 315 'type' => 'string', 316 'title' => __( 'Size', 'ipsum' ), 317 'description' => __( 'The CSS spacing size value.', 'ipsum' ), 318 ), 319 array( 'slug', 'size' ) 320 ), 321 'layout' => array( 322 'type' => 'object', 323 'title' => __( 'Layout', 'ipsum' ), 324 'description' => __( 'The width of the content, and of wide-aligned blocks.', 'ipsum' ), 325 'properties' => array( 326 'contentSize' => array( 327 'type' => array( 'string', 'null' ), 328 'title' => __( 'Content Size', 'ipsum' ), 329 'description' => __( 'The default content width, or null when unset.', 'ipsum' ), 330 ), 331 'wideSize' => array( 332 'type' => array( 'string', 'null' ), 333 'title' => __( 'Wide Size', 'ipsum' ), 334 'description' => __( 'The wide-aligned content width, or null when unset.', 'ipsum' ), 335 ), 336 ), 337 'required' => array( 'contentSize', 'wideSize' ), 338 'additionalProperties' => false, 339 ), 340 'styleVariations' => $object_list( 341 __( 'Style Variations', 'ipsum' ), 342 __( 'The style variations available for this theme. Each item lists only what that variation changes: colorScheme and colors for the palette, bodyFont and headingFont for the fonts, and blocks for the style properties it sets on each block. Optional fields are omitted when the variation does not change them. Their full data is at /wp/v2/global-styles/themes/{stylesheet}/variations.', 'ipsum' ), 343 array( 344 'title' => array( 345 'type' => 'string', 346 'title' => __( 'Title', 'ipsum' ), 347 'description' => __( 'The style variation title.', 'ipsum' ), 348 ), 349 'colorScheme' => $scheme, 350 'colors' => array( 351 'type' => 'object', 352 'title' => __( 'Colors', 'ipsum' ), 353 'description' => __( 'Palette colors the variation sets, keyed by slug.', 'ipsum' ), 354 'additionalProperties' => $string, 355 ), 356 'bodyFont' => array_merge( 357 $nullable, 358 array( 'title' => __( 'Body Font', 'ipsum' ) ) 359 ), 360 'headingFont' => array_merge( 361 $nullable, 362 array( 'title' => __( 'Heading Font', 'ipsum' ) ) 363 ), 364 'blocks' => array( 365 'type' => 'object', 366 'title' => __( 'Blocks', 'ipsum' ), 367 'description' => __( 'Style property names the variation sets on each block, keyed by block name.', 'ipsum' ), 368 'additionalProperties' => $strings, 369 ), 370 ), 371 array( 'title' ) 372 ), 373 'blockStyles' => $object_list( 374 __( 'Block Styles', 'ipsum' ), 375 __( 'The block styles registered on this site, beyond the ones blocks define themselves. Add className to a block of one of the blockTypes to apply it.', 'ipsum' ), 376 array( 377 'name' => array( 378 'type' => 'string', 379 'title' => __( 'Name', 'ipsum' ), 380 'description' => __( 'The block style slug.', 'ipsum' ), 381 ), 382 'label' => array( 383 'type' => 'string', 384 'title' => __( 'Label', 'ipsum' ), 385 'description' => __( 'The human-readable block style label.', 'ipsum' ), 386 ), 387 'className' => array( 388 'type' => 'string', 389 'title' => __( 'Class Name', 'ipsum' ), 390 'description' => __( 'The CSS class to add to the block, such as is-style-outline.', 'ipsum' ), 391 ), 392 'blockTypes' => array_merge( 393 $strings, 394 array( 395 'title' => __( 'Block Types', 'ipsum' ), 396 'description' => __( 'The block names this style can be applied to.', 'ipsum' ), 397 ) 398 ), 399 ), 400 array( 'name', 'label', 'className', 'blockTypes' ) 401 ), 402 'patterns' => $object_list( 403 __( 'Patterns', 'ipsum' ), 404 __( 'The patterns registered by the active theme, without their markup, which is at /wp/v2/block-patterns/patterns. Includes both insertable patterns and hidden ones that fill templates and template parts. Optional fields (blockTypes, templateTypes, postTypes) are omitted when empty.', 'ipsum' ), 405 array( 406 'slug' => array( 407 'type' => 'string', 408 'title' => __( 'Slug', 'ipsum' ), 409 'description' => __( 'The pattern slug.', 'ipsum' ), 410 ), 411 'title' => array( 412 'type' => 'string', 413 'title' => __( 'Title', 'ipsum' ), 414 'description' => __( 'The pattern title.', 'ipsum' ), 415 ), 416 'description' => array( 417 'type' => 'string', 418 'title' => __( 'Description', 'ipsum' ), 419 'description' => __( 'The pattern description.', 'ipsum' ), 420 ), 421 'categories' => array_merge( 422 $strings, 423 array( 424 'title' => __( 'Categories', 'ipsum' ), 425 'description' => __( 'Pattern categories for the inserter.', 'ipsum' ), 426 ) 427 ), 428 'blockTypes' => array_merge( 429 $strings, 430 array( 431 'title' => __( 'Block Types', 'ipsum' ), 432 'description' => __( 'Block types this pattern is associated with, such as for the block transformer. Omitted when empty.', 'ipsum' ), 433 ) 434 ), 435 'templateTypes' => array_merge( 436 $strings, 437 array( 438 'title' => __( 'Template Types', 'ipsum' ), 439 'description' => __( 'Template types this pattern is meant to fill. Omitted when empty.', 'ipsum' ), 440 ) 441 ), 442 'postTypes' => array_merge( 443 $strings, 444 array( 445 'title' => __( 'Post Types', 'ipsum' ), 446 'description' => __( 'Limits which post types the pattern is available for. Omitted when empty.', 'ipsum' ), 447 ) 448 ), 449 'hidden' => array( 450 'type' => 'boolean', 451 'title' => __( 'Hidden', 'ipsum' ), 452 'description' => __( 'Whether the pattern is excluded from the inserter. Hidden does not mean private; these patterns typically fill templates and template parts.', 'ipsum' ), 453 ), 454 ), 455 array( 'slug', 'title', 'description', 'categories', 'hidden' ) 456 ), 457 'templates' => $object_list( 458 __( 'Templates', 'ipsum' ), 459 __( 'The block templates available on the site, including theme templates and any customized copies, with the pattern each delegates to when its markup is a single Pattern block.', 'ipsum' ), 460 array( 461 'slug' => array( 462 'type' => 'string', 463 'title' => __( 'Slug', 'ipsum' ), 464 'description' => __( 'The template slug.', 'ipsum' ), 465 ), 466 'title' => array( 467 'type' => 'string', 468 'title' => __( 'Title', 'ipsum' ), 469 'description' => __( 'The template title.', 'ipsum' ), 470 ), 471 'description' => array( 472 'type' => 'string', 473 'title' => __( 'Description', 'ipsum' ), 474 'description' => __( 'The template description.', 'ipsum' ), 475 ), 476 'pattern' => array( 477 'type' => array( 'string', 'null' ), 478 'title' => __( 'Pattern', 'ipsum' ), 479 'description' => __( 'The pattern slug when the template markup is a single Pattern block, otherwise null. A customized template can still reference a pattern.', 'ipsum' ), 480 ), 481 'customized' => array( 482 'type' => 'boolean', 483 'title' => __( 'Customized', 'ipsum' ), 484 'description' => __( 'Whether the template has a saved customization on this site.', 'ipsum' ), 485 ), 486 'postTypes' => array_merge( 487 $strings, 488 array( 489 'title' => __( 'Post Types', 'ipsum' ), 490 'description' => __( 'Post types the template applies to. Omitted when empty.', 'ipsum' ), 491 ) 492 ), 493 ), 494 array( 'slug', 'title', 'description', 'pattern', 'customized' ) 495 ), 496 'templateParts' => $object_list( 497 __( 'Template Parts', 'ipsum' ), 498 __( 'The template parts available on the site, including theme parts and any customized copies, with their area and the pattern each delegates to when its markup is a single Pattern block.', 'ipsum' ), 499 array( 500 'slug' => array( 501 'type' => 'string', 502 'title' => __( 'Slug', 'ipsum' ), 503 'description' => __( 'The template part slug.', 'ipsum' ), 504 ), 505 'title' => array( 506 'type' => 'string', 507 'title' => __( 'Title', 'ipsum' ), 508 'description' => __( 'The template part title.', 'ipsum' ), 509 ), 510 'area' => array( 511 'type' => array( 'string', 'null' ), 512 'title' => __( 'Area', 'ipsum' ), 513 'description' => __( 'The template part area, such as header, footer, or uncategorized, or null when the part has no area term.', 'ipsum' ), 514 ), 515 'pattern' => array( 516 'type' => array( 'string', 'null' ), 517 'title' => __( 'Pattern', 'ipsum' ), 518 'description' => __( 'The pattern slug when the template part markup is a single Pattern block, otherwise null. A customized template part can still reference a pattern.', 'ipsum' ), 519 ), 520 'customized' => array( 521 'type' => 'boolean', 522 'title' => __( 'Customized', 'ipsum' ), 523 'description' => __( 'Whether the template part has a saved customization on this site.', 'ipsum' ), 524 ), 525 ), 526 array( 'slug', 'title', 'area', 'pattern', 'customized' ) 527 ), 528 'templatePartAreas' => $object_list( 529 __( 'Template Part Areas', 'ipsum' ), 530 __( 'The areas a template part can belong to on this site.', 'ipsum' ), 531 array( 532 'area' => array( 533 'type' => 'string', 534 'title' => __( 'Area', 'ipsum' ), 535 'description' => __( 'The template part area slug.', 'ipsum' ), 536 ), 537 'label' => array( 538 'type' => 'string', 539 'title' => __( 'Label', 'ipsum' ), 540 'description' => __( 'The human-readable area label.', 'ipsum' ), 541 ), 542 'description' => array( 543 'type' => 'string', 544 'title' => __( 'Description', 'ipsum' ), 545 'description' => __( 'What the area is for.', 'ipsum' ), 546 ), 547 ), 548 array( 'area', 'label', 'description' ) 549 ), 550 'guidelines' => array( 551 'type' => 'array', 552 'title' => __( 'Guidelines', 'ipsum' ), 553 'description' => __( 'How the theme is meant to be built with.', 'ipsum' ), 554 'items' => $string, 555 ), 556 ), 557 ); 558 } 559 endif; 560 561 if ( ! function_exists( 'ipsum_get_design_guide_presets' ) ) : 562 /** 563 * Lists the theme's presets of one kind, followed by any the site adds. 564 * 565 * @since Ipsum 1.0 566 * 567 * @param mixed $presets The presets from the global settings, keyed by origin. 568 * Anything other than an array gives an empty list. 569 * @param string[] $keys The preset properties to keep. 570 * @return array<int, array<string, mixed>> The presets, with only the given properties. Empty when 571 * `$presets` isn't an array. 572 */ 573 function ipsum_get_design_guide_presets( $presets, $keys ) { 574 $items = array(); 575 if ( ! is_array( $presets ) ) { 576 return $items; 577 } 578 foreach ( array( 'theme', 'custom' ) as $origin ) { 579 if ( ! isset( $presets[ $origin ] ) || ! is_array( $presets[ $origin ] ) ) { 580 continue; 581 } 582 foreach ( $presets[ $origin ] as $preset ) { 583 if ( ! is_array( $preset ) ) { 584 continue; 585 } 586 $item = array(); 587 foreach ( $keys as $key ) { 588 if ( isset( $preset[ $key ] ) ) { 589 $item[ $key ] = $preset[ $key ]; 590 } 591 } 592 $items[] = $item; 593 } 594 } 595 return $items; 596 } 597 endif; 598 599 if ( ! function_exists( 'ipsum_get_design_guide_font_slug' ) ) : 600 /** 601 * Returns the slug of the font family preset a style refers to. 602 * 603 * @since Ipsum 1.0 604 * 605 * @param mixed $font_family A fontFamily style value. 606 * @return string|null The preset slug, the value itself when it is not a 607 * preset, or null when there is no value. 608 */ 609 function ipsum_get_design_guide_font_slug( $font_family ) { 610 if ( ! is_string( $font_family ) || '' === $font_family ) { 611 return null; 612 } 613 if ( preg_match( '/^var(?::preset\|font-family\||\(--wp--preset--font-family--)([a-z0-9-]+)\)?$/', $font_family, $matches ) ) { 614 return $matches[1]; 615 } 616 return $font_family; 617 } 618 endif; 619 620 if ( ! function_exists( 'ipsum_get_design_guide_pattern_slug' ) ) : 621 /** 622 * Returns the pattern a template or template part delegates to. 623 * 624 * @since Ipsum 1.0 625 * 626 * @param string $content The template or template part markup. 627 * @return string|null The pattern slug, or null when the markup is not a 628 * single Pattern block. 629 */ 630 function ipsum_get_design_guide_pattern_slug( $content ) { 631 $blocks = array_values( 632 array_filter( 633 parse_blocks( (string) $content ), 634 static function ( $block ) { 635 return null !== $block['blockName']; 636 } 637 ) 638 ); 639 if ( 1 === count( $blocks ) && 'core/pattern' === $blocks[0]['blockName'] && isset( $blocks[0]['attrs']['slug'] ) && is_string( $blocks[0]['attrs']['slug'] ) && '' !== $blocks[0]['attrs']['slug'] ) { 640 return $blocks[0]['attrs']['slug']; 641 } 642 return null; 643 } 644 endif; 645 646 if ( ! function_exists( 'ipsum_get_design_guide' ) ) : 647 /** 648 * Builds the design guide from the site as it is, so it follows the active 649 * style variation and any change made in Global Styles or the Site Editor. 650 * 651 * @since Ipsum 1.0 652 * 653 * @param array $input { 654 * The ability input. 655 * 656 * @type string[] $fields Optional. The sections to return. When omitted 657 * or empty, returns the basic design sections 658 * from ipsum_get_design_guide_default_fields(). 659 * } 660 * @return array<string, mixed> The design guide, keyed by section. 661 */ 662 function ipsum_get_design_guide( $input = array() ) { 663 $all_sections = array_keys( ipsum_get_design_guide_schema()['properties'] ); 664 if ( ! empty( $input['fields'] ) ) { 665 $sections = array_values( array_intersect( $all_sections, $input['fields'] ) ); 666 } else { 667 $sections = ipsum_get_design_guide_default_fields(); 668 } 669 $sections = array_flip( $sections ); 670 671 // Resolving the merged data also registers the block styles in styles/blocks. 672 $settings = wp_get_global_settings(); 673 $styles = wp_get_global_styles(); 674 $guide = array(); 675 676 if ( isset( $sections['theme'] ) ) { 677 $guide['theme'] = array( 678 'name' => wp_get_theme()->get( 'Name' ), 679 'stylesheet' => get_stylesheet(), 680 ); 681 } 682 683 if ( isset( $sections['colorScheme'] ) ) { 684 // The audio player's CSS also reads a palette that sets no scheme as light. 685 $guide['colorScheme'] = 'dark' === ( $settings['custom']['controlScheme'] ?? '' ) ? 'dark' : 'light'; 686 } 687 688 if ( isset( $sections['colors'] ) ) { 689 $roles = ipsum_get_color_roles(); 690 $guide['colors'] = array(); 691 foreach ( ipsum_get_design_guide_presets( $settings['color']['palette'] ?? array(), array( 'slug', 'name', 'color' ) ) as $color ) { 692 if ( isset( $color['slug'] ) && is_string( $color['slug'] ) && isset( $roles[ $color['slug'] ] ) ) { 693 $color['role'] = $roles[ $color['slug'] ]; 694 } 695 $guide['colors'][] = $color; 696 } 697 } 698 699 if ( isset( $sections['gradients'] ) ) { 700 $guide['gradients'] = ipsum_get_design_guide_presets( $settings['color']['gradients'] ?? array(), array( 'slug', 'name', 'gradient' ) ); 701 } 702 703 if ( isset( $sections['duotone'] ) ) { 704 $guide['duotone'] = ipsum_get_design_guide_presets( $settings['color']['duotone'] ?? array(), array( 'slug', 'name', 'colors' ) ); 705 } 706 707 if ( isset( $sections['typography'] ) ) { 708 $body_font = ipsum_get_design_guide_font_slug( $styles['typography']['fontFamily'] ?? '' ); 709 $block_fonts = array(); 710 $blocks = $styles['blocks'] ?? null; 711 foreach ( is_array( $blocks ) ? $blocks : array() as $block_type => $block_styles ) { 712 $block_font = ipsum_get_design_guide_font_slug( $block_styles['typography']['fontFamily'] ?? '' ); 713 if ( null !== $block_font && $block_font !== $body_font ) { 714 $block_fonts[ $block_font ][] = $block_type; 715 } 716 } 717 $guide['typography'] = array( 718 'bodyFont' => $body_font, 719 'headingFont' => ipsum_get_design_guide_font_slug( $styles['elements']['heading']['typography']['fontFamily'] ?? '' ) ?? $body_font, 720 'blockFonts' => $block_fonts, 721 'fontFamilies' => ipsum_get_design_guide_presets( $settings['typography']['fontFamilies'] ?? array(), array( 'slug', 'name', 'fontFamily' ) ), 722 'fontSizes' => ipsum_get_design_guide_presets( $settings['typography']['fontSizes'] ?? array(), array( 'slug', 'name', 'size' ) ), 723 ); 724 } 725 726 if ( isset( $sections['spacing'] ) ) { 727 $guide['spacing'] = ipsum_get_design_guide_presets( $settings['spacing']['spacingSizes'] ?? array(), array( 'slug', 'name', 'size' ) ); 728 } 729 730 if ( isset( $sections['layout'] ) ) { 731 $guide['layout'] = array( 732 'contentSize' => $settings['layout']['contentSize'] ?? null, 733 'wideSize' => $settings['layout']['wideSize'] ?? null, 734 ); 735 } 736 737 if ( isset( $sections['styleVariations'] ) ) { 738 $guide['styleVariations'] = array(); 739 foreach ( WP_Theme_JSON_Resolver::get_style_variations() as $variation ) { 740 $variation_settings = $variation['settings'] ?? array(); 741 $variation_styles = $variation['styles'] ?? array(); 742 $item = array( 'title' => $variation['title'] ); 743 744 if ( isset( $variation_settings['custom']['controlScheme'] ) ) { 745 $item['colorScheme'] = 'dark' === $variation_settings['custom']['controlScheme'] ? 'dark' : 'light'; 746 } 747 if ( ! empty( $variation_settings['color']['palette'] ) ) { 748 $item['colors'] = array_column( ipsum_get_design_guide_presets( $variation_settings['color']['palette'], array( 'slug', 'color' ) ), 'color', 'slug' ); 749 } 750 if ( ! empty( $variation_styles['typography']['fontFamily'] ) ) { 751 $item['bodyFont'] = ipsum_get_design_guide_font_slug( $variation_styles['typography']['fontFamily'] ); 752 } 753 if ( ! empty( $variation_styles['elements']['heading']['typography']['fontFamily'] ) ) { 754 $item['headingFont'] = ipsum_get_design_guide_font_slug( $variation_styles['elements']['heading']['typography']['fontFamily'] ); 755 } 756 if ( ! empty( $variation_styles['blocks'] ) && is_array( $variation_styles['blocks'] ) ) { 757 $item['blocks'] = array_map( 758 static function ( $block_styles ) { 759 return is_array( $block_styles ) ? array_keys( $block_styles ) : array(); 760 }, 761 $variation_styles['blocks'] 762 ); 763 } 764 $guide['styleVariations'][] = $item; 765 } 766 } 767 768 if ( isset( $sections['blockStyles'] ) ) { 769 $block_styles = array(); 770 foreach ( WP_Block_Styles_Registry::get_instance()->get_all_registered() as $block_type => $block_type_styles ) { 771 foreach ( $block_type_styles as $name => $style ) { 772 if ( ! isset( $block_styles[ $name ] ) ) { 773 $block_styles[ $name ] = array( 774 'name' => $name, 775 'label' => $style['label'] ?? $name, 776 'className' => 'is-style-' . $name, 777 'blockTypes' => array(), 778 ); 779 } 780 $block_styles[ $name ]['blockTypes'][] = $block_type; 781 } 782 } 783 $guide['blockStyles'] = array_values( $block_styles ); 784 } 785 786 if ( isset( $sections['patterns'] ) ) { 787 // Look up the theme's patterns by slug, since the registry drops a pattern's filePath once its content is loaded. 788 $theme = wp_get_theme(); 789 $theme_pattern_slugs = array(); 790 foreach ( array_filter( array( $theme, $theme->parent() ) ) as $pattern_theme ) { 791 foreach ( $pattern_theme->get_block_patterns() as $file_pattern ) { 792 if ( isset( $file_pattern['slug'] ) && is_string( $file_pattern['slug'] ) ) { 793 $theme_pattern_slugs[] = $file_pattern['slug']; 794 } 795 } 796 } 797 798 $registry = WP_Block_Patterns_Registry::get_instance(); 799 $guide['patterns'] = array(); 800 foreach ( array_unique( $theme_pattern_slugs ) as $slug ) { 801 $pattern = $registry->get_registered( $slug ); 802 if ( null === $pattern ) { 803 continue; 804 } 805 $item = array( 806 'slug' => $pattern['name'], 807 'title' => $pattern['title'], 808 'description' => $pattern['description'] ?? '', 809 'categories' => $pattern['categories'] ?? array(), 810 ); 811 foreach ( array( 'blockTypes', 'templateTypes', 'postTypes' ) as $key ) { 812 if ( ! empty( $pattern[ $key ] ) ) { 813 $item[ $key ] = $pattern[ $key ]; 814 } 815 } 816 $item['hidden'] = isset( $pattern['inserter'] ) && ! $pattern['inserter']; 817 $guide['patterns'][] = $item; 818 } 819 } 820 821 foreach ( array( 822 'templates' => 'wp_template', 823 'templateParts' => 'wp_template_part', 824 ) as $section => $template_type ) { 825 if ( ! isset( $sections[ $section ] ) ) { 826 continue; 827 } 828 $guide[ $section ] = array(); 829 foreach ( get_block_templates( array(), $template_type ) as $template ) { 830 $item = array( 831 'slug' => $template->slug, 832 'title' => $template->title, 833 ); 834 if ( 'wp_template' === $template_type ) { 835 $item['description'] = (string) $template->description; 836 } else { 837 $item['area'] = $template->area; 838 } 839 $item['pattern'] = ipsum_get_design_guide_pattern_slug( $template->content ); 840 $item['customized'] = 'custom' === $template->source; 841 if ( ! empty( $template->post_types ) ) { 842 $item['postTypes'] = $template->post_types; 843 } 844 $guide[ $section ][] = $item; 845 } 846 } 847 848 if ( isset( $sections['templatePartAreas'] ) ) { 849 $guide['templatePartAreas'] = array(); 850 foreach ( get_allowed_block_template_part_areas() as $area ) { 851 $guide['templatePartAreas'][] = array( 852 'area' => $area['area'], 853 'label' => $area['label'], 854 'description' => $area['description'], 855 ); 856 } 857 } 858 859 if ( isset( $sections['guidelines'] ) ) { 860 $guide['guidelines'] = array( 861 __( 'Style with the presets in this guide, by slug, rather than with literal values. Every style variation redefines the colors, so a literal color keeps the look of one variation under all the others.', 'ipsum' ), 862 __( 'Each template and template part holds a single Pattern block, and that pattern holds the markup. To change one on this site, save new markup to the template or template part itself.', 'ipsum' ), 863 __( 'Hidden patterns are there to fill templates and template parts, not to be inserted on their own.', 'ipsum' ), 864 ); 865 } 866 867 return $guide; 868 } 869 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 24 08:20:01 2026 | Cross-referenced by PHPXref |