| 1 |
<?php |
| 2 |
/** |
| 3 |
* Initializes plugin |
| 4 |
* |
| 5 |
* @package WP_Stream; |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Plugin |
| 12 |
*/ |
| 13 |
class Plugin { |
| 14 |
/** |
| 15 |
* Plugin version number. |
| 16 |
* |
| 17 |
* TODO Maybe pass this as a constructor dependency? |
| 18 |
* |
| 19 |
* @const string |
| 20 |
*/ |
| 21 |
const VERSION = '3.7.0'; |
| 22 |
|
| 23 |
/** |
| 24 |
* WP-CLI command |
| 25 |
* |
| 26 |
* @const string |
| 27 |
*/ |
| 28 |
const WP_CLI_COMMAND = 'stream'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Holds and manages WordPress Admin configurations. |
| 32 |
* |
| 33 |
* @var Admin |
| 34 |
*/ |
| 35 |
public $admin; |
| 36 |
|
| 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 |
* |
| 54 |
* @var Connectors |
| 55 |
*/ |
| 56 |
public $connectors; |
| 57 |
|
| 58 |
/** |
| 59 |
* Holds and manages DB connections. |
| 60 |
* |
| 61 |
* @var DB |
| 62 |
*/ |
| 63 |
public $db; |
| 64 |
|
| 65 |
/** |
| 66 |
* Holds and manages records. |
| 67 |
* |
| 68 |
* @var Log |
| 69 |
*/ |
| 70 |
public $log; |
| 71 |
|
| 72 |
/** |
| 73 |
* Stores and manages WordPress settings. |
| 74 |
* |
| 75 |
* @var Settings |
| 76 |
*/ |
| 77 |
public $settings; |
| 78 |
|
| 79 |
/** |
| 80 |
* Process DB migrations. |
| 81 |
* |
| 82 |
* @var Install |
| 83 |
*/ |
| 84 |
public $install; |
| 85 |
|
| 86 |
/** |
| 87 |
* URLs and Paths used by the plugin |
| 88 |
* |
| 89 |
* @var array |
| 90 |
*/ |
| 91 |
public $locations = array(); |
| 92 |
|
| 93 |
/** |
| 94 |
* Class constructor |
| 95 |
*/ |
| 96 |
public function __construct() { |
| 97 |
$locate = $this->locate_plugin(); |
| 98 |
|
| 99 |
$this->locations = array( |
| 100 |
'plugin' => $locate['plugin_basename'], |
| 101 |
'dir' => $locate['dir_path'], |
| 102 |
'url' => $locate['dir_url'], |
| 103 |
'inc_dir' => $locate['dir_path'] . 'includes/', |
| 104 |
'class_dir' => $locate['dir_path'] . 'classes/', |
| 105 |
); |
| 106 |
|
| 107 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 108 |
|
| 109 |
// Load helper functions. |
| 110 |
require_once $this->locations['inc_dir'] . 'functions.php'; |
| 111 |
|
| 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 ); |
| 119 |
} |
| 120 |
|
| 121 |
$error = false; |
| 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 ) { |
| 129 |
wp_die( |
| 130 |
esc_html( $error ), |
| 131 |
esc_html__( 'Stream DB Error', 'stream' ) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
// Load languages. |
| 136 |
add_action( 'plugins_loaded', array( $this, 'i18n' ) ); |
| 137 |
|
| 138 |
// Load logger class. |
| 139 |
$this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) ); |
| 140 |
|
| 141 |
// Load settings and connectors after widgets_init and before the default init priority. |
| 142 |
add_action( 'init', array( $this, 'init' ), 9 ); |
| 143 |
|
| 144 |
// Add frontend indicator. |
| 145 |
add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 146 |
|
| 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 ) ) { |
| 152 |
$this->admin = new Admin( $this ); |
| 153 |
$this->install = $driver->setup_storage( $this ); |
| 154 |
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 155 |
$this->admin = new Admin( $this, $driver ); |
| 156 |
} |
| 157 |
|
| 158 |
// Load WP-CLI command. |
| 159 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 160 |
\WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Autoloader for classes |
| 166 |
* |
| 167 |
* @param string $class Fully qualified classname to be loaded. |
| 168 |
*/ |
| 169 |
public function autoload( $class ) { |
| 170 |
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
static $reflection; |
| 175 |
|
| 176 |
if ( empty( $reflection ) ) { |
| 177 |
$reflection = new \ReflectionObject( $this ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$autoload_name = $matches['autoload']; |
| 185 |
$autoload_dir = \trailingslashit( $this->locations['class_dir'] ); |
| 186 |
$autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) ); |
| 187 |
|
| 188 |
if ( is_readable( $autoload_path ) ) { |
| 189 |
require_once $autoload_path; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Loads the translation files. |
| 195 |
* |
| 196 |
* @action plugins_loaded |
| 197 |
*/ |
| 198 |
public function i18n() { |
| 199 |
load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Load Settings, Notifications, and Connectors |
| 204 |
* |
| 205 |
* @action init |
| 206 |
*/ |
| 207 |
public function init() { |
| 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 |
|
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Displays an HTML comment in the frontend head to indicate that Stream is activated, |
| 217 |
* and which version of Stream is currently in use. |
| 218 |
* |
| 219 |
* @action wp_head |
| 220 |
* |
| 221 |
* @return string|void An HTML comment, or nothing if the value is filtered out. |
| 222 |
*/ |
| 223 |
public function frontend_indicator() { |
| 224 |
/* translators: Localization not needed */ |
| 225 |
$comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) ); |
| 226 |
|
| 227 |
/** |
| 228 |
* Filter allows the HTML output of the frontend indicator comment |
| 229 |
* to be altered or removed, if desired. |
| 230 |
* |
| 231 |
* @return string The content of the HTML comment |
| 232 |
*/ |
| 233 |
$comment = apply_filters( 'wp_stream_frontend_indicator', $comment ); |
| 234 |
|
| 235 |
if ( ! empty( $comment ) ) { |
| 236 |
echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok. |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Version of plugin_dir_url() which works for plugins installed in the plugins directory, |
| 242 |
* and for plugins bundled with themes. |
| 243 |
* |
| 244 |
* @return array |
| 245 |
*/ |
| 246 |
private function locate_plugin() { |
| 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'; |
| 251 |
|
| 252 |
return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Getter for the version number. |
| 257 |
* |
| 258 |
* @return string |
| 259 |
*/ |
| 260 |
public function get_version() { |
| 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; |
| 317 |
} |
| 318 |
} |
| 319 |
|