Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -195,6 +195,10 @@ public function prepare_item_for_response( $item, $request ) {
$data['presentation'] = $widget_type->presentation;
}

if ( rest_is_field_included( 'category', $fields ) ) {
$data['category'] = $widget_type->category;
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down Expand Up @@ -245,6 +249,13 @@ public function get_item_schema() {
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),

'category' => array(
'description' => __( 'Grouping category for the widget type.', 'gutenberg' ),

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.

Somehow sounds clunky/technical. What does cattery description for blocks say? Not a blocker.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, Mikael. Nice call. Updated.

'type' => array( 'string', 'null' ),
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
);

Expand Down
10 changes: 10 additions & 0 deletions lib/experimental/dashboard-widgets/class-wp-widget-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class WP_Widget_Type {
*/
public $presentation = null;

/**
* Grouping category for the widget type. Static and declarative;
* not a user-editable attribute.
*
* Null when the widget did not declare the field.
*
* @var string|null
*/
public $category = null;

/**
* Constructor.
*
Expand Down
1 change: 1 addition & 0 deletions lib/experimental/dashboard-widgets/widget-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function gutenberg_register_widget_types() {
'render_module' => $widget['render_module'] ?? null,
'widget_module' => $widget['widget_module'] ?? null,
'presentation' => $widget['presentation'] ?? null,
'category' => $widget['category'] ?? null,
)
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/widget-primitives/src/hooks/use-widget-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export function useWidgetTypes(
...( record.presentation
? { presentation: record.presentation }
: {} ),
...( record.category
? { category: record.category }
: {} ),
} as WidgetType;
} catch {
return null;
Expand Down
11 changes: 9 additions & 2 deletions packages/widget-primitives/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export type WidgetIcon = ReactElement< ComponentProps< 'svg' > >;
* whose `id` is narrowed to the keys of the widget's attribute object (`Item`).
*/
export type WidgetAttributeField< Item > = Field< Item > & {
// `& string` drops the number/symbol keys `keyof` can yield; `Field.id`
// is a string.
/*
* `& string` drops the number/symbol keys `keyof` can yield; `Field.id`
* is a string.
*/
id: keyof Item & string;
};

Expand Down Expand Up @@ -203,4 +205,9 @@ export interface WidgetModuleRecord {
* Authoring presentation hint; overrides the metadata module's value.
*/
presentation?: WidgetTypeMetadata[ 'presentation' ] | null;

/**
* Grouping category; overrides the metadata module's value.
*/
category?: WidgetTypeMetadata[ 'category' ] | null;
}
7 changes: 6 additions & 1 deletion packages/wp-build/lib/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ async function buildAllWidgets() {
* Discover all widgets and collect their registry-facing data.
* Widgets without a valid widget.json are skipped.
*
* @return {Array<{ name: string, dirName: string, hasRender: boolean, hasWidget: boolean, presentation: string | null }>} Array of widget objects.
* @return {Array<{ name: string, dirName: string, hasRender: boolean, hasWidget: boolean, presentation: string | null, category: string | null }>} Array of widget objects.
*/
function collectWidgets() {
return getAllWidgets( ROOT_DIR ).flatMap( ( widgetName ) => {
Expand All @@ -2038,6 +2038,7 @@ function collectWidgets() {
hasRender: widgetFiles.hasRender,
hasWidget: widgetFiles.hasWidget,
presentation: metadata.presentation ?? null,
category: metadata.category ?? null,
},
];
} );
Expand Down Expand Up @@ -2066,12 +2067,16 @@ async function generateWidgetRegistry( widgets, replacements ) {
const presentationStr = widget.presentation
? `'${ widget.presentation }'`
: 'null';
const categoryStr = widget.category
? `'${ widget.category }'`
: 'null';
return `\tarray(
'name' => '${ widget.name }',
'dir_name' => '${ widget.dirName }',
'has_render' => ${ hasRenderStr },
'has_widget' => ${ hasWidgetStr },
'presentation' => ${ presentationStr },
'category' => ${ categoryStr },
)`;
} )
.join( ',\n' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private function register_sample_widgets() {
array(
'render_module' => 'wp/widgets/widget-a/render',
'widget_module' => 'wp/widgets/widget-a/widget',
'category' => 'dashboard',
)
);

Expand Down Expand Up @@ -124,6 +125,7 @@ public function test_get_item_returns_widget_by_name() {
$this->assertSame( 'test-plugin/widget-a', $data['name'] );
$this->assertSame( 'wp/widgets/widget-a/render', $data['render_module'] );
$this->assertSame( 'wp/widgets/widget-a/widget', $data['widget_module'] );
$this->assertSame( 'dashboard', $data['category'] );
}

public function test_get_item_returns_404_for_unknown_name() {
Expand Down Expand Up @@ -169,8 +171,10 @@ public function test_schema_exposes_expected_properties() {
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'render_module', $properties );
$this->assertArrayHasKey( 'widget_module', $properties );
$this->assertArrayHasKey( 'category', $properties );
$this->assertSame( 'string', $properties['name']['type'] );
$this->assertSame( array( 'string', 'null' ), $properties['render_module']['type'] );
$this->assertSame( array( 'string', 'null' ), $properties['widget_module']['type'] );
$this->assertSame( array( 'string', 'null' ), $properties['category']['type'] );
}
}
Loading