| @@ -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.2.1'; | |
| 21 | + const VERSION = '3.8.2'; | |
| 11 | 22 | |
| 12 | 23 | /** |
| 13 | 24 | * WP-CLI command |
| 14 | 25 | * |
| @@ -16,43 +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 | + * | |
| 25 | 40 | * @var Alerts |
| 26 | 41 | */ |
| 27 | 42 | public $alerts; |
| 28 | 43 | |
| 29 | 44 | /** |
| 45 | + * Holds and manages alerts lists. | |
| 46 | + * | |
| 30 | 47 | * @var Alerts_List |
| 31 | 48 | */ |
| 32 | 49 | public $alerts_list; |
| 33 | 50 | |
| 34 | 51 | /** |
| 52 | + * Holds and manages connectors | |
| 53 | + * | |
| 35 | 54 | * @var Connectors |
| 36 | 55 | */ |
| 37 | 56 | public $connectors; |
| 38 | 57 | |
| 39 | 58 | /** |
| 59 | + * Holds and manages DB connections. | |
| 60 | + * | |
| 40 | 61 | * @var DB |
| 41 | 62 | */ |
| 42 | 63 | public $db; |
| 43 | 64 | |
| 44 | 65 | /** |
| 66 | + * Holds and manages records. | |
| 67 | + * | |
| 45 | 68 | * @var Log |
| 46 | 69 | */ |
| 47 | 70 | public $log; |
| 48 | 71 | |
| 49 | 72 | /** |
| 73 | + * Stores and manages WordPress settings. | |
| 74 | + * | |
| 50 | 75 | * @var Settings |
| 51 | 76 | */ |
| 52 | 77 | public $settings; |
| 53 | 78 | |
| 54 | 79 | /** |
| 80 | + * Process DB migrations. | |
| 81 | + * | |
| 55 | 82 | * @var Install |
| 56 | 83 | */ |
| 57 | 84 | public $install; |
| 58 | 85 | |
| @@ -78,12 +105,12 @@ | ||
| 78 | 105 | ); |
| 79 | 106 | |
| 80 | 107 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 81 | 108 | |
| 82 | - // Load helper functions | |
| 109 | + // Load helper functions. | |
| 83 | 110 | require_once $this->locations['inc_dir'] . 'functions.php'; |
| 84 | 111 | |
| 85 | - // Load DB helper interface/class | |
| 112 | + // Load DB helper interface/class. | |
| 86 | 113 | $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' ); |
| 87 | 114 | $driver = null; |
| 88 | 115 | |
| 89 | 116 | if ( class_exists( $driver_class ) ) { |
| @@ -104,24 +131,24 @@ | ||
| 104 | 131 | esc_html__( 'Stream DB Error', 'stream' ) |
| 105 | 132 | ); |
| 106 | 133 | } |
| 107 | 134 | |
| 108 | - // Load languages | |
| 135 | + // Load languages. | |
| 109 | 136 | add_action( 'plugins_loaded', array( $this, 'i18n' ) ); |
| 110 | 137 | |
| 111 | - // Load logger class | |
| 138 | + // Load logger class. | |
| 112 | 139 | $this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) ); |
| 113 | 140 | |
| 114 | - // 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. | |
| 115 | 142 | add_action( 'init', array( $this, 'init' ), 9 ); |
| 116 | 143 | |
| 117 | - // Add frontend indicator | |
| 144 | + // Add frontend indicator. | |
| 118 | 145 | add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 119 | 146 | |
| 120 | - // Change DB driver after plugin loaded if any add-ons want to replace | |
| 147 | + // Change DB driver after plugin loaded if any add-ons want to replace. | |
| 121 | 148 | add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 ); |
| 122 | 149 | |
| 123 | - // Load admin area classes | |
| 150 | + // Load admin area classes. | |
| 124 | 151 | if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 125 | 152 | $this->admin = new Admin( $this ); |
| 126 | 153 | $this->install = $driver->setup_storage( $this ); |
| 127 | 154 | } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| @@ -127,9 +154,9 @@ | ||
| 127 | 154 | } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 128 | 155 | $this->admin = new Admin( $this, $driver ); |
| 129 | 156 | } |
| 130 | 157 | |
| 131 | - // Load WP-CLI command | |
| 158 | + // Load WP-CLI command. | |
| 132 | 159 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 133 | 160 | \WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' ); |
| 134 | 161 | } |
| 135 | 162 | } |
| @@ -136,11 +163,11 @@ | ||
| 136 | 163 | |
| 137 | 164 | /** |
| 138 | 165 | * Autoloader for classes |
| 139 | 166 | * |
| 140 | - * @param string $class | |
| 167 | + * @param string $class Fully qualified classname to be loaded. | |
| 141 | 168 | */ |
| 142 | - function autoload( $class ) { | |
| 169 | + public function autoload( $class ) { | |
| 143 | 170 | if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) { |
| 144 | 171 | return; |
| 145 | 172 | } |
| 146 | 173 | |
| @@ -171,18 +198,18 @@ | ||
| 171 | 198 | public function i18n() { |
| 172 | 199 | load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' ); |
| 173 | 200 | } |
| 174 | 201 | |
| 175 | - /* | |
| 202 | + /** | |
| 176 | 203 | * Load Settings, Notifications, and Connectors |
| 177 | 204 | * |
| 178 | 205 | * @action init |
| 179 | 206 | */ |
| 180 | 207 | 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 ); | |
| 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 ); | |
| 185 | 212 | |
| 186 | 213 | } |
| 187 | 214 | |
| 188 | 215 | /** |
| @@ -193,9 +220,10 @@ | ||
| 193 | 220 | * |
| 194 | 221 | * @return string|void An HTML comment, or nothing if the value is filtered out. |
| 195 | 222 | */ |
| 196 | 223 | public function frontend_indicator() { |
| 197 | - $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() ) ); | |
| 198 | 226 | |
| 199 | 227 | /** |
| 200 | 228 | * Filter allows the HTML output of the frontend indicator comment |
| 201 | 229 | * to be altered or removed, if desired. |
| @@ -204,9 +232,9 @@ | ||
| 204 | 232 | */ |
| 205 | 233 | $comment = apply_filters( 'wp_stream_frontend_indicator', $comment ); |
| 206 | 234 | |
| 207 | 235 | if ( ! empty( $comment ) ) { |
| 208 | - echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok | |
| 236 | + echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok. | |
| 209 | 237 | } |
| 210 | 238 | } |
| 211 | 239 | |
| 212 | 240 | /** |
| @@ -212,17 +240,15 @@ | ||
| 212 | 240 | /** |
| 213 | 241 | * Version of plugin_dir_url() which works for plugins installed in the plugins directory, |
| 214 | 242 | * and for plugins bundled with themes. |
| 215 | 243 | * |
| 216 | - * @throws \Exception | |
| 217 | - * | |
| 218 | 244 | * @return array |
| 219 | 245 | */ |
| 220 | 246 | 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'; | |
| 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'; | |
| 225 | 251 | |
| 226 | 252 | return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); |
| 227 | 253 | } |
| 228 | 254 | |
| @@ -238,9 +264,9 @@ | ||
| 238 | 264 | /** |
| 239 | 265 | * Change plugin database driver in case driver plugin loaded after stream |
| 240 | 266 | */ |
| 241 | 267 | public function plugins_loaded() { |
| 242 | - // Load DB helper interface/class | |
| 268 | + // Load DB helper interface/class. | |
| 243 | 269 | $driver_class = apply_filters( 'wp_stream_db_driver', '\WP_Stream\DB_Driver_WPDB' ); |
| 244 | 270 | |
| 245 | 271 | if ( class_exists( $driver_class ) ) { |
| 246 | 272 | $driver = new $driver_class(); |
| @@ -245,6 +271,48 @@ | ||
| 245 | 271 | if ( class_exists( $driver_class ) ) { |
| 246 | 272 | $driver = new $driver_class(); |
| 247 | 273 | $this->db = new DB( $driver ); |
| 248 | 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; | |
| 249 | 317 | } |
| 250 | 318 | } |