This repository has been archived by the owner on Nov 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathwp-graphiql.php
140 lines (117 loc) · 5.49 KB
/
wp-graphiql.php
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
<?php
/**
* Plugin Name: WP GraphiQL
* Plugin URI: https://wpgraphql.com
* Description: This plugin provides the GraphiQL IDE as an admin page in WordPress, allowing the GraphQL WPGraphQL
* schema to be browsed from within WordPress.
* Author: WPGraphQL, Digital First Media, Jason Bahl
* Author URI: http://wpgraphql.com
* Text Domain: wp-graphiql
* Domain Path: /languages
* Version: 1.0.1
*
* @package WPGraphiql
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPGraphiQL' ) ) :
class WPGraphiQL {
public function init() {
if ( ! defined( 'WPGRAPHIQL_PLUGIN_DIR' ) ) {
define( 'WPGRAPHIQL_PLUGIN_DIR', plugin_dir_url( __FILE__ ) );
}
add_action( 'admin_menu', [ $this, 'register_admin_page' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_react_app' ] );
}
public function is_wpgraphql_active() {
return class_exists( 'WPGraphQL' );
}
public function register_admin_page() {
add_menu_page(
__( 'GraphiQL', 'wp-graphiql' ),
'GraphiQL',
'manage_options',
'wp-graphiql/wp-graphiql.php',
[ $this, 'render_admin_page' ],
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjA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uNyAzMS0yMi40IDQ3LjctMTIuOCAxNi43IDkuNyAyMi40IDMxIDEyLjggNDcuN00zMC41IDI5Ny45Yy05LjYtMTYuNy0zLjktMzggMTIuOC00Ny43IDE2LjctOS42IDM4LTMuOSA0Ny43IDEyLjggOS42IDE2LjcgMy45IDM4LTEyLjggNDcuNy0xNi44IDkuNi0zOC4xIDMuOS00Ny43LTEyLjhNMzA5LjEgMTM3Yy05LjYtMTYuNy0zLjktMzggMTIuOC00Ny43IDE2LjctOS42IDM4LTMuOSA0Ny43IDEyLjggOS42IDE2LjcgMy45IDM4LTEyLjggNDcuNy0xNi43IDkuNi0zOC4xIDMuOS00Ny43LTEyLjhNMjAwIDM5NS44Yy0xOS4zIDAtMzQuOS0xNS42LTM0LjktMzQuOSAwLTE5LjMgMTUuNi0zNC45IDM0LjktMzQuOSAxOS4zIDAgMzQuOSAxNS42IDM0LjkgMzQuOSAwIDE5LjItMTUuNiAzNC45LTM0LjkgMzQuOU0yMDAgNzRjLTE5LjMgMC0zNC45LTE1LjYtMzQuOS0zNC45IDAtMTkuMyAxNS42LTM0LjkgMzQuOS0zNC45IDE5LjMgMCAzNC45IDE1LjYgMzQuOSAzNC45IDAgMTkuMy0xNS42IDM0LjktMzQuOSAzNC45Ii8+PC9zdmc+'
);
}
public function render_admin_page() {
if ( $this->is_wpgraphql_active() ) {
echo '<div class="wrap"><div id="wp-graphiql" class="graphiql-container">Loading ...</div></div>';
} else {
echo '<div class="wrap"><h1>This plugin requires WPGraphQL to be installed to work. Please install WPGraphQL (https://github.com/wp-graphql/wp-graphql) and visit this page again.</h1></div>';
}
}
/**
* Gets the contents of the Create React App manifest file
*
* @return array|bool|string
*/
public function get_app_manifest() {
$manifest = file_get_contents( dirname( __FILE__ ) . '/assets/app/build/asset-manifest.json' );
$manifest = (array) json_decode( $manifest );
return $manifest;
}
/**
* Gets the path to the stylesheet compiled by Create React App
*
* @return string
*/
public function get_app_stylesheet() {
$manifest = $this->get_app_manifest();
if ( empty( $manifest['main.css'] ) ) {
return '';
}
return WPGRAPHIQL_PLUGIN_DIR . 'assets/app/build/' . $manifest['main.css'];
}
/**
* Gets the path to the built javascript file compiled by Create React App
*
* @return string
*/
public function get_app_script() {
$manifest = $this->get_app_manifest();
if ( empty( $manifest['main.js'] ) ) {
return '';
}
return WPGRAPHIQL_PLUGIN_DIR . 'assets/app/build/' . $manifest['main.js'];
}
public function get_app_script_helpers() {
$manifest = $this->get_app_manifest();
if ( empty( $manifest['main.js'] ) ) {
return '';
}
return WPGRAPHIQL_PLUGIN_DIR . 'assets/js/wp-graphiql-helpers.js';
}
/**
* Enqueues the stylesheet and js for the WPGraphiQL app
*/
public function enqueue_react_app() {
/**
* Only enqueue the assets on the proper admin page, and only if WPGraphQL is also active
*/
if ( strpos( get_current_screen()->id, 'wp-graphiql/wp-graphiql' ) && $this->is_wpgraphql_active() ) {
wp_enqueue_style( 'wp-graphiql', $this->get_app_stylesheet(), array(), false, false );
wp_enqueue_script( 'wp-graphiql-helpers', $this->get_app_script_helpers(), array( 'jquery' ), false, true );
wp_enqueue_script( 'wp-graphiql', $this->get_app_script(), array(), false, true );
/**
* Create a nonce
*/
wp_localize_script(
'wp-graphiql',
'wpGraphiQLSettings',
array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'graphqlEndpoint' => trailingslashit( site_url() ) . 'index.php?' . \WPGraphQL\Router::$route,
)
);
}
}
}
endif; // End if class_exists()
add_action( 'plugins_loaded', function() {
$wp_graphiql = new WPGraphiQL();
$wp_graphiql->init();
} );