-
Notifications
You must be signed in to change notification settings - Fork 6
Get list of pattern categories via pattern category registry (Patterns view) #96
Conversation
…tional on bottom)
wp-modules/app/app.php
Outdated
'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(), |
There was a problem hiding this comment.
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.
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; | ||
}; |
There was a problem hiding this comment.
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.
label: 'Some category', | ||
label: 'Some Category', |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wp-modules/api-data/api-data.php
Outdated
$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 ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!!!
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
label: 'Some category', | ||
label: 'Some Category', |
There was a problem hiding this comment.
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.
@@ -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(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
Currently, the list of categories for the
app
module is parsed from pattern files. Since categories in the pattern header are saved by theirname
, parsing from the pattern file can be problematic if the category was registered with aname
that differs from itslabel
.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:
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:The displayed values are the actual registered
label
values registered by Frost.How to test
label
valuesNotes
Hydrating the
app
with the list ofpatternCategories
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.