-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Backport: Block template utils and rest templates controller #3221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e690417
a416a0a
f1958f4
0ae99e1
09ec609
1bf24b4
adb4dca
d4a2bb8
c63eb7d
06c0082
942ef32
2bdd393
2dcfe25
d0c2010
33a4eba
4ef9146
5deea6b
5ba2daf
0d2d57c
4b32c15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,54 +186,58 @@ public function prepare_item_for_response( $item, $request ) { | |
| $fields = $this->get_fields_for_response( $request ); | ||
| $data = array(); | ||
|
|
||
| if ( in_array( 'capabilities', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'capabilities', $fields ) ) { | ||
| $data['capabilities'] = $post_type->cap; | ||
| } | ||
|
|
||
| if ( in_array( 'description', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'description', $fields ) ) { | ||
| $data['description'] = $post_type->description; | ||
| } | ||
|
|
||
| if ( in_array( 'hierarchical', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'hierarchical', $fields ) ) { | ||
| $data['hierarchical'] = $post_type->hierarchical; | ||
| } | ||
|
|
||
| if ( in_array( 'visibility', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'visibility', $fields ) ) { | ||
| $data['visibility'] = array( | ||
| 'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus, | ||
| 'show_ui' => (bool) $post_type->show_ui, | ||
| ); | ||
| } | ||
|
|
||
| if ( in_array( 'viewable', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'viewable', $fields ) ) { | ||
| $data['viewable'] = is_post_type_viewable( $post_type ); | ||
| } | ||
|
|
||
| if ( in_array( 'labels', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'labels', $fields ) ) { | ||
| $data['labels'] = $post_type->labels; | ||
| } | ||
|
|
||
| if ( in_array( 'name', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'name', $fields ) ) { | ||
| $data['name'] = $post_type->label; | ||
| } | ||
|
|
||
| if ( in_array( 'slug', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'slug', $fields ) ) { | ||
| $data['slug'] = $post_type->name; | ||
| } | ||
|
|
||
| if ( in_array( 'supports', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'icon', $fields ) ) { | ||
| $data['icon'] = $post_type->menu_icon; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this value is null, should it maybe default to something?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's okay to be null since it's not required and the consumers can decide what to do with it. For example in php core for post types we assign some default icon if it's in the |
||
| } | ||
|
|
||
| if ( rest_is_field_included( 'supports', $fields ) ) { | ||
| $data['supports'] = $supports; | ||
| } | ||
|
|
||
| if ( in_array( 'taxonomies', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'taxonomies', $fields ) ) { | ||
| $data['taxonomies'] = array_values( $taxonomies ); | ||
| } | ||
|
|
||
| if ( in_array( 'rest_base', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'rest_base', $fields ) ) { | ||
| $data['rest_base'] = $base; | ||
| } | ||
|
|
||
| if ( in_array( 'rest_namespace', $fields, true ) ) { | ||
| if ( rest_is_field_included( 'rest_namespace', $fields ) ) { | ||
| $data['rest_namespace'] = $namespace; | ||
| } | ||
|
|
||
|
|
@@ -287,6 +291,7 @@ protected function prepare_links( $post_type ) { | |
| * @since 4.7.0 | ||
| * @since 4.8.0 The `supports` property was added. | ||
| * @since 5.9.0 The `visibility` and `rest_namespace` properties were added. | ||
| * @since 6.1.0 The `icon` property was added. | ||
| * | ||
| * @return array Item schema data. | ||
| */ | ||
|
|
@@ -385,6 +390,12 @@ public function get_item_schema() { | |
| ), | ||
| ), | ||
| ), | ||
| 'icon' => array( | ||
| 'description' => __( 'The icon for the post type.' ), | ||
| 'type' => array( 'string', 'null' ), | ||
| 'context' => array( 'view', 'edit', 'embed' ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be in the view context? The icon is not public right now?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean public? I think it would be good to have in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you make a property as view it means it can't be viewed pubilically. Do we want to expose this data publically?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no harm in it, but if you have a strong opinion on this, I can remove it. |
||
| 'readonly' => true, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @group block-templates | ||
| * @covers ::get_template_hierarchy | ||
| */ | ||
| abstract class WP_Block_Templates_UnitTestCase extends WP_UnitTestCase { | ||
| const TEST_THEME = 'block-theme'; | ||
|
|
||
| protected static $template_post; | ||
| protected static $template_part_post; | ||
|
|
||
| public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
| /* | ||
| * Set up a template post corresponding to a different theme. | ||
| * Do this to ensure resolution and slug creation works as expected, | ||
| * even with another post of that same name present for another theme. | ||
| */ | ||
| self::$template_post = $factory->post->create_and_get( | ||
| array( | ||
| 'post_type' => 'wp_template', | ||
| 'post_name' => 'my_template', | ||
| 'post_title' => 'My Template', | ||
| 'post_content' => 'Content', | ||
| 'post_excerpt' => 'Description of my template', | ||
| 'tax_input' => array( | ||
| 'wp_theme' => array( | ||
| 'this-theme-should-not-resolve', | ||
| ), | ||
| ), | ||
| ) | ||
| ); | ||
|
|
||
| wp_set_post_terms( self::$template_post->ID, 'this-theme-should-not-resolve', 'wp_theme' ); | ||
|
|
||
| // Set up template post. | ||
| self::$template_post = $factory->post->create_and_get( | ||
| array( | ||
| 'post_type' => 'wp_template', | ||
| 'post_name' => 'my_template', | ||
| 'post_title' => 'My Template', | ||
| 'post_content' => 'Content', | ||
| 'post_excerpt' => 'Description of my template', | ||
| 'tax_input' => array( | ||
| 'wp_theme' => array( | ||
| self::TEST_THEME, | ||
| ), | ||
| ), | ||
| ) | ||
| ); | ||
|
|
||
| wp_set_post_terms( self::$template_post->ID, self::TEST_THEME, 'wp_theme' ); | ||
|
|
||
| // Set up template part post. | ||
| self::$template_part_post = $factory->post->create_and_get( | ||
| array( | ||
| 'post_type' => 'wp_template_part', | ||
| 'post_name' => 'my_template_part', | ||
| 'post_title' => 'My Template Part', | ||
| 'post_content' => 'Content', | ||
| 'post_excerpt' => 'Description of my template part', | ||
| 'tax_input' => array( | ||
| 'wp_theme' => array( | ||
| self::TEST_THEME, | ||
| ), | ||
| 'wp_template_part_area' => array( | ||
| WP_TEMPLATE_PART_AREA_HEADER, | ||
| ), | ||
| ), | ||
| ) | ||
| ); | ||
|
|
||
| wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); | ||
| wp_set_post_terms( self::$template_part_post->ID, self::TEST_THEME, 'wp_theme' ); | ||
| } | ||
|
|
||
| public static function wpTearDownAfterClass() { | ||
| wp_delete_post( self::$template_post->ID ); | ||
| wp_delete_post( self::$template_part_post->ID ); | ||
| } | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| switch_theme( self::TEST_THEME ); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.