diff --git a/readme.txt b/readme.txt index 66a8805..e4ae832 100644 --- a/readme.txt +++ b/readme.txt @@ -6,7 +6,7 @@ Tested up to: 6.0.2 Requires PHP: 7.4 Requires Posts To Posts: 1.6.6 Requires WPGraphQL: 1.8.1 -Stable tag: 0.5.0.1 +Stable tag: 0.5.0.2 Maintained at: https://github.com/harness-software/wp-graphql-posts-to-posts License: GPL-3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/src/Mutations/AbstractMutation.php b/src/Mutations/AbstractMutation.php index 99e9d16..d548cc0 100644 --- a/src/Mutations/AbstractMutation.php +++ b/src/Mutations/AbstractMutation.php @@ -10,7 +10,7 @@ abstract class AbstractMutation implements Hookable { public function register_hooks() : void { - add_action( 'graphql_register_types_late', [ $this, 'register_input_fields' ] ); + add_action( 'graphql_register_types', [ $this, 'register_input_fields' ] ); } abstract public function register_input_fields() : void; diff --git a/src/Mutations/PostMutation.php b/src/Mutations/PostMutation.php index 0ec928e..09a19f2 100644 --- a/src/Mutations/PostMutation.php +++ b/src/Mutations/PostMutation.php @@ -16,8 +16,15 @@ public function register_hooks(): void { public function register_input_fields(): void { $post_types = WPGraphQL::get_allowed_post_types( 'objects' ); + $types_wtith_connections = Fields::get_post_types_with_connections(); + foreach ( $post_types as $post_type ) { - $graphql_single_name = $post_type->graphql_single_name; + // Bail if no P2P connection registered for type. + if ( ! in_array( $post_type->name, $types_wtith_connections, true ) ) { + continue; + } + + $graphql_single_name = ucfirst( $post_type->graphql_single_name ); register_graphql_field( 'Update' . $graphql_single_name . 'Input', diff --git a/src/Types/Fields.php b/src/Types/Fields.php index aa778e8..7cfaab5 100644 --- a/src/Types/Fields.php +++ b/src/Types/Fields.php @@ -34,6 +34,19 @@ public static function get_p2p_connections() : array { return array_filter( self::$p2p_connections, [ __CLASS__, 'should_create_connection' ] ); } + public static function get_post_types_with_connections() : array { + $connections = self::$p2p_connections; + + $post_types = []; + + foreach ( $connections as $name => $args ) { + $post_types[] = $args['from']; + $post_types[] = $args['to']; + } + + return array_unique( $post_types ); + } + public static function should_create_connection( array $connection ) : bool { return self::should_connect_object( $connection['from'] ) && self::should_connect_object( $connection['to'] ); diff --git a/src/Types/Post.php b/src/Types/Post.php index d82b7e4..ec29373 100644 --- a/src/Types/Post.php +++ b/src/Types/Post.php @@ -16,8 +16,15 @@ public function register_hooks() : void { public function register_where_input_fields() : void { $post_types = WPGraphQL::get_allowed_post_types( 'objects' ); + $types_wtith_connections = Fields::get_post_types_with_connections(); + foreach ( $post_types as $post_type ) { - $graphql_single_name = $post_type->graphql_single_name; + // Bail if no P2P connection registered for type. + if ( ! in_array( $post_type->name, $types_wtith_connections, true ) ) { + continue; + } + + $graphql_single_name = ucfirst( $post_type->graphql_single_name ); register_graphql_field( 'RootQueryTo' . $graphql_single_name . 'ConnectionWhereArgs', diff --git a/src/Types/User.php b/src/Types/User.php index 8c98135..6750e28 100644 --- a/src/Types/User.php +++ b/src/Types/User.php @@ -8,7 +8,7 @@ class User implements Hookable { public function register_hooks() : void { - add_action( 'graphql_register_types_late', [ $this, 'register_where_input_fields' ] ); + add_action( 'graphql_register_types', [ $this, 'register_where_input_fields' ] ); add_filter( 'graphql_map_input_fields_to_wp_user_query', [ $this, 'modify_query_input_fields' ], 10 ); } diff --git a/wp-graphql-posts-to-posts.php b/wp-graphql-posts-to-posts.php index 4d016df..cf69644 100644 --- a/wp-graphql-posts-to-posts.php +++ b/wp-graphql-posts-to-posts.php @@ -2,7 +2,7 @@ /** * Plugin Name: WPGraphQL for Posts 2 Posts * Description: Creates WPGraphQL connections for all of your Posts 2 Posts connections. - * Version: 0.5.0.1 + * Version: 0.5.0.2 * Author: Harness Software, Kellen Mace * Author URI: https://harnessup.com/ * License: GPLv2 or later