Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created January 14, 2022 17:40
Show Gist options
  • Save jasonbahl/15c1a63ce581c2f88ffe0ea7c5caac2a to your computer and use it in GitHub Desktop.
Save jasonbahl/15c1a63ce581c2f88ffe0ea7c5caac2a to your computer and use it in GitHub Desktop.
Adding Gutenberg Full Site Editing settings to WPGraphQL
// NOTE, THIS IS VERY EXPERIMENTAL. TAKE THIS WITH A BIG GRAIN OF SALT, BUT DO WHAT YOU WILL WITH IT.
register_graphql_object_type("ThemeSettings", [
'description' => 'Theme Settings',
'fields' => [
'primaryColor' => [
'type' => 'string',
'description' => 'Primary Color',
'resolve' => function ($settings) {
$colors = $settings->settings->color->palette->theme;
$primary_color = null;
foreach ($colors as $color) {
if ($color->slug === 'primary') {
$primary_color = $color->color;
}
}
return $primary_color;
}
],
'isGlobalStylesUserThemeJSON' => [
'type' => 'boolean'
],
'version' => [
'type' => 'integer'
],
'colorPalette' => [
'type' => [
'list_of' => 'ThemeStylesPalette'
],
'resolve' => function ($settings) {
// wp_send_json([$settings->settings->color->palette->theme]);
return $settings->settings->color->palette->theme ?? null;
}
]
]
]);
register_graphql_object_type("ThemeStylesPalette", [
'fields' => [
'name' => [
'type' => 'string',
],
'slug' => [
'type' => 'string',
],
'color' => [
'type' => 'string',
]
]
]);
register_graphql_field("RootQuery", "themeSettings", [
'type' => 'ThemeSettings',
'resolve' => function () {
$settingsPost = get_page_by_path('wp-global-styles-gravity-platform', OBJECT, 'wp_global_styles');
// graphql_debug($settingsPost);
// wp_send_json([$settingsPost]);
if (empty($settingsPost->post_content)) {
return null;
}
$decoded = json_decode($settingsPost->post_content);
//wp_send_json([$decoded]);
return $decoded;
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment