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

270 lines 7.7 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 * @deprecated
138 */
139 function register_conditional_tags_field() {
140 register_graphql_object_type(
141 'ConditionalTags',
142 array(
143 'description' => __( 'GraphQL representation of WordPress Conditional Tags.', 'faustwp' ),
144 'fields' => array_map(
145 function ( $tag ) {
146 return array(
147 'type' => 'Boolean',
148 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ),
149 'description' => $tag['description'],
150 );
151 },
152 array_combine(
153 array_map(
154 '\WPE\FaustWP\Utilities\camelcase',
155 array_keys( get_conditional_tags() )
156 ),
157 array_values( get_conditional_tags() )
158 )
159 ),
160 )
161 );
162
163 register_graphql_field(
164 'UniformResourceIdentifiable',
165 'conditionalTags',
166 array(
167 'type' => 'ConditionalTags',
168 'deprecationReason' => __( 'Deprecated in favor of using Next.js pages', 'faustwp' ),
169 'resolve' => __NAMESPACE__ . '\\conditional_tags_resolver',
170 )
171 );
172 }
173
174 /**
175 * Resolver for conditionalTags field.
176 *
177 * @param array $root GraphQL Root Object.
178 * @param array $args Args passed to query.
179 * @param AppContext $context The context of the query to pass along.
180 * @param ResolveInfo $info The ResolveInfo object.
181 *
182 * @return array
183 */
184 function conditional_tags_resolver( $root, $args, AppContext $context, ResolveInfo $info ) {
185 $conditional_tag_values = array();
186
187 foreach ( get_conditional_tags() as $tag => $tag_info ) {
188 $conditional_tag_values[ \WPE\FaustWP\Utilities\camelcase( $tag ) ] = $tag();
189 }
190
191 return $conditional_tag_values;
192 }
193
194 add_action( 'graphql_register_types', __NAMESPACE__ . '\\register_generate_ac_mutation' );
195 /**
196 * Register a mutation for exchanging a username/email and password for a short-lived authorization code.
197 *
198 * @return void
199 */
200 function register_generate_ac_mutation() {
201 register_graphql_mutation(
202 'generateAuthorizationCode',
203 array(
204 'inputFields' => array(
205 'username' => array(
206 'type' => 'String',
207 'description' => __( 'Username for WordPress user', 'faustwp' ),
208 ),
209 'email' => array(
210 'type' => 'String',
211 'description' => __( 'Email for WordPress user', 'faustwp' ),
212 ),
213 'password' => array(
214 'type' => 'String',
215 'description' => __( 'Password for WordPress user', 'faustwp' ),
216 ),
217 ),
218 'outputFields' => array(
219 'code' => array(
220 'type' => 'String',
221 'description' => __( 'Authorization code used for requesting refresh/access tokens', 'faustwp' ),
222 ),
223 'error' => array(
224 'type' => 'String',
225 'description' => __( 'Error encountered during user authentication, if any', 'faustwp' ),
226 ),
227 ),
228 'mutateAndGetPayload' => function( $input, $context, $info ) {
229 $is_email = isset( $input['email'] ) ? true : false;
230 $username = isset( $input['username'] ) ? $input['username'] : null;
231 $email = isset( $input['email'] ) ? $input['email'] : null;
232 $password = isset( $input['password'] ) ? $input['password'] : null;
233
234 if ( $is_email ) {
235 $authenticate = wp_authenticate_email_password(
236 null,
237 $email,
238 $password
239 );
240
241 $user = get_user_by( 'email', $email );
242 } else {
243 $authenticate = wp_authenticate_username_password(
244 null,
245 $username,
246 $password
247 );
248
249 $user = get_user_by( 'login', $username );
250 }
251
252 if ( is_wp_error( $authenticate ) ) {
253 return array(
254 'code' => null,
255 'error' => $authenticate->get_error_message(),
256 );
257 }
258
259 // Generate an authorization code that expires in 1 minute.
260 $code = generate_authorization_code( $user, MINUTE_IN_SECONDS * 1 );
261
262 return array(
263 'code' => $code,
264 'error' => null,
265 );
266 },
267 )
268 );
269 }
270