PluginProbe
Faust.js / 1.0.2
Faust.js v1.0.2
1.8.12 1.8.11 1.4.1 1.8.0 1.8.8 1.8.9 trunk 0.7.0 0.7.1 0.7.10 0.7.11 0.7.3 0.7.4 0.7.5 0.7.6 0.7.7 0.7.8 0.7.9 0.8.0 0.8.1 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 All 40 releases
faustwp / includes / graphql / callbacks.php

callbacks.php in Faust.js 1.0.2, at includes/graphql/callbacks.php

354 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Various callbacks to augment WP GraphQL by adding types, fields, etc.
4 *
5 * @package FaustWP
6 */
7
8 namespace WPE\FaustWP\GraphQL;
9
10 use function WPE\FaustWP\Auth\generate_authorization_code;
11 use GraphQL\Type\Definition\ResolveInfo;
12 use WPGraphQL\AppContext;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_templates_field' );
19 /**
20 * Registers templates field on the UniformResourceIdentifiable type in WP GraphQL. This templates field lists out the
21 * templates that WordPress would typically try to locate and load for a given URI.
22 *
23 * A good amount of the logic in the resolver for this type is extracted from WordPress' template-loader.php file. It's
24 * important that we keep this in sync as much as possible to ensure that the returned array is consistent with what
25 * WordPress would actually do.
26 *
27 * @return void
28 *
29 * @uses WPE\FaustWP\GraphQL\log_template_hierarchy
30 * @uses WPE\FaustWP\GraphQL\template_hierarchy_types
31 * @uses WPE\FaustWP\GraphQL\get_conditional_tags
32 *
33 * @see template-loader.php
34 * @uses register_graphql_field
35 */
36 function register_templates_field() {
37 register_graphql_field(
38 'UniformResourceIdentifiable',
39 'templates',
40 array(
41 'type' => array( 'list_of' => 'String' ),
42 'resolve' => __NAMESPACE__ . '\\templates_resolver',
43 )
44 );
45 }
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
131 /**
132 * Resolver for getting the templates that will be loaded on a given node
133 *
134 * @param array $root GraphQL Root Object.
135 * @param array $args Args passed to query.
136 * @param AppContext $context The context of the query to pass along.
137 * @param ResolveInfo $info The ResolveInfo object.
138 *
139 * @return array|string[]
140 */
141 function templates_resolver( $root, $args, AppContext $context, ResolveInfo $info ) {
142 global $wp_query;
143 global $faustwp_checked_templates;
144 $faustwp_checked_templates = array();
145
146 // Loop through each of the template conditionals, and find the appropriate template file.
147 foreach ( template_hierarchy_types() as $type ) {
148 add_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' );
149 }
150
151 $template = false;
152
153 /**
154 * Force $wp_query to initialize as WP GraphQL does not always parse the query. This is important for getting
155 * all of the templates for the site root.
156 */
157 $wp_query->parse_query();
158
159 foreach ( get_conditional_tags() as $tag => $tag_info ) {
160 if ( empty( $tag_info['template_getter'] ) ) {
161 continue;
162 }
163
164 $template_getter = $tag_info['template_getter'];
165
166 if ( call_user_func( $tag ) ) {
167 $template = call_user_func( $template_getter );
168 }
169
170 if ( $template ) {
171 if ( 'is_attachment' === $tag ) {
172 remove_filter( 'the_content', 'prepend_attachment' );
173 }
174
175 break;
176 }
177 }
178
179 foreach ( template_hierarchy_types() as $type ) {
180 remove_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' );
181 }
182
183 /* Strip PHP extension from the checked templates */
184 $faustwp_checked_templates = array_map(
185 function ( $template ) {
186 return basename( $template, '.php' );
187 },
188 $faustwp_checked_templates
189 );
190
191 /**
192 * Add index as the default template.
193 */
194 return array_unique( array_merge( $faustwp_checked_templates, array( 'index' ) ) );
195 }
196
197 /**
198 * Callback to log the templates that will be located by WordPress into a global variable.
199 *
200 * @param string[] $templates Templates being loaded.
201 *
202 * @return array
203 */
204 function log_template_hierarchy( $templates ) {
205 global $faustwp_checked_templates;
206 $faustwp_checked_templates = array_merge( $faustwp_checked_templates, $templates );
207 return $faustwp_checked_templates;
208 }
209
210 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_conditional_tags_field' );
211 /**
212 * Adds ConditionalTags type and registers it as a field on the UniformResourceIdentifiable type. This type handles
213 * evaluating the supported conditional tags and providing it over GraphQL for a given URI.
214 *
215 * @return void
216 * @uses register_graphql_field
217 * @uses WPE\FaustWP\GraphQL\get_conditional_tags
218 *
219 * @uses register_graphql_object_type
220 *
221 * @deprecated
222 */
223 function register_conditional_tags_field() {
224 register_graphql_object_type(
225 'ConditionalTags',
226 array(
227 'description' => __( 'GraphQL representation of WordPress Conditional Tags.', 'faustwp' ),
228 'fields' => array_map(
229 function ( $tag ) {
230 return array(
231 'type' => 'Boolean',
232 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ),
233 'description' => $tag['description'],
234 );
235 },
236 array_combine(
237 array_map(
238 '\WPE\FaustWP\Utilities\camelcase',
239 array_keys( get_conditional_tags() )
240 ),
241 array_values( get_conditional_tags() )
242 )
243 ),
244 )
245 );
246
247 register_graphql_field(
248 'UniformResourceIdentifiable',
249 'conditionalTags',
250 array(
251 'type' => 'ConditionalTags',
252 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ),
253 'resolve' => __NAMESPACE__ . '\\conditional_tags_resolver',
254 )
255 );
256 }
257
258 /**
259 * Resolver for conditionalTags field.
260 *
261 * @param array $root GraphQL Root Object.
262 * @param array $args Args passed to query.
263 * @param AppContext $context The context of the query to pass along.
264 * @param ResolveInfo $info The ResolveInfo object.
265 *
266 * @return array
267 */
268 function conditional_tags_resolver( $root, $args, AppContext $context, ResolveInfo $info ) {
269 $conditional_tag_values = array();
270
271 foreach ( get_conditional_tags() as $tag => $tag_info ) {
272 $conditional_tag_values[ \WPE\FaustWP\Utilities\camelcase( $tag ) ] = $tag();
273 }
274
275 return $conditional_tag_values;
276 }
277
278 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_generate_ac_mutation' );
279 /**
280 * Register a mutation for exchanging a username/email and password for a short-lived authorization code.
281 *
282 * @return void
283 */
284 function register_generate_ac_mutation() {
285 register_graphql_mutation(
286 'generateAuthorizationCode',
287 array(
288 'inputFields' => array(
289 'username' => array(
290 'type' => 'String',
291 'description' => __( 'Username for WordPress user', 'faustwp' ),
292 ),
293 'email' => array(
294 'type' => 'String',
295 'description' => __( 'Email for WordPress user', 'faustwp' ),
296 ),
297 'password' => array(
298 'type' => 'String',
299 'description' => __( 'Password for WordPress user', 'faustwp' ),
300 ),
301 ),
302 'outputFields' => array(
303 'code' => array(
304 'type' => 'String',
305 'description' => __( 'Authorization code used for requesting refresh/access tokens', 'faustwp' ),
306 ),
307 'error' => array(
308 'type' => 'String',
309 'description' => __( 'Error encountered during user authentication, if any', 'faustwp' ),
310 ),
311 ),
312 'mutateAndGetPayload' => function( $input, $context, $info ) {
313 $is_email = isset( $input['email'] ) ? true : false;
314 $username = isset( $input['username'] ) ? $input['username'] : null;
315 $email = isset( $input['email'] ) ? $input['email'] : null;
316 $password = isset( $input['password'] ) ? $input['password'] : null;
317
318 if ( $is_email ) {
319 $authenticate = wp_authenticate_email_password(
320 null,
321 $email,
322 $password
323 );
324
325 $user = get_user_by( 'email', $email );
326 } else {
327 $authenticate = wp_authenticate_username_password(
328 null,
329 $username,
330 $password
331 );
332
333 $user = get_user_by( 'login', $username );
334 }
335
336 if ( is_wp_error( $authenticate ) ) {
337 return array(
338 'code' => null,
339 'error' => $authenticate->get_error_message(),
340 );
341 }
342
343 // Generate an authorization code that expires in 1 minute.
344 $code = generate_authorization_code( $user, MINUTE_IN_SECONDS * 1 );
345
346 return array(
347 'code' => $code,
348 'error' => null,
349 );
350 },
351 )
352 );
353 }
354