-
Notifications
You must be signed in to change notification settings - Fork 12
/
class-bp-attachments.php
203 lines (175 loc) · 5.08 KB
/
class-bp-attachments.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
<?php
/**
* BP Attachments is a BuddyPress add-on to manage your community members media.
*
* @package BP Attachments
* @author The BuddyPress community
* @license GPL-2.0+
* @link http://buddypress.org
*
* @buddypress-plugin
* Plugin Name: BP Attachments
* Plugin URI: https://github.com/buddypress/bp-attachments
* Description: BP Attachments is a BuddyPress add-on to manage your community members media.
* Version: 1.3.0-alpha
* Author: The BuddyPress Community
* Author URI: http://buddypress.org/community/members/
* Text Domain: bp-attachments
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages/
* GitHub Plugin URI: https://github.com/buddypress/bp-attachments
* Requires at least: 6.1
* Requires PHP: 5.6
* Requires Plugins: buddypress
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* BP Attachments Main Class
*
* @since 1.0.0
*/
class BP_Attachments {
/**
* Plugin Main Instance.
*
* @since 1.0.0
* @var object
*/
protected static $instance = null;
/**
* Checks whether BuddyPress is active.
*
* @since 1.0.0
*/
public static function is_buddypress_active() {
$bp_plugin_basename = 'buddypress/bp-loader.php';
$is_buddypress_active = false;
$sitewide_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
if ( $sitewide_plugins ) {
$is_buddypress_active = isset( $sitewide_plugins[ $bp_plugin_basename ] );
}
if ( ! $is_buddypress_active ) {
$plugins = (array) get_option( 'active_plugins', array() );
$is_buddypress_active = in_array( $bp_plugin_basename, $plugins, true );
}
return $is_buddypress_active;
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*/
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 the genuine BuddyPress.
if ( ! self::is_buddypress_active() && ! defined( 'BP_ATTACHMENTS_TESTS_DIR' ) ) {
return false;
}
self::$instance = new self();
}
return self::$instance;
}
/**
* Include the plugin files.
*
* @since 1.0.0
*/
private function __construct() {
// Autoload Classes.
spl_autoload_register( array( $this, 'autoload' ) );
// Load the Component's loader.
require plugin_dir_path( __FILE__ ) . 'bp-attachments/bp-attachments-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-attachments' ) && 'bp-medium' !== $name ) {
return;
}
$path = plugin_dir_path( __FILE__ ) . "bp-attachments/classes/class-{$name}.php";
// Sanity check.
if ( ! file_exists( $path ) ) {
return;
}
require $path;
}
/**
* Activate the BP Attachments Component.
*
* @since 1.0.0
*/
public static function activate() {
if ( ! function_exists( 'buddypress' ) ) {
return;
}
$active_components = array_merge(
bp_get_option( 'bp-active-components', array() ),
// Use the Component's id.
array( 'attachments' => 1 )
);
bp_update_option( 'bp-active-components', $active_components );
}
/**
* Deactivate the BP Attachments Component.
*
* @since 1.0.0
*/
public static function deactivate() {
if ( ! function_exists( 'buddypress' ) ) {
return;
}
$active_components = array_diff_key(
bp_get_option( 'bp-active-components', array() ),
// Use the Component's id.
array( 'attachments' => 1 )
);
bp_update_option( 'bp-active-components', $active_components );
}
/**
* Displays an admin notice to explain how to activate BP Attachments.
*
* @since 1.0.0
*/
public static function admin_notice() {
if ( self::is_buddypress_active() ) {
return false;
}
$bp_plugin_link = sprintf( '<a href="%s">BuddyPress</a>', esc_url( _x( 'https://wordpress.org/plugins/buddypress', 'BuddyPress WP plugin directory URL', 'bp-attachments' ) ) );
printf(
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
sprintf(
/* translators: 1. is the link to the BuddyPress plugin on the WordPress.org plugin directory. */
esc_html__( 'BP Attachments requires the %1$s plugin to be active. Please deactivate BP Attachments, activate %1$s and only then, reactivate BP Attachments.', 'bp-attachments' ),
$bp_plugin_link // phpcs:ignore
)
);
}
}
/**
* Let's start !
*
* @since 1.0.0
*/
function bp_attachments() {
return BP_Attachments::start();
}
add_action( 'bp_loaded', 'bp_attachments', 0 );
/**
* Use Activation and Deactivation hooks to update the
* BuddyPress active components.
*/
register_activation_hook( __FILE__, array( 'BP_Attachments', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'BP_Attachments', 'deactivate' ) );
// Displays a notice to inform BP Attachments needs to be activated after BuddyPress.
add_action( 'admin_notices', array( 'BP_Attachments', 'admin_notice' ) );