PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-plugin.php +145 -33 3.14.0.2 View file →
@@ -1,14 +1,25 @@
1 1 <?php
2 +/**
3 + * Initializes plugin
4 + *
5 + * @package WP_Stream;
6 + */
7 +
2 8 namespace WP_Stream;
3 9
10 +/**
11 + * Class Plugin
12 + */
4 13 class Plugin {
5 14 /**
6 - * Plugin version number
15 + * Plugin version number.
7 16 *
17 + * TODO Maybe pass this as a constructor dependency?
18 + *
8 19 * @const string
9 20 */
10 - const VERSION = '3.1';
21 + const VERSION = '4.0.2';
11 22
12 23 /**
13 24 * WP-CLI command
14 25 *
@@ -16,43 +27,59 @@
16 27 */
17 28 const WP_CLI_COMMAND = 'stream';
18 29
19 30 /**
31 + * Holds and manages WordPress Admin configurations.
32 + *
20 33 * @var Admin
21 34 */
22 35 public $admin;
23 36
24 37 /**
38 + * Holds and manages alerts.
39 + *
25 40 * @var Alerts
26 41 */
27 42 public $alerts;
28 43
29 44 /**
45 + * Holds and manages alerts lists.
46 + *
30 47 * @var Alerts_List
31 48 */
32 49 public $alerts_list;
33 50
34 51 /**
52 + * Holds and manages connectors
53 + *
35 54 * @var Connectors
36 55 */
37 56 public $connectors;
38 57
39 58 /**
59 + * Holds and manages DB connections.
60 + *
40 61 * @var DB
41 62 */
42 63 public $db;
43 64
44 65 /**
66 + * Holds and manages records.
67 + *
45 68 * @var Log
46 69 */
47 70 public $log;
48 71
49 72 /**
73 + * Stores and manages WordPress settings.
74 + *
50 75 * @var Settings
51 76 */
52 77 public $settings;
53 78
54 79 /**
80 + * Process DB migrations.
81 + *
55 82 * @var Install
56 83 */
57 84 public $install;
58 85
@@ -63,8 +90,15 @@
63 90 */
64 91 public $locations = array();
65 92
66 93 /**
94 + * IP address for the current request to be associated with the log entry.
95 + *
96 + * @var null|false|string Valid IP address, null if not set, false if invalid.
97 + */
98 + protected $client_ip_address;
99 +
100 + /**
67 101 * Class constructor
68 102 */
69 103 public function __construct() {
70 104 $locate = $this->locate_plugin();
@@ -78,45 +112,61 @@
78 112 );
79 113
80 114 spl_autoload_register( array( $this, 'autoload' ) );
81 115
82 - // Load helper functions
116 + // Load helper functions.
83 117 require_once $this->locations['inc_dir'] . 'functions.php';
84 118
85 - // Load DB helper interface/class
86 - $driver = '\WP_Stream\DB';
87 - if ( class_exists( $driver ) ) {
88 - $this->db = new DB( $this );
119 + // Load DB helper interface/class.
120 + $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
121 + $driver = null;
122 +
123 + if ( class_exists( $driver_class ) ) {
124 + $driver = new $driver_class();
125 + $this->db = new DB( $driver );
89 126 }
90 127
128 + $error = false;
91 129 if ( ! $this->db ) {
130 + $error = esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' );
131 + } elseif ( ! $driver instanceof DB_Driver ) {
132 + $error = esc_html__( 'Stream: DB driver must implement DB Driver interface.', 'stream' );
133 + }
134 +
135 + if ( $error ) {
92 136 wp_die(
93 - esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' ),
137 + esc_html( $error ),
94 138 esc_html__( 'Stream DB Error', 'stream' )
95 139 );
96 140 }
97 141
98 - // Load languages
142 + // Load languages.
99 143 add_action( 'plugins_loaded', array( $this, 'i18n' ) );
100 144
101 - // Load logger class
145 + // Load logger class.
102 146 $this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) );
103 147
104 - // Load settings and connectors after widgets_init and before the default init priority
148 + // Set the IP address for the current request.
149 + $this->client_ip_address = wp_stream_filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP );
150 +
151 + // Load settings and connectors after widgets_init and before the default init priority.
105 152 add_action( 'init', array( $this, 'init' ), 9 );
106 153
107 - // Add frontend indicator
154 + // Add frontend indicator.
108 155 add_action( 'wp_head', array( $this, 'frontend_indicator' ) );
109 156
110 - // Load admin area classes
157 + // Change DB driver after plugin loaded if any add-ons want to replace.
158 + add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 );
159 +
160 + // Load admin area classes.
111 161 if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
112 162 $this->admin = new Admin( $this );
113 - $this->install = new Install( $this );
163 + $this->install = $driver->setup_storage( $this );
114 164 } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
115 - $this->admin = new Admin( $this );
165 + $this->admin = new Admin( $this, $driver );
116 166 }
117 167
118 - // Load WP-CLI command
168 + // Load WP-CLI command.
119 169 if ( defined( 'WP_CLI' ) && WP_CLI ) {
120 170 \WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' );
121 171 }
122 172 }
@@ -123,12 +173,12 @@
123 173
124 174 /**
125 175 * Autoloader for classes
126 176 *
127 - * @param string $class
177 + * @param string $class_name Fully qualified classname to be loaded.
128 178 */
129 - function autoload( $class ) {
130 - if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) {
179 + public function autoload( $class_name ) {
180 + if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class_name, $matches ) ) {
131 181 return;
132 182 }
133 183
134 184 static $reflection;
@@ -158,19 +208,18 @@
158 208 public function i18n() {
159 209 load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' );
160 210 }
161 211
162 - /*
212 + /**
163 213 * Load Settings, Notifications, and Connectors
164 214 *
165 215 * @action init
166 216 */
167 217 public function init() {
168 - $this->settings = new Settings( $this );
169 - $this->connectors = new Connectors( $this );
170 - $this->alerts = new Alerts( $this );
171 - $this->alerts_list = new Alerts_List( $this );
172 -
218 + $this->settings = new Settings( $this );
219 + $this->connectors = new Connectors( $this );
220 + $this->alerts = new Alerts( $this );
221 + $this->alerts_list = new Alerts_List( $this );
173 222 }
174 223
175 224 /**
176 225 * Displays an HTML comment in the frontend head to indicate that Stream is activated,
@@ -180,9 +229,10 @@
180 229 *
181 230 * @return string|void An HTML comment, or nothing if the value is filtered out.
182 231 */
183 232 public function frontend_indicator() {
184 - $comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) ); // Localization not needed
233 + /* translators: Localization not needed */
234 + $comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) );
185 235
186 236 /**
187 237 * Filter allows the HTML output of the frontend indicator comment
188 238 * to be altered or removed, if desired.
@@ -191,9 +241,9 @@
191 241 */
192 242 $comment = apply_filters( 'wp_stream_frontend_indicator', $comment );
193 243
194 244 if ( ! empty( $comment ) ) {
195 - echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok
245 + printf( "<!-- %s -->\n", esc_html( $comment ) );
196 246 }
197 247 }
198 248
199 249 /**
@@ -199,17 +249,15 @@
199 249 /**
200 250 * Version of plugin_dir_url() which works for plugins installed in the plugins directory,
201 251 * and for plugins bundled with themes.
202 252 *
203 - * @throws \Exception
204 - *
205 253 * @return array
206 254 */
207 255 private function locate_plugin() {
208 - $dir_url = trailingslashit( plugins_url( '', dirname( __FILE__ ) ) );
209 - $dir_path = plugin_dir_path( dirname( __FILE__ ) );
210 - $dir_basename = basename( $dir_path );
211 - $plugin_basename = trailingslashit( $dir_basename ) . $dir_basename . '.php';
256 + $dir_url = trailingslashit( plugins_url( '', __DIR__ ) );
257 + $dir_path = plugin_dir_path( __DIR__ );
258 + $dir_basename = basename( $dir_path );
259 + $plugin_basename = trailingslashit( $dir_basename ) . $dir_basename . '.php';
212 260
213 261 return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' );
214 262 }
215 263
@@ -219,6 +267,70 @@
219 267 * @return string
220 268 */
221 269 public function get_version() {
222 270 return self::VERSION;
271 + }
272 +
273 + /**
274 + * Change plugin database driver in case driver plugin loaded after stream
275 + */
276 + public function plugins_loaded() {
277 + // Load DB helper interface/class.
278 + $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
279 +
280 + if ( class_exists( $driver_class ) ) {
281 + $driver = new $driver_class();
282 + $this->db = new DB( $driver );
283 + }
284 + }
285 +
286 + /**
287 + * Returns true if Stream is network activated, otherwise false
288 + *
289 + * @return bool
290 + */
291 + public function is_network_activated() {
292 +
293 + $is_network_activated = false;
294 +
295 + if ( $this->is_mustuse() ) {
296 + $is_network_activated = true;
297 + } else {
298 + if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
299 + require_once ABSPATH . '/wp-admin/includes/plugin.php';
300 + }
301 + $is_network_activated = is_plugin_active_for_network( $this->locations['plugin'] );
302 + }
303 +
304 + /**
305 + * Filter allows the network activated detection to be overridden.
306 + *
307 + * @param string $is_network_activated Whether the plugin is network activated.
308 + * @param WP_Stream\Plugin $plugin The stream plugin object.
309 + */
310 + return apply_filters( 'wp_stream_is_network_activated', $is_network_activated, $this );
311 + }
312 +
313 + /**
314 + * Returns true if Stream is a must-use plugin, otherwise false
315 + *
316 + * @return bool
317 + */
318 + public function is_mustuse() {
319 + $stream_php = trailingslashit( WPMU_PLUGIN_DIR ) . $this->locations['plugin'];
320 +
321 + if ( file_exists( $stream_php ) && class_exists( 'WP_Stream\Plugin' ) ) {
322 + return true;
323 + }
324 +
325 + return false;
326 + }
327 +
328 + /**
329 + * Get the IP address for the current request.
330 + *
331 + * @return false|null|string Valid IP address, null if not set, false if invalid.
332 + */
333 + public function get_client_ip_address() {
334 + return apply_filters( 'wp_stream_client_ip_address', $this->client_ip_address );
223 335 }
224 336 }