forked from funkhaus/wp-gql-gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
149 lines (125 loc) · 4.38 KB
/
plugin.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
141
142
143
144
145
146
147
148
149
<?php
/**
* Plugin Name: WP GQL Gutenberg
* Plugin URI: https://github.com/funkhaus/wp-gql-gutenberg
* Description: Enable blocks in WP GraphQL.
* Author: pristas-peter, funkhaus
* Author URI:
* Version: 0.4.1
* Requires at least: 5.4
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
namespace WPGraphQLGutenberg;
use WPGraphQLGutenberg\Blocks\Registry;
if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}
if ( ! class_exists( 'WPGraphQLGutenberg' ) ) {
final class WPGraphQLGutenberg {
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new WPGraphQLGutenberg();
}
return self::$instance;
}
public $server;
private function setup_autoload() {
/**
* WP_GATSBY_AUTOLOAD can be set to "false" to prevent the autoloader from running.
* In most cases, this is not something that should be disabled, but some environments
* may bootstrap their dependencies in a global autoloader that will autoload files
* before we get to this point, and requiring the autoloader again can trigger fatal errors.
*
* The codeception tests are an example of an environment where adding the autoloader again causes issues
* so this is set to false for tests.
*/
if ( defined( 'WP_GRAPHQL_GUTENBERG_AUTOLOAD' ) && true === WP_GRAPHQL_GUTENBERG_AUTOLOAD ) {
// Autoload Required Classes.
include_once WP_GRAPHQL_GUTENBERG_PLUGIN_DIR . 'vendor/autoload.php';
}
}
private function setup_constants() {
// // Plugin version.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_VERSION' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_VERSION', '0.4.1' );
}
// Plugin Folder Path.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_PLUGIN_DIR' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_PLUGIN_URL' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_PLUGIN_FILE' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_PLUGIN_FILE', __FILE__ );
}
// Whether to autoload the files or not.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_AUTOLOAD' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_AUTOLOAD', true );
}
// Whether to run the plugin in debug mode. Default is false.
if ( ! defined( 'WP_GRAPHQL_GUTENBERG_DEBUG' ) ) {
define( 'WP_GRAPHQL_GUTENBERG_DEBUG', false );
}
}
public function check_dependencies() {
if ( class_exists( 'Classic_Editor' ) ) {
if ( current_user_can( 'manage_options' ) ) {
add_action(
'admin_notices',
function() {
?>
<div class="error notice">
<p>
<?php
esc_html_e(
'Classic Editor is enabled and you can\'t use Gutenberg editor.
Please deactivate Classic Editor plugin to make WP GQL Gutenberg works properly.',
'wp-graphql-gutenberg'
);
?>
</p>
</div>
<?php
}
);
}
}
}
public function init() {
$this->setup_constants();
$this->setup_autoload();
new \WPGraphQLGutenberg\PostTypes\ReusableBlock();
new \WPGraphQLGutenberg\PostTypes\BlockEditorPreview();
new \WPGraphQLGutenberg\Admin\Editor();
new \WPGraphQLGutenberg\Admin\Settings();
new \WPGraphQLGutenberg\Rest\Rest();
new \WPGraphQLGutenberg\Blocks\Plugin();
add_action('admin_init', [$this, 'check_dependencies']);
add_action('init_graphql_request', function () {
new \WPGraphQLGutenberg\Schema\Schema();
$this->server = new \WPGraphQLGutenberg\Server\Server();
});
add_filter('graphql_request_data', function ( $request_data ) {
if ( $this->server->enabled() && $this->server->gutenberg_fields_in_query( $request_data['query'] ) ) {
Registry::update_registry( Registry::normalize( $this->server->get_block_types() ) );
}
return $request_data;
});
// Init Gutenberg ACF if WPGraphQLGutenbergACF plugin is not installed and GraphQLACF installed.
add_action( 'acf/init', function() {
if ( ! class_exists( 'WPGraphQLGutenbergACF' ) && class_exists( 'WPGraphQL\ACF\Config' ) ) {
\WPGraphQLGutenberg\Config::instance();
}
} );
register_deactivation_hook(__FILE__, function () {
\WPGraphQLGutenberg\Server\Server::cleanup();
});
}
}
}
WPGraphQLGutenberg::instance()->init();