PluginProbe
Faust.js / 0.7.1
Faust.js v0.7.1
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 0.7.1, at includes/graphql/callbacks.php

266 lines 7.4 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 /**
48 * Resolver for getting the templates that will be loaded on a given node
49 *
50 * @param array $root GraphQL Root Object.
51 * @param array $args Args passed to query.
52 * @param AppContext $context The context of the query to pass along.
53 * @param ResolveInfo $info The ResolveInfo object.
54 *
55 * @return array|string[]
56 */
57 function templates_resolver( $root, $args, AppContext $context, ResolveInfo $info ) {
58 global $wp_query;
59 global $faustwp_checked_templates;
60 $faustwp_checked_templates = array();
61
62 // Loop through each of the template conditionals, and find the appropriate template file.
63 foreach ( template_hierarchy_types() as $type ) {
64 add_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' );
65 }
66
67 $template = false;
68
69 /**
70 * Force $wp_query to initialize as WP GraphQL does not always parse the query. This is important for getting
71 * all of the templates for the site root.
72 */
73 $wp_query->parse_query();
74
75 foreach ( get_conditional_tags() as $tag => $tag_info ) {
76 if ( empty( $tag_info['template_getter'] ) ) {
77 continue;
78 }
79
80 $template_getter = $tag_info['template_getter'];
81
82 if ( call_user_func( $tag ) ) {
83 $template = call_user_func( $template_getter );
84 }
85
86 if ( $template ) {
87 if ( 'is_attachment' === $tag ) {
88 remove_filter( 'the_content', 'prepend_attachment' );
89 }
90
91 break;
92 }
93 }
94
95 foreach ( template_hierarchy_types() as $type ) {
96 remove_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\log_template_hierarchy' );
97 }
98
99 /* Strip PHP extension from the checked templates */
100 $faustwp_checked_templates = array_map(
101 function ( $template ) {
102 return basename( $template, '.php' );
103 },
104 $faustwp_checked_templates
105 );
106
107 /**
108 * Add index as the default template.
109 */
110 return array_unique( array_merge( $faustwp_checked_templates, array( 'index' ) ) );
111 }
112
113 /**
114 * Callback to log the templates that will be located by WordPress into a global variable.
115 *
116 * @param string[] $templates Templates being loaded.
117 *
118 * @return array
119 */
120 function log_template_hierarchy( $templates ) {
121 global $faustwp_checked_templates;
122 $faustwp_checked_templates = array_merge( $faustwp_checked_templates, $templates );
123 return $faustwp_checked_templates;
124 }
125
126 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_conditional_tags_field' );
127 /**
128 * Adds ConditionalTags type and registers it as a field on the UniformResourceIdentifiable type. This type handles
129 * evaluating the supported conditional tags and providing it over GraphQL for a given URI.
130 *
131 * @return void
132 * @uses register_graphql_field
133 * @uses WPE\FaustWP\GraphQL\get_conditional_tags
134 *
135 * @uses register_graphql_object_type
136 */
137 function register_conditional_tags_field() {
138 register_graphql_object_type(
139 'ConditionalTags',
140 array(
141 'description' => __( 'GraphQL representation of WordPress Conditional Tags.', 'faustwp' ),
142 'fields' => array_map(
143 function ( $tag ) {
144 return array(
145 'type' => 'Boolean',
146 'description' => $tag['description'],
147 );
148 },
149 array_combine(
150 array_map(
151 '\WPE\FaustWP\Utilities\camelcase',
152 array_keys( get_conditional_tags() )
153 ),
154 array_values( get_conditional_tags() )
155 )
156 ),
157 )
158 );
159
160 register_graphql_field(
161 'UniformResourceIdentifiable',
162 'conditionalTags',
163 array(
164 'type' => 'ConditionalTags',
165 'resolve' => __NAMESPACE__ . '\\conditional_tags_resolver',
166 )
167 );
168 }
169
170 /**
171 * Resolver for conditionalTags field.
172 *
173 * @param array $root GraphQL Root Object.
174 * @param array $args Args passed to query.
175 * @param AppContext $context The context of the query to pass along.
176 * @param ResolveInfo $info The ResolveInfo object.
177 *
178 * @return array
179 */
180 function conditional_tags_resolver( $root, $args, AppContext $context, ResolveInfo $info ) {
181 $conditional_tag_values = array();
182
183 foreach ( get_conditional_tags() as $tag => $tag_info ) {
184 $conditional_tag_values[ \WPE\FaustWP\Utilities\camelcase( $tag ) ] = $tag();
185 }
186
187 return $conditional_tag_values;
188 }
189
190 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_generate_ac_mutation' );
191 /**
192 * Register a mutation for exchanging a username/email and password for a short-lived authorization code.
193 *
194 * @return void
195 */
196 function register_generate_ac_mutation() {
197 register_graphql_mutation(
198 'generateAuthorizationCode',
199 array(
200 'inputFields' => array(
201 'username' => array(
202 'type' => 'String',
203 'description' => __( 'Username for WordPress user', 'faustwp' ),
204 ),
205 'email' => array(
206 'type' => 'String',
207 'description' => __( 'Email for WordPress user', 'faustwp' ),
208 ),
209 'password' => array(
210 'type' => 'String',
211 'description' => __( 'Password for WordPress user', 'faustwp' ),
212 ),
213 ),
214 'outputFields' => array(
215 'code' => array(
216 'type' => 'String',
217 'description' => __( 'Authorization code used for requesting refresh/access tokens', 'faustwp' ),
218 ),
219 'error' => array(
220 'type' => 'String',
221 'description' => __( 'Error encountered during user authentication, if any', 'faustwp' ),
222 ),
223 ),
224 'mutateAndGetPayload' => function( $input, $context, $info ) {
225 $is_email = isset( $input['email'] ) ? true : false;
226 $username = isset( $input['username'] ) ? $input['username'] : null;
227 $email = isset( $input['email'] ) ? $input['email'] : null;
228 $password = isset( $input['password'] ) ? $input['password'] : null;
229
230 if ( $is_email ) {
231 $authenticate = wp_authenticate_email_password(
232 null,
233 $email,
234 $password
235 );
236
237 $user = get_user_by( 'email', $email );
238 } else {
239 $authenticate = wp_authenticate_username_password(
240 null,
241 $username,
242 $password
243 );
244
245 $user = get_user_by( 'login', $username );
246 }
247
248 if ( is_wp_error( $authenticate ) ) {
249 return array(
250 'code' => null,
251 'error' => $authenticate->get_error_message(),
252 );
253 }
254
255 // Generate an authorization code that expires in 1 minute.
256 $code = generate_authorization_code( $user, MINUTE_IN_SECONDS * 1 );
257
258 return array(
259 'code' => $code,
260 'error' => null,
261 );
262 },
263 )
264 );
265 }
266