| @@ -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.0.0'; | |
| 12 | + const VERSION = '3.4.2'; | |
| 11 | 13 | |
| 12 | 14 | /** |
| 13 | 15 | * WP-CLI command |
| 14 | 16 | * |
| @@ -21,8 +23,18 @@ | ||
| 21 | 23 | */ |
| 22 | 24 | public $admin; |
| 23 | 25 | |
| 24 | 26 | /** |
| 27 | + * @var Alerts | |
| 28 | + */ | |
| 29 | + public $alerts; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @var Alerts_List | |
| 33 | + */ | |
| 34 | + public $alerts_list; | |
| 35 | + | |
| 36 | + /** | |
| 25 | 37 | * @var Connectors |
| 26 | 38 | */ |
| 27 | 39 | public $connectors; |
| 28 | 40 | |
| @@ -72,16 +84,26 @@ | ||
| 72 | 84 | // Load helper functions |
| 73 | 85 | require_once $this->locations['inc_dir'] . 'functions.php'; |
| 74 | 86 | |
| 75 | 87 | // Load DB helper interface/class |
| 76 | - $driver = '\WP_Stream\DB'; | |
| 77 | - if ( class_exists( $driver ) ) { | |
| 78 | - $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 ); | |
| 79 | 94 | } |
| 80 | 95 | |
| 96 | + $error = false; | |
| 81 | 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 ) { | |
| 82 | 104 | wp_die( |
| 83 | - esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' ), | |
| 105 | + esc_html( $error ), | |
| 84 | 106 | esc_html__( 'Stream DB Error', 'stream' ) |
| 85 | 107 | ); |
| 86 | 108 | } |
| 87 | 109 | |
| @@ -96,12 +118,17 @@ | ||
| 96 | 118 | |
| 97 | 119 | // Add frontend indicator |
| 98 | 120 | add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 99 | 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 | + | |
| 100 | 125 | // Load admin area classes |
| 101 | - if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) ) { | |
| 126 | + if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| 102 | 127 | $this->admin = new Admin( $this ); |
| 103 | - $this->install = new Install( $this ); | |
| 128 | + $this->install = $driver->setup_storage( $this ); | |
| 129 | + } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { | |
| 130 | + $this->admin = new Admin( $this, $driver ); | |
| 104 | 131 | } |
| 105 | 132 | |
| 106 | 133 | // Load WP-CLI command |
| 107 | 134 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| @@ -113,9 +140,9 @@ | ||
| 113 | 140 | * Autoloader for classes |
| 114 | 141 | * |
| 115 | 142 | * @param string $class |
| 116 | 143 | */ |
| 117 | - function autoload( $class ) { | |
| 144 | + public function autoload( $class ) { | |
| 118 | 145 | if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) { |
| 119 | 146 | return; |
| 120 | 147 | } |
| 121 | 148 | |
| @@ -147,15 +174,18 @@ | ||
| 147 | 174 | load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' ); |
| 148 | 175 | } |
| 149 | 176 | |
| 150 | 177 | /* |
| 151 | - * Load Settings and Connectors | |
| 178 | + * Load Settings, Notifications, and Connectors | |
| 152 | 179 | * |
| 153 | 180 | * @action init |
| 154 | 181 | */ |
| 155 | 182 | public function init() { |
| 156 | - $this->settings = new Settings( $this ); | |
| 157 | - $this->connectors = new Connectors( $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 ); | |
| 187 | + | |
| 158 | 188 | } |
| 159 | 189 | |
| 160 | 190 | /** |
| 161 | 191 | * Displays an HTML comment in the frontend head to indicate that Stream is activated, |
| @@ -189,46 +219,77 @@ | ||
| 189 | 219 | * |
| 190 | 220 | * @return array |
| 191 | 221 | */ |
| 192 | 222 | private function locate_plugin() { |
| 193 | - $reflection = new \ReflectionObject( $this ); | |
| 194 | - $file_name = $reflection->getFileName(); | |
| 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'; | |
| 195 | 227 | |
| 196 | - if ( '/' !== \DIRECTORY_SEPARATOR ) { | |
| 197 | - $file_name = str_replace( \DIRECTORY_SEPARATOR, '/', $file_name ); // Windows compat | |
| 198 | - } | |
| 228 | + return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); | |
| 229 | + } | |
| 199 | 230 | |
| 200 | - $plugin_dir = preg_replace( '#(.*plugins[^/]*/[^/]+)(/.*)?#', '$1', $file_name, 1, $count ); | |
| 231 | + /** | |
| 232 | + * Getter for the version number. | |
| 233 | + * | |
| 234 | + * @return string | |
| 235 | + */ | |
| 236 | + public function get_version() { | |
| 237 | + return self::VERSION; | |
| 238 | + } | |
| 201 | 239 | |
| 202 | - if ( 0 === $count ) { | |
| 203 | - throw new \Exception( "Class not located within a directory tree containing 'plugins': $file_name" ); | |
| 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 ); | |
| 204 | 250 | } |
| 251 | + } | |
| 205 | 252 | |
| 206 | - // Make sure that we can reliably get the relative path inside of the content directory | |
| 207 | - $content_dir = trailingslashit( WP_CONTENT_DIR ); | |
| 253 | + /** | |
| 254 | + * Returns true if Stream is network activated, otherwise false | |
| 255 | + * | |
| 256 | + * @return bool | |
| 257 | + */ | |
| 258 | + public function is_network_activated() { | |
| 208 | 259 | |
| 209 | - if ( '/' !== \DIRECTORY_SEPARATOR ) { | |
| 210 | - $content_dir = str_replace( \DIRECTORY_SEPARATOR, '/', $content_dir ); // Windows compat | |
| 211 | - } | |
| 260 | + $is_network_activated = false; | |
| 212 | 261 | |
| 213 | - if ( 0 !== strpos( $plugin_dir, $content_dir ) ) { | |
| 214 | - throw new \Exception( 'Plugin dir is not inside of WP_CONTENT_DIR' ); | |
| 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'] ); | |
| 215 | 269 | } |
| 216 | 270 | |
| 217 | - $content_sub_path = substr( $plugin_dir, strlen( $content_dir ) ); | |
| 218 | - $dir_url = content_url( trailingslashit( $content_sub_path ) ); | |
| 219 | - $dir_path = trailingslashit( $plugin_dir ); | |
| 220 | - $dir_basename = basename( $plugin_dir ); | |
| 221 | - $plugin_basename = trailingslashit( $dir_basename ) . $dir_basename. '.php'; | |
| 222 | - | |
| 223 | - return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); | |
| 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 ); | |
| 224 | 278 | } |
| 225 | 279 | |
| 226 | 280 | /** |
| 227 | - * Getter for the version number. | |
| 281 | + * Returns true if Stream is a must-use plugin, otherwise false | |
| 228 | 282 | * |
| 229 | - * @return string | |
| 283 | + * @return bool | |
| 230 | 284 | */ |
| 231 | - public function get_version() { | |
| 232 | - return self::VERSION; | |
| 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; | |
| 233 | 294 | } |
| 234 | 295 | } |