| @@ -3,14 +3,33 @@ | ||
| 3 | 3 | * Entity view configuration API. |
| 4 | 4 | * |
| 5 | 5 | * Builds the default view configuration for an entity and exposes it through |
| 6 | 6 | * the dynamic `get_entity_view_config_{$kind}_{$name}` filter so core and third |
| 7 | - * parties can provide the configuration for a specific entity. | |
| 7 | + * parties can provide the configuration for a specific entity. The dynamic | |
| 8 | + * portions of the hook name are lowercased, e.g. | |
| 9 | + * `get_entity_view_config_posttype_page` for the `page` post type. | |
| 8 | 10 | * |
| 9 | 11 | * @package gutenberg |
| 10 | 12 | */ |
| 11 | 13 | |
| 12 | 14 | /** |
| 15 | + * Builds the name of the dynamic filter that provides the view configuration | |
| 16 | + * for an entity. | |
| 17 | + * | |
| 18 | + * The entity kind and name are embedded in the hook name lowercased, so the | |
| 19 | + * hook follows the WordPress convention of lowercase hook names regardless of | |
| 20 | + * how the entity identifiers are spelled: the `postType`/`page` entity maps to | |
| 21 | + * the `get_entity_view_config_posttype_page` hook. | |
| 22 | + * | |
| 23 | + * @param string $kind The entity kind (e.g. `postType`). | |
| 24 | + * @param string $name The entity name (e.g. `page`). | |
| 25 | + * @return string The filter name. | |
| 26 | + */ | |
| 27 | +function gutenberg_get_entity_view_config_hook_name( $kind, $name ) { | |
| 28 | + return strtolower( "get_entity_view_config_{$kind}_{$name}" ); | |
| 29 | +} | |
| 30 | + | |
| 31 | +/** | |
| 13 | 32 | * Builds the default `form` configuration for post types that don't provide their own. |
| 14 | 33 | * |
| 15 | 34 | * It is a sensible default for `post`, `page`, and custom post types alike rather |
| 16 | 35 | * than being tailored per type. Post types that need a different shape can replace |
| @@ -23,9 +42,9 @@ | ||
| 23 | 42 | * returns `false`. |
| 24 | 43 | * |
| 25 | 44 | * @return array The default form configuration. |
| 26 | 45 | */ |
| 27 | -function _gutenberg_get_default_post_type_form() { | |
| 46 | +function _gutenberg_get_default_posttype_form() { | |
| 28 | 47 | return array( |
| 29 | 48 | 'layout' => array( 'type' => 'panel' ), |
| 30 | 49 | 'fields' => array( |
| 31 | 50 | array( |
| @@ -93,10 +112,12 @@ | ||
| 93 | 112 | /** |
| 94 | 113 | * Returns the view configuration for the given entity. |
| 95 | 114 | * |
| 96 | 115 | * Builds the default configuration shared by all entities and then exposes it |
| 97 | - * through the dynamic `get_entity_view_config_{$kind}_{$name}` filter so that core | |
| 98 | - * and third parties can provide the configuration for a specific entity. | |
| 116 | + * through the dynamic `get_entity_view_config_{$kind}_{$name}` filter — with the | |
| 117 | + * dynamic portions lowercased, see gutenberg_get_entity_view_config_hook_name() | |
| 118 | + * — so that core and third parties can provide the configuration for a | |
| 119 | + * specific entity. | |
| 99 | 120 | * |
| 100 | 121 | * @param string $kind The entity kind (e.g. `postType`). |
| 101 | 122 | * @param string $name The entity name (e.g. `page`). |
| 102 | 123 | * @return array { |
| @@ -142,63 +163,14 @@ | ||
| 142 | 163 | $config = array( |
| 143 | 164 | 'default_view' => $default_view, |
| 144 | 165 | 'default_layouts' => $default_layouts, |
| 145 | 166 | 'view_list' => $view_list, |
| 146 | - 'form' => 'postType' === $kind ? _gutenberg_get_default_post_type_form() : array(), | |
| 167 | + 'form' => 'postType' === $kind ? _gutenberg_get_default_posttype_form() : array(), | |
| 147 | 168 | ); |
| 148 | 169 | |
| 149 | 170 | $data = new Gutenberg_View_Config_Data( $config ); |
| 150 | 171 | |
| 151 | - /** | |
| 152 | - * Filters the view configuration for a given entity. | |
| 153 | - * | |
| 154 | - * The dynamic portions of the hook name, `$kind` and `$name`, refer to the | |
| 155 | - * entity kind (e.g. `postType`) and the entity name (e.g. `page`). | |
| 156 | - * | |
| 157 | - * Callbacks receive a Gutenberg_View_Config_Data object and change the | |
| 158 | - * configuration through its methods: the `update_*()` methods merge | |
| 159 | - * partial changes into the current configuration, while `set()` replaces | |
| 160 | - * a whole top-level key. Callbacks must return the object they were | |
| 161 | - * given. | |
| 162 | - * | |
| 163 | - * @param Gutenberg_View_Config_Data $data The view configuration container | |
| 164 | - * for the entity, exposing the | |
| 165 | - * `default_view`, `default_layouts`, | |
| 166 | - * `view_list`, and `form` keys. | |
| 167 | - * @param array $entity { | |
| 168 | - * The entity the configuration is built for. | |
| 169 | - * | |
| 170 | - * @type string $kind The entity kind. | |
| 171 | - * @type string $name The entity name. | |
| 172 | - * } | |
| 173 | - */ | |
| 174 | - $filtered = apply_filters( | |
| 175 | - "get_entity_view_config_{$kind}_{$name}", | |
| 176 | - $data, | |
| 177 | - array( | |
| 178 | - 'kind' => $kind, | |
| 179 | - 'name' => $name, | |
| 180 | - ) | |
| 181 | - ); | |
| 182 | - | |
| 183 | - // A well-behaved callback returns the object it was given. Fall back to the | |
| 184 | - // unfiltered config if a callback replaced it with something else. | |
| 185 | - if ( ! $filtered instanceof Gutenberg_View_Config_Data ) { | |
| 186 | - _doing_it_wrong( | |
| 187 | - __FUNCTION__, | |
| 188 | - sprintf( | |
| 189 | - /* translators: %s: the filter hook name. */ | |
| 190 | - esc_html__( 'A "%s" filter callback must return the Gutenberg_View_Config_Data object it was given.', 'gutenberg' ), | |
| 191 | - esc_html( "get_entity_view_config_{$kind}_{$name}" ) | |
| 192 | - ), | |
| 193 | - '7.1.0' | |
| 194 | - ); | |
| 195 | - return $config; | |
| 196 | - } | |
| 197 | - | |
| 198 | - // Backfill any dropped keys with their defaults, then discard any keys the | |
| 199 | - // filter introduced that are not part of the documented configuration shape. | |
| 200 | - return array_intersect_key( array_merge( $config, $filtered->get_config() ), $config ); | |
| 172 | + return $data->apply_filters( $kind, $name ); | |
| 201 | 173 | } |
| 202 | 174 | |
| 203 | 175 | /** |
| 204 | 176 | * Provides the view configuration for the `page` post type. |
| @@ -205,9 +177,9 @@ | ||
| 205 | 177 | * |
| 206 | 178 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 207 | 179 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 208 | 180 | */ |
| 209 | -function _gutenberg_get_entity_view_config_post_type_page( $data ) { | |
| 181 | +function _gutenberg_get_entity_view_config_posttype_page( $data ) { | |
| 210 | 182 | $default_layouts = array( |
| 211 | 183 | 'table' => array( |
| 212 | 184 | 'layout' => array( |
| 213 | 185 | 'styles' => array( |
| @@ -323,13 +295,18 @@ | ||
| 323 | 295 | ), |
| 324 | 296 | ), |
| 325 | 297 | ); |
| 326 | 298 | |
| 327 | - $data->set( 'default_layouts', $default_layouts, 1 ); | |
| 328 | - $data->set( 'default_view', $default_view, 1 ); | |
| 299 | + $data->set( | |
| 300 | + array( | |
| 301 | + 'default_view' => $default_view, | |
| 302 | + 'default_layouts' => $default_layouts, | |
| 303 | + ), | |
| 304 | + 1 | |
| 305 | + ); | |
| 329 | 306 | // Append the status views, thereby preserving the base "all items" view, |
| 330 | 307 | // so its post-type-specific title is kept. |
| 331 | - $data->update_view_list_items( array_column( $view_list, null, 'slug' ), 1 ); | |
| 308 | + $data->merge( array( 'view_list' => $view_list ), 1 ); | |
| 332 | 309 | |
| 333 | 310 | return $data; |
| 334 | 311 | } |
| 335 | 312 | |
| @@ -338,9 +315,9 @@ | ||
| 338 | 315 | * |
| 339 | 316 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 340 | 317 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 341 | 318 | */ |
| 342 | -function _gutenberg_get_entity_view_config_post_type_wp_block( $data ) { | |
| 319 | +function _gutenberg_get_entity_view_config_posttype_wp_block( $data ) { | |
| 343 | 320 | $default_layouts = array( |
| 344 | 321 | 'table' => array( |
| 345 | 322 | 'layout' => array( |
| 346 | 323 | 'styles' => array( |
| @@ -366,11 +343,8 @@ | ||
| 366 | 343 | 'filters' => array(), |
| 367 | 344 | 'layout' => $default_layouts['grid']['layout'], |
| 368 | 345 | ); |
| 369 | 346 | |
| 370 | - $data->set( 'default_layouts', $default_layouts, 1 ); | |
| 371 | - $data->set( 'default_view', $default_view, 1 ); | |
| 372 | - | |
| 373 | 347 | $view_list = array( |
| 374 | 348 | array( |
| 375 | 349 | 'title' => __( 'All patterns', 'gutenberg' ), |
| 376 | 350 | 'slug' => 'all-patterns', |
| @@ -416,32 +390,36 @@ | ||
| 416 | 390 | 'slug' => $category_name, |
| 417 | 391 | ); |
| 418 | 392 | } |
| 419 | 393 | |
| 420 | - $data->set( 'view_list', $view_list, 1 ); | |
| 394 | + $form = array( | |
| 395 | + 'layout' => array( 'type' => 'panel' ), | |
| 396 | + 'fields' => array( | |
| 397 | + array( | |
| 398 | + 'id' => 'excerpt', | |
| 399 | + 'layout' => array( | |
| 400 | + 'type' => 'panel', | |
| 401 | + 'labelPosition' => 'top', | |
| 402 | + ), | |
| 403 | + ), | |
| 404 | + array( | |
| 405 | + 'id' => 'post-content-info', | |
| 406 | + 'layout' => array( | |
| 407 | + 'type' => 'regular', | |
| 408 | + 'labelPosition' => 'none', | |
| 409 | + ), | |
| 410 | + ), | |
| 411 | + 'sync-status', | |
| 412 | + 'revisions', | |
| 413 | + ), | |
| 414 | + ); | |
| 421 | 415 | |
| 422 | 416 | $data->set( |
| 423 | - 'form', | |
| 424 | 417 | array( |
| 425 | - 'layout' => array( 'type' => 'panel' ), | |
| 426 | - 'fields' => array( | |
| 427 | - array( | |
| 428 | - 'id' => 'excerpt', | |
| 429 | - 'layout' => array( | |
| 430 | - 'type' => 'panel', | |
| 431 | - 'labelPosition' => 'top', | |
| 432 | - ), | |
| 433 | - ), | |
| 434 | - array( | |
| 435 | - 'id' => 'post-content-info', | |
| 436 | - 'layout' => array( | |
| 437 | - 'type' => 'regular', | |
| 438 | - 'labelPosition' => 'none', | |
| 439 | - ), | |
| 440 | - ), | |
| 441 | - 'sync-status', | |
| 442 | - 'revisions', | |
| 443 | - ), | |
| 418 | + 'default_view' => $default_view, | |
| 419 | + 'default_layouts' => $default_layouts, | |
| 420 | + 'view_list' => $view_list, | |
| 421 | + 'form' => $form, | |
| 444 | 422 | ), |
| 445 | 423 | 1 |
| 446 | 424 | ); |
| 447 | 425 | |
| @@ -453,9 +431,9 @@ | ||
| 453 | 431 | * |
| 454 | 432 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 455 | 433 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 456 | 434 | */ |
| 457 | -function _gutenberg_get_entity_view_config_post_type_wp_template_part( $data ) { | |
| 435 | +function _gutenberg_get_entity_view_config_posttype_wp_template_part( $data ) { | |
| 458 | 436 | $default_layouts = array( |
| 459 | 437 | 'table' => array( |
| 460 | 438 | 'layout' => array( |
| 461 | 439 | 'styles' => array( |
| @@ -479,11 +457,8 @@ | ||
| 479 | 457 | 'filters' => array(), |
| 480 | 458 | 'layout' => $default_layouts['grid']['layout'], |
| 481 | 459 | ); |
| 482 | 460 | |
| 483 | - $data->set( 'default_layouts', $default_layouts, 1 ); | |
| 484 | - $data->set( 'default_view', $default_view, 1 ); | |
| 485 | - | |
| 486 | 461 | $view_list = array( |
| 487 | 462 | array( |
| 488 | 463 | 'title' => __( 'All template parts', 'gutenberg' ), |
| 489 | 464 | 'slug' => 'all-parts', |
| @@ -523,24 +498,28 @@ | ||
| 523 | 498 | ), |
| 524 | 499 | ); |
| 525 | 500 | } |
| 526 | 501 | |
| 527 | - $data->set( 'view_list', $view_list, 1 ); | |
| 502 | + $form = array( | |
| 503 | + 'layout' => array( 'type' => 'panel' ), | |
| 504 | + 'fields' => array( | |
| 505 | + array( | |
| 506 | + 'id' => 'last_edited_date', | |
| 507 | + 'layout' => array( | |
| 508 | + 'type' => 'panel', | |
| 509 | + 'labelPosition' => 'none', | |
| 510 | + ), | |
| 511 | + ), | |
| 512 | + 'revisions', | |
| 513 | + ), | |
| 514 | + ); | |
| 528 | 515 | |
| 529 | 516 | $data->set( |
| 530 | - 'form', | |
| 531 | 517 | array( |
| 532 | - 'layout' => array( 'type' => 'panel' ), | |
| 533 | - 'fields' => array( | |
| 534 | - array( | |
| 535 | - 'id' => 'last_edited_date', | |
| 536 | - 'layout' => array( | |
| 537 | - 'type' => 'panel', | |
| 538 | - 'labelPosition' => 'none', | |
| 539 | - ), | |
| 540 | - ), | |
| 541 | - 'revisions', | |
| 542 | - ), | |
| 518 | + 'default_view' => $default_view, | |
| 519 | + 'default_layouts' => $default_layouts, | |
| 520 | + 'view_list' => $view_list, | |
| 521 | + 'form' => $form, | |
| 543 | 522 | ), |
| 544 | 523 | 1 |
| 545 | 524 | ); |
| 546 | 525 | |
| @@ -552,9 +531,9 @@ | ||
| 552 | 531 | * |
| 553 | 532 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 554 | 533 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 555 | 534 | */ |
| 556 | -function _gutenberg_get_entity_view_config_post_type_wp_template( $data ) { | |
| 535 | +function _gutenberg_get_entity_view_config_posttype_wp_template( $data ) { | |
| 557 | 536 | $default_view = array( |
| 558 | 537 | 'type' => 'grid', |
| 559 | 538 | 'perPage' => 20, |
| 560 | 539 | 'sort' => array( |
| @@ -574,11 +553,8 @@ | ||
| 574 | 553 | 'grid' => array( 'showMedia' => true ), |
| 575 | 554 | 'list' => array( 'showMedia' => false ), |
| 576 | 555 | ); |
| 577 | 556 | |
| 578 | - $data->set( 'default_view', $default_view, 1 ); | |
| 579 | - $data->set( 'default_layouts', $default_layouts, 1 ); | |
| 580 | - | |
| 581 | 557 | $view_list = array( |
| 582 | 558 | array( |
| 583 | 559 | 'title' => __( 'All templates', 'gutenberg' ), |
| 584 | 560 | 'slug' => 'all', |
| @@ -715,46 +691,50 @@ | ||
| 715 | 691 | } |
| 716 | 692 | } |
| 717 | 693 | } |
| 718 | 694 | |
| 719 | - $data->set( 'view_list', array_merge( $view_list, $registered_authors, $user_authors ), 1 ); | |
| 720 | - | |
| 721 | - $data->set( | |
| 722 | - 'form', | |
| 723 | - array( | |
| 724 | - 'layout' => array( 'type' => 'panel' ), | |
| 725 | - 'fields' => array( | |
| 726 | - array( | |
| 727 | - 'id' => 'description', | |
| 728 | - 'layout' => array( | |
| 729 | - 'type' => 'panel', | |
| 730 | - 'labelPosition' => 'top', | |
| 731 | - ), | |
| 695 | + $form = array( | |
| 696 | + 'layout' => array( 'type' => 'panel' ), | |
| 697 | + 'fields' => array( | |
| 698 | + array( | |
| 699 | + 'id' => 'description', | |
| 700 | + 'layout' => array( | |
| 701 | + 'type' => 'panel', | |
| 702 | + 'labelPosition' => 'top', | |
| 732 | 703 | ), |
| 733 | - array( | |
| 734 | - 'id' => 'description_readonly', | |
| 735 | - 'layout' => array( | |
| 736 | - 'type' => 'regular', | |
| 737 | - 'labelPosition' => 'none', | |
| 738 | - ), | |
| 704 | + ), | |
| 705 | + array( | |
| 706 | + 'id' => 'description_readonly', | |
| 707 | + 'layout' => array( | |
| 708 | + 'type' => 'regular', | |
| 709 | + 'labelPosition' => 'none', | |
| 739 | 710 | ), |
| 740 | - array( | |
| 741 | - 'id' => 'last_edited_date', | |
| 742 | - 'layout' => array( | |
| 743 | - 'type' => 'panel', | |
| 744 | - 'labelPosition' => 'none', | |
| 745 | - ), | |
| 711 | + ), | |
| 712 | + array( | |
| 713 | + 'id' => 'last_edited_date', | |
| 714 | + 'layout' => array( | |
| 715 | + 'type' => 'panel', | |
| 716 | + 'labelPosition' => 'none', | |
| 746 | 717 | ), |
| 747 | - 'revisions', | |
| 748 | - // The following fields are only meaningful in the `home`/`index` | |
| 749 | - // template summary. They edit other entities (`root/site` and the | |
| 750 | - // posts page); the editor merges those records into the form data | |
| 751 | - // under a namespace and controls when the fields are shown. | |
| 752 | - 'posts_page_title', | |
| 753 | - 'posts_per_page', | |
| 754 | - 'default_comment_status', | |
| 755 | 718 | ), |
| 719 | + 'revisions', | |
| 720 | + // The following fields are only meaningful in the `home`/`index` | |
| 721 | + // template summary. They edit other entities (`root/site` and the | |
| 722 | + // posts page); the editor merges those records into the form data | |
| 723 | + // under a namespace and controls when the fields are shown. | |
| 724 | + 'posts_page_title', | |
| 725 | + 'posts_per_page', | |
| 726 | + 'default_comment_status', | |
| 756 | 727 | ), |
| 728 | + ); | |
| 729 | + | |
| 730 | + $data->set( | |
| 731 | + array( | |
| 732 | + 'default_view' => $default_view, | |
| 733 | + 'default_layouts' => $default_layouts, | |
| 734 | + 'view_list' => array_merge( $view_list, $registered_authors, $user_authors ), | |
| 735 | + 'form' => $form, | |
| 736 | + ), | |
| 757 | 737 | 1 |
| 758 | 738 | ); |
| 759 | 739 | |
| 760 | 740 | return $data; |
| @@ -763,9 +743,9 @@ | ||
| 763 | 743 | /** |
| 764 | 744 | * Registers the Gutenberg entity view configuration filters, overriding any |
| 765 | 745 | * defaults that WordPress core may have already registered. |
| 766 | 746 | * |
| 767 | - * Core registers its own `_wp_get_entity_view_config_post_type_*` callbacks on | |
| 747 | + * Core registers its own `_wp_get_entity_view_config_posttype_*` callbacks on | |
| 768 | 748 | * the shared `get_entity_view_config_{$kind}_{$name}` hooks. The Gutenberg |
| 769 | 749 | * plugin always ships the newest configuration, so it removes the core defaults |
| 770 | 750 | * and installs its own `_gutenberg_*` callbacks instead. |
| 771 | 751 | * |
| @@ -780,11 +760,11 @@ | ||
| 780 | 760 | function gutenberg_register_entity_view_config_filters() { |
| 781 | 761 | $post_types = array( 'page', 'post', 'wp_block', 'wp_template_part', 'wp_template' ); |
| 782 | 762 | |
| 783 | 763 | foreach ( $post_types as $post_type ) { |
| 784 | - $hook = "get_entity_view_config_postType_{$post_type}"; | |
| 785 | - $wp_callback = "_wp_get_entity_view_config_post_type_{$post_type}"; | |
| 786 | - $gb_callback = "_gutenberg_get_entity_view_config_post_type_{$post_type}"; | |
| 764 | + $hook = gutenberg_get_entity_view_config_hook_name( 'postType', $post_type ); | |
| 765 | + $wp_callback = "_wp_get_entity_view_config_posttype_{$post_type}"; | |
| 766 | + $gb_callback = "_gutenberg_get_entity_view_config_posttype_{$post_type}"; | |
| 787 | 767 | |
| 788 | 768 | // has_filter() returns the priority the callback was registered at. |
| 789 | 769 | $wp_priority = has_filter( $hook, $wp_callback ); |
| 790 | 770 | if ( false !== $wp_priority ) { |