| 1 |
<?php |
| 2 |
namespace WP_Stream; |
| 3 |
|
| 4 |
class Plugin { |
| 5 |
/** |
| 6 |
* Plugin version number |
| 7 |
* |
| 8 |
* @const string |
| 9 |
*/ |
| 10 |
const VERSION = '3.0.5'; |
| 11 |
|
| 12 |
/** |
| 13 |
* WP-CLI command |
| 14 |
* |
| 15 |
* @const string |
| 16 |
*/ |
| 17 |
const WP_CLI_COMMAND = 'stream'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Admin |
| 21 |
*/ |
| 22 |
public $admin; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Connectors |
| 26 |
*/ |
| 27 |
public $connectors; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var DB |
| 31 |
*/ |
| 32 |
public $db; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var Log |
| 36 |
*/ |
| 37 |
public $log; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var Settings |
| 41 |
*/ |
| 42 |
public $settings; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var Install |
| 46 |
*/ |
| 47 |
public $install; |
| 48 |
|
| 49 |
/** |
| 50 |
* URLs and Paths used by the plugin |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
public $locations = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Class constructor |
| 58 |
*/ |
| 59 |
public function __construct() { |
| 60 |
$locate = $this->locate_plugin(); |
| 61 |
|
| 62 |
$this->locations = array( |
| 63 |
'plugin' => $locate['plugin_basename'], |
| 64 |
'dir' => $locate['dir_path'], |
| 65 |
'url' => $locate['dir_url'], |
| 66 |
'inc_dir' => $locate['dir_path'] . 'includes/', |
| 67 |
'class_dir' => $locate['dir_path'] . 'classes/', |
| 68 |
); |
| 69 |
|
| 70 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 71 |
|
| 72 |
// Load helper functions |
| 73 |
require_once $this->locations['inc_dir'] . 'functions.php'; |
| 74 |
|
| 75 |
// Load DB helper interface/class |
| 76 |
$driver = '\WP_Stream\DB'; |
| 77 |
if ( class_exists( $driver ) ) { |
| 78 |
$this->db = new DB( $this ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! $this->db ) { |
| 82 |
wp_die( |
| 83 |
esc_html__( 'Stream: Could not load chosen DB driver.', 'stream' ), |
| 84 |
esc_html__( 'Stream DB Error', 'stream' ) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
// Load languages |
| 89 |
add_action( 'plugins_loaded', array( $this, 'i18n' ) ); |
| 90 |
|
| 91 |
// Load logger class |
| 92 |
$this->log = apply_filters( 'wp_stream_log_handler', new Log( $this ) ); |
| 93 |
|
| 94 |
// Load settings and connectors after widgets_init and before the default init priority |
| 95 |
add_action( 'init', array( $this, 'init' ), 9 ); |
| 96 |
|
| 97 |
// Add frontend indicator |
| 98 |
add_action( 'wp_head', array( $this, 'frontend_indicator' ) ); |
| 99 |
|
| 100 |
// Load admin area classes |
| 101 |
if ( is_admin() || ( defined( 'WP_STREAM_DEV_DEBUG' ) && WP_STREAM_DEV_DEBUG ) ) { |
| 102 |
$this->admin = new Admin( $this ); |
| 103 |
$this->install = new Install( $this ); |
| 104 |
} |
| 105 |
|
| 106 |
// Load WP-CLI command |
| 107 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 108 |
\WP_CLI::add_command( self::WP_CLI_COMMAND, 'WP_Stream\CLI' ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Autoloader for classes |
| 114 |
* |
| 115 |
* @param string $class |
| 116 |
*/ |
| 117 |
function autoload( $class ) { |
| 118 |
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
static $reflection; |
| 123 |
|
| 124 |
if ( empty( $reflection ) ) { |
| 125 |
$reflection = new \ReflectionObject( $this ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$autoload_name = $matches['autoload']; |
| 133 |
$autoload_dir = \trailingslashit( $this->locations['class_dir'] ); |
| 134 |
$autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) ); |
| 135 |
|
| 136 |
if ( is_readable( $autoload_path ) ) { |
| 137 |
require_once $autoload_path; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Loads the translation files. |
| 143 |
* |
| 144 |
* @action plugins_loaded |
| 145 |
*/ |
| 146 |
public function i18n() { |
| 147 |
load_plugin_textdomain( 'stream', false, dirname( $this->locations['plugin'] ) . '/languages/' ); |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
* Load Settings and Connectors |
| 152 |
* |
| 153 |
* @action init |
| 154 |
*/ |
| 155 |
public function init() { |
| 156 |
$this->settings = new Settings( $this ); |
| 157 |
$this->connectors = new Connectors( $this ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Displays an HTML comment in the frontend head to indicate that Stream is activated, |
| 162 |
* and which version of Stream is currently in use. |
| 163 |
* |
| 164 |
* @action wp_head |
| 165 |
* |
| 166 |
* @return string|void An HTML comment, or nothing if the value is filtered out. |
| 167 |
*/ |
| 168 |
public function frontend_indicator() { |
| 169 |
$comment = sprintf( 'Stream WordPress user activity plugin v%s', esc_html( $this->get_version() ) ); // Localization not needed |
| 170 |
|
| 171 |
/** |
| 172 |
* Filter allows the HTML output of the frontend indicator comment |
| 173 |
* to be altered or removed, if desired. |
| 174 |
* |
| 175 |
* @return string The content of the HTML comment |
| 176 |
*/ |
| 177 |
$comment = apply_filters( 'wp_stream_frontend_indicator', $comment ); |
| 178 |
|
| 179 |
if ( ! empty( $comment ) ) { |
| 180 |
echo sprintf( "<!-- %s -->\n", esc_html( $comment ) ); // xss ok |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Version of plugin_dir_url() which works for plugins installed in the plugins directory, |
| 186 |
* and for plugins bundled with themes. |
| 187 |
* |
| 188 |
* @throws \Exception |
| 189 |
* |
| 190 |
* @return array |
| 191 |
*/ |
| 192 |
private function locate_plugin() { |
| 193 |
$dir_url = trailingslashit( plugins_url( '', dirname( __FILE__ ) ) ); |
| 194 |
$dir_path = plugin_dir_path( dirname( __FILE__ ) ); |
| 195 |
$dir_basename = basename( $dir_path ); |
| 196 |
$plugin_basename = trailingslashit( $dir_basename ) . $dir_basename. '.php'; |
| 197 |
|
| 198 |
return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Getter for the version number. |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
public function get_version() { |
| 207 |
return self::VERSION; |
| 208 |
} |
| 209 |
} |
| 210 |
|