-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuddydrive.php
414 lines (331 loc) · 12 KB
/
buddydrive.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/*
Plugin Name: BuddyDrive
Plugin URI: https://wordpress.org/plugins/buddydrive/
Description: A plugin to share files, the BuddyPress way!
Version: 2.1.1
Author: mrpritchett
Author URI: http://pritchett.media
License: GPLv2
Text Domain: buddydrive
Domain Path: /languages/
*/
// Create a helper function for easy SDK access.
function buddydrive_fs() {
global $buddydrive_fs;
if ( ! isset( $buddydrive_fs ) ) {
// Include Freemius SDK.
require_once dirname(__FILE__) . '/freemius/start.php';
$buddydrive_fs = fs_dynamic_init( array(
'id' => '619',
'slug' => 'buddydrive',
'type' => 'plugin',
'public_key' => 'pk_c302f2a54e3a828af10c04778ebc5',
'is_premium' => false,
'has_addons' => false,
'has_paid_plans' => false,
'menu' => array(
'slug' => 'buddydrive-files',
'first-path' => 'index.php?page=buddydrive-about',
'account' => false,
'contact' => false,
),
) );
}
return $buddydrive_fs;
}
// Init Freemius.
buddydrive_fs();
// Signal that SDK was initiated.
do_action( 'buddydrive_fs_loaded' );
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'BuddyDrive' ) ) :
/**
* Main BuddyDrive Class
*
* Inspired by bbpress 2.3
*/
class BuddyDrive {
private $data;
private static $instance;
/**
* Required BuddyPress version for the plugin.
*
* @package BuddyDrive
* @since 1.2.0
*
* @var string
*/
public static $required_bp_version = '2.5.0';
/**
* BuddyPress config.
*
* @package BuddyDrive
* @since 1.2.0
*
* @var array
*/
public static $bp_config = array();
/**
* Main BuddyDrive Instance
*
* Inspired by bbpress 2.3
*
* Avoids the use of a global
*
* @package BuddyDrive
* @since 1.0
*
* @uses BuddyDrive::setup_globals() to set the global needed
* @uses BuddyDrive::includes() to include the required files
* @uses BuddyDrive::setup_actions() to set up the hooks
* @return object the instance
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new BuddyDrive;
self::$instance->setup_globals();
self::$instance->includes();
self::$instance->setup_actions();
}
return self::$instance;
}
private function __construct() { /* Do nothing here */ }
public function __clone() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'buddydrive' ), '1.2.0' ); }
public function __wakeup() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'buddydrive' ), '1.2.0' ); }
public function __isset( $key ) { return isset( $this->data[$key] ); }
public function __get( $key ) { return isset( $this->data[$key] ) ? $this->data[$key] : null; }
public function __set( $key, $value ) { $this->data[$key] = $value; }
public function __unset( $key ) { if ( isset( $this->data[$key] ) ) unset( $this->data[$key] ); }
public function __call( $name = '', $args = array() ) { unset( $name, $args ); return null; }
/**
* Some usefull vars
*
* @package BuddyDrive
* @since 1.0
*
* @uses plugin_basename()
* @uses plugin_dir_path() to build BuddyDrive plugin path
* @uses plugin_dir_url() to build BuddyDrive plugin url
*/
private function setup_globals() {
/** Version ***********************************************************/
$this->version = '2.1.1';
$this->db_version = 211;
/** Paths *************************************************************/
// Setup some base path and URL information
$this->file = __FILE__;
$this->basename = apply_filters( 'buddydrive_plugin_basename', plugin_basename( $this->file ) );
$this->plugin_dir = apply_filters( 'buddydrive_plugin_dir_path', plugin_dir_path( $this->file ) );
$this->plugin_url = apply_filters( 'buddydrive_plugin_dir_url', plugin_dir_url ( $this->file ) );
// Includes
$this->includes_dir = apply_filters( 'buddydrive_includes_dir', trailingslashit( $this->plugin_dir . 'includes' ) );
$this->includes_url = apply_filters( 'buddydrive_includes_url', trailingslashit( $this->plugin_url . 'includes' ) );
$this->upload_dir = false;
$this->upload_url = false;
$this->images_url = apply_filters( 'buddydrive_images_url', trailingslashit( $this->includes_url . 'images' ) );
// Languages
$this->lang_dir = apply_filters( 'buddydrive_lang_dir', trailingslashit( $this->plugin_dir . 'languages' ) );
// BuddyDrive slug and name
$this->buddydrive_slug = apply_filters( 'buddydrive_slug', 'buddydrive' );
$this->buddydrive_name = apply_filters( 'buddydrive_name', 'BuddyDrive' );
// Post type identifiers
$this->buddydrive_file_post_type = apply_filters( 'buddydrive_file_post_type', 'buddydrive-file' );
$this->buddydrive_folder_post_type = apply_filters( 'buddydrive_folder_post_type', 'buddydrive-folder' );
/** Misc **************************************************************/
$this->domain = 'buddydrive';
$this->errors = new WP_Error(); // Feedback
$this->users_file_count = array();
}
/**
* includes the needed files
*
* @package BuddyDrive
* @since 1.0
*
* @uses is_admin() for the settings files
*/
private function includes() {
require( $this->includes_dir . 'buddydrive-actions.php' );
require( $this->includes_dir . 'buddydrive-functions.php' );
if( is_admin() ){
require( $this->includes_dir . 'admin/buddydrive-admin.php' );
}
}
/**
* It's about hooks!
*
* @package BuddyDrive
* @since 1.0
*
* The main hook used is bp_include to load our custom BuddyPress component
*/
private function setup_actions() {
// Add actions to plugin activation and deactivation hooks
add_action( 'activate_' . $this->basename, 'buddydrive_activation' );
add_action( 'deactivate_' . $this->basename, 'buddydrive_deactivation' );
add_action( 'bp_loaded', array( $this, 'load_textdomain' ) );
add_action( 'bp_include', array( $this, 'load_component' ) );
do_action_ref_array( 'buddydrive_after_setup_actions', array( &$this ) );
}
/**
* Loads the translation
*
* @package BuddyDrive
* @since 1.0
* @uses get_locale()
* @uses load_textdomain()
*/
public function load_textdomain() {
// try to get locale
$locale = apply_filters( 'buddydrive_load_textdomain_get_locale', get_locale(), $this->domain );
$mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
// Setup paths to a buddydrive subfolder in WP LANG DIR
$mofile_global = WP_LANG_DIR . '/buddydrive/' . $mofile;
// Look in global /wp-content/languages/buddydrive folder
if ( ! load_textdomain( $this->domain, $mofile_global ) ) {
// Look in local /wp-content/plugins/buddydrive/languages/ folder
// or /wp-content/languages/plugins/
load_plugin_textdomain( $this->domain, false, basename( $this->plugin_dir ) . '/languages' );
}
}
/**
* Finally, let's safely load our component
*
* @package BuddyDrive
* @since 1.0
*/
public function load_component() {
if ( self::bail() ) {
add_action( self::$bp_config['network_admin'] ? 'network_admin_notices' : 'admin_notices', array( $this, 'warning' ) );
} else {
require( $this->includes_dir . 'buddydrive-component.php' );
}
}
/** Utilities *****************************************************************************/
/**
* Checks BuddyPress version
*
* @package BuddyDrive
* @since 1.2.0
*/
public static function version_check() {
// taking no risk
if ( ! defined( 'BP_VERSION' ) )
return false;
return version_compare( BP_VERSION, self::$required_bp_version, '>=' );
}
/**
* Checks if your plugin's config is similar to BuddyPress
*
* @package BuddyDrive
* @since 1.2.0
*/
public static function config_check() {
/**
* blog_status : true if your plugin is activated on the same blog
* network_active : true when your plugin is activated on the network
* network_status : BuddyPress & your plugin share the same network status
*/
self::$bp_config = array(
'blog_status' => false,
'network_active' => false,
'network_status' => true,
'network_admin' => false
);
$buddypress = false;
if ( function_exists( 'buddypress' ) ) {
$buddypress = buddypress()->basename;
}
if ( $buddypress && get_current_blog_id() == bp_get_root_blog_id() ) {
self::$bp_config['blog_status'] = true;
}
$network_plugins = get_site_option( 'active_sitewide_plugins', array() );
// No Network plugins
if ( empty( $network_plugins ) )
return self::$bp_config;
$buddydrive = plugin_basename( __FILE__ );
// Looking for BuddyDrive
$check = array( $buddydrive );
// And for BuddyPress if set
if ( ! empty( $buddypress ) )
$check = array_merge( array( $buddypress ), $check );
// Are they active on the network ?
$network_active = array_diff( $check, array_keys( $network_plugins ) );
// If result is 1, your plugin is network activated
// and not BuddyPress or vice & versa. Config is not ok
if ( count( $network_active ) == 1 )
self::$bp_config['network_status'] = false;
self::$bp_config['network_active'] = isset( $network_plugins[ $buddydrive ] );
// We need to know if the BuddyPress is network activated to choose the right
// notice ( admin or network_admin ) to display the warning message.
self::$bp_config['network_admin'] = ! empty( $buddypress ) && isset( $network_plugins[ $buddypress ] );
return self::$bp_config;
}
/**
* Bail if BuddyPress config is different than this plugin
*
* @package BuddyDrive
* @since 1.2.0
*/
public static function bail() {
$retval = false;
$config = self::config_check();
if ( ! self::version_check() || ! $config['blog_status'] || ! $config['network_status'] )
$retval = true;
return $retval;
}
/**
* Display a warning message to admin
*
* @package BuddyDrive
* @since 1.2.0
*/
public function warning() {
$warnings = $resolve = array();
if ( ! self::version_check() ) {
$warnings[] = sprintf( esc_html__( 'BuddyDrive requires at least version %s of BuddyPress.', 'buddydrive' ), self::$required_bp_version );
$resolve[] = sprintf( esc_html__( 'Upgrade BuddyPress to at least version %s', 'buddydrive' ), self::$required_bp_version );
}
if ( ! empty( self::$bp_config ) ) {
$config = self::$bp_config;
} else {
$config = self::config_check();
}
if ( ! $config['blog_status'] ) {
$warnings[] = esc_html__( 'BuddyDrive requires to be activated on the blog where BuddyPress is activated.', 'buddydrive' );
$resolve[] = esc_html__( 'Activate BuddyDrive on the same blog than BuddyPress', 'buddydrive' );
}
if ( ! $config['network_status'] ) {
$warnings[] = esc_html__( 'BuddyDrive and BuddyPress need to share the same network configuration.', 'buddydrive' );
$resolve[] = esc_html__( 'Make sure BuddyDrive is activated at the same level than BuddyPress on the network', 'buddydrive' );
}
if ( ! empty( $warnings ) ) {
// Give some more explanations to administrator
if ( is_super_admin() ) {
$deactivate_link = ! empty( $config['network_active'] ) ? network_admin_url( 'plugins.php' ) : admin_url( 'plugins.php' );
$deactivate_link = '<a href="' . esc_url( $deactivate_link ) . '">' . esc_html__( 'deactivate', 'buddydrive' ) . '</a>';
$resolve_message = '<ol><li>' . sprintf( __( 'You should %s BuddyDrive', 'buddydrive' ), $deactivate_link ) . '</li>';
foreach ( (array) $resolve as $step ) {
$resolve_message .= '<li>' . $step . '</li>';
}
if ( $config['network_status'] && $config['blog_status'] )
$resolve_message .= '<li>' . esc_html__( 'Once done try to activate BuddyDrive again.', 'buddydrive' ) . '</li></ol>';
$warnings[] = $resolve_message;
}
?>
<div id="message" class="error">
<?php foreach ( $warnings as $warning ) : ?>
<p><?php echo $warning; ?></p>
<?php endforeach ; ?>
</div>
<?php
}
}
}
function buddydrive() {
return buddydrive::instance();
}
buddydrive();
endif;