This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbp-graphql.php
246 lines (210 loc) · 5.59 KB
/
bp-graphql.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
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
<?php
/**
* BP GraphQL
*
* @package BPGraphQL
* @author The BuddyPress Community
* @copyright 2018 BuddyPress
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: BP GraphQL
* Plugin URI: https://github.com/buddypress/bp-graphql
* Description: GraphQL API for BuddyPress
* Version: 0.0.1-alpha
* Author: The BuddyPress Community
* Author URI: https://buddypress.org/
* Text Domain: bp-graphql
* Domain Path: /languages/
* Requires PHP: 5.5
* Requires WP: 4.9
* Tested up to: 5.0-alpha-43320
* GitHub Plugin URI: https://github.com/buddypress/bp-graphql
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'BPGraphQL' ) ) :
/**
* This is the one true BPGraphQL class
*/
final class BPGraphQL {
/**
* Stores the instance of the BPGraphQL class
*
* @access private
* @since 0.0.1-alpha
* @var BPGraphQL The one true BPGraphQL
*/
private static $instance;
/**
* The instance of the BPGraphQL object
*
* @access public
* @since 0.0.1-alpha
* @return object|BPGraphQL - The one true BPGraphQL
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( is_a( self::$instance, 'BPGraphQL' ) ) ) {
self::$instance = new BPGraphQL();
self::$instance->setup_constants();
self::$instance->dependencies();
self::$instance->includes();
self::$instance->actions();
self::$instance->filters();
}
/**
* Return the BPGraphQL 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.
*
* @access public
* @since 0.0.1-alpha
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'The BPGraphQL class should not be cloned.', 'bp-graphql' ), '0.0.1-alpha' );
}
/**
* Disable unserializing of the class.
*
* @access public
* @since 0.0.1-alpha
* @return void
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'De-serializing instances of the BPGraphQL class is not allowed', 'bp-graphql' ), '0.0.1-alpha' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 0.0.1-alpha
* @return void
*/
private function setup_constants() {
// Plugin Folder Path.
if ( ! defined( 'BPGRAPHQL_PLUGIN_DIR' ) ) {
define( 'BPGRAPHQL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'BPGRAPHQL_PLUGIN_URL' ) ) {
define( 'BPGRAPHQL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'BPGRAPHQL_PLUGIN_FILE' ) ) {
define( 'BPGRAPHQL_PLUGIN_FILE', __FILE__ );
}
}
/**
* Uses composer's autoload to include required files.
*
* @access private
* @since 0.0.1-alpha
* @return void
*/
private function includes() {
// Autoload required classes.
require_once( BPGRAPHQL_PLUGIN_DIR . 'vendor/autoload.php' );
}
/**
* Class dependencies
*
* @access private
* @since 0.0.1-alpha
* @return void
*/
private function dependencies() {
// Checks if BuddyPress is installed.
if ( ! class_exists( 'BuddyPress' ) ) {
add_action( 'admin_notices', array( $this, 'buddypress_missing_notice' ) );
return;
}
// Checks if WPGraphQL is installed.
if ( ! class_exists( 'WPGraphQL' ) ) {
add_action( 'admin_notices', array( $this, 'wpgraphql_missing_notice' ) );
return;
}
}
/**
* Sets up actions to run at certain spots throughout WordPress and the BPGraphQL execution cycle
*
* @access private
* @since 0.0.1-alpha
* @return void
*/
private function actions() {
/**
* Init BPGraphQL after themes have been setup,
* allowing for both plugins and themes to register
* things before graphql_init
*/
add_action( 'after_setup_theme', function() {
/**
* Fire off init action
*
* @param BPGraphQL $instance The instance of the BPGraphQL class
*/
do_action( 'bpgraphql_init', self::$instance );
} );
}
/**
* Hook BuddyPress fields.
*
* @access private
* @since 0.0.1-alpha
* @return void
*/
private function filters() {
add_filter( 'graphql_root_queries', [ '\BPGraphQL\Filters', 'add_fields' ], 10, 1 );
}
/**
* BuddyPress missing notice.
*
* @access public
* @since 0.0.1-alpha
* @return void
*/
public function buddypress_missing_notice() {
?>
<div class="error">
<p><strong><?php esc_html_e( 'BP GraphQL', 'bp-graphql' ); ?></strong> <?php esc_html_e( 'depends on the lastest version of Buddypress to work!', 'bp-graphql' ); ?></p>
</div>
<?php
}
/**
* WPGraphQL missing notice.
*
* @access public
* @since 0.0.1-alpha
* @return void
*/
public function wpgraphql_missing_notice() {
?>
<div class="error">
<p><strong><?php esc_html_e( 'BP GraphQL', 'bp-graphql' ); ?></strong> <?php esc_html_e( 'depends on the lastest version of WPGraphQL to work!', 'bp-graphql' ); ?></p>
</div>
<?php
}
}
endif;
/**
* Function that instantiates the plugins main class
*
* @since 0.0.1-alpha
*/
function bpgraphql_init() {
/**
* Return an instance of the action
*/
return \BPGraphQL::instance();
}
bpgraphql_init();