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

fix: use WPGraphQL's list of allow post types #15

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Stable tag: 0.5.0.1
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
Expand Down
5 changes: 2 additions & 3 deletions src/Connections/ConnectionsRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace WPGraphQLPostsToPosts\Connections;

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection;
use WPGraphQL\Model\User;
use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;
use WPGraphQLPostsToPosts\Types\Fields;

class ConnectionsRegistrar implements Hookable {
use ObjectsTrait;

public function register_hooks() : void {
add_action( 'graphql_register_types', [ $this, 'register_connections' ], 11 );
Expand Down Expand Up @@ -78,7 +77,7 @@ private static function get_graphql_single_name( string $object_name ) : string
return 'User';
}

$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );

$post_object = self::array_find( $post_types, fn( $post_type ) => $post_type->name === $object_name );

Expand Down
2 changes: 0 additions & 2 deletions src/Mutations/AbstractMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

use GraphQL\Error\UserError;
use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;

abstract class AbstractMutation implements Hookable {
use ObjectsTrait;

public function register_hooks() : void {
add_action( 'graphql_register_types_late', [ $this, 'register_input_fields' ] );
Expand Down
5 changes: 3 additions & 2 deletions src/Mutations/PostMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WPGraphQLPostsToPosts\Mutations;

use WP_Post_Type;
use WPGraphQL;
use WPGraphQLPostsToPosts\Types\Fields;

class PostMutation extends AbstractMutation {
Expand All @@ -13,7 +14,7 @@ public function register_hooks(): void {
}

public function register_input_fields(): void {
$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );

foreach ( $post_types as $post_type ) {
$graphql_single_name = $post_type->graphql_single_name;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function save_additional_data( int $post_id, array $input, WP_Post_Type $
return;
}

$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );
foreach ( $post_types as $post_type ) {
if ( self::camel_case_to_underscores( $mutation_name ) !== $post_type->name ) {
continue;
Expand Down
22 changes: 0 additions & 22 deletions src/Traits/ObjectsTrait.php

This file was deleted.

11 changes: 4 additions & 7 deletions src/Types/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace WPGraphQLPostsToPosts\Types;

use P2P_Connection_Type;
use WPGraphQL;
use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;

class Fields implements Hookable {
use ObjectsTrait;

const PARENT_QUERY_TYPE = 'PostToPostConnectionQuery';
const QUERY_TYPE = 'PostToPostConnections';
const MUTATION_TYPE = 'PostToPostConnectionsMutate';
Expand All @@ -23,7 +21,7 @@ class Fields implements Hookable {

public function register_hooks() : void {
add_action( 'p2p_registered_connection_type', [ $this, 'capture_p2p_connections' ], 10, 2 );
add_action( get_graphql_register_action(), [ $this, 'register_connection_name_enum' ], 9 );
add_action( 'graphql_register_types_late', [ $this, 'register_connection_name_enum' ], 9 );
}

public function capture_p2p_connections( P2P_Connection_Type $ctype, array $args ) : void {
Expand All @@ -46,10 +44,9 @@ public static function should_connect_object( string $object_name ) : bool {
}

public static function is_post_type_in_schema( string $post_type_name ) : bool {
$post_type_names = array_map( fn( $post_type ) => $post_type->name, self::get_post_types() );

return in_array( $post_type_name, $post_type_names, true );
return in_array( $post_type_name, WPGraphQL::get_allowed_post_types(), true );
}

public function register_connection_name_enum() : void {
$p2p_connections_to_map = self::get_p2p_connections();

Expand Down
11 changes: 5 additions & 6 deletions src/Types/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
namespace WPGraphQLPostsToPosts\Types;

use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;
use WPGraphQLPostsToPosts\Types\Fields;
use WPGraphQL;

class Post implements Hookable {

use ObjectsTrait;

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_query', [ $this, 'modify_query_input_fields' ], 10, 6 );
}

public function register_where_input_fields() : void {
$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );

foreach ( $post_types as $post_type ) {
$graphql_single_name = $post_type->graphql_single_name;

Expand All @@ -38,7 +37,7 @@ public function modify_query_input_fields( array $query_args ) : array {
$field_names = [];
$post__in = [];

$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );

foreach ( $post_types as $post_type ) {
$connection_name = $post_type->name;
Expand Down
7 changes: 2 additions & 5 deletions src/Types/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace WPGraphQLPostsToPosts\Types;

use WPGraphQLPostsToPosts\Interfaces\Hookable;
use WPGraphQLPostsToPosts\Traits\ObjectsTrait;
use WPGraphQLPostsToPosts\Types\Fields;

use WPGraphQL;
class User implements Hookable {

use ObjectsTrait;

public function register_hooks() : void {
add_action( 'graphql_register_types_late', [ $this, 'register_where_input_fields' ] );
add_filter( 'graphql_map_input_fields_to_wp_user_query', [ $this, 'modify_query_input_fields' ], 10 );
Expand All @@ -32,7 +29,7 @@ public function modify_query_input_fields( array $query_args ) : array {
$field_names = [];
$include = [];

$post_types = self::get_post_types();
$post_types = WPGraphQL::get_allowed_post_types( 'objects' );

foreach ( $post_types as $post_type ) {
$connection_name = $post_type->name;
Expand Down
1 change: 0 additions & 1 deletion vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'WPGraphQLPostsToPosts\\Mutations\\AbstractMutation' => $baseDir . '/src/Mutations/AbstractMutation.php',
'WPGraphQLPostsToPosts\\Mutations\\PostMutation' => $baseDir . '/src/Mutations/PostMutation.php',
'WPGraphQLPostsToPosts\\Mutations\\UserMutation' => $baseDir . '/src/Mutations/UserMutation.php',
'WPGraphQLPostsToPosts\\Traits\\ObjectsTrait' => $baseDir . '/src/Traits/ObjectsTrait.php',
'WPGraphQLPostsToPosts\\Types\\Fields' => $baseDir . '/src/Types/Fields.php',
'WPGraphQLPostsToPosts\\Types\\Inputs' => $baseDir . '/src/Types/Inputs.php',
'WPGraphQLPostsToPosts\\Types\\Post' => $baseDir . '/src/Types/Post.php',
Expand Down
1 change: 0 additions & 1 deletion vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ComposerStaticInit88e43d1b9362b0d65a816daf17f8926a
'WPGraphQLPostsToPosts\\Mutations\\AbstractMutation' => __DIR__ . '/../..' . '/src/Mutations/AbstractMutation.php',
'WPGraphQLPostsToPosts\\Mutations\\PostMutation' => __DIR__ . '/../..' . '/src/Mutations/PostMutation.php',
'WPGraphQLPostsToPosts\\Mutations\\UserMutation' => __DIR__ . '/../..' . '/src/Mutations/UserMutation.php',
'WPGraphQLPostsToPosts\\Traits\\ObjectsTrait' => __DIR__ . '/../..' . '/src/Traits/ObjectsTrait.php',
'WPGraphQLPostsToPosts\\Types\\Fields' => __DIR__ . '/../..' . '/src/Types/Fields.php',
'WPGraphQLPostsToPosts\\Types\\Inputs' => __DIR__ . '/../..' . '/src/Types/Inputs.php',
'WPGraphQLPostsToPosts\\Types\\Post' => __DIR__ . '/../..' . '/src/Types/Post.php',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '195c7faad1b52e35c9fc65dd4b5f0dab1bb766dc',
'reference' => 'c833fdd31822b9b0a8120d97b99681ccef5ee968',
'name' => 'harness-software/wp-graphql-posts-to-posts',
'dev' => false,
),
Expand All @@ -16,7 +16,7 @@
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '195c7faad1b52e35c9fc65dd4b5f0dab1bb766dc',
'reference' => 'c833fdd31822b9b0a8120d97b99681ccef5ee968',
'dev_requirement' => false,
),
),
Expand Down
2 changes: 1 addition & 1 deletion wp-graphql-posts-to-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* Version: 0.5.0.1
* Author: Harness Software, Kellen Mace
* Author URI: https://harnessup.com/
* License: GPLv2 or later
Expand Down