PluginProbe
Stream – Activity Log & Audit Trail / 4.0.1
Stream – Activity Log & Audit Trail v4.0.1
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
stream / classes / class-plugin.php

class-plugin.php in Stream – Activity Log & Audit Trail 4.0.1, at classes/class-plugin.php

337 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Initializes plugin
4 *
5 * @package WP_Stream;
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class Plugin
12 */
13 class Plugin {
14 /**
15 * Plugin version number.
16 *
17 * TODO Maybe pass this as a constructor dependency?
18 *
19 * @const string
20 */
21 const VERSION = '4.0.1';
22
23 /**
24 * WP-CLI command
25 *
26 * @const string
27 */
28 const WP_CLI_COMMAND = 'stream';
29
30 /**
31 * Holds and manages WordPress Admin configurations.
32 *
33 * @var Admin
34 */
35 public $admin;
36
37 /**
38 * Holds and manages alerts.
39 *
40 * @var Alerts
41 */
42 public $alerts;
43
44 /**
45 * Holds and manages alerts lists.
46 *
47 * @var Alerts_List
48 */
49 public $alerts_list;
50
51 /**
52 * Holds and manages connectors
53 *
54 * @var Connectors
55 */
56 public $connectors;
57
58 /**
59 * Holds and manages DB connections.
60 *
61 * @var DB
62 */
63 public $db;
64
65 /**
66 * Holds and manages records.
67 *
68 * @var Log
69 */
70 public $log;
71
72 /**
73 * Stores and manages WordPress settings.
74 *
75 * @var Settings
76 */
77 public $settings;
78
79 /**
80 * Process DB migrations.
81 *
82 * @var Install
83 */
84 public $install;
85
86 /**
87 * URLs and Paths used by the plugin
88 *
89 * @var array
90 */
91 public $locations = array();
92
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 /**
101 * Class constructor
102 */
103 public function __construct() {
104 $locate = $this->locate_plugin();
105
106 $this->locations = array(
107 'plugin' => $locate['plugin_basename'],
108 'dir' => $locate['dir_path'],
109 'url' => $locate['dir_url'],
110 'inc_dir' => $locate['dir_path'] . 'includes/',
111 'class_dir' => $locate['dir_path'] . 'classes/',
112 );
113
114 spl_autoload_register( array( $this, 'autoload' ) );
115
116 // Load helper functions.
117 require_once $this->locations['inc_dir'] . 'functions.php';
118
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 );
126 }
127
128 $error = false;
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 ) {
136 wp_die(
137 esc_html( $error ),
138 esc_html__( 'Stream DB Error', 'stream' )
139 );
140 }
141
142 // Load languages.
143 add_action( 'plugins_loaded', array( $this, 'i18n' ) );
144
145 // Load logger class.
146 $this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) );
147
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.
152 add_action( 'init', array( $this, 'init' ), 9 );
153
154 // Add frontend indicator.
155 add_action( 'wp_head', array( $this, 'frontend_indicator' ) );
156
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.
161 if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
162 $this->admin = new Admin( $this );
163 $this->install = $driver->setup_storage( $this );
164 } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
165 $this->admin = new Admin( $this, $driver );
166 }
167
168 // Load WP-CLI command.
169 if ( defined( 'WP_CLI' ) && WP_CLI ) {
170 \WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' );
171 }
172 }
173
174 /**
175 * Autoloader for classes
176 *
177 * @param string $class_name Fully qualified classname to be loaded.
178 */
179 public function autoload( $class_name ) {
180 if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class_name, $matches ) ) {
181 return;
182 }
183
184 static $reflection;
185
186 if ( empty( $reflection ) ) {
187 $reflection = new \ReflectionObject( $this );
188 }
189
190 if ( $reflection->getNamespaceName() !== $matches['namespace'] ) {
191 return;
192 }
193
194 $autoload_name = $matches['autoload'];
195 $autoload_dir = \trailingslashit( $this->locations['class_dir'] );
196 $autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) );
197
198 if ( is_readable( $autoload_path ) ) {
199 require_once $autoload_path;
200 }
201 }
202
203 /**
204 * Loads the translation files.
205 *
206 * @action plugins_loaded
207 */
208 public function i18n() {
209 load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' );
210 }
211
212 /**
213 * Load Settings, Notifications, and Connectors
214 *
215 * @action init
216 */
217 public function init() {
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 );
222 }
223
224 /**
225 * Displays an HTML comment in the frontend head to indicate that Stream is activated,
226 * and which version of Stream is currently in use.
227 *
228 * @action wp_head
229 *
230 * @return string|void An HTML comment, or nothing if the value is filtered out.
231 */
232 public function frontend_indicator() {
233 /* translators: Localization not needed */
234 $comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) );
235
236 /**
237 * Filter allows the HTML output of the frontend indicator comment
238 * to be altered or removed, if desired.
239 *
240 * @return string The content of the HTML comment
241 */
242 $comment = apply_filters( 'wp_stream_frontend_indicator', $comment );
243
244 if ( ! empty( $comment ) ) {
245 printf( "<!-- %s -->\n", esc_html( $comment ) );
246 }
247 }
248
249 /**
250 * Version of plugin_dir_url() which works for plugins installed in the plugins directory,
251 * and for plugins bundled with themes.
252 *
253 * @return array
254 */
255 private function locate_plugin() {
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';
260
261 return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' );
262 }
263
264 /**
265 * Getter for the version number.
266 *
267 * @return string
268 */
269 public function get_version() {
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 );
335 }
336 }
337