PluginProbe
Stream – Activity Log & Audit Trail / 3.4.1
Stream – Activity Log & Audit Trail v3.4.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 3.4.1, at classes/class-plugin.php

296 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WP_Stream;
3
4 class Plugin {
5 /**
6 * Plugin version number.
7 *
8 * TODO Maybe pass this as a constructor dependency?
9 *
10 * @const string
11 */
12 const VERSION = '3.4.1';
13
14 /**
15 * WP-CLI command
16 *
17 * @const string
18 */
19 const WP_CLI_COMMAND = 'stream';
20
21 /**
22 * @var Admin
23 */
24 public $admin;
25
26 /**
27 * @var Alerts
28 */
29 public $alerts;
30
31 /**
32 * @var Alerts_List
33 */
34 public $alerts_list;
35
36 /**
37 * @var Connectors
38 */
39 public $connectors;
40
41 /**
42 * @var DB
43 */
44 public $db;
45
46 /**
47 * @var Log
48 */
49 public $log;
50
51 /**
52 * @var Settings
53 */
54 public $settings;
55
56 /**
57 * @var Install
58 */
59 public $install;
60
61 /**
62 * URLs and Paths used by the plugin
63 *
64 * @var array
65 */
66 public $locations = array();
67
68 /**
69 * Class constructor
70 */
71 public function __construct() {
72 $locate = $this->locate_plugin();
73
74 $this->locations = array(
75 'plugin' => $locate['plugin_basename'],
76 'dir' => $locate['dir_path'],
77 'url' => $locate['dir_url'],
78 'inc_dir' => $locate['dir_path'] . 'includes/',
79 'class_dir' => $locate['dir_path'] . 'classes/',
80 );
81
82 spl_autoload_register( array( $this, 'autoload' ) );
83
84 // Load helper functions
85 require_once $this->locations['inc_dir'] . 'functions.php';
86
87 // Load DB helper interface/class
88 $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
89 $driver = null;
90
91 if ( class_exists( $driver_class ) ) {
92 $driver = new $driver_class();
93 $this->db = new DB( $driver );
94 }
95
96 $error = false;
97 if ( ! $this->db ) {
98 $error = esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' );
99 } elseif ( ! $driver instanceof DB_Driver ) {
100 $error = esc_html__( 'Stream: DB driver must implement DB Driver interface.', 'stream' );
101 }
102
103 if ( $error ) {
104 wp_die(
105 esc_html( $error ),
106 esc_html__( 'Stream DB Error', 'stream' )
107 );
108 }
109
110 // Load languages
111 add_action( 'plugins_loaded', array( $this, 'i18n' ) );
112
113 // Load logger class
114 $this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) );
115
116 // Load settings and connectors after widgets_init and before the default init priority
117 add_action( 'init', array( $this, 'init' ), 9 );
118
119 // Add frontend indicator
120 add_action( 'wp_head', array( $this, 'frontend_indicator' ) );
121
122 // Change DB driver after plugin loaded if any add-ons want to replace
123 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 );
124
125 // Load admin area classes
126 if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
127 $this->admin = new Admin( $this );
128 $this->install = $driver->setup_storage( $this );
129 } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
130 $this->admin = new Admin( $this, $driver );
131 }
132
133 // Load WP-CLI command
134 if ( defined( 'WP_CLI' ) && WP_CLI ) {
135 \WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' );
136 }
137 }
138
139 /**
140 * Autoloader for classes
141 *
142 * @param string $class
143 */
144 public function autoload( $class ) {
145 if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) {
146 return;
147 }
148
149 static $reflection;
150
151 if ( empty( $reflection ) ) {
152 $reflection = new \ReflectionObject( $this );
153 }
154
155 if ( $reflection->getNamespaceName() !== $matches['namespace'] ) {
156 return;
157 }
158
159 $autoload_name = $matches['autoload'];
160 $autoload_dir = \trailingslashit( $this->locations['class_dir'] );
161 $autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) );
162
163 if ( is_readable( $autoload_path ) ) {
164 require_once $autoload_path;
165 }
166 }
167
168 /**
169 * Loads the translation files.
170 *
171 * @action plugins_loaded
172 */
173 public function i18n() {
174 load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' );
175 }
176
177 /*
178 * Load Settings, Notifications, and Connectors
179 *
180 * @action init
181 */
182 public function init() {
183 $this->settings = new Settings( $this );
184 $this->connectors = new Connectors( $this );
185 $this->alerts = new Alerts( $this );
186 $this->alerts_list = new Alerts_List( $this );
187
188 }
189
190 /**
191 * Displays an HTML comment in the frontend head to indicate that Stream is activated,
192 * and which version of Stream is currently in use.
193 *
194 * @action wp_head
195 *
196 * @return string|void An HTML comment, or nothing if the value is filtered out.
197 */
198 public function frontend_indicator() {
199 $comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) ); // Localization not needed
200
201 /**
202 * Filter allows the HTML output of the frontend indicator comment
203 * to be altered or removed, if desired.
204 *
205 * @return string The content of the HTML comment
206 */
207 $comment = apply_filters( 'wp_stream_frontend_indicator', $comment );
208
209 if ( ! empty( $comment ) ) {
210 echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok
211 }
212 }
213
214 /**
215 * Version of plugin_dir_url() which works for plugins installed in the plugins directory,
216 * and for plugins bundled with themes.
217 *
218 * @throws \Exception
219 *
220 * @return array
221 */
222 private function locate_plugin() {
223 $dir_url = trailingslashit( plugins_url( '', dirname( __FILE__ ) ) );
224 $dir_path = plugin_dir_path( dirname( __FILE__ ) );
225 $dir_basename = basename( $dir_path );
226 $plugin_basename = trailingslashit( $dir_basename ) . $dir_basename . '.php';
227
228 return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' );
229 }
230
231 /**
232 * Getter for the version number.
233 *
234 * @return string
235 */
236 public function get_version() {
237 return self::VERSION;
238 }
239
240 /**
241 * Change plugin database driver in case driver plugin loaded after stream
242 */
243 public function plugins_loaded() {
244 // Load DB helper interface/class
245 $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' );
246
247 if ( class_exists( $driver_class ) ) {
248 $driver = new $driver_class();
249 $this->db = new DB( $driver );
250 }
251 }
252
253 /**
254 * Returns true if Stream is network activated, otherwise false
255 *
256 * @return bool
257 */
258 public function is_network_activated() {
259
260 $is_network_activated = false;
261
262 if ( $this->is_mustuse() ) {
263 $is_network_activated = true;
264 } else {
265 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
266 require_once ABSPATH . '/wp-admin/includes/plugin.php';
267 }
268 $is_network_activated = is_plugin_active_for_network( $this->locations['plugin'] );
269 }
270
271 /**
272 * Filter allows the network activated detection to be overridden.
273 *
274 * @param string $is_network_activated Whether the plugin is network activated
275 * @param WP_Stream\Plugin $plugin The stream plugin object
276 */
277 return apply_filters( 'wp_stream_is_network_activated', $is_network_activated, $this );
278 }
279
280 /**
281 * Returns true if Stream is a must-use plugin, otherwise false
282 *
283 * @return bool
284 */
285 public function is_mustuse() {
286
287 $stream_php = trailingslashit( WPMU_PLUGIN_DIR ) . $this->locations['plugin'];
288
289 if ( file_exists( $stream_php ) && class_exists( 'WP_Stream\Plugin' ) ) {
290 return true;
291 }
292
293 return false;
294 }
295 }
296