-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwp-graphql-custom-post-type-ui.php
342 lines (301 loc) · 12.6 KB
/
wp-graphql-custom-post-type-ui.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Plugin Name: WPGraphQL Custom Post Type UI
* Description: Adds WPGraphQL settings to Custom Post Type UI
* Author: WPGraphQL, rachelbahl, jasonbahl
* Author URI: https://wpgraphql.com
* Version: 1.2.0
* Text Domain: wp-graphql-custom-post-type-ui
* Requires at least: 5.1.0
* Tested up to: 5.2.0
* Requires PHP: 5.6
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Plugin URI: https://github.com/wp-graphql/wp-graphql-custom-post-type-ui
* GitHub Plugin URI: https://github.com/wp-graphql/wp-graphql-custom-post-type-ui
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* This class creates settings for Custom Post Type UI to show Custom Post Types in GraphQL
*/
class WPGraphQL_CPT_UI {
/**
* @var bool
*/
protected $show_in_graphql = false;
/**
* @var string
*/
protected $graphql_single_name = '';
/**
* @var string
*/
protected $graphql_plural_name = '';
/**
* Initializes the plugin functionality
*/
public function init() {
// Taxonomies
add_action( 'cptui_taxonomy_after_fieldsets', [
$this,
'add_taxonomy_graphql_settings'
], 10, 1 );
add_filter( 'cptui_before_update_taxonomy', [ $this, 'before_update_taxonomy' ], 10, 2 );
add_filter( 'cptui_pre_register_taxonomy', [
$this,
'add_graphql_settings_to_registry'
], 10, 3 );
add_filter( 'cptui_pre_save_taxonomy', [ $this, 'save_graphql_settings' ], 10, 2 );
// Post Types
add_action( 'cptui_post_type_after_fieldsets', [
$this,
'add_graphql_post_type_settings'
], 10, 1 );
add_filter( 'cptui_before_update_post_type', [ $this, 'before_update_post_type' ], 10, 2 );
add_filter( 'cptui_pre_register_post_type', [
$this,
'add_graphql_settings_to_registry'
], 10, 3 );
add_filter( 'cptui_pre_save_post_type', [ $this, 'save_graphql_settings' ], 10, 2 );
}
/**
* Adds the GraphQL Settings from CPT UI to the post_type and taxonomy registry args.
*
* @param array $args The args for the registry
* @param string $name The name of the type
* @param array $type The array that composes the Type
*
* @return array
*/
public function add_graphql_settings_to_registry( array $args, string $name, array $type ): array {
// If the type is not set to show_in_graphql, return the args as-is
if ( ! isset( $type['show_in_graphql'] ) || true !== (bool) $type['show_in_graphql'] ) {
return $args;
}
// If the type has no graphql_plural_name, return the args as-is, but
// add a message to the debug log for why the Type is not in the Schema
if ( ! isset( $type['graphql_plural_name'] ) || empty( $type['graphql_plural_name'] ) ) {
graphql_debug( sprintf( __( 'The graphql_plural_name is empty for the "%s" Post Type or Taxonomy registered by Custom Post Type UI.' ), $type['name'] ) );
return $args;
}
// If the type has no graphql_single_name, return the args as-is, but
// add a message to the debug log for why the Type is not in the Schema
if ( ! isset( $type['graphql_single_name'] ) || empty( $type['graphql_single_name'] ) ) {
graphql_debug( sprintf( __( 'The graphql_single_name is empty for the "%s" Post Type or Taxonomy registered by Custom Post Type UI.' ), $type['name'] ) );
return $args;
}
$args['show_in_graphql'] = isset( $type['show_in_graphql'] ) ? (bool) $type['show_in_graphql'] : false;
$args['graphql_single_name'] = ! empty( $type['graphql_single_name'] ) ? $type['graphql_single_name'] : null;
$args['graphql_plural_name'] = ! empty( $type['graphql_plural_name'] ) ? $type['graphql_plural_name'] : null;
return $args;
}
/**
* Capture post type settings from form submission for saving
*
* @param array $data
*/
public function before_update_post_type( array $data ) {
$this->show_in_graphql = isset( $data['cpt_custom_post_type']['show_in_graphql'] ) ? $data['cpt_custom_post_type']['show_in_graphql'] : false;
$this->graphql_single_name = isset( $data['cpt_custom_post_type']['graphql_single_name'] ) ? \WPGraphQL\Utils\Utils::format_type_name( $data['cpt_custom_post_type']['graphql_single_name'] ) : '';
$this->graphql_plural_name = isset( $data['cpt_custom_post_type']['graphql_plural_name'] ) ? \WPGraphQL\Utils\Utils::format_type_name( $data['cpt_custom_post_type']['graphql_plural_name'] ) : '';
}
/**
* Capture taxonomy settings from form submission for saving
*
* @param array $data
*/
public function before_update_taxonomy( array $data ) {
$this->show_in_graphql = isset( $data['cpt_custom_tax']['show_in_graphql'] ) ? $data['cpt_custom_tax']['show_in_graphql'] : false;
$this->graphql_single_name = isset( $data['cpt_custom_tax']['graphql_single_name'] ) ? \WPGraphQL\Utils\Utils::format_type_name( $data['cpt_custom_tax']['graphql_single_name'] ) : '';
$this->graphql_plural_name = isset( $data['cpt_custom_tax']['graphql_plural_name'] ) ? \WPGraphQL\Utils\Utils::format_type_name( $data['cpt_custom_tax']['graphql_plural_name'] ) : '';
}
/**
* Save values from form submission
*
* @param array $type
* @param string $name
*
* @return array
*/
public function save_graphql_settings( array $type, string $name ): array {
$type[ $name ]['show_in_graphql'] = $this->show_in_graphql;
$type[ $name ]['graphql_single_name'] = \WPGraphQL\Utils\Utils::format_type_name( $this->graphql_single_name );
$type[ $name ]['graphql_plural_name'] = \WPGraphQL\Utils\Utils::format_type_name( $this->graphql_plural_name );
return $type;
}
/**
* Add settings fields to Custom Post Type UI form
*
* @param cptui_admin_ui $ui Admin UI instance
*/
public function add_graphql_post_type_settings( cptui_admin_ui $ui ) {
$tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new';
$current = [];
$name_array = 'cpt_custom_post_type';
if ( 'edit' === $tab ) {
$post_types = cptui_get_post_type_data();
$selected_post_type = cptui_get_current_post_type( false );
if ( $selected_post_type ) {
if ( array_key_exists( $selected_post_type, $post_types ) ) {
$current = $post_types[ $selected_post_type ];
}
}
}
echo $this->get_setting_fields( $ui, $current, $name_array );
}
/**
* Add settings fields to Custom Post Type UI form
*
* @param cptui_admin_ui $ui Admin UI instance
*/
public function add_taxonomy_graphql_settings( cptui_admin_ui $ui ) {
$tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new';
$name_array = 'cpt_custom_tax';
$current = [];
if ( 'edit' === $tab ) {
$taxonomies = cptui_get_taxonomy_data();
$selected_taxonomy = cptui_get_current_taxonomy( false );
if ( $selected_taxonomy ) {
if ( array_key_exists( $selected_taxonomy, $taxonomies ) ) {
$current = $taxonomies[ $selected_taxonomy ];
}
}
}
echo $this->get_setting_fields( $ui, $current, $name_array );
}
/**
* Get the settings fields to render for the form
*
* @param cptui_admin_ui $ui Admin UI instance
* @param array $current
* @param string $name_array
*/
public function get_setting_fields( cptui_admin_ui $ui, array $current, string $name_array ) {
?>
<div class="cptui-section postbox">
<div class="postbox-header">
<h2 class="hndle ui-sortable-handle">
<span><?php esc_html_e( 'GraphQL Settings', 'wp-graphql-custom-post-type-ui' ); ?></span>
</h2>
<div class="handle-actions hide-if-no-js">
<button type="button" class="handlediv">
<span class="screen-reader-text"><?php esc_html_e( 'Toggle panel: GraphQL Settings', 'wp-graphql-custom-post-type-ui' ); ?></span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
</div>
</div>
<div class="inside">
<div class="main">
<table class="form-table cptui-table">
<?php
$selections = [
'options' => [
[
'attr' => '0',
'text' => esc_attr__( 'False', 'wp-graphql-custom-post-type-ui' )
],
[
'attr' => '1',
'text' => esc_attr__( 'True', 'wp-graphql-custom-post-type-ui' ),
],
],
];
$selected = ( isset( $current ) && ! empty( $current['show_in_graphql'] ) ) ? disp_boolean( $current['show_in_graphql'] ) : '';
$selections['selected'] = ( ! empty( $selected ) && ! empty( $current['show_in_graphql'] ) ) ? $current['show_in_graphql'] : '0';
echo $ui->get_select_input( [
'namearray' => 'cpt_custom_tax',
'name' => 'show_in_graphql',
'labeltext' => esc_html__( 'Show in GraphQL', 'wp-graphql-custom-post-type-ui' ),
'aftertext' => esc_html__( 'Whether or not to show data of this type in the WPGraphQL. Default: false', 'wp-graphql-custom-post-type-ui' ),
'selections' => $selections,
'required' => true,
'default' => false,
] );
echo $ui->get_text_input( [
'namearray' => 'cpt_custom_tax',
'name' => 'graphql_single_name',
'labeltext' => esc_html__( 'GraphQL Single Name', 'wp-graphql-custom-post-type-ui' ),
'aftertext' => esc_attr__( 'Singular name for reference in the GraphQL API.', 'wp-graphql-custom-post-type-ui' ),
'textvalue' => ( isset( $current['graphql_single_name'] ) ) ? esc_attr( $current['graphql_single_name'] ) : '',
'required' => true,
] );
echo $ui->get_text_input( [
'namearray' => 'cpt_custom_tax',
'name' => 'graphql_plural_name',
'labeltext' => esc_html__( 'GraphQL Plural Name', 'wp-graphql-custom-post-type-ui' ),
'aftertext' => esc_attr__( 'Plural name for reference in the GraphQL API.', 'wp-graphql-custom-post-type-ui' ),
'textvalue' => ( isset( $current['graphql_plural_name'] ) ) ? esc_attr( $current['graphql_plural_name'] ) : '',
'required' => true,
] );
?>
</table>
</div>
</div>
</div>
<?php
$this->graphql_field_helpers();
}
/**
* JavaScript helpers to add conditional logic and support for the GraphQL setting fields
*/
public function graphql_field_helpers() {
// This script provides helpers for the GraphQL fields in the CPT UI screen.
// If the Post Type or Taxonomy is not set to show_in_graphql the single/plural names
// should not be required.
?>
<script type="application/javascript">
var singleName = document.getElementById('graphql_single_name');
var singleNameRow = singleName.closest('tr');
var pluralName = document.getElementById('graphql_plural_name');
var pluralNameRow = pluralName.closest('tr');
var showInGraphQL = document.getElementById('show_in_graphql');
var label = document.getElementById('label');
var singleLabel = document.getElementById('singular_label');
// Set the values of the GraphQL fields and their display state
function updateGraphQlFields() {
// Set default state for field values and display state
// If the show_in_graphql value is true (or '1') show the
// fields and set them as required
// Else hide the fields and leave them as not-required
if (showInGraphQL.value === '1') {
singleName.required = true;
pluralName.required = true;
pluralNameRow.style.display = "table-row";
singleNameRow.style.display = "table-row";
} else {
singleName.required = false;
pluralName.required = false;
pluralNameRow.style.display = "none";
singleNameRow.style.display = "none";
}
// If single_name has no value, but single_label does, use single_label as the value
if (!singleName.value.length) {
singleName.value = singleLabel.value;
}
// If single_name has no value, but single_label does, use single_label as the value
if (!pluralName.value.length) {
pluralName.value = label.value;
}
}
// Once the DOM is ready, listen for events
document.addEventListener("DOMContentLoaded", function () {
updateGraphQlFields();
// When the show in graphql field changes, re-apply GraphQL Field Values
showInGraphQL.addEventListener('input', function () {
updateGraphQlFields();
});
});
</script>
<?php
}
}
/**
* Load WPGraphQL for CPT UI
*/
add_action( 'plugins_loaded', 'init_wpgraphql_cptui' );
function init_wpgraphql_cptui() {
$wpgraphql_cpt_ui = new WPGraphQL_CPT_UI();
$wpgraphql_cpt_ui->init();
}