Skip to content
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

Chrome: Support custom taxonomies #2340

Merged
merged 5 commits into from
Aug 11, 2017
Merged

Chrome: Support custom taxonomies #2340

merged 5 commits into from
Aug 11, 2017

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Aug 10, 2017

closes #1342

This PR introduces support for custom taxonomies. It transforms the current CategorySelector and TagSelector to more generic components: HierarchicalTaxonomy and FlatTaxonomy.

This PR uses the same technique used for Custom Post Types to retrieve the Model and the Collection of a Custom Hierarchy in the API client.

Caveats

  • This works only for taxonomies with show_in_rest => true (issue being worked-on on Trac)

Testing instructions

  • Check that the tags/categories selectors works same as master for posts
  • Add some custom taxonomies like this (you can add it to the functions.php of your theme for example):
register_taxonomy(
   	'genre',
   	'post',
   	array(
   		'label' => __( 'Genre' ),
   		'rewrite' => array( 'slug' => 'genre' ),
   		'hierarchical' => false,
   		'show_ui' => true, 
   		'show_in_rest' => true,
   	)
   );

And check that you can set terms to the post for this custom taxonomy. (try switching the hierarchical flag)

@youknowriad youknowriad added the General Interface Parts of the UI which don't fall neatly under other labels. label Aug 10, 2017
@youknowriad youknowriad self-assigned this Aug 10, 2017
@youknowriad youknowriad requested review from jasmussen and aduth August 10, 2017 13:22
@youknowriad youknowriad changed the title Chrome: Allow settings custom taxonomies Chrome: Support custom taxonomies Aug 10, 2017
@youknowriad youknowriad force-pushed the add/custom-taxonomies branch from 56f4b62 to e713c0e Compare August 10, 2017 13:40
@codecov
Copy link

codecov bot commented Aug 10, 2017

Codecov Report

Merging #2340 into master will increase coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2340      +/-   ##
==========================================
+ Coverage   24.69%   24.69%   +<.01%     
==========================================
  Files         152      152              
  Lines        4738     4993     +255     
  Branches      799      853      +54     
==========================================
+ Hits         1170     1233      +63     
- Misses       3014     3158     +144     
- Partials      554      602      +48
Impacted Files Coverage Δ
...ebar/post-taxonomies/hierarchical-term-selector.js 0% <0%> (ø)
...itor/sidebar/post-taxonomies/flat-term-selector.js 0% <0%> (ø)
editor/sidebar/post-taxonomies/index.js 0% <0%> (ø) ⬆️
blocks/library/separator/index.js 42.85% <0%> (-7.15%) ⬇️
components/higher-order/with-focus-return/index.js 92.85% <0%> (-7.15%) ⬇️
blocks/library/cover-text/index.js 29.41% <0%> (-5.59%) ⬇️
blocks/library/heading/index.js 18.91% <0%> (-4.9%) ⬇️
blocks/library/latest-posts/index.js 7.22% <0%> (-2.78%) ⬇️
blocks/library/gallery/index.js 23.8% <0%> (-1.2%) ⬇️
blocks/library/list/index.js 6.72% <0%> (-0.51%) ⬇️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 147644b...3bcb09e. Read the comment docs.

@jasmussen
Copy link
Contributor

What's the best way to test this?

@youknowriad
Copy link
Contributor Author

@jasmussen good question, I added some instructions to the description

@jasmussen
Copy link
Contributor

Yup! Seems to be working for me, nice work! 👍 👍

screen shot 2017-08-10 at 16 55 41

@aduth
Copy link
Member

aduth commented Aug 10, 2017

It transforms the current CategorySelector and TagSelector to more generic components: HierarchicalTaxonomy and FlatTaxonomy.

Not sure about the rename, since without context, it's not clear what a "HierarchicalTaxonomy" component is. Might suggest instead: HierarchicalTermSelector, FlatTermSelector.

On the fence about even dropping "Flat" in favor of TermSelector (non-hierarchical) and HierarchicalTermSelector.

@aduth
Copy link
Member

aduth commented Aug 10, 2017

Do we still want this to show an "Add New Tag" placeholder for the tag type specifically?

Add tag

(Maybe as a small wrapper of flat term selector)

@aduth
Copy link
Member

aduth commented Aug 10, 2017

Do custom taxonomies belong under the "Categories & Tags" heading? In Calypso we have Categories & Tags containing.... categories and tags. But custom taxonomies under their own panels.

* External dependencies
*/
import { connect } from 'react-redux';
import { unescape, find, throttle } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

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

