Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,18 @@ public static function get_config_schema() {
'type' => 'object',
'additionalProperties' => false,
'properties' => array(
'public' => array( 'type' => 'boolean' ),
'hierarchical' => array( 'type' => 'boolean' ),
'has_archive' => array( 'type' => 'boolean' ),
'show_in_rest' => array( 'type' => 'boolean' ),
'public' => array( 'type' => 'boolean' ),
'hierarchical' => array( 'type' => 'boolean' ),
'has_archive' => array( 'type' => 'boolean' ),
'show_in_rest' => array( 'type' => 'boolean' ),
'menu_icon' => array(
'type' => array( 'string', 'null' ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need some special validation here. The register_post_type documentation recognizes the special string 'none' and dashicons-* slugs. I think a pattern (e.g. dashicons slug OR data: URI OR http(s): URL OR none) would catch typos at the REST layer, in addition to the max length limit that I'm also mentioning below.

),
'menu_position' => array(
'type' => array( 'integer', 'null' ),
'minimum' => 1,
'maximum' => 100,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure 100 should be the practical maximum here. Technically, folks can input PHP_INT_MAX if they want. I think we should remove the upper limit, or make it something way larger than this, since folks might want to specify a custom specific order beyond 100.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we shouldn't have an upper limit if it's not enforced anywhere in PHP. Additionally we should add the same validation rules for the JS field.

),
// Caps payload size; well above any reasonable description.
'description' => array(
'type' => 'string',
Expand Down
8 changes: 8 additions & 0 deletions lib/experimental/content-types/post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ function gutenberg_build_user_post_type_args( WP_Post $record ) {
$args['description'] = (string) $config['description'];
}

if ( ! empty( $config['menu_icon'] ) ) {
$args['menu_icon'] = (string) $config['menu_icon'];
}

if ( isset( $config['menu_position'] ) && is_int( $config['menu_position'] ) ) {
$args['menu_position'] = (int) $config['menu_position'];
}

// `taxonomies` here is the inverse of the taxonomy record's `object_type`:
// it lists the taxonomies attached to this post type. Only existing
// taxonomies are passed through so we never reference unregistered slugs.
Expand Down
4 changes: 4 additions & 0 deletions packages/content-types/src/post-types/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import {
itemsListNavigationField,
labelsActionsField,
labelsForm,
menuIconField,
menuNameField,
menuPositionField,
newItemField,
notFoundField,
notFoundInTrashField,
Expand Down Expand Up @@ -152,6 +154,8 @@ function PostTypePage( {
hierarchicalField,
hasArchiveField,
showInRestField,
menuIconField,
menuPositionField,
statusField,
// Labels
labelsActionsField,
Expand Down
42 changes: 42 additions & 0 deletions packages/content-types/src/post-types/fields/general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,46 @@ export const defaultForm: Form = {
],
};

export const menuIconField: Field< PostTypeFormData > = {
id: 'menu_icon',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm noticing we don't add a max length for the menu icon field. Considering users can put in long base64 data URI blobs, this is a problematic source of problems. One way around it is to discourage data URIs and just work with either dashicons or empty icons that they can style with CSS.

cc @ntsekouras who added some of the limits for the other fields - what limits would you put here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For start I believe we should only start with dashicons and the max length could be related to that decision. I haven't investigated the menu_icon, so I'm not sure about possible nuances with base64-encoded SVGs.

label: __( 'Menu icon' ),
type: 'text',
description: __(
'Dashicon slug (e.g. "dashicons-book") or a data: URI for a custom icon shown in the admin menu.'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same example of dashicon as in the placeholder?

),
placeholder: 'dashicons-admin-post',
getValue: ( { item } ) => item.config.menu_icon ?? '',
setValue: ( { item, value } ) => ( {
config: {
...item.config,
menu_icon: value ? String( value ) : null,
},
} ),
filterBy: false,
enableSorting: false,
};

export const menuPositionField: Field< PostTypeFormData > = {
id: 'menu_position',
label: __( 'Menu position' ),
type: 'integer',
description: __(
'Position in the admin menu. Common values: 5 (below Posts), 10 (below Media), 20 (below Pages). Leave empty to use the default position.'
),
getValue: ( { item } ) => item.config.menu_position ?? null,
setValue: ( { item, value } ) => ( {
config: {
...item.config,
menu_position:
value !== '' && value !== null && value !== undefined
? Number( value )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the min/max in the REST API reflected here. We should ensure we're consistent between the UI and the REST API

: null,
},
} ),
filterBy: false,
enableSorting: false,
};

export const generalForm: Form = {
layout: { type: 'regular' },
fields: [
Expand All @@ -262,6 +302,8 @@ export const generalForm: Form = {
'hierarchical',
'has_archive',
'show_in_rest',
'menu_icon',
'menu_position',
Comment on lines +305 to +306

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this form is starting to get a bit cluttered and we might want to split it to multiple groups just like Taxonomies, so General will have just a few basic fields.

Also, should these go right at the bottom, even bellow the status field?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to split it to multiple groups

Agreed. Probably a Visibility section or an Advanced could be fine to start with and we can reorganize later.

'status',
],
};
4 changes: 4 additions & 0 deletions packages/content-types/src/post-types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface StoredConfig {
hierarchical?: boolean;
has_archive?: boolean;
show_in_rest?: boolean;
menu_icon?: string | null;
menu_position?: number | null;
}

import type { ContentType } from '../types';
Expand All @@ -84,5 +86,7 @@ export interface PostTypeFormData extends ContentType {
taxonomies: string[];
supports: SupportFeature[];
has_archive: boolean;
menu_icon: string | null;
menu_position: number | null;
};
}
6 changes: 6 additions & 0 deletions packages/content-types/src/post-types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const BLANK_RECORD: PostTypeFormData = {
hierarchical: false,
has_archive: false,
show_in_rest: true,
menu_icon: null,
menu_position: null,
},
};

Expand Down Expand Up @@ -204,6 +206,8 @@ export function toFormData( row: PostTypeRecord ): PostTypeFormData {
hierarchical: config.hierarchical ?? false,
has_archive: config.has_archive ?? false,
show_in_rest: config.show_in_rest ?? true,
menu_icon: config.menu_icon ?? null,
menu_position: config.menu_position ?? null,
},
};
}
Expand Down Expand Up @@ -233,6 +237,8 @@ export function serializeForSave( data: PostTypeFormData ) {
has_archive: config.has_archive,
show_in_rest: config.show_in_rest,
...( description !== '' ? { description } : {} ),
menu_icon: config.menu_icon,
menu_position: config.menu_position,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,60 @@ public function test_create_item_rejects_unknown_supports_value() {
$this->assertSame( 400, $response->get_status() );
}

/**
* `config.menu_icon` and `config.menu_position` round-trip through the
* REST API and are persisted in the stored config JSON.
*/
public function test_create_item_with_menu_icon_and_position() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'POST', self::REST_BASE );
$request->set_body_params(
array(
'slug' => 'recipe',
'title' => 'Recipes',
'status' => 'publish',
'config' => array(
'labels' => array( 'singular_name' => 'Recipe' ),
'public' => true,
'menu_icon' => 'dashicons-food',
'menu_position' => 25,
),
)
);

$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 201, $response->get_status() );
$this->assertSame( 'dashicons-food', $data['config']['menu_icon'] );
$this->assertSame( 25, $data['config']['menu_position'] );

wp_delete_post( $data['id'], true );
}

/**
* `menu_position` out of the allowed range (1–100) is rejected with a 400.
*/
public function test_create_item_rejects_out_of_range_menu_position() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'POST', self::REST_BASE );
$request->set_body_params(
array(
'slug' => 'bad_pos',
'title' => 'Bad Position',
'config' => array(
'labels' => array( 'singular_name' => 'Bad' ),
'menu_position' => 999,
),
)
);

$response = rest_get_server()->dispatch( $request );
$this->assertSame( 400, $response->get_status() );
}

/**
* `config.taxonomies` is stored inline and round-trips cleanly. There is
* no separate top-level field and no collection-param filter on this
Expand Down
Loading