-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-bp-classic.php
323 lines (274 loc) · 8.59 KB
/
class-bp-classic.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
<?php
/**
* BuddyPress Classic backwards compatibility add-on.
*
* @package bp-classic
* @author The BuddyPress Community
* @license GPL-2.0+
* @link https://buddypress.org
*
* @buddypress-plugin
* Plugin Name: BP Classic
* Plugin URI: https://github.com/buddypress/bp-classic
* Description: BuddyPress Classic backwards compatibility add-on.
* Version: 1.4.0
* Author: The BuddyPress Community
* Author URI: https://buddypress.org
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages/
* Text Domain: bp-classic
* GitHub Plugin URI: https://github.com/buddypress/bp-classic
* Requires at least: 5.8
* Requires PHP: 5.6
* Requires Plugins: buddypress
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main Class
*
* @since 1.0.0
*/
final class BP_Classic {
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Used to check if BuddyPress & BP Classic are sharing the same network config.
*
* @var object
*/
protected static $network_config = null;
/**
* Used to store dynamic properties.
*
* @var array
*/
private $data = array();
/**
* Initialize the plugin.
*
* @since 1.0.0
*/
private function __construct() {
// Autoload Classes.
spl_autoload_register( array( $this, 'autoload' ) );
// Load Globals & Functions.
$inc_path = plugin_dir_path( __FILE__ ) . 'inc/';
require $inc_path . 'globals.php';
require $inc_path . 'functions.php';
require $inc_path . 'loader.php';
}
/**
* Class Autoload function
*
* @since 1.0.0
*
* @param string $class The class name.
*/
public function autoload( $class ) {
$name = str_replace( '_', '-', strtolower( $class ) );
if ( 0 !== strpos( $name, 'bp-classic' ) ) {
return;
}
$name_parts = explode( '-', $name );
$component = $name_parts[2];
$path = plugin_dir_path( __FILE__ ) . "inc/{$component}/classes/class-{$name}.php";
// Sanity check.
if ( ! file_exists( $path ) ) {
return;
}
require $path;
}
/**
* Magic method for checking the existence of a plugin global variable.
*
* @since 1.0.0
*
* @param string $key Key to check the set status for.
* @return bool
*/
public function __isset( $key ) {
return isset( $this->data[ $key ] );
}
/**
* Magic method for getting a plugin global variable.
*
* @since 1.0.0
*
* @param string $key Key to return the value for.
* @return mixed
*/
public function __get( $key ) {
$retval = null;
if ( isset( $this->data[ $key ] ) ) {
$retval = $this->data[ $key ];
}
return $retval;
}
/**
* Magic method for setting a plugin global variable.
*
* @since 1.0.0
*
* @param string $key Key to set a value for.
* @param mixed $value Value to set.
*/
public function __set( $key, $value ) {
$this->data[ $key ] = $value;
}
/**
* Magic method for unsetting a plugin global variable.
*
* @since 1.0.0
*
* @param string $key Key to unset a value for.
*/
public function __unset( $key ) {
if ( isset( $this->data[ $key ] ) ) {
unset( $this->data[ $key ] );
}
}
/**
* Checks whether BuddyPress is active.
*
* @since 1.0.0
*/
public static function is_buddypress_supported() {
// Skip the check when running PHP Unit Tests.
if ( function_exists( 'tests_add_filter' ) ) {
return true;
}
$bp_plugin_basename = 'buddypress/bp-loader.php';
$is_buddypress_supported = false;
$sitewide_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
if ( $sitewide_plugins ) {
self::$network_config = new stdClass();
$is_buddypress_supported = isset( $sitewide_plugins[ $bp_plugin_basename ] );
// Informs about whether BuddyPress is network activated or not.
self::$network_config->bp_is_network_active = $is_buddypress_supported;
$bp_classic_plugin_basename = plugin_basename( __FILE__ );
// Informs about whether BP Classic is network activated or not.
$bp_classic_is_network_active = isset( $sitewide_plugins[ $bp_classic_plugin_basename ] );
// If BP Classic is being activated on the network set it as network activated to run the migration script.
if ( ! $bp_classic_is_network_active && is_network_admin() && 'activate_bp-classic/class-bp-classic.php' === current_action() ) {
$bp_classic_is_network_active = true;
}
self::$network_config->bp_classic_is_network_active = $bp_classic_is_network_active;
// BP & BP Classic need to be activated the same way in Multisite configs.
if ( self::$network_config->bp_classic_is_network_active !== self::$network_config->bp_is_network_active ) {
self::$network_config->same = 'no';
return false;
} else {
self::$network_config->same = 'yes';
}
}
if ( ! $is_buddypress_supported ) {
$plugins = (array) get_option( 'active_plugins', array() );
$is_buddypress_supported = in_array( $bp_plugin_basename, $plugins, true );
}
if ( $is_buddypress_supported ) {
$is_buddypress_supported = version_compare( bp_get_version(), '12.0.0-alpha', '>=' );
}
return $is_buddypress_supported;
}
/**
* Displays an admin notice to explain BP Classic requires BuddyPress 12.0.0.
*
* @since 1.0.0
*/
public static function admin_notice() {
if ( ! function_exists( 'buddypress' ) || self::is_buddypress_supported() ) {
return false;
}
if ( 'network_admin_notices' === current_action() && empty( self::$network_config->bp_classic_is_network_active ) ) {
return false;
}
$show_notice = get_site_transient( 'bp_classic_show_notice' );
if ( ! $show_notice ) {
printf(
'<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
sprintf(
/* translators: %s. is current version of BuddyPress. */
esc_html__( 'Friendly reminder: BP Classic only runs when BuddyPress version is 12.0.0 (or up). Your current version of BuddyPress is: %s. Unless you activated BP Classic in anticipation of the 12.0.0 upgrade, you can deactivate it. This information will be shown regularly (every week) and as long as your BuddyPress version is lower than 12.0.0.', 'bp-classic' ),
bp_get_version() // phpcs:ignore
)
);
if ( isset( self::$network_config->same ) && 'no' === self::$network_config->same ) {
$error = __( 'Please, make sure to activate BP Classic at the network level, just like BuddyPress is.', 'bp-classic' );
if ( false === self::$network_config->bp_is_network_active ) {
$error = __( 'Please, make sure to activate BP Classic on the same site of the network BuddyPress is activated on.', 'bp-classic' );
}
printf(
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
esc_html( $error )
);
}
set_site_transient( 'bp_classic_show_notice', true, WEEK_IN_SECONDS );
}
}
/**
* Switch BP Directory pages post type, if needed.
*
* @since 1.0.0
*/
public static function switch_directory_post_type() {
if ( ! self::is_buddypress_supported() ) {
return;
}
require_once plugin_dir_path( __FILE__ ) . 'inc/migrate.php';
$post_type = 'page';
if ( 'page' === bp_core_get_directory_post_type() ) {
$post_type = 'buddypress';
bp_classic_restore_default_theme();
}
// Run the switcher.
bp_classic_switch_directory_post_type( $post_type );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return BP_Classic|false An instance of this class.
* False if BuddyPress is not supported.
*/
public static function start() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
// This plugin is only usable with BuddyPress.
if ( ! self::is_buddypress_supported() ) {
return false;
}
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Start Add-on.
*
* @since 1.0.0
*
* @return BP_Classic|false An instance of this class.
* False if BuddyPress is not supported.
*/
function bp_classic() {
return BP_Classic::start();
}
add_action( 'bp_loaded', 'bp_classic', -1 );
// Displays a notice to inform BP Classic needs to be activated after BuddyPress.
add_action( 'network_admin_notices', array( 'BP_Classic', 'admin_notice' ) );
add_action( 'admin_notices', array( 'BP_Classic', 'admin_notice' ) );
/*
* Use Activation and Deactivation to switch directory pages post type between WP pages
* and BuddyPress one.
*/
register_activation_hook( __FILE__, array( 'BP_Classic', 'switch_directory_post_type' ) );
register_deactivation_hook( __FILE__, array( 'BP_Classic', 'switch_directory_post_type' ) );