Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created January 8, 2021 09:18
Show Gist options
  • Save jasonbahl/bb1fb4a8e468d8312458b806b03197d4 to your computer and use it in GitHub Desktop.
Save jasonbahl/bb1fb4a8e468d8312458b806b03197d4 to your computer and use it in GitHub Desktop.
Initial support for WPGraphQL for the Redux Framework
$opt_name = 'graphql_demo';
$theme = wp_get_theme(); // For use with some settings. Not necessary.
$args = array(
'display_name' => $theme->get( 'Name' ),
'display_version' => $theme->get( 'Version' ),
'menu_title' => esc_html__( 'Sample Options', 'redux-framework-demo' ),
'customizer' => true,
);
Redux::setArgs( $opt_name, $args );
Redux::setSection( $opt_name, array(
'title' => esc_html__( 'Basic Field', 'redux-framework-demo' ),
'show_in_graphql' => true,
'graphql_name' => 'myReduxSettingsSection',
'id' => 'basic',
'desc' => esc_html__( 'Basic field with no subsections.', 'redux-framework-demo' ),
'icon' => 'el el-home',
'fields' => array(
array(
'id' => 'opt-text',
'type' => 'text',
'show_in_graphql' => true,
'graphql_name' => 'exampleText',
'title' => esc_html__( 'Example Text', 'redux-framework-demo' ),
'desc' => esc_html__( 'Example description.', 'redux-framework-demo' ),
'subtitle' => esc_html__( 'Example subtitle.', 'redux-framework-demo' ),
'hint' => array(
'content' => 'This is a <b>hint</b> tool-tip for the text field.<br/><br/>Add any HTML based text you like here.',
)
)
)
) );
add_action( 'plugins_loaded', function() {
if ( ! class_exists( 'Redux' ) ) {
return;
}
add_action( 'redux/loaded', function( $redux ) {
if ( ! isset( $redux->sections ) || empty( $redux->sections ) ) {
return;
}
$opt_name = $redux->args['opt_name'];
foreach ( $redux->sections as $section_id => $section ) {
if ( isset( $section['show_in_graphql'] ) && true === $section['show_in_graphql'] ) {
$graphql_field_name = isset( $section['graphql_name'] ) ? \WPGraphQL\Utils\Utils::format_field_name( $section['graphql_name'] ) : \WPGraphQL\Utils\Utils::format_field_name( $section['title'] . '_Settings' );
$graphql_type_name = ! empty( $graphql_field_name ) ? \WPGraphQL\Utils\Utils::format_type_name( $graphql_field_name ) : null;
if ( empty( $graphql_type_name ) ) {
return;
}
if ( ! isset( $section['fields'] ) || empty( $section['fields'] ) || ! is_array( $section['fields'] ) ) {
return;
}
$fields = [];
foreach ( $section['fields'] as $setting_field ) {
if ( isset( $setting_field['show_in_graphql'] ) && false === $setting_field['show_in_graphql'] ) {
return;
}
$field_type = 'String';
$field_description = isset( $setting_field['desc'] ) ? $setting_field['desc'] : '';
$field_name = isset( $setting_field['graphql_name'] ) ? \WPGraphQL\Utils\Utils::format_field_name( $setting_field['graphql_name'] ) : \WPGraphQL\Utils\Utils::format_field_name( $setting_field['title'] );
$fields[ $field_name ] = [
'type' => $field_type,
'description' => $field_description,
'resolve' => function() use ( $opt_name, $redux, $setting_field ) {
return Redux::getOption( $opt_name, $setting_field['id'], 'goo' );
}
];
}
if ( empty( $fields ) ) {
return;
}
register_graphql_object_type( $graphql_type_name, [
'description' => ! empty( $section['desc'] ) ? $section['desc'] : sprintf( __( '%s Settings Section from Redux Framework', 'wp-graphql-redux' ) ),
'fields' => $fields
] );
register_graphql_field( 'RootQuery', $graphql_field_name, [
'type' => $graphql_field_name,
'resolve' => function() use ( $section ) {
return $section;
}
] );
}
}
} );
} );
@jasonbahl
Copy link
Author

The file named wp-graphql-redux-framework is the start of what could become formal support for WPGraphQL for the Redux Framework.

It hooks into Redux, reads the registered settings sections and maps them to the WPGraphQL Schema.

The file named register-redux-settings is an example of registering settings with Redux and using show_in_graphql and graphql_field_name to tell which settings to add the the Schema.

With this, I now have the following Options in my Admin:

Screen Shot 2021-01-08 at 2 21 18 AM

And I can query like so in GraphQL:

Screen Shot 2021-01-08 at 2 22 05 AM

@jasonbahl
Copy link
Author

^ based on Redux v3.6.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment