| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Autoloads files with classes when needed. |
| 5 |
* |
| 6 |
* @since 0.0.0 |
| 7 |
* @param string $class_name Name of the class being requested. |
| 8 |
*/ |
| 9 |
function pd_troubleshoot_autoload_classes( $class_name ) { |
| 10 |
|
| 11 |
// If our class doesn't have our prefix, don't load it. |
| 12 |
if ( 0 !== strpos( $class_name, 'PDT_' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
// Set up our filename. |
| 17 |
$filename = strtolower( str_replace( '_', '-', substr( $class_name, strlen( 'PDT_' ) ) ) ); |
| 18 |
// Include our file. |
| 19 |
PD_Troubleshoot::include_file( 'includes/class-' . $filename ); |
| 20 |
} |
| 21 |
spl_autoload_register( 'pd_troubleshoot_autoload_classes' ); |
| 22 |
|
| 23 |
/** |
| 24 |
* Main initiation class. |
| 25 |
* |
| 26 |
* @since 0.0.0 |
| 27 |
*/ |
| 28 |
final class PD_Troubleshoot { |
| 29 |
|
| 30 |
/** |
| 31 |
* Current version. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
* @since 0.0.0 |
| 35 |
*/ |
| 36 |
const VERSION = '1.2'; |
| 37 |
|
| 38 |
/** |
| 39 |
* URL of plugin directory. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
* @since 0.0.0 |
| 43 |
*/ |
| 44 |
protected $url = ''; |
| 45 |
|
| 46 |
/** |
| 47 |
* Path of plugin directory. |
| 48 |
* |
| 49 |
* @var string |
| 50 |
* @since 0.0.0 |
| 51 |
*/ |
| 52 |
protected $path = ''; |
| 53 |
|
| 54 |
/** |
| 55 |
* Plugin basename. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
* @since 0.0.0 |
| 59 |
*/ |
| 60 |
protected $basename = ''; |
| 61 |
|
| 62 |
/** |
| 63 |
* Detailed activation error messages. |
| 64 |
* |
| 65 |
* @var array |
| 66 |
* @since 0.0.0 |
| 67 |
*/ |
| 68 |
protected $activation_errors = array(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Singleton instance of plugin. |
| 72 |
* |
| 73 |
* @var PD_Troubleshoot |
| 74 |
* @since 0.0.0 |
| 75 |
*/ |
| 76 |
protected static $single_instance = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* Instance of PDT_Filesystem |
| 80 |
* |
| 81 |
* @since0.0.0 |
| 82 |
* @var PDT_Filesystem |
| 83 |
*/ |
| 84 |
protected $filesystem; |
| 85 |
|
| 86 |
/** |
| 87 |
* Instance of PDT_Constants |
| 88 |
* |
| 89 |
* @since0.0.0 |
| 90 |
* @var PDT_Constants |
| 91 |
*/ |
| 92 |
protected $constants; |
| 93 |
|
| 94 |
/** |
| 95 |
* Creates or returns an instance of this class. |
| 96 |
* |
| 97 |
* @since 0.0.0 |
| 98 |
* @return PD_Troubleshoot A single instance of this class. |
| 99 |
*/ |
| 100 |
public static function get_instance() { |
| 101 |
if ( null === self::$single_instance ) { |
| 102 |
self::$single_instance = new self(); |
| 103 |
} |
| 104 |
|
| 105 |
return self::$single_instance; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Sets up our plugin. |
| 110 |
* |
| 111 |
* @since 0.0.0 |
| 112 |
*/ |
| 113 |
protected function __construct() { |
| 114 |
$this->constants = new PDT_Constants( $this ); |
| 115 |
$this->bootstrap = new PDT_Bootstrap( $this ); |
| 116 |
|
| 117 |
if ( !defined('WP_CONTENT_URL') ) |
| 118 |
define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); // full url - WP_CONTENT_DIR is defined further up |
| 119 |
|
| 120 |
/** |
| 121 |
* Allows for the plugins directory to be moved from the default location. |
| 122 |
* |
| 123 |
* @since 2.6.0 |
| 124 |
*/ |
| 125 |
if ( !defined('WP_PLUGIN_URL') ) |
| 126 |
define( 'WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins' ); // full url, no trailing slash |
| 127 |
|
| 128 |
/** |
| 129 |
* Allows for the mu-plugins directory to be moved from the default location. |
| 130 |
* |
| 131 |
* @since 2.8.0 |
| 132 |
*/ |
| 133 |
if ( !defined('WPMU_PLUGIN_URL') ) |
| 134 |
define( 'WPMU_PLUGIN_URL', WP_CONTENT_URL . '/mu-plugins' ); // full url, no trailing slash |
| 135 |
|
| 136 |
|
| 137 |
$this->basename = plugin_basename( __FILE__ ); |
| 138 |
$this->url = plugin_dir_url( __FILE__ ); |
| 139 |
$this->path = plugin_dir_path( __FILE__ ); |
| 140 |
|
| 141 |
$this->plugin_classes(); |
| 142 |
} |
| 143 |
|
| 144 |
public static function maybe_fix_protocol( $url, $desired_protocol = null ) { |
| 145 |
$pos_of_colon_slash_slash = strpos( $url, '://' ); |
| 146 |
if ( $pos_of_colon_slash_slash === false ) { |
| 147 |
return $url; |
| 148 |
} |
| 149 |
|
| 150 |
if ( empty( $desired_protocol ) ) { |
| 151 |
if ( defined( 'PD_PROTOCOL' ) && str_to_lower( PD_PROTOCOL ) === 'https' ) { |
| 152 |
$desired_protocol = 'https'; |
| 153 |
} else { |
| 154 |
$desired_protocol = 'http'; |
| 155 |
} |
| 156 |
if ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) { |
| 157 |
$desired_protocol = 'https'; |
| 158 |
} elseif ( !empty( $_SERVER['REDIRECT_HTTPS'] ) && $_SERVER['REDIRECT_HTTPS'] !== 'off' ) { |
| 159 |
$desired_protocol = 'https'; |
| 160 |
} elseif ( !empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) { |
| 161 |
$desired_protocol = 'https'; |
| 162 |
} elseif ( !empty( $_SERVER['protocol'] ) ) { |
| 163 |
$desired_protocol = strtolower( substr( $_SERVER["SERVER_PROTOCOL"], 0, 5 ) ) == 'https' ? 'https' : 'http'; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$url = $desired_protocol . '://' . substr( $url, $pos_of_colon_slash_slash + 3 ); |
| 168 |
|
| 169 |
return $url; |
| 170 |
} |
| 171 |
|
| 172 |
public static function maybe_fix_www_prefix( $url, $should_be_www = null ) { |
| 173 |
$pos_of_colon_slash_slash = strpos( $url, '://' ); |
| 174 |
if ( $pos_of_colon_slash_slash === false ) { |
| 175 |
return $url; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $should_be_www === null ) { |
| 179 |
if ( strpos( $_SERVER['HTTP_HOST'], 'www.' ) === 0 ) { |
| 180 |
$should_be_www = true; |
| 181 |
} else { |
| 182 |
$should_be_www = false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( !empty( $should_be_www ) ) { |
| 187 |
$url = str_replace( array( |
| 188 |
'://', |
| 189 |
'://www.www.', |
| 190 |
), array( |
| 191 |
'://www.', |
| 192 |
'://www.', |
| 193 |
), $url ); |
| 194 |
} else { |
| 195 |
$url = str_replace( array( |
| 196 |
'://www.www.', |
| 197 |
'://www.', |
| 198 |
), array( |
| 199 |
'://', |
| 200 |
'://', |
| 201 |
), $url ); |
| 202 |
} |
| 203 |
|
| 204 |
return $url; |
| 205 |
} |
| 206 |
|
| 207 |
public function get_api_params() { |
| 208 |
return array( |
| 209 |
'site_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( site_url() ) ), |
| 210 |
'api_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'api.php' ) ) ), |
| 211 |
'static_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'app/dist/static' ) ) ), |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Attach other plugin classes to the base plugin class. |
| 217 |
* |
| 218 |
* @since 0.0.0 |
| 219 |
*/ |
| 220 |
public function plugin_classes() { |
| 221 |
$this->filesystem = new PDT_Filesystem( $this ); |
| 222 |
$this->auth = new PDT_Auth( $this ); |
| 223 |
$this->api = new PDT_Api( $this ); |
| 224 |
$this->plugins = new PDT_Plugins( $this ); |
| 225 |
$this->installed = new PDT_Installed( $this ); |
| 226 |
$this->cases = new PDT_Cases( $this ); |
| 227 |
$this->clues = new PDT_Clues( $this ); |
| 228 |
$this->detective = new PDT_Detective( $this ); |
| 229 |
} // END OF PLUGIN CLASSES FUNCTION |
| 230 |
|
| 231 |
/** |
| 232 |
* Activate the plugin. |
| 233 |
* |
| 234 |
* @since 0.0.0 |
| 235 |
*/ |
| 236 |
public function _activate() { |
| 237 |
// Bail early if requirements aren't met. |
| 238 |
if ( ! $this->check_requirements() ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
// Make sure any rewrite functionality has been loaded. |
| 243 |
flush_rewrite_rules(); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Deactivate the plugin. |
| 248 |
* Uninstall routines should be in uninstall.php. |
| 249 |
* |
| 250 |
* @since 0.0.0 |
| 251 |
*/ |
| 252 |
public function _deactivate() { |
| 253 |
// Add deactivation cleanup functionality here. |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Init hooks |
| 258 |
* |
| 259 |
* @since 0.0.0 |
| 260 |
*/ |
| 261 |
public function init() { |
| 262 |
|
| 263 |
// Bail early if requirements aren't met. |
| 264 |
if ( ! $this->check_requirements() ) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
// Load translated strings for plugin. |
| 269 |
load_plugin_textdomain( 'plugin-detective', false, dirname( $this->basename ) . '/languages/' ); |
| 270 |
|
| 271 |
|
| 272 |
// Initialize plugin classes. |
| 273 |
$this->plugin_classes(); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Check if the plugin meets requirements and |
| 278 |
* disable it if they are not present. |
| 279 |
* |
| 280 |
* @since 0.0.0 |
| 281 |
* |
| 282 |
* @return boolean True if requirements met, false if not. |
| 283 |
*/ |
| 284 |
public function check_requirements() { |
| 285 |
|
| 286 |
// Bail early if plugin meets requirements. |
| 287 |
if ( $this->meets_requirements() ) { |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
// Add a dashboard notice. |
| 292 |
add_action( 'all_admin_notices', array( $this, 'requirements_not_met_notice' ) ); |
| 293 |
|
| 294 |
// Deactivate our plugin. |
| 295 |
add_action( 'admin_init', array( $this, 'deactivate_me' ) ); |
| 296 |
|
| 297 |
// Didn't meet the requirements. |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Deactivates this plugin, hook this function on admin_init. |
| 303 |
* |
| 304 |
* @since 0.0.0 |
| 305 |
*/ |
| 306 |
public function deactivate_me() { |
| 307 |
|
| 308 |
// We do a check for deactivate_plugins before calling it, to protect |
| 309 |
// any developers from accidentally calling it too early and breaking things. |
| 310 |
if ( function_exists( 'deactivate_plugins' ) ) { |
| 311 |
deactivate_plugins( $this->basename ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Check that all plugin requirements are met. |
| 317 |
* |
| 318 |
* @since 0.0.0 |
| 319 |
* |
| 320 |
* @return boolean True if requirements are met. |
| 321 |
*/ |
| 322 |
public function meets_requirements() { |
| 323 |
|
| 324 |
// Do checks for required classes / functions or similar. |
| 325 |
// Add detailed messages to $this->activation_errors array. |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Adds a notice to the dashboard if the plugin requirements are not met. |
| 331 |
* |
| 332 |
* @since 0.0.0 |
| 333 |
*/ |
| 334 |
public function requirements_not_met_notice() { |
| 335 |
|
| 336 |
// Compile default message. |
| 337 |
$default_message = sprintf( __( 'Troubleshoot is missing requirements and has been <a href="%s">deactivated</a>. Please make sure all requirements are available.', 'troubleshoot' ), admin_url( 'plugins.php' ) ); |
| 338 |
|
| 339 |
// Default details to null. |
| 340 |
$details = null; |
| 341 |
|
| 342 |
// Add details if any exist. |
| 343 |
if ( $this->activation_errors && is_array( $this->activation_errors ) ) { |
| 344 |
$details = '<small>' . implode( '</small><br /><small>', $this->activation_errors ) . '</small>'; |
| 345 |
} |
| 346 |
|
| 347 |
// Output errors. |
| 348 |
?> |
| 349 |
<div id="message" class="error"> |
| 350 |
<p><?php echo wp_kses_post( $default_message ); ?></p> |
| 351 |
<?php echo wp_kses_post( $details ); ?> |
| 352 |
</div> |
| 353 |
<?php |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Magic getter for our object. |
| 358 |
* |
| 359 |
* @since 0.0.0 |
| 360 |
* |
| 361 |
* @param string $field Field to get. |
| 362 |
* @throws Exception Throws an exception if the field is invalid. |
| 363 |
* @return mixed Value of the field. |
| 364 |
*/ |
| 365 |
public function __get( $field ) { |
| 366 |
switch ( $field ) { |
| 367 |
case 'version': |
| 368 |
return self::VERSION; |
| 369 |
case 'basename': |
| 370 |
case 'url': |
| 371 |
case 'path': |
| 372 |
case 'filesystem': |
| 373 |
case 'constants': |
| 374 |
case 'bootstrap': |
| 375 |
case 'auth': |
| 376 |
case 'api': |
| 377 |
case 'plugins': |
| 378 |
case 'installed': |
| 379 |
case 'cases': |
| 380 |
case 'clues': |
| 381 |
case 'detective': |
| 382 |
return $this->$field; |
| 383 |
default: |
| 384 |
throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . $field ); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Include a file from the includes directory. |
| 390 |
* |
| 391 |
* @since 0.0.0 |
| 392 |
* |
| 393 |
* @param string $filename Name of the file to be included. |
| 394 |
* @return boolean Result of include call. |
| 395 |
*/ |
| 396 |
public static function include_file( $filename ) { |
| 397 |
$file = self::dir( $filename . '.php' ); |
| 398 |
if ( file_exists( $file ) ) { |
| 399 |
return include_once( $file ); |
| 400 |
} |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* This plugin's directory. |
| 406 |
* |
| 407 |
* @since 0.0.0 |
| 408 |
* |
| 409 |
* @param string $path (optional) appended path. |
| 410 |
* @return string Directory and path. |
| 411 |
*/ |
| 412 |
public static function dir( $path = '' ) { |
| 413 |
static $dir; |
| 414 |
$dir = $dir ? $dir : rtrim( dirname( __FILE__ ), '/\\' ).'/'; |
| 415 |
return $dir . $path; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* This plugin's url. |
| 420 |
* |
| 421 |
* @since 0.0.0 |
| 422 |
* |
| 423 |
* @param string $path (optional) appended path. |
| 424 |
* @return string URL and path. |
| 425 |
*/ |
| 426 |
public static function url( $path = '' ) { |
| 427 |
static $url; |
| 428 |
$url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) ); |
| 429 |
return $url . $path; |
| 430 |
} |
| 431 |
|
| 432 |
public function get_translations() { |
| 433 |
include dirname( $this->dir() ) . '/languages/troubleshoot-app-translations.php'; |
| 434 |
|
| 435 |
return $translations; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Grab the PD_Troubleshoot object and return it. |
| 441 |
* Wrapper for PD_Troubleshoot::get_instance(). |
| 442 |
* |
| 443 |
* @since 0.0.0 |
| 444 |
* @return PD_Troubleshoot Singleton instance of plugin class. |
| 445 |
*/ |
| 446 |
function pd_troubleshoot() { |
| 447 |
return PD_Troubleshoot::get_instance(); |
| 448 |
} |