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( $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( $type, $name ) { $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( $ui ) { $tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new'; // phpcs:ignore WordPress.Security.NonceVerification $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 ); // phpcs:ignore. } /** * Add settings fields to Custom Post Type UI form * * @param cptui_admin_ui $ui Admin UI instance. */ public function add_taxonomy_graphql_settings( $ui ) { $tab = ( ! empty( $_GET ) && ! empty( $_GET['action'] ) && 'edit' === $_GET['action'] ) ? 'edit' : 'new'; // phpcs:ignore WordPress.Security.NonceVerification $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 ); // phpcs:ignore. } /** * 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( $ui, $current, $name_array ) { ?>

[ [ 'attr' => '0', 'text' => esc_attr__( 'False', 'custom-post-type-ui' ), ], [ 'attr' => '1', 'text' => esc_attr__( 'True', '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( // phpcs:ignore. [ 'namearray' => esc_attr( $name_array ), 'name' => 'show_in_graphql', 'labeltext' => esc_html__( 'Show in GraphQL', 'custom-post-type-ui' ), 'aftertext' => esc_html__( 'Whether or not to show data of this type in the WPGraphQL. Default: false', 'custom-post-type-ui' ), 'selections' => $selections, // phpcs:ignore. 'default' => false, 'required' => true, ] ); echo $ui->get_text_input( // phpcs:ignore. [ 'namearray' => esc_attr( $name_array ), 'name' => 'graphql_single_name', 'labeltext' => esc_html__( 'GraphQL Single Name', 'custom-post-type-ui' ), 'aftertext' => esc_attr__( 'Singular name for reference in the GraphQL API.', 'custom-post-type-ui' ), 'textvalue' => ( isset( $current['graphql_single_name'] ) ) ? esc_attr( $current['graphql_single_name'] ) : '', // phpcs:ignore. 'required' => true, ] ); echo $ui->get_text_input( // phpcs:ignore. [ 'namearray' => esc_attr( $name_array ), 'name' => 'graphql_plural_name', 'labeltext' => esc_html__( 'GraphQL Plural Name', 'custom-post-type-ui' ), 'aftertext' => esc_attr__( 'Plural name for reference in the GraphQL API.', 'custom-post-type-ui' ), 'textvalue' => ( isset( $current['graphql_plural_name'] ) ) ? esc_attr( $current['graphql_plural_name'] ) : '', // phpcs:ignore. 'required' => true, ] ); ?>
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. ?>

de-activate the "WPGraphQL for Custom Post Type UI" extension to proceed.', 'custom-post-type-ui' ), esc_url( $link ) ); ?>

init(); }