| @@ -43,8 +43,92 @@ | ||
| 43 | 43 | ) |
| 44 | 44 | ); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | +add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_faust_toolbar_field' ); | |
| 48 | +/** | |
| 49 | + * Registers a field on the User model called "shouldShowFaustToolbar" which | |
| 50 | + * gets the user's WP Admin toolbar preference. This is then used to conditionally | |
| 51 | + * show the Faust toolbar on the headless frontend. | |
| 52 | + * | |
| 53 | + * @return void | |
| 54 | + * | |
| 55 | + * @uses register_graphql_field | |
| 56 | + * @uses wp_get_current_user | |
| 57 | + * @uses get_user_meta | |
| 58 | + */ | |
| 59 | +function register_faust_toolbar_field() { | |
| 60 | + register_graphql_field( | |
| 61 | + 'User', | |
| 62 | + 'shouldShowFaustToolbar', | |
| 63 | + array( | |
| 64 | + 'type' => 'Boolean', | |
| 65 | + 'resolve' => function() { | |
| 66 | + $user = wp_get_current_user(); | |
| 67 | + $toolbar_preference_meta = get_user_meta( $user->ID, 'show_admin_bar_front', true ); | |
| 68 | + | |
| 69 | + return 'true' === $toolbar_preference_meta ? true : false; | |
| 70 | + }, | |
| 71 | + ) | |
| 72 | + ); | |
| 73 | +} | |
| 74 | + | |
| 75 | +add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_global_stylesheet_field' ); | |
| 76 | +/** | |
| 77 | + * Registers a field on the User model called "globalStylesheet" which | |
| 78 | + * returns the stylesheet resulting of merging core, theme, and user data. | |
| 79 | + * | |
| 80 | + * @return void | |
| 81 | + * | |
| 82 | + * @uses register_graphql_enum_type | |
| 83 | + * @uses register_graphql_field | |
| 84 | + * @uses wp_get_global_stylesheet | |
| 85 | + * | |
| 86 | + * @link https://developer.wordpress.org/reference/functions/wp_get_global_stylesheet/ | |
| 87 | + */ | |
| 88 | +function register_global_stylesheet_field() { | |
| 89 | + register_graphql_enum_type( | |
| 90 | + 'GlobalStylesheetTypesEnum', | |
| 91 | + array( | |
| 92 | + 'description' => __( 'Types of styles to load', 'faustwp' ), | |
| 93 | + 'values' => array( | |
| 94 | + 'VARIABLES' => array( | |
| 95 | + 'value' => 'variables', | |
| 96 | + ), | |
| 97 | + 'PRESETS' => array( | |
| 98 | + 'value' => 'presets', | |
| 99 | + ), | |
| 100 | + 'STYLES' => array( | |
| 101 | + 'value' => 'styles', | |
| 102 | + ), | |
| 103 | + 'BASE_LAYOUT_STYLES' => array( | |
| 104 | + 'value' => 'base-layout-styles', | |
| 105 | + ), | |
| 106 | + ), | |
| 107 | + ) | |
| 108 | + ); | |
| 109 | + | |
| 110 | + register_graphql_field( | |
| 111 | + 'RootQuery', | |
| 112 | + 'globalStylesheet', | |
| 113 | + array( | |
| 114 | + 'type' => 'String', | |
| 115 | + 'args' => array( | |
| 116 | + 'types' => array( | |
| 117 | + 'type' => array( 'list_of' => 'GlobalStylesheetTypesEnum' ), | |
| 118 | + 'description' => __( 'Types of styles to load.', 'faustwp' ), | |
| 119 | + ), | |
| 120 | + ), | |
| 121 | + 'description' => __( 'Returns the stylesheet resulting of merging core, theme, and user data.', 'faustwp' ), | |
| 122 | + 'resolve' => function( $root, $args, $context, $info ) { | |
| 123 | + $types = $args['types'] ?? null; | |
| 124 | + | |
| 125 | + return wp_get_global_stylesheet( $types ); | |
| 126 | + }, | |
| 127 | + ) | |
| 128 | + ); | |
| 129 | +} | |
| 130 | + | |
| 47 | 131 | /** |
| 48 | 132 | * Resolver for getting the templates that will be loaded on a given node |
| 49 | 133 | * |
| 50 | 134 | * @param array $root GraphQL Root Object. |