-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-graphql-cpt.php
139 lines (119 loc) · 4.86 KB
/
wp-graphql-cpt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Plugin Name: WP GraphQL Custom Post Types and Custom Taxonomies
* Description: Exposes all registered Custom Post Types and Custom Taxonomies to the WPGraphQL EndPoint.
* Author: Niklas Dahlqvist
* Author URI: https://www.niklasdahlqvist.com
* Version: 0.7
* License: GPL2+
*/
namespace WPGraphQL\Extensions;
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('\WPGraphQL\Extensions\CPT')) {
class CPT
{
public function __construct()
{
// Actions
// Filters
add_filter('register_post_type_args', [$this, 'filterPostTypes'], 10, 2);
add_filter('register_taxonomy_args', [$this, 'filterTaxonomies'], 10, 2);
}
public function filterPostTypes($args, $post_type)
{
$graphQLArgs = [];
$wp_default_post_types = [
'post',
'page',
'attachment',
'revision',
'nav_menu_item',
'custom_css',
'customize_changeset',
'oembed_cache',
'user_request',
'wp_block',
'wp_template',
'wp_template_part',
'wp_global_styles',
'wp_navigation',
// Exclude ACF Field Groups
'acf-field-group',
// Exclude WooCommerce products
'product',
'product_variation',
'shop_coupon',
'shop_order',
'shop_order_refund',
'shop_order_placehold'
];
$wp_default_post_types = apply_filters( 'graphql_cpt_excluded_post_types', $wp_default_post_types );
// Filter Out Truly Custom Post Types, we don't want to mess around with the others
if (!in_array($post_type, $wp_default_post_types) && !$this->graphQLKeysExists($args)) {
if (isset($args['labels']) && isset($args['public']) && $args['public'] == true) {
$graphQLArgs = apply_filters( 'graphql_cpt_post_type_graphql_args', [
'show_in_graphql' => true,
'graphql_single_name' => $this->cleanStrings($args['labels']['singular_name']),
'graphql_plural_name' => $this->cleanStrings($args['labels']['name'])
], $post_type );
$graphQLArgs = apply_filters( "graphql_cpt_{$post_type}_graphql_args", $graphQLArgs );
// Merge args together.
return apply_filters( "graphql_cpt_{$post_type}_merged_args", array_merge($args, $graphQLArgs) );
}
}
return $args;
}
public function filterTaxonomies($args, $taxonomy)
{
$wp_default_taxonomies = [
'category',
'post_tag',
'nav_menu',
'link_category',
'nav_menu_item',
'post_format',
'wp_theme',
'wp_template_part_area',
'product_type',
'product_visibility',
'product_cat',
'product_tag',
'product_shipping_class',
];
$wp_default_taxonomies = apply_filters( 'graphql_cpt_excluded_taxonomies', $wp_default_taxonomies );
// Filter Out Truly Custom Taxonomies, we don't want to mess around with the others
if (!in_array($taxonomy, $wp_default_taxonomies) && !$this->graphQLKeysExists($args)) {
if (isset($args['labels'])) {
$graphQLArgs = apply_filters( 'graphql_cpt_taxonomy_graphql_args', [
'show_in_graphql' => true,
'graphql_single_name' => $this->cleanStrings($args['labels']['singular_name']),
'graphql_plural_name' => $this->cleanStrings($args['labels']['name'])
], $taxonomy );
$graphQLArgs = apply_filters( "graphql_cpt_{$taxonomy}_graphql_args", $graphQLArgs );
// Merge args together.
return apply_filters( "graphql_cpt_{$taxonomy}_merged_args", array_merge($args, $graphQLArgs) );
}
}
return $args;
}
public function graphQLKeysExists($args)
{
$graphQLKeys = apply_filters( 'graphql_cpt_existing_keys_to_check_against', [
'show_in_graphql',
'graphql_single_name',
'graphql_plural_name'
] );
return !array_diff_key(array_flip($graphQLKeys), $args);
}
public function cleanStrings($string)
{
return preg_replace('/[^a-zA-Z0-9_]+/', '', lcfirst(ucwords($string)));
}
}
}
// Boot Plugin
add_action('plugins_loaded', function () {
new CPT();
});