| @@ -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,9 +163,9 @@ | ||
| 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 | |
| @@ -156,9 +177,9 @@ | ||
| 156 | 177 | * |
| 157 | 178 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 158 | 179 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 159 | 180 | */ |
| 160 | -function _gutenberg_get_entity_view_config_post_type_page( $data ) { | |
| 181 | +function _gutenberg_get_entity_view_config_posttype_page( $data ) { | |
| 161 | 182 | $default_layouts = array( |
| 162 | 183 | 'table' => array( |
| 163 | 184 | 'layout' => array( |
| 164 | 185 | 'styles' => array( |
| @@ -294,9 +315,9 @@ | ||
| 294 | 315 | * |
| 295 | 316 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 296 | 317 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 297 | 318 | */ |
| 298 | -function _gutenberg_get_entity_view_config_post_type_wp_block( $data ) { | |
| 319 | +function _gutenberg_get_entity_view_config_posttype_wp_block( $data ) { | |
| 299 | 320 | $default_layouts = array( |
| 300 | 321 | 'table' => array( |
| 301 | 322 | 'layout' => array( |
| 302 | 323 | 'styles' => array( |
| @@ -410,9 +431,9 @@ | ||
| 410 | 431 | * |
| 411 | 432 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 412 | 433 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 413 | 434 | */ |
| 414 | -function _gutenberg_get_entity_view_config_post_type_wp_template_part( $data ) { | |
| 435 | +function _gutenberg_get_entity_view_config_posttype_wp_template_part( $data ) { | |
| 415 | 436 | $default_layouts = array( |
| 416 | 437 | 'table' => array( |
| 417 | 438 | 'layout' => array( |
| 418 | 439 | 'styles' => array( |
| @@ -510,9 +531,9 @@ | ||
| 510 | 531 | * |
| 511 | 532 | * @param Gutenberg_View_Config_Data $data The view configuration container for the entity. |
| 512 | 533 | * @return Gutenberg_View_Config_Data The updated view configuration container. |
| 513 | 534 | */ |
| 514 | -function _gutenberg_get_entity_view_config_post_type_wp_template( $data ) { | |
| 535 | +function _gutenberg_get_entity_view_config_posttype_wp_template( $data ) { | |
| 515 | 536 | $default_view = array( |
| 516 | 537 | 'type' => 'grid', |
| 517 | 538 | 'perPage' => 20, |
| 518 | 539 | 'sort' => array( |
| @@ -722,9 +743,9 @@ | ||
| 722 | 743 | /** |
| 723 | 744 | * Registers the Gutenberg entity view configuration filters, overriding any |
| 724 | 745 | * defaults that WordPress core may have already registered. |
| 725 | 746 | * |
| 726 | - * 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 | |
| 727 | 748 | * the shared `get_entity_view_config_{$kind}_{$name}` hooks. The Gutenberg |
| 728 | 749 | * plugin always ships the newest configuration, so it removes the core defaults |
| 729 | 750 | * and installs its own `_gutenberg_*` callbacks instead. |
| 730 | 751 | * |
| @@ -739,11 +760,11 @@ | ||
| 739 | 760 | function gutenberg_register_entity_view_config_filters() { |
| 740 | 761 | $post_types = array( 'page', 'post', 'wp_block', 'wp_template_part', 'wp_template' ); |
| 741 | 762 | |
| 742 | 763 | foreach ( $post_types as $post_type ) { |
| 743 | - $hook = "get_entity_view_config_postType_{$post_type}"; | |
| 744 | - $wp_callback = "_wp_get_entity_view_config_post_type_{$post_type}"; | |
| 745 | - $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}"; | |
| 746 | 767 | |
| 747 | 768 | // has_filter() returns the priority the callback was registered at. |
| 748 | 769 | $wp_priority = has_filter( $hook, $wp_callback ); |
| 749 | 770 | if ( false !== $wp_priority ) { |