forked from renatonascalves/wp-graphql-buddypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-graphql-buddypress.php
387 lines (327 loc) · 9.3 KB
/
wp-graphql-buddypress.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
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
386
387
<?php
/**
* WPGraphQL BuddyPress
*
* @package WPGraphQL\Extensions\BuddyPress
* @author Renato Alves
* @version 0.0.1-alpha
* @license GPL-3
*
* @wordpress-plugin
* Plugin Name: WPGraphQL BuddyPress
* Plugin URI: https://github.com/wp-graphql/wp-graphql-buddypress
* GitHub Plugin URI: https://github.com/wp-graphql/wp-graphql-buddypress
* Description: Adds BuddyPress functionality to the WPGraphQL schema.
* Version: 0.0.1-alpha
* Author: Renato Alves
* Author URI: https://ralv.es
* Text Domain: wp-graphql-buddypress
* Domain Path: /languages/
* Requires PHP: 7.4
* Requires WP: 5.9
* Tested up to: 5.9
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_GraphQL_BuddyPress' ) ) :
/**
* This is the one true WP_GraphQL_BuddyPress class
*/
final class WP_GraphQL_BuddyPress {
/**
* Stores the instance of the WP_GraphQL_BuddyPress class
*
* @since 0.0.1-alpha
*
* @var WP_GraphQL_BuddyPress The one true WP_GraphQL_BuddyPress
*/
private static $instance;
/**
* The instance of the WP_GraphQL_BuddyPress object
*
* @since 0.0.1-alpha
*
* @return WP_GraphQL_BuddyPress The one true WP_GraphQL_BuddyPress
*/
public static function instance(): self {
if ( ! isset( self::$instance ) && ! ( is_a( self::$instance, __CLASS__ ) ) ) {
self::$instance = new self();
self::$instance->setup_constants();
self::$instance->dependencies();
if ( self::$instance->includes() ) {
self::$instance->actions();
self::$instance->filters();
}
}
/**
* Fire off init action.
*
* @param WP_GraphQL_BuddyPress $instance The instance of the WP_GraphQL_BuddyPress class
*/
do_action( 'graphql_buddypress_init', self::$instance );
// Return the WP_GraphQL_BuddyPress 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-alpha
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong(
__FUNCTION__,
esc_html__(
'The WP_GraphQL_BuddyPress class should not be cloned.',
'wp-graphql-buddypress'
),
'0.0.1-alpha'
);
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1-alpha
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong(
__FUNCTION__,
esc_html__(
'De-serializing instances of the WP_GraphQL_BuddyPress class is not allowed.',
'wp-graphql-buddypress'
),
'0.0.1-alpha'
);
}
/**
* Setup plugin constants.
*
* @since 0.0.1-alpha
*/
private function setup_constants(): void {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_BUDDYPRESS_VERSION' ) ) {
define( 'WPGRAPHQL_BUDDYPRESS_VERSION', '0.0.1-alpha' );
}
// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_BUDDYPRESS_PLUGIN_FILE', __FILE__ );
}
// Whether to autoload the files or not.
if ( ! defined( 'WPGRAPHQL_BUDDYPRESS_AUTOLOAD' ) ) {
define( 'WPGRAPHQL_BUDDYPRESS_AUTOLOAD', true );
}
}
/**
* Uses composer's autoload to include required files.
*
* @since 0.0.1-alpha
*
* @return bool
*/
private function includes(): bool {
// Autoload Required Classes.
if ( defined( 'WPGRAPHQL_BUDDYPRESS_AUTOLOAD' ) && false !== WPGRAPHQL_BUDDYPRESS_AUTOLOAD ) {
if ( file_exists( WPGRAPHQL_BUDDYPRESS_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
require_once WPGRAPHQL_BUDDYPRESS_PLUGIN_DIR . 'vendor/autoload.php';
}
// Bail if installed incorrectly.
if ( ! class_exists( '\WPGraphQL\Extensions\BuddyPress\TypeRegistry' ) ) {
add_action( 'admin_notices', [ $this, 'wp_graphql_buddypress_missing_notice' ] );
return false;
}
}
return true;
}
/**
* Class dependencies.
*
* @since 0.0.1-alpha
*/
private function dependencies(): void {
// Checks if BuddyPress is installed.
if ( ! class_exists( 'BuddyPress' ) ) {
add_action( 'admin_notices', [ $this, 'buddypress_missing_notice' ] );
return;
}
// Checks if WPGraphQL is installed.
if ( ! class_exists( 'WPGraphQL' ) ) {
add_action( 'admin_notices', [ $this, 'wpgraphql_missing_notice' ] );
return;
}
}
/**
* WPGraphQL BuddyPress missing notice.
*
* @since 0.0.1-alpha
*/
public function wp_graphql_buddypress_missing_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="notice notice-error">
<p>
<?php esc_html_e( 'WPGraphQL BuddyPress appears to have been installed without its dependencies. It will not work properly until dependencies are installed. This likely means you have cloned WPGraphQL BuddyPress from Github and need to run the command `composer install`.', 'wp-graphql-buddypress' ); ?>
</p>
</div>
<?php
}
/**
* BuddyPress missing notice.
*
* @since 0.0.1-alpha
*/
public function buddypress_missing_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="notice notice-error">
<p><strong><?php esc_html_e( 'WP GraphQL BuddyPress', 'wp-graphql-buddypress' ); ?></strong> <?php esc_html_e( 'depends on the lastest version of Buddypress to work!', 'wp-graphql-buddypress' ); ?></p>
</div>
<?php
}
/**
* WPGraphQL missing notice.
*
* @since 0.0.1-alpha
*/
public function wpgraphql_missing_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="notice notice-error">
<p><strong><?php esc_html_e( 'WP GraphQL BuddyPress', 'wp-graphql-buddypress' ); ?></strong> <?php esc_html_e( 'depends on the lastest version of WPGraphQL to work!', 'wp-graphql-buddypress' ); ?></p>
</div>
<?php
}
/**
* Sets up actions.
*
* @since 0.0.1-alpha
*/
private function actions(): void {
// Setup actions.
\WPGraphQL\Extensions\BuddyPress\TypeRegistry::add_actions();
}
/**
* Set up filters.
*
* @since 0.0.1-alpha
*/
private function filters(): void {
// Setup filters.
\WPGraphQL\Extensions\BuddyPress\TypeRegistry::add_filters();
/**
* Allow regular BuddyPress members to delete their own account, if allowed.
*
* Multisite uses this hook.
*/
add_filter(
'map_meta_cap',
function( $caps, $cap, $user_id ) {
// Apply to GraphQL request only.
if ( false === is_graphql_request() ) {
return $caps;
}
// Check for settings to confirm if users can delete their own accounts.
if ( true === bp_disable_account_deletion() ) {
return $caps;
}
// Check if user is logged in.
if ( false === is_user_logged_in() ) {
return $caps;
}
// Confirm user to check against with logged in user.
if ( bp_loggedin_user_id() !== $user_id ) {
return $caps;
}
foreach ( $caps as $key => $capability ) {
if ( 'do_not_allow' !== $capability ) {
continue;
}
switch ( $cap ) {
case 'delete_user':
case 'delete_users':
$caps[ $key ] = 'delete_users';
break;
}
}
return $caps;
},
1,
3
);
/**
* Allow regular BuddyPress members to delete their own account, if allowed.
*/
add_filter(
'user_has_cap',
function ( $caps, $cap, $args ) {
// Apply to GraphQL request only.
if ( false === is_graphql_request() ) {
return $caps;
}
// Required field.
if ( empty( $args[0] ) ) {
return $caps;
}
// Bail if not checking the 'delete_users' cap.
if ( 'delete_users' !== $args[0] ) {
return $caps;
}
// Bail if already with permissions (eg.: admins).
if ( isset( $caps['delete_users'] ) && true === $caps['delete_users'] ) {
return $caps;
}
// Check for settings to confirm if users can delete their own accounts.
if ( true === bp_disable_account_deletion() ) {
return $caps;
}
// Check if user is logged in.
if ( false === is_user_logged_in() ) {
return $caps;
}
// Confirm user to check against with logged in user.
if ( isset( $args[2] ) && bp_loggedin_user_id() !== absint( $args[2] ) ) {
return $caps;
}
// Allow for this one.
$caps[ $args[0] ] = true;
return $caps;
},
10,
3
);
}
}
endif;
/**
* Function that instantiates the plugin main class.
*
* @since 0.0.1-alpha
*
* @return WP_GraphQL_BuddyPress The one true WP_GraphQL_BuddyPress
*/
function wp_graphql_buddypress_init(): \WP_GraphQL_BuddyPress {
// Return an instance of the action.
return \WP_GraphQL_BuddyPress::instance();
}
add_action( 'graphql_init', 'wp_graphql_buddypress_init' );