forked from wp-graphql/wp-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-graphql.php
More file actions
385 lines (330 loc) · 10.3 KB
/
Copy pathwp-graphql.php
File metadata and controls
385 lines (330 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Plugin Name: WP GraphQL
* Plugin URI: https://github.com/dfmedia/wp-graphql
* Description: GraphQL API for WordPress
* Author: Jason Bahl, Digital First Media
* Author URI: http://www.wpgraphql.com
* Version: 0.0.5
* Text Domain: wp-graphql
* Domain Path: /languages/
* Requires at least: 4.7.0
* Tested up to: 4.7.1
* @package WPGraphQL
* @category Core
* @author Digital First Media, Jason Bahl, Ryan Kanner
* @version 0.0.5
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPGraphQL' ) ) :
/**
* This is the one true WPGraphQL class
*/
final class WPGraphQL {
/**
* Stores the instance of the WPGraphQL class
*
* @var WPGraphQL The one true WPGraphQL
* @since 0.0.1
* @access private
*/
private static $instance;
/**
* Stores an array of allowed post types
*
* @var array allowed_post_types
* @since 0.0.5
* @access public
*/
public static $allowed_post_types;
/**
* Stores an array of allowed taxonomies
*
* @var array allowed_taxonomies
* @since 0.0.5
* @access public
*/
public static $allowed_taxonomies;
/**
* The instance of the WPGraphQL object
*
* @return object|WPGraphQL - The one true WPGraphQL
* @since 0.0.1
* @access public
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPGraphQL ) ) {
self::$instance = new WPGraphQL;
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->router = new \WPGraphQL\Router();
}
/**
* Fire off init action
*
* @param WPGraphQL $instance The instance of the WPGraphQL class
*/
do_action( 'graphql_init', self::$instance );
/**
* Return the WPGraphQL Instance
*/
return self::$instance;
}
/**
* Throw error on object clone.
* The whole idea of the singleton design pattern is that there is a single object
* therefore, we don't want the object to be cloned.
*
* @since 0.0.1
* @access public
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'The WPGraphQL class should not be cloned.', 'wp-graphql' ), '0.0.1' );
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1
* @access protected
* @return void
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'De-serializing instances of the WPGraphQL class is not allowed', 'wp-graphql' ), '0.0.1' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 0.0.1
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_VERSION' ) ) {
define( 'WPGRAPHQL_VERSION', '0.0.5' );
}
// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_PLUGIN_FILE', __FILE__ );
}
}
/**
* Include required files.
* Uses composer's autoload
*
* @access private
* @since 0.0.1
* @return void
*/
private function includes() {
// Autoload Required Classes
require_once( WPGRAPHQL_PLUGIN_DIR . 'vendor/autoload.php' );
// This required here as it is not an autoload class
require_once( WPGRAPHQL_PLUGIN_DIR . 'access-functions.php' );
}
/**
* This sets up built-in post_types and taxonomies to show in the GraphQL Schema
*
* @since 0.0.2
* @access public
* @return void
*/
public static function show_in_graphql() {
global $wp_post_types, $wp_taxonomies;
// Adds GraphQL support for attachments
if ( isset( $wp_post_types['attachment'] ) ) {
$wp_post_types['attachment']->show_in_graphql = true;
$wp_post_types['attachment']->graphql_single_name = 'mediaItem';
$wp_post_types['attachment']->graphql_plural_name = 'mediaItems';
}
// Adds GraphQL support for pages
if ( isset( $wp_post_types['page'] ) ) {
$wp_post_types['page']->show_in_graphql = true;
$wp_post_types['page']->graphql_single_name = 'page';
$wp_post_types['page']->graphql_plural_name = 'pages';
}
// Adds GraphQL support for posts
if ( isset( $wp_post_types['post'] ) ) {
$wp_post_types['post']->show_in_graphql = true;
$wp_post_types['post']->graphql_single_name = 'post';
$wp_post_types['post']->graphql_plural_name = 'posts';
}
// Adds GraphQL support for categories
if ( isset( $wp_taxonomies['category'] ) ) {
$wp_taxonomies['category']->show_in_graphql = true;
$wp_taxonomies['category']->graphql_single_name = 'category';
$wp_taxonomies['category']->graphql_plural_name = 'categories';
}
// Adds GraphQL support for tags
if ( isset( $wp_taxonomies['post_tag'] ) ) {
$wp_taxonomies['post_tag']->show_in_graphql = true;
$wp_taxonomies['post_tag']->graphql_single_name = 'postTag';
$wp_taxonomies['post_tag']->graphql_plural_name = 'postTags';
}
}
/**
* Get the post types that are allowed to be used in GraphQL. This gets all post_types that
* are set to show_in_graphql, but allows for external code (plugins/theme) to filter the
* list of allowed_post_types to add/remove additional post_types
*
* @return array
* @since 0.0.4
* @access public
*/
public static function get_allowed_post_types() {
/**
* Get all post_types that have been registered to "show_in_graphql"
*/
$post_types = get_post_types( [ 'show_in_graphql' => true ] );
/**
* Define the $allowed_post_types to be exposed by GraphQL Queries Pass through a filter
* to allow the post_types to be modified (for example if a certain post_type should
* not be exposed to the GraphQL API)
*
* @since 0.0.2
* @param array $post_types Array of post types
* @return array
*/
self::$allowed_post_types = apply_filters( 'graphql_post_entities_allowed_post_types', $post_types );
/**
* Returns the array of allowed_post_types
*/
return self::$allowed_post_types;
}
/**
* Get the taxonomies that are allowed to be used in GraphQL/This gets all taxonomies that
* are set to "show_in_graphql" but allows for external code (plugins/themes) to filter
* the list of allowed_taxonomies to add/remove additional taxonomies
*
* @since 0.0.4
* @access public
* @return array
*/
public static function get_allowed_taxonomies() {
/**
* Get all taxonomies that have been registered to "show_in_graphql"
*/
$taxonomies = get_taxonomies( [ 'show_in_graphql' => true ] );
/**
* Define the $allowed_taxonomies to be exposed by GraphQL Queries Pass through a filter
* to allow the taxonomies to be modified (for example if a certain taxonomy should not
* be exposed to the GraphQL API)
*
* @since 0.0.2
* @return array
* @param array $taxonomies Array of taxonomy objects
*/
self::$allowed_taxonomies = apply_filters( 'graphql_term_entities_allowed_taxonomies', $taxonomies );
/**
* Returns the array of $allowed_taxonomies
*/
return self::$allowed_taxonomies;
}
/**
* This processes a GraphQL request, given a $query and optional $variables
*
* This function is used to resolve the HTTP requests for the GraphQL API, but can also be
* used internally to run GraphQL queries inside WordPress via PHP.
*
* @since 0.0.5
* @param string $query The GraphQL query to be run
* @param array|null $variables Variables to be passed to your GraphQL query
* @return array $result The results of your query
*/
public static function do_graphql_request( $query, $variables = null ) {
/**
* Get the Schema Dependencies
* @since 0.0.5
*/
\WPGraphQL::show_in_graphql();
\WPGraphQL::get_allowed_post_types();
\WPGraphQL::get_allowed_taxonomies();
/**
* Whether it's a GraphQL Request (http or internal)
* @since 0.0.5
*/
if ( ! defined( 'GRAPHQL_REQUEST' ) ) {
define( 'GRAPHQL_REQUEST', true );
}
/**
* Configure the app_context which gets passed down to all the resolvers.
* @since 0.0.4
*/
$app_context = new \WPGraphQL\AppContext();
$app_context->viewer = wp_get_current_user();
$app_context->root_url = get_bloginfo( 'url' );
$app_context->request = ! empty( $_REQUEST ) ? $_REQUEST : null;
/**
* Run an action before generating the schema
* This is a great spot for plugins/themes to hook in to customize the schema.
*
* @since 0.0.5
* @param string $query The query to be run by GraphQL
* @param array|null $variables Variables to be passed to your GraphQL query
* @param AppContext object The AppContext object containing all of the
* information about the context we know at this point
*/
do_action( 'graphql_generate_schema', $query, $variables, $app_context );
/**
* Generate the Schema
*/
$schema = new \GraphQL\Schema( [
'query' => \WPGraphQL\Types::root_query(),
] );
/**
* Executes the query and stores the result
*/
$result = \GraphQL\GraphQL::execute(
$schema,
$query,
null,
$app_context,
(array) $variables
);
/**
* Run an action. This is a good place for debug tools to hook in to log things, etc.
*
* @since 0.0.4
* @param array $result The result of your GraphQL query
* @param Schema object $schema The schema object for the root query
* @param string $query The query that GraphQL ran
* @param array|null $variables Variables to passed to your GraphQL query
*/
do_action( 'graphql_execute', $result, $schema, $query, $variables );
/**
* Return the result of the query
*/
return $result;
}
}
endif;
/**
* Function that instantiates the plugins main class
*
* @since 0.0.1
*/
function graphql_init() {
/**
* Return an instance of the action
*/
return \WPGraphQL::instance();
}
/**
* Instantiate the plugin
* @since 0.0.2
*/
add_action( 'after_setup_theme', 'graphql_init', 10 );