-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp-api.php
executable file
·264 lines (205 loc) · 6.29 KB
/
bp-api.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
<?php
/*
Plugin Name: BP API
Plugin URI: https://github.com/zarcode/bp-api
Description: json API for BuddyPress. This plugin creates json api endpoints for https://github.com/WP-API
Author: zarcode, vapvarun
Version: 0.1
Author URI: https://github.com/zarcode/bp-api
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
if ( !class_exists( 'BuddyPress_API' ) ) :
/**
* Main BuddyPress API Class
*/
class BuddyPress_API {
/**
* Main BuddyPress API Instance.
*/
public static function instance() {
// Store the instance locally to avoid private static replication
static $instance = null;
// Only run these methods if they haven't been run previously
if ( null === $instance ) {
$instance = new BuddyPress_API;
$instance->constants();
$instance->actions();
}
// Always return the instance
return $instance;
}
/**
* A dummy constructor to prevent BuddyPress API from being loaded more than once.
*
*/
private function __construct() { /* Do nothing here */ }
/**
* Bootstrap constants.
*
*/
private function constants() {
// define plugin translation string
if ( ! defined( 'BP_API_PLUGIN_SLUG' ) ) {
define( 'BP_API_PLUGIN_SLUG', 'bp_api' );
}
// define api endpint prefix
if ( ! defined( 'BP_API_SLUG' ) ) {
define( 'BP_API_SLUG', 'bp' );
}
// Define a constant that can be checked to see if the component is installed or not.
if ( ! defined( 'BP_API_IS_INSTALLED' ) ) {
define( 'BP_API_IS_INSTALLED', 1 );
}
// Define a constant that will hold the current version number of the component
// This can be useful if you need to run update scripts or do compatibility checks in the future
if ( ! defined( 'BP_API_VERSION' ) ) {
define( 'BP_API_VERSION', '0.1' );
}
// Define a constant that we can use to construct file paths and url
if ( ! defined( 'BP_API_PLUGIN_DIR' ) ) {
define( 'BP_API_PLUGIN_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
}
if ( ! defined( 'BP_API_PLUGIN_URL' ) ) {
$plugin_url = plugin_dir_url( __FILE__ );
// If we're using https, update the protocol. Workaround for WP13941, WP15928, WP19037.
if ( is_ssl() )
$plugin_url = str_replace( 'http://', 'https://', $plugin_url );
define( 'BP_API_PLUGIN_URL', $plugin_url );
}
}
/**
* BP-API Actions
*
* Includes actions for init, activation/deactivation hooks, and admin notices.
*
*/
private function actions() {
register_activation_hook( __FILE__, array( $this, 'bp_api_activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'bp_api_deactivate' ) );
add_action( 'plugins_loaded', array( $this, 'check_if_exists' ), 9999 );
add_action( 'bp_include', array( $this, 'bp_api_include' ) );
}
/**
* check_if_exists function.
*
* checks for plugin dependency and deactivates if not found
*
* @access public
* @return void
*/
public function check_if_exists() {
// is BuddyPress plugin active? If not, throw a notice and deactivate
if ( !class_exists( 'BuddyPress' ) ) {
add_action( 'all_admin_notices', array( $this, 'bp_api_buddypress_required' ) );
return;
}
// is JSON API plugin active? If not, throw a notice and deactivate
if ( !class_exists('WP_REST_Server') ) {
add_action( 'all_admin_notices', array( $this, 'bp_api_wp_api_required' ) );
return;
}
}
/**
* bp_api_init function.
*
* much files, so include
*
* @access public
* @return void
*/
public function bp_api_include() {
// requires BP 2.0 or greater.
if ( version_compare( BP_VERSION, '2.0', '>' ) ) {
include_once( dirname( __FILE__ ) . '/endpoints/bp-api-core.php' );
include_once( dirname( __FILE__ ) . '/endpoints/bp-api-activity.php' );
include_once( dirname( __FILE__ ) . '/endpoints/bp-api-xprofile.php' );
include_once( dirname( __FILE__ ) . '/endpoints/bp-api-message.php' );
}
add_action( 'rest_api_init', array( $this, 'bp_api_init' ), 0 );
}
/**
* bp_api_activate function.
*
* @access public
* @return void
*/
public function bp_api_activate() {
}
/**
* bp_api_deactivate function.
*
* @access public
* @return void
*/
public function bp_api_deactivate() {
}
/**
* bp_api_buddypress_required function.
*
* @access public
* @return void
*/
public function bp_api_buddypress_required() {
echo '<div id="message" class="error"><p>'. sprintf( __( '%1$s requires the <a href="https://buddypress.org/">BuddyPress plugin</a> to be installed/activated. %1$s has been deactivated.', 'bp-api' ), 'BuddyPress API' ) .'</p></div>';
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
/**
* bp_api_wp_api_required function.
*
* @access public
* @return void
*/
public function bp_api_wp_api_required() {
echo '<div id="message" class="error"><p>'. sprintf( __( '%1$s requires the <a href="https://github.com/WP-API/WP-API/releases/tag/2.0-beta3">WP API V2 plugin</a> to be installed/activated. %1$s has been deactivated.', 'bp-api' ), 'BuddyPress API' ) .'</p></div>';
deactivate_plugins( plugin_basename( __FILE__ ), true );
}
/**
* create_bp_endpoints function.
*
* adds BuddyPress data endpoints to WP-API
*
* @access public
* @return void
*/
public function bp_api_init() {
/*
* BP Core
*/
$bp_api_core = new BP_API_Core;
$bp_api_core->register_routes();
/*
* BP Activity
*/
if ( bp_is_active( 'activity' ) ) {
$bp_api_activity = new BP_API_Activity;
$bp_api_activity->register_routes();
}
/*
* BP Message
*/
if ( bp_is_active( 'messages' ) ) {
$bp_api_messages = new BP_API_Messages;
$bp_api_messages->register_routes();
}
/*
* BP xProfile
*/
if ( bp_is_active( 'xprofile' ) ) {
$bp_api_xprofile = new BP_API_xProfile;
register_rest_route( BP_API_SLUG, '/xprofile', array(
'methods' => 'GET',
'callback' => array( $bp_api_xprofile, 'get_items' ),
) );
register_rest_route( BP_API_SLUG, '/xprofile/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => array( $bp_api_xprofile, 'get_item' ),
) );
}
}
}
endif;
function bp_api() {
return BuddyPress_API::instance();
}
bp_api();