-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathclass-plugin.php
More file actions
executable file
·318 lines (267 loc) · 7.44 KB
/
class-plugin.php
File metadata and controls
executable file
·318 lines (267 loc) · 7.44 KB
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
<?php
/**
* Initializes plugin
*
* @package WP_Stream;
*/
namespace WP_Stream;
/**
* Class Plugin
*/
class Plugin {
/**
* Plugin version number.
*
* TODO Maybe pass this as a constructor dependency?
*
* @const string
*/
const VERSION = '3.7.0';
/**
* WP-CLI command
*
* @const string
*/
const WP_CLI_COMMAND = 'stream';
/**
* Holds and manages WordPress Admin configurations.
*
* @var Admin
*/
public $admin;
/**
* Holds and manages alerts.
*
* @var Alerts
*/
public $alerts;
/**
* Holds and manages alerts lists.
*
* @var Alerts_List
*/
public $alerts_list;
/**
* Holds and manages connectors
*
* @var Connectors
*/
public $connectors;
/**
* Holds and manages DB connections.
*
* @var DB
*/
public $db;
/**
* Holds and manages records.
*
* @var Log
*/
public $log;
/**
* Stores and manages WordPress settings.
*
* @var Settings
*/
public $settings;
/**
* Process DB migrations.
*
* @var Install
*/
public $install;
/**
* URLs and Paths used by the plugin
*
* @var array
*/
public $locations = array();
/**
* Class constructor
*/
public function __construct() {
$locate = $this->locate_plugin();
$this->locations = array(
'plugin' => $locate['plugin_basename'],
'dir' => $locate['dir_path'],
'url' => $locate['dir_url'],
'inc_dir' => $locate['dir_path'] . 'includes/',
'class_dir' => $locate['dir_path'] . 'classes/',
);
spl_autoload_register( array( $this, 'autoload' ) );
// Load helper functions.
require_once $this->locations['inc_dir'] . 'functions.php';
// Load DB helper interface/class.
$driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
$driver = null;
if ( class_exists( $driver_class ) ) {
$driver = new $driver_class();
$this->db = new DB( $driver );
}
$error = false;
if ( ! $this->db ) {
$error = esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' );
} elseif ( ! $driver instanceof DB_Driver ) {
$error = esc_html__( 'Stream: DB driver must implement DB Driver interface.', 'stream' );
}
if ( $error ) {
wp_die(
esc_html( $error ),
esc_html__( 'Stream DB Error', 'stream' )
);
}
// Load languages.
add_action( 'plugins_loaded', array( $this, 'i18n' ) );
// Load logger class.
$this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) );
// Load settings and connectors after widgets_init and before the default init priority.
add_action( 'init', array( $this, 'init' ), 9 );
// Add frontend indicator.
add_action( 'wp_head', array( $this, 'frontend_indicator' ) );
// Change DB driver after plugin loaded if any add-ons want to replace.
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 );
// Load admin area classes.
if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
$this->admin = new Admin( $this );
$this->install = $driver->setup_storage( $this );
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
$this->admin = new Admin( $this, $driver );
}
// Load WP-CLI command.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' );
}
}
/**
* Autoloader for classes
*
* @param string $class Fully qualified classname to be loaded.
*/
public function autoload( $class ) {
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) {
return;
}
static $reflection;
if ( empty( $reflection ) ) {
$reflection = new \ReflectionObject( $this );
}
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) {
return;
}
$autoload_name = $matches['autoload'];
$autoload_dir = \trailingslashit( $this->locations['class_dir'] );
$autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) );
if ( is_readable( $autoload_path ) ) {
require_once $autoload_path;
}
}
/**
* Loads the translation files.
*
* @action plugins_loaded
*/
public function i18n() {
load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' );
}
/**
* Load Settings, Notifications, and Connectors
*
* @action init
*/
public function init() {
$this->settings = new Settings( $this );
$this->connectors = new Connectors( $this );
$this->alerts = new Alerts( $this );
$this->alerts_list = new Alerts_List( $this );
}
/**
* Displays an HTML comment in the frontend head to indicate that Stream is activated,
* and which version of Stream is currently in use.
*
* @action wp_head
*
* @return string|void An HTML comment, or nothing if the value is filtered out.
*/
public function frontend_indicator() {
/* translators: Localization not needed */
$comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) );
/**
* Filter allows the HTML output of the frontend indicator comment
* to be altered or removed, if desired.
*
* @return string The content of the HTML comment
*/
$comment = apply_filters( 'wp_stream_frontend_indicator', $comment );
if ( ! empty( $comment ) ) {
echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok.
}
}
/**
* Version of plugin_dir_url() which works for plugins installed in the plugins directory,
* and for plugins bundled with themes.
*
* @return array
*/
private function locate_plugin() {
$dir_url = trailingslashit( plugins_url( '', dirname( __FILE__ ) ) );
$dir_path = plugin_dir_path( dirname( __FILE__ ) );
$dir_basename = basename( $dir_path );
$plugin_basename = trailingslashit( $dir_basename ) . $dir_basename . '.php';
return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' );
}
/**
* Getter for the version number.
*
* @return string
*/
public function get_version() {
return self::VERSION;
}
/**
* Change plugin database driver in case driver plugin loaded after stream
*/
public function plugins_loaded() {
// Load DB helper interface/class.
$driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
if ( class_exists( $driver_class ) ) {
$driver = new $driver_class();
$this->db = new DB( $driver );
}
}
/**
* Returns true if Stream is network activated, otherwise false
*
* @return bool
*/
public function is_network_activated() {
$is_network_activated = false;
if ( $this->is_mustuse() ) {
$is_network_activated = true;
} else {
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$is_network_activated = is_plugin_active_for_network( $this->locations['plugin'] );
}
/**
* Filter allows the network activated detection to be overridden.
*
* @param string $is_network_activated Whether the plugin is network activated.
* @param WP_Stream\Plugin $plugin The stream plugin object.
*/
return apply_filters( 'wp_stream_is_network_activated', $is_network_activated, $this );
}
/**
* Returns true if Stream is a must-use plugin, otherwise false
*
* @return bool
*/
public function is_mustuse() {
$stream_php = trailingslashit( WPMU_PLUGIN_DIR ) . $this->locations['plugin'];
if ( file_exists( $stream_php ) && class_exists( 'WP_Stream\Plugin' ) ) {
return true;
}
return false;
}
}