PluginProbe
Plugin Detective – Troubleshooting Conflicts / 1.2.33
Plugin Detective – Troubleshooting Conflicts v1.2.33
1.2.35 1.2.33 1.2.32 1.2.31 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2 1.2.1 1.2.10 1.2.12 1.2.13 1.2.14 1.2.16 1.2.19 1.2.20 1.2.22 1.2.23 1.2.24 All 54 releases
plugin-detective / troubleshoot / troubleshoot.php

troubleshoot.php in Plugin Detective – Troubleshooting Conflicts 1.2.33, at troubleshoot/troubleshoot.php

468 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.33';
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();
468 }