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