| @@ -2,13 +2,15 @@ | ||
| 2 | 2 | namespace WP_Stream; |
| 3 | 3 | |
| 4 | 4 | class Plugin { |
| 5 | 5 | /** |
| 6 | - * Plugin version number | |
| 6 | + * Plugin version number. | |
| 7 | 7 | * |
| 8 | + * TODO Maybe pass this as a constructor dependency? | |
| 9 | + * | |
| 8 | 10 | * @const string |
| 9 | 11 | */ |
| 10 | - const VERSION = '3.1'; | |
| 12 | + const VERSION = '3.4.2'; | |
| 11 | 13 | |
| 12 | 14 | /** |
| 13 | 15 | * WP-CLI command |
| 14 | 16 | * |
| @@ -82,16 +84,26 @@ | ||
| 82 | 84 | // Load helper functions |
| 83 | 85 | require_once $this->locations['inc_dir'] . 'functions.php'; |
| 84 | 86 | |
| 85 | 87 | // Load DB helper interface/class |
| 86 | - $driver = '\WP_Stream\DB'; | |
| 87 | - if ( class_exists( $driver ) ) { | |
| 88 | - $this->db = new DB( $this ); | |
| 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 ); | |
| 89 | 94 | } |
| 90 | 95 | |
| 96 | + $error = false; | |
| 91 | 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 ) { | |
| 92 | 104 | wp_die( |
| 93 | - esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' ), | |
| 105 | + esc_html( $error ), | |
| 94 | 106 | esc_html__( 'Stream DB Error', 'stream' ) |
| 95 | 107 | ); |
| 96 | 108 | } |
| 97 | 109 | |
| @@ -106,14 +118,17 @@ | ||
| 106 | 118 | |
| 107 | 119 | // Add frontend indicator |
| 108 | 120 | add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 109 | 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 | + | |
| 110 | 125 | // Load admin area classes |
| 111 | 126 | if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 112 | 127 | $this->admin = new Admin( $this ); |
| 113 | - $this->install = new Install( $this ); | |
| 128 | + $this->install = $driver->setup_storage( $this ); | |
| 114 | 129 | } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 115 | - $this->admin = new Admin( $this ); | |
| 130 | + $this->admin = new Admin( $this, $driver ); | |
| 116 | 131 | } |
| 117 | 132 | |
| 118 | 133 | // Load WP-CLI command |
| 119 | 134 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| @@ -125,9 +140,9 @@ | ||
| 125 | 140 | * Autoloader for classes |
| 126 | 141 | * |
| 127 | 142 | * @param string $class |
| 128 | 143 | */ |
| 129 | - function autoload( $class ) { | |
| 144 | + public function autoload( $class ) { | |
| 130 | 145 | if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) { |
| 131 | 146 | return; |
| 132 | 147 | } |
| 133 | 148 | |
| @@ -164,12 +179,12 @@ | ||
| 164 | 179 | * |
| 165 | 180 | * @action init |
| 166 | 181 | */ |
| 167 | 182 | 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 ); | |
| 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 ); | |
| 172 | 187 | |
| 173 | 188 | } |
| 174 | 189 | |
| 175 | 190 | /** |
| @@ -204,12 +219,12 @@ | ||
| 204 | 219 | * |
| 205 | 220 | * @return array |
| 206 | 221 | */ |
| 207 | 222 | 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'; | |
| 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'; | |
| 212 | 227 | |
| 213 | 228 | return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); |
| 214 | 229 | } |
| 215 | 230 | |
| @@ -219,6 +234,62 @@ | ||
| 219 | 234 | * @return string |
| 220 | 235 | */ |
| 221 | 236 | public function get_version() { |
| 222 | 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; | |
| 223 | 294 | } |
| 224 | 295 | } |