PluginProbe ʕ •ᴥ•ʔ
Custom Post Type UI / 1.16.0
Custom Post Type UI v1.16.0
1.19.2 1.19.1 1.19.0 trunk 0.7.0.0 0.7.1.0 0.7.2.0 0.8.0.0 0.8.1 0.8.2 0.8.3 0.8.4 0.8.5 0.9.0 0.9.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.2 1.11.0 1.11.1 1.11.2 1.12.0 1.12.1 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.13.7 1.14.0 1.15.0 1.15.1 1.16.0 1.17.0 1.17.1 1.17.2 1.17.3 1.18.0 1.18.1 1.18.2 1.18.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.8.0 1.8.1 1.8.2 1.9.0 1.9.1 1.9.2
custom-post-type-ui / external / wpgraphql.php
custom-post-type-ui / external Last commit date
wpgraphql.php 2 years ago
wpgraphql.php
356 lines
1 <?php
2
3 namespace CPTUI;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * This class creates settings for Custom Post Type UI to show Custom Post Types in GraphQL
11 */
12 class CPTUI_GraphQL {
13
14 /**
15 * @var bool
16 */
17 protected $show_in_graphql = false;
18
19 /**
20 * @var string
21 */
22 protected $graphql_single_name = '';
23
24 /**
25 * @var string
26 */
27 protected $graphql_plural_name = '';
28
29 /**
30 * Initializes the plugin functionality
31 */
32 public function init() {
33
34 // Post Types.
35 add_action( 'cptui_post_type_after_fieldsets', [ $this, 'add_graphql_post_type_settings' ], 10, 1 );
36 add_filter( 'cptui_before_update_post_type', [ $this, 'before_update_post_type' ], 10, 2 );
37 add_filter( 'cptui_pre_register_post_type', [ $this, 'add_graphql_settings_to_registry' ], 10, 3 );
38 add_filter( 'cptui_pre_save_post_type', [ $this, 'save_graphql_settings' ], 10, 2 );
39
40 // Taxonomies.
41 add_action( 'cptui_taxonomy_after_fieldsets', [ $this, 'add_taxonomy_graphql_settings' ], 10, 1 );
42 add_filter( 'cptui_before_update_taxonomy', [ $this, 'before_update_taxonomy' ], 10, 2 );
43 add_filter( 'cptui_pre_register_taxonomy', [ $this, 'add_graphql_settings_to_registry' ], 10, 3 );
44 add_filter( 'cptui_pre_save_taxonomy', [ $this, 'save_graphql_settings' ], 10, 2 );
45 }
46
47 /**
48 * Adds the GraphQL Settings from CPT UI to the post_type and taxonomy registry args.
49 *
50 * @param array $args The args for the registry.
51 * @param string $name The name of the type.
52 * @param array $type The array that composes the Type.
53 *
54 * @return array
55 */
56 public function add_graphql_settings_to_registry( $args, $name, $type ) {
57
58 // If the type is not set to show_in_graphql, return the args as-is.
59 if ( ! isset( $type['show_in_graphql'] ) || true !== (bool) $type['show_in_graphql'] ) {
60 return $args;
61 }
62
63 // If the type has no graphql_plural_name, return the args as-is, but
64 // add a message to the debug log for why the Type is not in the Schema.
65 if ( ! isset( $type['graphql_plural_name'] ) || empty( $type['graphql_plural_name'] ) ) {
66 graphql_debug(
67 sprintf(
68 // phpcs:ignore.
69 esc_attr__( 'The graphql_plural_name is empty for the "%s" Post Type or Taxonomy registered by Custom Post Type UI.' ),
70 $type['name']
71 )
72 );
73
74 return $args;
75 }
76
77 // If the type has no graphql_single_name, return the args as-is, but
78 // add a message to the debug log for why the Type is not in the Schema.
79 if ( ! isset( $type['graphql_single_name'] ) || empty( $type['graphql_single_name'] ) ) {
80 graphql_debug(
81 sprintf(
82 // phpcs:ignore.
83 esc_attr__( 'The graphql_single_name is empty for the "%s" Post Type or Taxonomy registered by Custom Post Type UI.' ),
84 $type['name']
85 )
86 );
87
88 return $args;
89 }
90
91 $args['show_in_graphql'] = isset( $type['show_in_graphql'] ) ? (bool) $type['show_in_graphql'] : false;
92 $args['graphql_single_name'] = ! empty( $type['graphql_single_name'] ) ? $type['graphql_single_name'] : null;
93 $args['graphql_plural_name'] = ! empty( $type['graphql_plural_name'] ) ? $type['graphql_plural_name'] : null;
94
95 return $args;
96 }
97
98 /**
99 * Capture post type settings from form submission for saving
100 *
101 * @param array $data
102 */
103 public function before_update_post_type( $data ) {
104 $this->show_in_graphql = isset( $data['cpt_custom_post_type']['show_in_graphql'] ) ? $data['cpt_custom_post_type']['show_in_graphql'] : false;
105 $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'] ) : '';
106 $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'] ) : '';
107 }
108
109 /**
110 * Capture taxonomy settings from form submission for saving
111 *
112 * @param array $data
113 */
114 public function before_update_taxonomy( $data ) {
115 $this->show_in_graphql = isset( $data['cpt_custom_tax']['show_in_graphql'] ) ? $data['cpt_custom_tax']['show_in_graphql'] : false;
116 $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'] ) : '';
117 $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'] ) : '';
118 }
119
120 /**
121 * Save values from form submission
122 *
123 * @param array $type
124 * @param string $name
125 *
126 * @return array
127 */
128 public function save_graphql_settings( $type, $name ) {
129 $type[ $name ]['show_in_graphql'] = $this->show_in_graphql;
130 $type[ $name ]['graphql_single_name'] = \WPGraphQL\Utils\Utils::format_type_name( $this->graphql_single_name );
131 $type[ $name ]['graphql_plural_name'] = \WPGraphQL\Utils\Utils::format_type_name( $this->graphql_plural_name );
132
133 return $type;
134 }
135
136 /**
137 * Add settings fields to Custom Post Type UI form
138 *
139 * @param cptui_admin_ui $ui Admin UI instance.
140 */
141 public function add_graphql_post_type_settings( $ui ) {
142 $tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new'; // phpcs:ignore WordPress.Security.NonceVerification
143 $current = [];
144 $name_array = 'cpt_custom_post_type';
145 if ( 'edit' === $tab ) {
146 $post_types = cptui_get_post_type_data();
147 $selected_post_type = cptui_get_current_post_type( false );
148 if ( $selected_post_type ) {
149 if ( array_key_exists( $selected_post_type, $post_types ) ) {
150 $current = $post_types[ $selected_post_type ];
151 }
152 }
153 }
154 echo $this->get_setting_fields( $ui, $current, $name_array ); // phpcs:ignore.
155 }
156
157 /**
158 * Add settings fields to Custom Post Type UI form
159 *
160 * @param cptui_admin_ui $ui Admin UI instance.
161 */
162 public function add_taxonomy_graphql_settings( $ui ) {
163 $tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new'; // phpcs:ignore WordPress.Security.NonceVerification
164 $name_array = 'cpt_custom_tax';
165 $current = [];
166 if ( 'edit' === $tab ) {
167 $taxonomies = cptui_get_taxonomy_data();
168 $selected_taxonomy = cptui_get_current_taxonomy( false );
169 if ( $selected_taxonomy ) {
170 if ( array_key_exists( $selected_taxonomy, $taxonomies ) ) {
171 $current = $taxonomies[ $selected_taxonomy ];
172 }
173 }
174 }
175 echo $this->get_setting_fields( $ui, $current, $name_array ); // phpcs:ignore.
176 }
177
178 /**
179 * Get the settings fields to render for the form
180 *
181 * @param cptui_admin_ui $ui Admin UI instance.
182 * @param array $current
183 * @param string $name_array
184 */
185 public function get_setting_fields( $ui, $current, $name_array ) {
186 ?>
187 <div class="cptui-section postbox">
188 <div class="postbox-header">
189
190 <h2 class="hndle ui-sortable-handle">
191 <span><?php esc_html_e( 'WPGraphQL', 'wp-graphql-custom-post-type-ui' ); ?></span>
192 </h2>
193 <div class="handle-actions hide-if-no-js">
194 <button type="button" class="handlediv">
195 <span class="screen-reader-text"><?php esc_html_e( 'Toggle panel: GraphQL Settings', 'wp-graphql-custom-post-type-ui' ); ?></span>
196 <span class="toggle-indicator" aria-hidden="true"></span>
197 </button>
198 </div>
199 </div>
200 <div class="inside">
201 <div class="main">
202 <table class="form-table cptui-table">
203 <?php
204
205 $selections = [
206 'options' => [
207 [
208 'attr' => '0',
209 'text' => esc_attr__( 'False', 'wp-graphql-custom-post-type-ui' ),
210 ],
211 [
212 'attr' => '1',
213 'text' => esc_attr__( 'True', 'wp-graphql-custom-post-type-ui' ),
214 ],
215 ],
216 ];
217
218 $selected = ( isset( $current ) && ! empty( $current['show_in_graphql'] ) ) ? disp_boolean( $current['show_in_graphql'] ) : '';
219 $selections['selected'] = ( ! empty( $selected ) && ! empty( $current['show_in_graphql'] ) ) ? $current['show_in_graphql'] : '0';
220
221 echo $ui->get_select_input( // phpcs:ignore.
222 [
223 'namearray' => $name_array,
224 'name' => 'show_in_graphql',
225 'labeltext' => esc_html__( 'Show in GraphQL', 'wp-graphql-custom-post-type-ui' ),
226 'aftertext' => esc_html__( 'Whether or not to show data of this type in the WPGraphQL. Default: false', 'wp-graphql-custom-post-type-ui' ),
227 'selections' => $selections, // phpcs:ignore.
228 'default' => false,
229 'required' => true,
230 ]
231 );
232
233 echo $ui->get_text_input( // phpcs:ignore.
234 [
235 'namearray' => $name_array,
236 'name' => 'graphql_single_name',
237 'labeltext' => esc_html__( 'GraphQL Single Name', 'wp-graphql-custom-post-type-ui' ),
238 'aftertext' => esc_attr__( 'Singular name for reference in the GraphQL API.', 'wp-graphql-custom-post-type-ui' ),
239 'textvalue' => ( isset( $current['graphql_single_name'] ) ) ? esc_attr( $current['graphql_single_name'] ) : '', // phpcs:ignore.
240 'required' => true,
241 ]
242 );
243
244 echo $ui->get_text_input( // phpcs:ignore.
245 [
246 'namearray' => $name_array,
247 'name' => 'graphql_plural_name',
248 'labeltext' => esc_html__( 'GraphQL Plural Name', 'wp-graphql-custom-post-type-ui' ),
249 'aftertext' => esc_attr__( 'Plural name for reference in the GraphQL API.', 'wp-graphql-custom-post-type-ui' ),
250 'textvalue' => ( isset( $current['graphql_plural_name'] ) ) ? esc_attr( $current['graphql_plural_name'] ) : '', // phpcs:ignore.
251 'required' => true,
252 ]
253 );
254 ?>
255 </table>
256 </div>
257 </div>
258 </div>
259 <?php
260 $this->graphql_field_helpers();
261 }
262
263 /**
264 * JavaScript helpers to add conditional logic and support for the GraphQL setting fields
265 */
266 public function graphql_field_helpers() {
267 // This script provides helpers for the GraphQL fields in the CPT UI screen.
268 // If the Post Type or Taxonomy is not set to show_in_graphql the single/plural names
269 // should not be required.
270 ?>
271 <script type="application/javascript">
272 let singleName = document.getElementById('graphql_single_name');
273 let singleNameRow = singleName.closest('tr');
274 let pluralName = document.getElementById('graphql_plural_name');
275 let pluralNameRow = pluralName.closest('tr');
276 let showInGraphQL = document.getElementById('show_in_graphql');
277 let label = document.getElementById('label');
278 let singleLabel = document.getElementById('singular_label');
279
280 // Set the values of the GraphQL fields and their display state
281 function updateGraphQlFields() {
282
283 // Set default state for field values and display state
284 // If the show_in_graphql value is true (or '1') show the
285 // fields and set them as required
286 // Else hide the fields and leave them as not-required
287 if (showInGraphQL.value === '1') {
288 singleName.required = true;
289 pluralName.required = true;
290 pluralNameRow.style.display = "table-row";
291 singleNameRow.style.display = "table-row";
292 } else {
293 singleName.required = false;
294 pluralName.required = false;
295 pluralNameRow.style.display = "none";
296 singleNameRow.style.display = "none";
297 }
298
299 // If single_name has no value, but single_label does, use single_label as the value
300 if (!singleName.value.length) {
301 singleName.value = singleLabel.value;
302 }
303
304 // If single_name has no value, but single_label does, use single_label as the value
305 if (!pluralName.value.length) {
306 pluralName.value = label.value;
307 }
308 }
309
310 // Once the DOM is ready, listen for events
311 document.addEventListener("DOMContentLoaded", function () {
312 updateGraphQlFields();
313 // When the show in graphql field changes, re-apply GraphQL Field Values
314 showInGraphQL.addEventListener('input', function () {
315 updateGraphQlFields();
316 });
317 });
318 </script>
319 <?php
320 }
321 }
322
323 /**
324 * Load WPGraphQL for CPT UI
325 */
326 add_action( 'cptui_loaded', __NAMESPACE__ . '\cptui_graphql_init' );
327
328 function cptui_graphql_init() {
329 if ( class_exists( 'WPGraphQL_CPT_UI' ) ) {
330 add_action(
331 'admin_notices',
332 function () {
333 $link = trailingslashit( admin_url() ) . 'plugins.php';
334 ?>
335 <div class="notice notice-error">
336 <p>
337 <?php
338 echo sprintf(
339 // phpcs:ignore.
340 esc_html__( 'Custom Post Type UI has native support for WPGraphQL. Please <a href="%s">de-active</a> the "WPGraphQL for Custom Post Type UI" extension to proceed.', 'custom-post-type-ui' ),
341 $link // phpcs:ignore.
342 );
343 ?>
344 </p>
345 </div>
346 <?php
347 }
348 );
349
350 return;
351 }
352
353 $wpgraphql_cpt_ui = new CPTUI_GraphQL();
354 $wpgraphql_cpt_ui->init();
355 }
356