| @@ -1,440 +1,468 @@ | ||
| 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.1.3'; | |
| 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 | - $desired_protocol = 'http'; | |
| 152 | - if ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) { | |
| 153 | - $desired_protocol = 'https'; | |
| 154 | - } elseif ( !empty( $_SERVER['protocol'] ) ) { | |
| 155 | - $desired_protocol = strtolower( substr( $_SERVER["SERVER_PROTOCOL"], 0, 5 ) ) == 'https' ? 'https' : 'http'; | |
| 156 | - } | |
| 157 | - } | |
| 158 | - | |
| 159 | - $url = $desired_protocol . '://' . substr( $url, $pos_of_colon_slash_slash + 3 ); | |
| 160 | - | |
| 161 | - return $url; | |
| 162 | - } | |
| 163 | - | |
| 164 | - public static function maybe_fix_www_prefix( $url, $should_be_www = null ) { | |
| 165 | - $pos_of_colon_slash_slash = strpos( $url, '://' ); | |
| 166 | - if ( $pos_of_colon_slash_slash === false ) { | |
| 167 | - return $url; | |
| 168 | - } | |
| 169 | - | |
| 170 | - if ( $should_be_www === null ) { | |
| 171 | - if ( strpos( $_SERVER['HTTP_HOST'], 'www.' ) === 0 ) { | |
| 172 | - $should_be_www = true; | |
| 173 | - } else { | |
| 174 | - $should_be_www = false; | |
| 175 | - } | |
| 176 | - } | |
| 177 | - | |
| 178 | - if ( !empty( $should_be_www ) ) { | |
| 179 | - $url = str_replace( array( | |
| 180 | - '://', | |
| 181 | - '://www.www.', | |
| 182 | - ), array( | |
| 183 | - '://www.', | |
| 184 | - '://www.', | |
| 185 | - ), $url ); | |
| 186 | - } else { | |
| 187 | - $url = str_replace( array( | |
| 188 | - '://www.www.', | |
| 189 | - '://www.', | |
| 190 | - ), array( | |
| 191 | - '://', | |
| 192 | - '://', | |
| 193 | - ), $url ); | |
| 194 | - } | |
| 195 | - | |
| 196 | - return $url; | |
| 197 | - } | |
| 198 | - | |
| 199 | - public function get_api_params() { | |
| 200 | - return array( | |
| 201 | - 'site_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( site_url() ) ), | |
| 202 | - 'api_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'api.php' ) ) ), | |
| 203 | - 'static_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'app/dist/static' ) ) ), | |
| 204 | - ); | |
| 205 | - } | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * Attach other plugin classes to the base plugin class. | |
| 209 | - * | |
| 210 | - * @since 0.0.0 | |
| 211 | - */ | |
| 212 | - public function plugin_classes() { | |
| 213 | - $this->filesystem = new PDT_Filesystem( $this ); | |
| 214 | - $this->auth = new PDT_Auth( $this ); | |
| 215 | - $this->api = new PDT_Api( $this ); | |
| 216 | - $this->plugins = new PDT_Plugins( $this ); | |
| 217 | - $this->installed = new PDT_Installed( $this ); | |
| 218 | - $this->cases = new PDT_Cases( $this ); | |
| 219 | - $this->clues = new PDT_Clues( $this ); | |
| 220 | - $this->detective = new PDT_Detective( $this ); | |
| 221 | - } // END OF PLUGIN CLASSES FUNCTION | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Activate the plugin. | |
| 225 | - * | |
| 226 | - * @since 0.0.0 | |
| 227 | - */ | |
| 228 | - public function _activate() { | |
| 229 | - // Bail early if requirements aren't met. | |
| 230 | - if ( ! $this->check_requirements() ) { | |
| 231 | - return; | |
| 232 | - } | |
| 233 | - | |
| 234 | - // Make sure any rewrite functionality has been loaded. | |
| 235 | - flush_rewrite_rules(); | |
| 236 | - } | |
| 237 | - | |
| 238 | - /** | |
| 239 | - * Deactivate the plugin. | |
| 240 | - * Uninstall routines should be in uninstall.php. | |
| 241 | - * | |
| 242 | - * @since 0.0.0 | |
| 243 | - */ | |
| 244 | - public function _deactivate() { | |
| 245 | - // Add deactivation cleanup functionality here. | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * Init hooks | |
| 250 | - * | |
| 251 | - * @since 0.0.0 | |
| 252 | - */ | |
| 253 | - public function init() { | |
| 254 | - | |
| 255 | - // Bail early if requirements aren't met. | |
| 256 | - if ( ! $this->check_requirements() ) { | |
| 257 | - return; | |
| 258 | - } | |
| 259 | - | |
| 260 | - // Load translated strings for plugin. | |
| 261 | - load_plugin_textdomain( 'plugin-detective', false, dirname( $this->basename ) . '/languages/' ); | |
| 262 | - | |
| 263 | - | |
| 264 | - // Initialize plugin classes. | |
| 265 | - $this->plugin_classes(); | |
| 266 | - } | |
| 267 | - | |
| 268 | - /** | |
| 269 | - * Check if the plugin meets requirements and | |
| 270 | - * disable it if they are not present. | |
| 271 | - * | |
| 272 | - * @since 0.0.0 | |
| 273 | - * | |
| 274 | - * @return boolean True if requirements met, false if not. | |
| 275 | - */ | |
| 276 | - public function check_requirements() { | |
| 277 | - | |
| 278 | - // Bail early if plugin meets requirements. | |
| 279 | - if ( $this->meets_requirements() ) { | |
| 280 | - return true; | |
| 281 | - } | |
| 282 | - | |
| 283 | - // Add a dashboard notice. | |
| 284 | - add_action( 'all_admin_notices', array( $this, 'requirements_not_met_notice' ) ); | |
| 285 | - | |
| 286 | - // Deactivate our plugin. | |
| 287 | - add_action( 'admin_init', array( $this, 'deactivate_me' ) ); | |
| 288 | - | |
| 289 | - // Didn't meet the requirements. | |
| 290 | - return false; | |
| 291 | - } | |
| 292 | - | |
| 293 | - /** | |
| 294 | - * Deactivates this plugin, hook this function on admin_init. | |
| 295 | - * | |
| 296 | - * @since 0.0.0 | |
| 297 | - */ | |
| 298 | - public function deactivate_me() { | |
| 299 | - | |
| 300 | - // We do a check for deactivate_plugins before calling it, to protect | |
| 301 | - // any developers from accidentally calling it too early and breaking things. | |
| 302 | - if ( function_exists( 'deactivate_plugins' ) ) { | |
| 303 | - deactivate_plugins( $this->basename ); | |
| 304 | - } | |
| 305 | - } | |
| 306 | - | |
| 307 | - /** | |
| 308 | - * Check that all plugin requirements are met. | |
| 309 | - * | |
| 310 | - * @since 0.0.0 | |
| 311 | - * | |
| 312 | - * @return boolean True if requirements are met. | |
| 313 | - */ | |
| 314 | - public function meets_requirements() { | |
| 315 | - | |
| 316 | - // Do checks for required classes / functions or similar. | |
| 317 | - // Add detailed messages to $this->activation_errors array. | |
| 318 | - return true; | |
| 319 | - } | |
| 320 | - | |
| 321 | - /** | |
| 322 | - * Adds a notice to the dashboard if the plugin requirements are not met. | |
| 323 | - * | |
| 324 | - * @since 0.0.0 | |
| 325 | - */ | |
| 326 | - public function requirements_not_met_notice() { | |
| 327 | - | |
| 328 | - // Compile default message. | |
| 329 | - $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' ) ); | |
| 330 | - | |
| 331 | - // Default details to null. | |
| 332 | - $details = null; | |
| 333 | - | |
| 334 | - // Add details if any exist. | |
| 335 | - if ( $this->activation_errors && is_array( $this->activation_errors ) ) { | |
| 336 | - $details = '<small>' . implode( '</small><br /><small>', $this->activation_errors ) . '</small>'; | |
| 337 | - } | |
| 338 | - | |
| 339 | - // Output errors. | |
| 340 | - ?> | |
| 341 | - <div id="message" class="error"> | |
| 342 | - <p><?php echo wp_kses_post( $default_message ); ?></p> | |
| 343 | - <?php echo wp_kses_post( $details ); ?> | |
| 344 | - </div> | |
| 345 | - <?php | |
| 346 | - } | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Magic getter for our object. | |
| 350 | - * | |
| 351 | - * @since 0.0.0 | |
| 352 | - * | |
| 353 | - * @param string $field Field to get. | |
| 354 | - * @throws Exception Throws an exception if the field is invalid. | |
| 355 | - * @return mixed Value of the field. | |
| 356 | - */ | |
| 357 | - public function __get( $field ) { | |
| 358 | - switch ( $field ) { | |
| 359 | - case 'version': | |
| 360 | - return self::VERSION; | |
| 361 | - case 'basename': | |
| 362 | - case 'url': | |
| 363 | - case 'path': | |
| 364 | - case 'filesystem': | |
| 365 | - case 'constants': | |
| 366 | - case 'bootstrap': | |
| 367 | - case 'auth': | |
| 368 | - case 'api': | |
| 369 | - case 'plugins': | |
| 370 | - case 'installed': | |
| 371 | - case 'cases': | |
| 372 | - case 'clues': | |
| 373 | - case 'detective': | |
| 374 | - return $this->$field; | |
| 375 | - default: | |
| 376 | - throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . $field ); | |
| 377 | - } | |
| 378 | - } | |
| 379 | - | |
| 380 | - /** | |
| 381 | - * Include a file from the includes directory. | |
| 382 | - * | |
| 383 | - * @since 0.0.0 | |
| 384 | - * | |
| 385 | - * @param string $filename Name of the file to be included. | |
| 386 | - * @return boolean Result of include call. | |
| 387 | - */ | |
| 388 | - public static function include_file( $filename ) { | |
| 389 | - $file = self::dir( $filename . '.php' ); | |
| 390 | - if ( file_exists( $file ) ) { | |
| 391 | - return include_once( $file ); | |
| 392 | - } | |
| 393 | - return false; | |
| 394 | - } | |
| 395 | - | |
| 396 | - /** | |
| 397 | - * This plugin's directory. | |
| 398 | - * | |
| 399 | - * @since 0.0.0 | |
| 400 | - * | |
| 401 | - * @param string $path (optional) appended path. | |
| 402 | - * @return string Directory and path. | |
| 403 | - */ | |
| 404 | - public static function dir( $path = '' ) { | |
| 405 | - static $dir; | |
| 406 | - $dir = $dir ? $dir : rtrim( dirname( __FILE__ ), '/\\' ).'/'; | |
| 407 | - return $dir . $path; | |
| 408 | - } | |
| 409 | - | |
| 410 | - /** | |
| 411 | - * This plugin's url. | |
| 412 | - * | |
| 413 | - * @since 0.0.0 | |
| 414 | - * | |
| 415 | - * @param string $path (optional) appended path. | |
| 416 | - * @return string URL and path. | |
| 417 | - */ | |
| 418 | - public static function url( $path = '' ) { | |
| 419 | - static $url; | |
| 420 | - $url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) ); | |
| 421 | - return $url . $path; | |
| 422 | - } | |
| 423 | - | |
| 424 | - public function get_translations() { | |
| 425 | - include dirname( $this->dir() ) . '/languages/troubleshoot-app-translations.php'; | |
| 426 | - | |
| 427 | - return $translations; | |
| 428 | - } | |
| 429 | -} | |
| 430 | - | |
| 431 | -/** | |
| 432 | - * Grab the PD_Troubleshoot object and return it. | |
| 433 | - * Wrapper for PD_Troubleshoot::get_instance(). | |
| 434 | - * | |
| 435 | - * @since 0.0.0 | |
| 436 | - * @return PD_Troubleshoot Singleton instance of plugin class. | |
| 437 | - */ | |
| 438 | -function pd_troubleshoot() { | |
| 439 | - return PD_Troubleshoot::get_instance(); | |
| 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.35'; | |
| 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 | + $server_protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_PROTOCOL'] ) ) : ''; | |
| 164 | + $desired_protocol = strtolower( substr( $server_protocol, 0, 5 ) ) == 'https' ? 'https' : 'http'; | |
| 165 | + } | |
| 166 | + } | |
| 167 | + | |
| 168 | + $url = $desired_protocol . '://' . substr( $url, $pos_of_colon_slash_slash + 3 ); | |
| 169 | + | |
| 170 | + return $url; | |
| 171 | + } | |
| 172 | + | |
| 173 | + public static function maybe_fix_www_prefix( $url, $should_be_www = null ) { | |
| 174 | + $pos_of_colon_slash_slash = strpos( $url, '://' ); | |
| 175 | + if ( $pos_of_colon_slash_slash === false ) { | |
| 176 | + return $url; | |
| 177 | + } | |
| 178 | + | |
| 179 | + if ( $should_be_www === null ) { | |
| 180 | + $http_host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; | |
| 181 | + if ( strpos( $http_host, 'www.' ) === 0 ) { | |
| 182 | + $should_be_www = true; | |
| 183 | + } else { | |
| 184 | + $should_be_www = false; | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + if ( !empty( $should_be_www ) ) { | |
| 189 | + $url = str_replace( array( | |
| 190 | + '://', | |
| 191 | + '://www.www.', | |
| 192 | + ), array( | |
| 193 | + '://www.', | |
| 194 | + '://www.', | |
| 195 | + ), $url ); | |
| 196 | + } else { | |
| 197 | + $url = str_replace( array( | |
| 198 | + '://www.www.', | |
| 199 | + '://www.', | |
| 200 | + ), array( | |
| 201 | + '://', | |
| 202 | + '://', | |
| 203 | + ), $url ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + return $url; | |
| 207 | + } | |
| 208 | + | |
| 209 | + public function get_api_params() { | |
| 210 | + return array( | |
| 211 | + 'site_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( site_url() ) ), | |
| 212 | + 'api_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'api.php' ) ) ), | |
| 213 | + 'static_url' => self::maybe_fix_www_prefix( self::maybe_fix_protocol( $this->url( 'app/dist/static' ) ) ), | |
| 214 | + ); | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Attach other plugin classes to the base plugin class. | |
| 219 | + * | |
| 220 | + * @since 0.0.0 | |
| 221 | + */ | |
| 222 | + public function plugin_classes() { | |
| 223 | + $this->filesystem = new PDT_Filesystem( $this ); | |
| 224 | + $this->auth = new PDT_Auth( $this ); | |
| 225 | + $this->api = new PDT_Api( $this ); | |
| 226 | + $this->plugins = new PDT_Plugins( $this ); | |
| 227 | + $this->installed = new PDT_Installed( $this ); | |
| 228 | + $this->cases = new PDT_Cases( $this ); | |
| 229 | + $this->clues = new PDT_Clues( $this ); | |
| 230 | + $this->detective = new PDT_Detective( $this ); | |
| 231 | + } // END OF PLUGIN CLASSES FUNCTION | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Activate the plugin. | |
| 235 | + * | |
| 236 | + * @since 0.0.0 | |
| 237 | + */ | |
| 238 | + public function _activate() { | |
| 239 | + // Bail early if requirements aren't met. | |
| 240 | + if ( ! $this->check_requirements() ) { | |
| 241 | + return; | |
| 242 | + } | |
| 243 | + | |
| 244 | + // Make sure any rewrite functionality has been loaded. | |
| 245 | + flush_rewrite_rules(); | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Deactivate the plugin. | |
| 250 | + * Uninstall routines should be in uninstall.php. | |
| 251 | + * | |
| 252 | + * @since 0.0.0 | |
| 253 | + */ | |
| 254 | + public function _deactivate() { | |
| 255 | + // Add deactivation cleanup functionality here. | |
| 256 | + } | |
| 257 | + | |
| 258 | + /** | |
| 259 | + * Init hooks | |
| 260 | + * | |
| 261 | + * @since 0.0.0 | |
| 262 | + */ | |
| 263 | + public function init() { | |
| 264 | + | |
| 265 | + // Bail early if requirements aren't met. | |
| 266 | + if ( ! $this->check_requirements() ) { | |
| 267 | + return; | |
| 268 | + } | |
| 269 | + | |
| 270 | + // Load translated strings for plugin. | |
| 271 | + // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- Standalone troubleshooter bootstrap; WordPress's automatic just-in-time textdomain loading does not run in this context. | |
| 272 | + load_plugin_textdomain( 'plugin-detective', false, dirname( $this->basename ) . '/languages/' ); | |
| 273 | + | |
| 274 | + | |
| 275 | + // Initialize plugin classes. | |
| 276 | + $this->plugin_classes(); | |
| 277 | + } | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * Check if the plugin meets requirements and | |
| 281 | + * disable it if they are not present. | |
| 282 | + * | |
| 283 | + * @since 0.0.0 | |
| 284 | + * | |
| 285 | + * @return boolean True if requirements met, false if not. | |
| 286 | + */ | |
| 287 | + public function check_requirements() { | |
| 288 | + | |
| 289 | + // Bail early if plugin meets requirements. | |
| 290 | + if ( $this->meets_requirements() ) { | |
| 291 | + return true; | |
| 292 | + } | |
| 293 | + | |
| 294 | + // Add a dashboard notice. | |
| 295 | + add_action( 'all_admin_notices', array( $this, 'requirements_not_met_notice' ) ); | |
| 296 | + | |
| 297 | + // Deactivate our plugin. | |
| 298 | + add_action( 'admin_init', array( $this, 'deactivate_me' ) ); | |
| 299 | + | |
| 300 | + // Didn't meet the requirements. | |
| 301 | + return false; | |
| 302 | + } | |
| 303 | + | |
| 304 | + /** | |
| 305 | + * Deactivates this plugin, hook this function on admin_init. | |
| 306 | + * | |
| 307 | + * @since 0.0.0 | |
| 308 | + */ | |
| 309 | + public function deactivate_me() { | |
| 310 | + | |
| 311 | + // We do a check for deactivate_plugins before calling it, to protect | |
| 312 | + // any developers from accidentally calling it too early and breaking things. | |
| 313 | + if ( function_exists( 'deactivate_plugins' ) ) { | |
| 314 | + deactivate_plugins( $this->basename ); | |
| 315 | + } | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 319 | + * Check that all plugin requirements are met. | |
| 320 | + * | |
| 321 | + * @since 0.0.0 | |
| 322 | + * | |
| 323 | + * @return boolean True if requirements are met. | |
| 324 | + */ | |
| 325 | + public function meets_requirements() { | |
| 326 | + | |
| 327 | + // Do checks for required classes / functions or similar. | |
| 328 | + // Add detailed messages to $this->activation_errors array. | |
| 329 | + return true; | |
| 330 | + } | |
| 331 | + | |
| 332 | + /** | |
| 333 | + * Adds a notice to the dashboard if the plugin requirements are not met. | |
| 334 | + * | |
| 335 | + * @since 0.0.0 | |
| 336 | + */ | |
| 337 | + public function requirements_not_met_notice() { | |
| 338 | + | |
| 339 | + // Compile default message. | |
| 340 | + /* translators: %s: URL of the WordPress plugins admin page. */ | |
| 341 | + $default_message = sprintf( __( 'Troubleshoot is missing requirements and has been <a href="%s">deactivated</a>. Please make sure all requirements are available.', 'plugin-detective' ), admin_url( 'plugins.php' ) ); | |
| 342 | + | |
| 343 | + // Default details to null. | |
| 344 | + $details = null; | |
| 345 | + | |
| 346 | + // Add details if any exist. | |
| 347 | + if ( $this->activation_errors && is_array( $this->activation_errors ) ) { | |
| 348 | + $details = '<small>' . implode( '</small><br /><small>', $this->activation_errors ) . '</small>'; | |
| 349 | + } | |
| 350 | + | |
| 351 | + // Output errors. | |
| 352 | + ?> | |
| 353 | + <div id="message" class="error"> | |
| 354 | + <p><?php echo wp_kses_post( $default_message ); ?></p> | |
| 355 | + <?php echo wp_kses_post( $details ); ?> | |
| 356 | + </div> | |
| 357 | + <?php | |
| 358 | + } | |
| 359 | + | |
| 360 | + /** | |
| 361 | + * Magic getter for our object. | |
| 362 | + * | |
| 363 | + * @since 0.0.0 | |
| 364 | + * | |
| 365 | + * @param string $field Field to get. | |
| 366 | + * @throws Exception Throws an exception if the field is invalid. | |
| 367 | + * @return mixed Value of the field. | |
| 368 | + */ | |
| 369 | + public function __get( $field ) { | |
| 370 | + switch ( $field ) { | |
| 371 | + case 'version': | |
| 372 | + return self::VERSION; | |
| 373 | + case 'basename': | |
| 374 | + case 'url': | |
| 375 | + case 'path': | |
| 376 | + case 'filesystem': | |
| 377 | + case 'constants': | |
| 378 | + case 'bootstrap': | |
| 379 | + case 'auth': | |
| 380 | + case 'api': | |
| 381 | + case 'plugins': | |
| 382 | + case 'installed': | |
| 383 | + case 'cases': | |
| 384 | + case 'clues': | |
| 385 | + case 'detective': | |
| 386 | + return $this->$field; | |
| 387 | + default: | |
| 388 | + throw new Exception( 'Invalid ' . __CLASS__ . ' property: ' . esc_html( $field ) ); | |
| 389 | + } | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * Include a file from the includes directory. | |
| 394 | + * | |
| 395 | + * @since 0.0.0 | |
| 396 | + * | |
| 397 | + * @param string $filename Name of the file to be included. | |
| 398 | + * @return boolean Result of include call. | |
| 399 | + */ | |
| 400 | + public static function include_file( $filename ) { | |
| 401 | + $file = self::dir( $filename . '.php' ); | |
| 402 | + if ( file_exists( $file ) ) { | |
| 403 | + return include_once( $file ); | |
| 404 | + } | |
| 405 | + return false; | |
| 406 | + } | |
| 407 | + | |
| 408 | + /** | |
| 409 | + * This plugin's directory. | |
| 410 | + * | |
| 411 | + * @since 0.0.0 | |
| 412 | + * | |
| 413 | + * @param string $path (optional) appended path. | |
| 414 | + * @return string Directory and path. | |
| 415 | + */ | |
| 416 | + public static function dir( $path = '' ) { | |
| 417 | + static $dir; | |
| 418 | + $dir = $dir ? $dir : rtrim( dirname( __FILE__ ), '/\\' ).'/'; | |
| 419 | + return $dir . $path; | |
| 420 | + } | |
| 421 | + | |
| 422 | + /** | |
| 423 | + * This plugin's url. | |
| 424 | + * | |
| 425 | + * @since 0.0.0 | |
| 426 | + * | |
| 427 | + * @param string $path (optional) appended path. | |
| 428 | + * @return string URL and path. | |
| 429 | + */ | |
| 430 | + public static function url( $path = '' ) { | |
| 431 | + static $url; | |
| 432 | + $url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) ); | |
| 433 | + return $url . $path; | |
| 434 | + } | |
| 435 | + | |
| 436 | + public function get_translations() { | |
| 437 | + include dirname( $this->dir() ) . '/languages/troubleshoot-app-translations.php'; | |
| 438 | + | |
| 439 | + return $translations; | |
| 440 | + } | |
| 441 | + | |
| 442 | + /** | |
| 443 | + * Attempt to delete .maintenance file. | |
| 444 | + * | |
| 445 | + */ | |
| 446 | + public function delete_maintenance_file() { | |
| 447 | + if (is_user_logged_in() && current_user_can('manage_options')) { | |
| 448 | + // remove .maintenance file | |
| 449 | + $filename = ABSPATH . '.maintenance'; | |
| 450 | + if ( file_exists( $filename ) ) { | |
| 451 | + wp_delete_file( $filename ); | |
| 452 | + return ! file_exists( $filename ); | |
| 453 | + } | |
| 454 | + } | |
| 455 | + return false; | |
| 456 | + } | |
| 457 | +} | |
| 458 | + | |
| 459 | +/** | |
| 460 | + * Grab the PD_Troubleshoot object and return it. | |
| 461 | + * Wrapper for PD_Troubleshoot::get_instance(). | |
| 462 | + * | |
| 463 | + * @since 0.0.0 | |
| 464 | + * @return PD_Troubleshoot Singleton instance of plugin class. | |
| 465 | + */ | |
| 466 | +function pd_troubleshoot() { | |
| 467 | + return PD_Troubleshoot::get_instance(); | |
| 440 | 468 | } |