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

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