PluginProbe
Plugin Detective – Troubleshooting Conflicts / 1.1.3
Plugin Detective – Troubleshooting Conflicts v1.1.3
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.1.3, at troubleshoot/troubleshoot.php

440 lines 10.4 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.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();
440 }