Note entirely in scope but: The reason I'd renamed this in the other component is because there is a global function by the same name:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape

This is problematic if the import is ever dropped for any reason, since linting will not surface any issues:

gutenberg (add/custom-taxonomies) $ git diff
diff --git a/editor/sidebar/post-taxonomies/flat-taxonomy.js b/editor/sidebar/post-taxonomies/flat-taxonomy.js
index 995aa17a..d21a7be6 100644
--- a/editor/sidebar/post-taxonomies/flat-taxonomy.js
+++ b/editor/sidebar/post-taxonomies/flat-taxonomy.js
@@ -2,7 +2,7 @@
  * External dependencies
  */
 import { connect } from 'react-redux';
-import { unescape, find, throttle } from 'lodash';
+import { find, throttle } from 'lodash';
 
 /**
  * WordPress dependencies
gutenberg (add/custom-taxonomies) $ npx eslint editor/sidebar/post-taxonomies/flat-taxonomy.js 
gutenberg (add/custom-taxonomies) $ 

const TaxonomyComponent = taxonomy.hierarchical ? HierarchicalTaxonomy : FlatTaxonomy;
return (
<TaxonomyComponent
key={ taxonomy.name }
Copy link
Member

Choose a reason for hiding this comment

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

Since name is human-readable label and not necessarily unique, should we use taxonomy.slug instead?

@@ -375,6 +385,18 @@ function gutenberg_extend_wp_api_backbone_client() {
return model.prototype.route && route === model.prototype.route.index;
} ) );
};
wp.api.getTaxonomyModel = function( taxonomy ) {
Copy link
Member

Choose a reason for hiding this comment

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

Eugh, why is rest_base a thing 😞

@@ -375,6 +385,18 @@ function gutenberg_extend_wp_api_backbone_client() {
return model.prototype.route && route === model.prototype.route.index;
} ) );
};
wp.api.getTaxonomyModel = function( taxonomy ) {
var route = '/' + wpApiSettings.versionString + this.taxonomyRestBaseMapping[ taxonomy ] + '/(?P<id>[\\\\d]+)';
return _.first( _.filter( wp.api.models, function( model ) {
Copy link
Member

Choose a reason for hiding this comment

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

I know this is copy-paste, but we should really use _.find here so we're not filtering the entire list if match is one of the first entries.

@youknowriad
Copy link
Contributor Author

Do we still want this to show an "Add New Tag" placeholder for the tag type specifically?

I don't like special cases but if we want this, ok.

Do custom taxonomies belong under the "Categories & Tags" heading?

No personal preference for me. We can use a generic "Taxonomies" Panel, or maybe have a Panel per taxonomy to avoid special-casing

@youknowriad
Copy link
Contributor Author

On the fence about even dropping "Flat" in favor of TermSelector (non-hierarchical) and HierarchicalTermSelector.

I prefer leaving "Flat" because it indicates that the component is not usable for hierarchical taxonomies (avoids ambiguity)

@aduth
Copy link
Member

aduth commented Aug 10, 2017

I have a feeling of déjà vu with this conversation around special treatment of Categories & Tags. I wasn't able to find much prior conversation aside from some of the related effort in Calypso (Automattic/wp-calypso#6744). I seem to recall some conversations with @mtias at the time about how we should treat Categories & Tags as special, independent from other taxonomies (together in their own accordion).

Perhaps good for UX because in most real scenarios they're the common taxonomies?

But also not consistent, in which case we could opt for:

  • One accordion per taxonomy
  • All term selectors under a single accordion

Testing this makes me wish we support remembering toggle states of accordions 😆

@youknowriad
Copy link
Contributor Author

But also not consistent, in which case we could opt for:

  • One accordion per taxonomy
  • All term selectors under a single accordion

Any thoughts on the which option to choose here? cc @jasmussen @mtias

@jasmussen
Copy link
Contributor

Any thoughts on the which option to choose here?

No strong opinion either way, though slightly leaning towards a single accordion. But I don't think this should hold off the PR from getting merged.

Testing this makes me wish we support remembering toggle states of accordions 😆

We should, long term, though.

@youknowriad
Copy link
Contributor Author

Ok I'm merging the PR as is now to include it in today's release

@youknowriad youknowriad merged commit 8a77b33 into master Aug 11, 2017
@youknowriad youknowriad deleted the add/custom-taxonomies branch August 11, 2017 12:34
ceyhun pushed a commit that referenced this pull request Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
General Interface Parts of the UI which don't fall neatly under other labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for custom taxonomies
3 participants