Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Get list of pattern categories via pattern category registry (Patterns view) #96

Merged
merged 11 commits into from
Mar 8, 2023

Conversation

mike-day
Copy link
Contributor

@mike-day mike-day commented Mar 8, 2023

Currently, the list of categories for the app module is parsed from pattern files. Since categories in the pattern header are saved by their name, parsing from the pattern file can be problematic if the category was registered with a name that differs from its label.

This PR hydrates the app with a list of registered categories, populated via call to the pattern category registry.

Fixes GF-3706.


For example, here is the current way, using Frost as reference:

Screenshot 2023-03-08 at 10 06 37 AM

The displayed values are all parsed directly from the name stored in pattern files, with dashes removed.


Here is the same categories list with label values mapped against the categories registry:

Screenshot 2023-03-08 at 10 06 22 AM

The displayed values are the actual registered label values registered by Frost.


How to test

  1. Checkout the branch
  2. With Frost active, navigate to the Patterns view
  3. The categories list should be populated with registered label values

Notes

Hydrating the app with the list of patternCategories will also be required for bulk assignment of categories to patterns, so having this data available now will be a useful head start for that work.

Comment on lines 24 to 31
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'apiEndpoints' => array(
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(),
'patternCategories' => \PatternManager\ApiData\get_registered_pattern_categories(),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'apiEndpoints' => array(
'deletePatternEndpoint' => get_rest_url( false, 'pattern-manager/v1/delete-pattern/' ),
),
'siteUrl' => get_bloginfo( 'url' ),
'adminUrl' => admin_url(),
'siteUrl' => get_bloginfo( 'url' ),
'adminUrl' => admin_url(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This diff looks more extensive than it is due to linting.

Comment on lines 12 to 36
export type InitialPatternManager = {
adminUrl: string;
apiEndpoints: {
deletePatternEndpoint: string;
};
apiNonce: string;
siteUrl: string;
adminUrl: string;
patternCategories: QueriedCategories;
patterns: Patterns;
siteUrl: string;
};

export type Pattern = {
content: string;
editorLink: string;
name: string;
title: string;
slug: string;
description?: string;
title: string;
blockTypes?: string[];
categories?: string[];
description?: string;
inserter?: boolean;
keywords?: string[];
blockTypes?: string[];
postTypes?: string[];
inserter?: boolean;
viewportWidth?: number;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This diff also looks more extensive than it is since I alphabetized the list of properties.

Comment on lines -62 to +72
label: 'Some category',
label: 'Some Category',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, any word succeeding the first would have been lowercase. Now they are capitalized since the label is pulled from the array of queriedCategories.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great idea to get the labels from the back end.

@@ -20,5 +20,6 @@ export default function usePatterns( initialPatterns: Patterns ) {
return {
data: patternsData,
deletePattern,
patternCategories: patternManager.patternCategories,
Copy link
Contributor Author

@mike-day mike-day Mar 8, 2023

Choose a reason for hiding this comment

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

Initially, I was getting the list of patternCategories from a fetch request within usePatterns, but I think it's better to just hydrate the app with the needed data instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely, it's simpler this way.

Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Hi @mike-day,
Good idea to get the categories from the back end.

A comment below about implementation, but this works well:

Screenshot 2023-03-08 at 11 35 17 AM

$request = new WP_REST_Request( 'GET', '/wp/v2/block-patterns/categories' );
$response = rest_do_request( $request );

return rest_get_server()->response_to_data( $response, false );
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not common to do a REST request in PHP to the same site.

Could this simply do a similar thing the request does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great feedback! It is working well to just simply use that registry instead of creating a REST request — thank you for the sugggestion.

This is covered by d0c544b.

export default function getUniquePatternCategories( patterns: Patterns ) {
export default function getUniquePatternCategories(
patterns: Patterns,
queriedCategories: QueriedCategories
Copy link
Contributor

@kienstra kienstra Mar 8, 2023

Choose a reason for hiding this comment

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

queriedCategories is a nice name. It's not a certain pattern's categories, it's the queried categories for all patterns.

@@ -1,4 +1,4 @@
import { useState, useRef } from '@wordpress/element';
import { useState } from '@wordpress/element';
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes!!!

Copy link
Contributor

Choose a reason for hiding this comment

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

An award whenever you remove useRef or useEffect !

🎉

@@ -20,5 +20,6 @@ export default function usePatterns( initialPatterns: Patterns ) {
return {
data: patternsData,
deletePattern,
patternCategories: patternManager.patternCategories,
Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely, it's simpler this way.

Comment on lines -62 to +72
label: 'Some category',
label: 'Some Category',
Copy link
Contributor

Choose a reason for hiding this comment

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

Great idea to get the labels from the back end.

@mike-day mike-day changed the title Get list of pattern categories via REST API (Patterns view) Get list of pattern categories via pattern category registry (Patterns view) Mar 8, 2023
@@ -22,7 +22,7 @@
function get_app_state() {
return array(
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(),
'patternCategories' => \PatternManager\ApiData\get_registered_pattern_categories(),
'patternCategories' => \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks again for the idea, it's a great find to just use that registry!

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

…into try/categories-from-rest-in-patterns-view
@mike-day mike-day merged commit dae40e2 into main Mar 8, 2023
@mike-day mike-day deleted the try/categories-from-rest-in-patterns-view branch March 8, 2023 21:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants