| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Stream |
| 4 |
* Plugin URI: https://wp-stream.com/ |
| 5 |
* Description: Stream tracks logged-in user activity so you can monitor every change made on your WordPress site in beautifully organized detail. All activity is organized by context, action and IP address for easy filtering. Developers can extend Stream with custom connectors to log any kind of action. |
| 6 |
* Version: 2.0.1 |
| 7 |
* Author: Stream |
| 8 |
* Author URI: https://wp-stream.com/ |
| 9 |
* License: GPLv2+ |
| 10 |
* Text Domain: stream |
| 11 |
* Domain Path: /languages |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Copyright (c) 2014 WP Stream Pty Ltd (https://wp-stream.com/) |
| 16 |
* |
| 17 |
* This program is free software; you can redistribute it and/or modify |
| 18 |
* it under the terms of the GNU General Public License, version 2 or, at |
| 19 |
* your discretion, any later version, as published by the Free |
| 20 |
* Software Foundation. |
| 21 |
* |
| 22 |
* This program is distributed in the hope that it will be useful, |
| 23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
* GNU General Public License for more details. |
| 26 |
* |
| 27 |
* You should have received a copy of the GNU General Public License |
| 28 |
* along with this program; if not, write to the Free Software |
| 29 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 |
*/ |
| 31 |
|
| 32 |
class WP_Stream { |
| 33 |
|
| 34 |
/** |
| 35 |
* Plugin version number |
| 36 |
* |
| 37 |
* @const string |
| 38 |
*/ |
| 39 |
const VERSION = '2.0.1'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Hold Stream instance |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
public static $instance; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var WP_Stream_DB_Base |
| 50 |
*/ |
| 51 |
public static $db; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var WP_Stream_API |
| 55 |
*/ |
| 56 |
public static $api; |
| 57 |
|
| 58 |
/** |
| 59 |
* Admin notices, collected and displayed on proper action |
| 60 |
* |
| 61 |
* @var array |
| 62 |
*/ |
| 63 |
public static $notices = array(); |
| 64 |
|
| 65 |
/** |
| 66 |
* An array of deprecated extension plugin class/dir pairs |
| 67 |
* |
| 68 |
* @var array |
| 69 |
*/ |
| 70 |
public static $deprecated_extensions = array( |
| 71 |
'WP_Stream_Cherry_Pick' => 'stream-cherry-pick/stream-cherry-pick.php', |
| 72 |
'WP_Stream_Data_Exporter' => 'stream-data-exporter/stream-data-exporter.php', |
| 73 |
'WP_Stream_Notifications' => 'stream-notifications/stream-notifications.php', |
| 74 |
'WP_Stream_Reports' => 'stream-reports/stream-reports.php', |
| 75 |
); |
| 76 |
|
| 77 |
/** |
| 78 |
* Class constructor |
| 79 |
*/ |
| 80 |
private function __construct() { |
| 81 |
define( 'WP_STREAM_PLUGIN', plugin_basename( __FILE__ ) ); |
| 82 |
define( 'WP_STREAM_DIR', plugin_dir_path( __FILE__ ) ); |
| 83 |
define( 'WP_STREAM_URL', plugin_dir_url( __FILE__ ) ); |
| 84 |
define( 'WP_STREAM_INC_DIR', WP_STREAM_DIR . 'includes/' ); |
| 85 |
define( 'WP_STREAM_CLASS_DIR', WP_STREAM_DIR . 'classes/' ); |
| 86 |
define( 'WP_STREAM_EXTENSIONS_DIR', WP_STREAM_DIR . 'extensions/' ); |
| 87 |
|
| 88 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 89 |
|
| 90 |
// Load helper functions |
| 91 |
require_once WP_STREAM_INC_DIR . 'functions.php'; |
| 92 |
|
| 93 |
// Load DB helper interface/class |
| 94 |
$driver = 'WP_Stream_DB'; |
| 95 |
if ( class_exists( $driver ) ) { |
| 96 |
self::$db = new $driver; |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! self::$db ) { |
| 100 |
wp_die( __( 'Stream: Could not load chosen DB driver.', 'stream' ), 'Stream DB Error' ); |
| 101 |
} |
| 102 |
|
| 103 |
// Load API helper interface/class |
| 104 |
self::$api = new WP_Stream_API; |
| 105 |
|
| 106 |
// Install the plugin |
| 107 |
add_action( 'wp_stream_before_db_notices', array( __CLASS__, 'install' ) ); |
| 108 |
|
| 109 |
// Load languages |
| 110 |
add_action( 'plugins_loaded', array( __CLASS__, 'i18n' ) ); |
| 111 |
|
| 112 |
// Load settings, enabling extensions to hook in |
| 113 |
add_action( 'init', array( 'WP_Stream_Settings', 'load' ), 9 ); |
| 114 |
|
| 115 |
// Load logger class |
| 116 |
add_action( 'plugins_loaded', array( 'WP_Stream_Log', 'load' ) ); |
| 117 |
|
| 118 |
// Load connectors after widgets_init, but before the default of 10 |
| 119 |
add_action( 'init', array( 'WP_Stream_Connectors', 'load' ), 9 ); |
| 120 |
|
| 121 |
// Load extensions |
| 122 |
foreach ( glob( WP_STREAM_EXTENSIONS_DIR . '*' ) as $extension ) { |
| 123 |
require_once sprintf( '%s/class-wp-stream-%s.php', $extension, basename( $extension ) ); |
| 124 |
} |
| 125 |
|
| 126 |
// Load support for feeds |
| 127 |
add_action( 'init', array( 'WP_Stream_Feeds', 'load' ) ); |
| 128 |
|
| 129 |
// Add frontend indicator |
| 130 |
add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 131 |
|
| 132 |
if ( is_admin() ) { |
| 133 |
add_action( 'plugins_loaded', array( 'WP_Stream_Admin', 'load' ) ); |
| 134 |
|
| 135 |
add_action( 'plugins_loaded', array( 'WP_Stream_Dashboard_Widget', 'load' ) ); |
| 136 |
|
| 137 |
add_action( 'plugins_loaded', array( 'WP_Stream_Live_Update', 'load' ) ); |
| 138 |
|
| 139 |
add_action( 'plugins_loaded', array( 'WP_Stream_Pointers', 'load' ) ); |
| 140 |
|
| 141 |
add_action( 'plugins_loaded', array( 'WP_Stream_Migrate', 'load' ) ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Invoked when the PHP version check fails. Load up the translations and |
| 147 |
* add the error message to the admin notices |
| 148 |
*/ |
| 149 |
public static function fail_php_version() { |
| 150 |
add_action( 'plugins_loaded', array( __CLASS__, 'i18n' ) ); |
| 151 |
self::notice( __( 'Stream requires PHP version 5.3+, plugin is currently NOT ACTIVE.', 'stream' ) ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* |
| 156 |
*/ |
| 157 |
public static function deprecated_plugins_exist() { |
| 158 |
foreach ( self::$deprecated_extensions as $class => $dir ) { |
| 159 |
if ( class_exists( $class ) ) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* |
| 169 |
*/ |
| 170 |
public static function deprecated_plugins_notice() { |
| 171 |
add_action( 'plugins_loaded', array( __CLASS__, 'i18n' ) ); |
| 172 |
|
| 173 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 174 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 175 |
} |
| 176 |
|
| 177 |
$message = null; |
| 178 |
|
| 179 |
foreach ( self::$deprecated_extensions as $class => $dir ) { |
| 180 |
if ( class_exists( $class ) ) { |
| 181 |
$data = get_plugin_data( sprintf( '%s/%s', WP_PLUGIN_DIR, $dir ) ); |
| 182 |
$name = isset( $data['Name'] ) ? $data['Name'] : $dir; |
| 183 |
$message .= sprintf( '<li><strong>%s</strong></li>', esc_html( $name ) ); |
| 184 |
|
| 185 |
deactivate_plugins( $dir ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if ( ! empty( $message ) ) { |
| 190 |
ob_start(); |
| 191 |
?> |
| 192 |
<h3><?php _e( 'Deprecated Plugins Found', 'stream' ) ?></h3> |
| 193 |
<p><?php _e( 'The following plugins are deprecated and will be deactivated in order to activate', 'stream' ) ?> <strong>Stream <?php echo esc_html( self::VERSION ) ?></strong>:</p> |
| 194 |
<ul> |
| 195 |
<?php |
| 196 |
$start = ob_get_clean(); |
| 197 |
|
| 198 |
ob_start(); |
| 199 |
?> |
| 200 |
</ul> |
| 201 |
<p> |
| 202 |
<a href='#' onclick="location.reload(true); return false;" class="button button-large"><?php _e( 'Continue', 'stream' ) ?></a> |
| 203 |
</p> |
| 204 |
<?php |
| 205 |
$end = ob_get_clean(); |
| 206 |
|
| 207 |
wp_die( $start . $message . $end, __( 'Deprecated Plugins Found', 'stream' ) ); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Autoloader for classes |
| 213 |
* |
| 214 |
* @param string $class |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
function autoload( $class ) { |
| 218 |
$class = strtolower( str_replace( '_', '-', $class ) ); |
| 219 |
$class_file = sprintf( '%sclass-%s.php', WP_STREAM_CLASS_DIR, $class ); |
| 220 |
|
| 221 |
if ( is_readable( $class_file ) ) { |
| 222 |
require_once $class_file; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Loads the translation files. |
| 228 |
* |
| 229 |
* @access public |
| 230 |
* @action plugins_loaded |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public static function i18n() { |
| 234 |
load_plugin_textdomain( 'stream', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Whether the current PHP version meets the minimum requirements |
| 239 |
* |
| 240 |
* @return bool |
| 241 |
*/ |
| 242 |
public static function is_valid_php_version() { |
| 243 |
return version_compare( PHP_VERSION, '5.3', '>=' ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Is Stream connected? |
| 248 |
* |
| 249 |
* @return bool |
| 250 |
*/ |
| 251 |
public static function is_connected() { |
| 252 |
return ( self::$api->api_key && self::$api->site_uuid ); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Is Stream in development mode? |
| 257 |
* |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
public static function is_development_mode() { |
| 261 |
$development_mode = false; |
| 262 |
|
| 263 |
if ( defined( 'WP_STREAM_DEV_DEBUG' ) ) { |
| 264 |
$development_mode = WP_STREAM_DEV_DEBUG; |
| 265 |
} else if ( site_url() && false === strpos( site_url(), '.' ) ) { |
| 266 |
$development_mode = true; |
| 267 |
} |
| 268 |
|
| 269 |
return apply_filters( 'wp_stream_development_mode', $development_mode ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Handle notice messages according to the appropriate context (WP-CLI or the WP Admin) |
| 274 |
* |
| 275 |
* @param string $message |
| 276 |
* @param bool $is_error |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
public static function notice( $message, $is_error = true ) { |
| 280 |
if ( defined( 'WP_CLI' ) ) { |
| 281 |
$message = strip_tags( $message ); |
| 282 |
if ( $is_error ) { |
| 283 |
WP_CLI::warning( $message ); |
| 284 |
} else { |
| 285 |
WP_CLI::success( $message ); |
| 286 |
} |
| 287 |
} else { |
| 288 |
// Trigger admin notices late, so that any notices which occur during page load are displayed |
| 289 |
add_action( 'shutdown', array( __CLASS__, 'admin_notices' ) ); |
| 290 |
|
| 291 |
$notice = compact( 'message', 'is_error' ); |
| 292 |
|
| 293 |
if ( ! in_array( $notice, self::$notices ) ) { |
| 294 |
self::$notices[] = $notice; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Show an error or other message in the WP Admin |
| 301 |
* |
| 302 |
* @action all_admin_notices |
| 303 |
* @return void |
| 304 |
*/ |
| 305 |
public static function admin_notices() { |
| 306 |
global $allowedposttags; |
| 307 |
|
| 308 |
$custom = array( |
| 309 |
'progress' => array( |
| 310 |
'class' => true, |
| 311 |
'id' => true, |
| 312 |
'max' => true, |
| 313 |
'style' => true, |
| 314 |
'value' => true, |
| 315 |
), |
| 316 |
); |
| 317 |
|
| 318 |
$allowed_html = array_merge( $allowedposttags, $custom ); |
| 319 |
|
| 320 |
ksort( $allowed_html ); |
| 321 |
|
| 322 |
foreach ( self::$notices as $notice ) { |
| 323 |
$class_name = empty( $notice['is_error'] ) ? 'updated' : 'error'; |
| 324 |
$html_message = sprintf( '<div class="%s">%s</div>', esc_attr( $class_name ), wpautop( $notice['message'] ) ); |
| 325 |
|
| 326 |
echo wp_kses( $html_message, $allowed_html ); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Displays an HTML comment in the frontend head to indicate that Stream is activated, |
| 332 |
* and which version of Stream is currently in use. |
| 333 |
* |
| 334 |
* @since 1.4.5 |
| 335 |
* |
| 336 |
* @action wp_head |
| 337 |
* @return string|void An HTML comment, or nothing if the value is filtered out. |
| 338 |
*/ |
| 339 |
public function frontend_indicator() { |
| 340 |
$comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( self::VERSION ) ); // Localization not needed |
| 341 |
|
| 342 |
/** |
| 343 |
* Filter allows the HTML output of the frontend indicator comment |
| 344 |
* to be altered or removed, if desired. |
| 345 |
* |
| 346 |
* @return string $comment The content of the HTML comment |
| 347 |
*/ |
| 348 |
$comment = apply_filters( 'wp_stream_frontend_indicator', $comment ); |
| 349 |
|
| 350 |
if ( ! empty( $comment ) ) { |
| 351 |
echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Return active instance of WP_Stream, create one if it doesn't exist |
| 357 |
* |
| 358 |
* @return WP_Stream |
| 359 |
*/ |
| 360 |
public static function get_instance() { |
| 361 |
if ( empty( self::$instance ) ) { |
| 362 |
$class = __CLASS__; |
| 363 |
self::$instance = new $class; |
| 364 |
} |
| 365 |
|
| 366 |
return self::$instance; |
| 367 |
} |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
if ( ! WP_Stream::is_valid_php_version() ) { |
| 372 |
WP_Stream::fail_php_version(); |
| 373 |
} elseif ( WP_Stream::deprecated_plugins_exist() ) { |
| 374 |
WP_Stream::deprecated_plugins_notice(); |
| 375 |
} else { |
| 376 |
$GLOBALS['wp_stream'] = WP_Stream::get_instance(); |
| 377 |
} |
| 378 |
|
| 379 |
register_deactivation_hook( __FILE__, array( 'WP_Stream_Admin', 'remove_api_authentication' ) ); |
| 380 |
|