Feat: Refactor get_allowed_post_types() and get_allowed_taxonomies()#2353
Conversation
|
|
||
| $tax_object = get_taxonomy( $taxonomy ); | ||
| // Initialize array of allowed post type objects. | ||
| if ( empty( self::$allowed_taxonomies ) ) { |
There was a problem hiding this comment.
Similar blocks of code found in 2 locations. Consider refactoring.
| /** @var string $post_type */ | ||
| $post_type_object = get_post_type_object( $post_type ); | ||
| // Initialize array of allowed post type objects. | ||
| if ( empty( self::$allowed_post_types ) ) { |
There was a problem hiding this comment.
Similar blocks of code found in 2 locations. Consider refactoring.
| 'show_in_graphql' => true, | ||
| ] | ||
| ); | ||
| public static function get_allowed_taxonomies( $output = 'names', $args = [] ) { |
There was a problem hiding this comment.
Function get_allowed_taxonomies has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
| * Get all post_types | ||
| */ | ||
| $post_types = get_post_types( array_merge( [ 'show_in_graphql' => true ], $args ) ); | ||
| public static function get_allowed_post_types( $output = 'names', $args = [] ) { |
There was a problem hiding this comment.
Function get_allowed_post_types has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
@justlevine where were you seeing significant calls to the database from these functions? Maybe when using something like Custom Post Type UI or something like that? Curious what was causing the extraneous calls to the DB |
Honestly not sure. Now that you mention it I'm remembering that the wp functions are supposed to hit the The original reason I wrote this was to eventually be able to clean up all the messy rehydration loops caused by only storing the names, and then I noticed both the drop in hits and how enforcing the pattern preemptively fixes bugs like with I'm way out of my league on writing performance benchmarks, but maybe that makes sense before replacing some of existing |
|
Post Types and Taxonomies in core are registered in code, so there's no database queries at all for them. I think the database queries must be from plugins that register Custom Post Types / Taxonomies via a GUI, like CPT UI. . .which is very common, so I'm happy to support those cases. It seems like those plugins should also be more responsible with how they filter I was curious to see if we could "prove" the impact this has. I think this is a beneficial refactor, regardless though.
err, hmm. Maybe it's not there 👀 ...ok, now that I think about it more, I don't think the benchmarks were a formal script. I believe it was me loading the Schema and recording the time in a spreadsheet 🤔 😆 |
Rank Math was the culprit. I was using a
Haha nvm then. I'ma see if theres anything I can do about those codeclimate smells, and let you know when this is ready for review. |
|
I've got nothing. post types and taxonomies are so similar, but any abstractions I tried made it even harder to read ( I'm open to suggestions or just leaving it as-is. |
|
@justlevine I updated the properties to be protected. I'm cool with that change.
Can you share how this closes that issue? I just ran the query from that issue, and I'm seeing the same results described in the issue: |
|
Oops. Forgot to disable my other plugins just now, and like with the db calls it was leaking through from my dev env. Will abstract and post the temporary workaround there. Since clearly I overpromised in the description, I'm going to go back and review everything from this pr. |
|
Code Climate has analyzed commit 5dadda1 and detected 6 issues on this pull request. Here's the issue category breakdown:
View more on Code Climate. |
|
Okay the PR description has been updated to describe what this actually does and why 😅 |


What does this implement/fix? Explain your changes
This PR refactors
WPGraphQL::get_allowed_post_types()andWPGraphQL::get_allowed_taxonomies()to correctly store data in theWPGraphQL::$allowed_post_typesandWPGraphQL::$allowed_taxonomiesclass properties.Additionally, it stores the
WP_Post_TypeandWP_Taxonomyobjects, allowing the user to retrieve either.This will ensure that the list of possible post types / taxonomies is the same throughout the schema, and will also let us significantly clean up the codebase by removing now-extraneous rehydration calls ( e.g.
get_post_type( \WPGraphQL::allowed_post_type( )[ $n ] )) in a future PR.Details
The array of
WP_Post_Types andWP_Taxonomiesare stored to their respective parameters after any...entities_allowed_{type}filters have been applied, but before any args passed to the methods.WPGraphQL::get_allowed_post_types()now takes an$outputargument as the first parameter that lets a user decide if they need thenamesor theobjects, and relocates the$args` to the second parameter. This is backwards compatible and if an array is passed as the first arg, it will automatically reassign it.WPGraphQL::get_allowed_taxonomies()accepts those same two parameters. There's no need to worry about back-compat, since it currently doesnt accept any.Numerous calls to
get_post_types()andget_taxonomies()have been replaced with the respectiveget_allowed...()methods. This fixes a handful of bugs where the list of "allowed" types was different throughout the plugin, which would break the schema (e.g. queryingcontentTypesafter deregistering a core type ).The
WPGraphQL::$allowed_post_typesandWPGraphQL::$allowed_taxonomiesproperties are nowprotected, to enforce the static getter pattern.Does this close any currently open issues?
Any relevant logs, error output, GraphiQL screenshots, etc?
Any other comments?
get_post_type()). That's a separate improvement deserving of its own PR.NodeByUri, since that's already a finicky piece of code, and ideally we'd first get chore: refactor NodeResolver #2342 and better test how using a cached list of post/tax types might affect filters on the stored query vars._doing_it_wrong()ourselves.WPGraphQL::$allowed_post_typesandWPGraphQL::$allowed_taxonomiesis non-breaking since theyre still array types, and they weren't actually holding anything beforehand. That said, while changing those properties toprotectedis ideal and wouldn't affect any users,since it's technically a breaking change, I'll leave it to @jasonbahl to decide.(edit: added by Jason).WPGraphQL::get_allowed_post_types()is to conform with PHP best practices, since it's significantly more likely to toggle betweennames/objectsoutputs than it is to pass args. Technically we don't even need to deprecate passing just the$argsas shorthand forWPGraphQL::get_allowed_post_types( 'names', $args ), and can just remove the commented out_doing_it_wrong. Again, I'll let @jasonbahl decide 😎.Where has this been tested?
Operating System: Ubuntu 20.04 (wsl2 + devilbox + php 8.0.15)
WordPress Version: 5.9.3