Complex classes like Plugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class Plugin { |
||
18 | /** |
||
19 | * @var string The plugin version. |
||
20 | * |
||
21 | * @api |
||
22 | * @since 2.0 |
||
23 | */ |
||
24 | public static $version = GV_PLUGIN_VERSION; |
||
25 | |||
26 | /** |
||
27 | * @var string Minimum WordPress version. |
||
28 | * |
||
29 | * GravityView requires at least this version of WordPress to function properly. |
||
30 | */ |
||
31 | private static $min_wp_version = GV_MIN_WP_VERSION; |
||
32 | |||
33 | /** |
||
34 | * @var string Minimum Gravity Forms version. |
||
35 | * |
||
36 | * GravityView requires at least this version of Gravity Forms to function properly. |
||
37 | */ |
||
38 | public static $min_gf_version = GV_MIN_GF_VERSION; |
||
39 | |||
40 | /** |
||
41 | * @var string Minimum PHP version. |
||
42 | * |
||
43 | * GravityView requires at least this version of PHP to function properly. |
||
44 | */ |
||
45 | private static $min_php_version = GV_MIN_PHP_VERSION; |
||
46 | |||
47 | /** |
||
48 | * @var string|bool Minimum future PHP version. |
||
49 | * |
||
50 | * GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
||
51 | */ |
||
52 | private static $future_min_php_version = GV_FUTURE_MIN_PHP_VERSION; |
||
53 | |||
54 | /** |
||
55 | * @var string|bool Minimum future Gravity Forms version. |
||
56 | * |
||
57 | * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
||
58 | */ |
||
59 | private static $future_min_gf_version = GV_FUTURE_MIN_GF_VERSION; |
||
60 | |||
61 | /** |
||
62 | * @var \GV\Plugin The \GV\Plugin static instance. |
||
63 | */ |
||
64 | private static $__instance = null; |
||
65 | |||
66 | /** |
||
67 | * @var \GV\Addon_Settings The plugin "addon" settings. |
||
68 | * |
||
69 | * @api |
||
70 | * @since 2.0 |
||
71 | */ |
||
72 | public $settings; |
||
73 | |||
74 | /** |
||
75 | * @var string The GFQuery functionality identifier. |
||
76 | */ |
||
77 | const FEATURE_GFQUERY = 'gfquery'; |
||
78 | |||
79 | /** |
||
80 | * @var string The joins functionality identifier. |
||
81 | */ |
||
82 | const FEATURE_JOINS = 'joins'; |
||
83 | |||
84 | /** |
||
85 | * @var string The unions functionality identifier. |
||
86 | */ |
||
87 | const FEATURE_UNIONS = 'unions'; |
||
88 | |||
89 | /** |
||
90 | * @var string The REST API functionality identifier. |
||
91 | */ |
||
92 | const FEATURE_REST = 'rest_api'; |
||
93 | |||
94 | /** |
||
95 | * Get the global instance of \GV\Plugin. |
||
96 | * |
||
97 | * @return \GV\Plugin The global instance of GravityView Plugin. |
||
98 | */ |
||
99 | public static function get() { |
||
105 | |||
106 | |||
107 | private function __construct() { |
||
123 | |||
124 | public function load_license_settings() { |
||
134 | |||
135 | /** |
||
136 | * Check whether GravityView is network activated. |
||
137 | * |
||
138 | * @return bool Whether it's network activated or not. |
||
139 | */ |
||
140 | 1 | public static function is_network_activated() { |
|
143 | |||
144 | /** |
||
145 | * Include more legacy stuff. |
||
146 | * |
||
147 | * @param boolean $force Whether to force the includes. |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | public function include_legacy_frontend( $force = false ) { |
||
172 | |||
173 | /** |
||
174 | * Load more legacy core files. |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | public function include_legacy_core() { |
||
234 | |||
235 | /** |
||
236 | * Load the translations. |
||
237 | * |
||
238 | * Order of look-ups: |
||
239 | * |
||
240 | * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core) |
||
241 | * 2. /wp-content/mu-plugins/gravityview-{locale}.mo |
||
242 | * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo |
||
243 | * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | public function load_textdomain() { |
||
283 | |||
284 | /** |
||
285 | * Register hooks that are fired when the plugin is activated and deactivated. |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | public function register_activation_hooks() { |
||
293 | |||
294 | /** |
||
295 | * Plugin activation function. |
||
296 | * |
||
297 | * @internal |
||
298 | * @return void |
||
299 | */ |
||
300 | 1 | public function activate() { |
|
301 | 1 | gravityview(); |
|
302 | |||
303 | 1 | if ( ! $this->is_compatible() ) { |
|
304 | return; |
||
305 | } |
||
306 | |||
307 | /** Register the gravityview post type upon WordPress core init. */ |
||
308 | 1 | require_once $this->dir( 'future/includes/class-gv-view.php' ); |
|
309 | 1 | View::register_post_type(); |
|
310 | |||
311 | /** Add the entry rewrite endpoint. */ |
||
312 | 1 | require_once $this->dir( 'future/includes/class-gv-entry.php' ); |
|
313 | 1 | Entry::add_rewrite_endpoint(); |
|
314 | |||
315 | /** Flush all URL rewrites. */ |
||
316 | 1 | flush_rewrite_rules(); |
|
317 | |||
318 | 1 | update_option( 'gv_version', self::$version ); |
|
319 | |||
320 | /** Add the transient to redirect to configuration page. */ |
||
321 | 1 | set_transient( '_gv_activation_redirect', true, 60 ); |
|
322 | |||
323 | /** Clear settings transient. */ |
||
324 | 1 | delete_transient( 'gravityview_edd-activate_valid' ); |
|
325 | |||
326 | 1 | \GravityView_Roles_Capabilities::get_instance()->add_caps(); |
|
327 | 1 | } |
|
328 | |||
329 | /** |
||
330 | * Plugin deactivation function. |
||
331 | * |
||
332 | * @internal |
||
333 | * @return void |
||
334 | */ |
||
335 | public function deactivate() { |
||
336 | flush_rewrite_rules(); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Retrieve an absolute path within the GravityView plugin directory. |
||
341 | * |
||
342 | * @api |
||
343 | * @since 2.0 |
||
344 | * |
||
345 | * @param string $path Optional. Append this extra path component. |
||
346 | * @return string The absolute path to the plugin directory. |
||
347 | */ |
||
348 | 109 | public function dir( $path = '' ) { |
|
349 | 109 | return wp_normalize_path( GRAVITYVIEW_DIR . ltrim( $path, '/' ) ); |
|
350 | } |
||
351 | |||
352 | /** |
||
353 | * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory |
||
354 | * |
||
355 | * @api |
||
356 | * @since 2.2.3 |
||
357 | * |
||
358 | * @param string $path Optional. Append this extra path component. |
||
359 | * @return string The relative path to the plugin directory from the plugin directory. |
||
360 | */ |
||
361 | 1 | public function relpath( $path = '' ) { |
|
362 | |||
363 | 1 | $dirname = trailingslashit( dirname( plugin_basename( GRAVITYVIEW_FILE ) ) ); |
|
364 | |||
365 | 1 | return wp_normalize_path( $dirname . ltrim( $path, '/' ) ); |
|
366 | } |
||
367 | |||
368 | /** |
||
369 | * Retrieve a URL within the GravityView plugin directory. |
||
370 | * |
||
371 | * @api |
||
372 | * @since 2.0 |
||
373 | * |
||
374 | * @param string $path Optional. Extra path appended to the URL. |
||
375 | * @return string The URL to this plugin, with trailing slash. |
||
376 | */ |
||
377 | 1 | public function url( $path = '/' ) { |
|
378 | 1 | return plugins_url( $path, $this->dir( 'gravityview.php' ) ); |
|
379 | } |
||
380 | |||
381 | /** |
||
382 | * Is everything compatible with this version of GravityView? |
||
383 | * |
||
384 | * @api |
||
385 | * @since 2.0 |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | 4 | public function is_compatible() { |
|
390 | return |
||
391 | 4 | $this->is_compatible_php() |
|
392 | 4 | && $this->is_compatible_wordpress() |
|
393 | 4 | && $this->is_compatible_gravityforms(); |
|
394 | } |
||
395 | |||
396 | /** |
||
397 | * Is this version of GravityView compatible with the current version of PHP? |
||
398 | * |
||
399 | * @api |
||
400 | * @since 2.0 |
||
401 | * |
||
402 | * @return bool true if compatible, false otherwise. |
||
403 | */ |
||
404 | 4 | public function is_compatible_php() { |
|
405 | 4 | return version_compare( $this->get_php_version(), self::$min_php_version, '>=' ); |
|
406 | } |
||
407 | |||
408 | /** |
||
409 | * Is this version of GravityView compatible with the future required version of PHP? |
||
410 | * |
||
411 | * @api |
||
412 | * @since 2.0 |
||
413 | * |
||
414 | * @return bool true if compatible, false otherwise. |
||
415 | */ |
||
416 | public function is_compatible_future_php() { |
||
417 | return version_compare( $this->get_php_version(), self::$future_min_php_version, '>=' ); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Is this version of GravityView compatible with the current version of WordPress? |
||
422 | * |
||
423 | * @api |
||
424 | * @since 2.0 |
||
425 | * |
||
426 | * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION |
||
427 | * |
||
428 | * @return bool true if compatible, false otherwise. |
||
429 | */ |
||
430 | 4 | public function is_compatible_wordpress( $version = null ) { |
|
431 | |||
432 | 4 | if( ! $version ) { |
|
|
|||
433 | 4 | $version = self::$min_wp_version; |
|
434 | } |
||
435 | |||
436 | 4 | return version_compare( $this->get_wordpress_version(), $version, '>=' ); |
|
437 | } |
||
438 | |||
439 | /** |
||
440 | * Is this version of GravityView compatible with the current version of Gravity Forms? |
||
441 | * |
||
442 | * @api |
||
443 | * @since 2.0 |
||
444 | * |
||
445 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
446 | */ |
||
447 | 4 | public function is_compatible_gravityforms() { |
|
448 | 4 | $version = $this->get_gravityforms_version(); |
|
449 | 4 | return $version ? version_compare( $version, self::$min_gf_version, '>=' ) : false; |
|
450 | } |
||
451 | |||
452 | /** |
||
453 | * Is this version of GravityView compatible with the future version of Gravity Forms? |
||
454 | * |
||
455 | * @api |
||
456 | * @since 2.0 |
||
457 | * |
||
458 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
459 | */ |
||
460 | public function is_compatible_future_gravityforms() { |
||
461 | $version = $this->get_gravityforms_version(); |
||
462 | return $version ? version_compare( $version, self::$future_min_gf_version, '>=' ) : false; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Retrieve the current PHP version. |
||
467 | * |
||
468 | * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
||
469 | * |
||
470 | * @return string The version of PHP. |
||
471 | */ |
||
472 | 3 | private function get_php_version() { |
|
473 | 3 | return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ? |
|
474 | 3 | $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion(); |
|
475 | } |
||
476 | |||
477 | /** |
||
478 | * Retrieve the current WordPress version. |
||
479 | * |
||
480 | * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
||
481 | * |
||
482 | * @return string The version of WordPress. |
||
483 | */ |
||
484 | 3 | private function get_wordpress_version() { |
|
485 | 3 | return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ? |
|
486 | 3 | $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version']; |
|
487 | } |
||
488 | |||
489 | /** |
||
490 | * Retrieve the current Gravity Forms version. |
||
491 | * |
||
492 | * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
||
493 | * |
||
494 | * @return string|null The version of Gravity Forms or null if inactive. |
||
495 | */ |
||
496 | 3 | private function get_gravityforms_version() { |
|
497 | 3 | if ( ! class_exists( '\GFCommon' ) || ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) { |
|
498 | gravityview()->log->error( 'Gravity Forms is inactive or not installed.' ); |
||
499 | return null; |
||
500 | } |
||
501 | |||
502 | 3 | return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ? |
|
503 | 3 | $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version; |
|
504 | } |
||
505 | |||
506 | /** |
||
507 | * Feature support detection. |
||
508 | * |
||
509 | * @param string $feature Feature name. Check FEATURE_* class constants. |
||
510 | * |
||
511 | * @return boolean |
||
512 | */ |
||
513 | 176 | public function supports( $feature ) { |
|
514 | 176 | if ( ! is_null( $supports = apply_filters( "gravityview/plugin/feature/$feature", null ) ) ) { |
|
515 | return $supports; |
||
516 | } |
||
517 | |||
518 | switch ( $feature ): |
||
519 | 176 | case self::FEATURE_GFQUERY: |
|
520 | 62 | return class_exists( '\GF_Query' ); |
|
521 | 176 | case self::FEATURE_JOINS: |
|
522 | 176 | case self::FEATURE_UNIONS: |
|
523 | 160 | return apply_filters( 'gravityview/query/class', false ) === '\GF_Patched_Query'; |
|
524 | 176 | case self::FEATURE_REST: |
|
525 | 176 | return class_exists( '\WP_REST_Controller' ); |
|
526 | default: |
||
527 | return false; |
||
528 | endswitch; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Delete GravityView Views, settings, roles, caps, etc. |
||
533 | * |
||
534 | * @return void |
||
535 | */ |
||
536 | public function uninstall() { |
||
537 | global $wpdb; |
||
538 | |||
539 | $suppress = $wpdb->suppress_errors(); |
||
540 | |||
541 | /** |
||
542 | * Posts. |
||
543 | */ |
||
544 | $items = get_posts( array( |
||
545 | 'post_type' => 'gravityview', |
||
546 | 'post_status' => 'any', |
||
547 | 'numberposts' => -1, |
||
548 | 'fields' => 'ids' |
||
549 | ) ); |
||
550 | |||
551 | foreach ( $items as $item ) { |
||
552 | wp_delete_post( $item, true ); |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Meta. |
||
557 | */ |
||
558 | $tables = array(); |
||
559 | |||
560 | if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) { |
||
561 | $tables []= \GFFormsModel::get_entry_meta_table_name(); |
||
562 | } |
||
563 | $tables []= \GFFormsModel::get_lead_meta_table_name(); |
||
564 | |||
565 | foreach ( $tables as $meta_table ) { |
||
566 | $sql = " |
||
567 | DELETE FROM $meta_table |
||
568 | WHERE ( |
||
569 | `meta_key` = 'is_approved' |
||
570 | ); |
||
571 | "; |
||
572 | $wpdb->query( $sql ); |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Notes. |
||
577 | */ |
||
578 | $tables = array(); |
||
579 | |||
580 | if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) { |
||
581 | $tables[] = \GFFormsModel::get_entry_notes_table_name(); |
||
582 | } |
||
583 | |||
584 | $tables[] = \GFFormsModel::get_lead_notes_table_name(); |
||
585 | |||
586 | $disapproved = __('Disapproved the Entry for GravityView', 'gravityview'); |
||
587 | $approved = __('Approved the Entry for GravityView', 'gravityview'); |
||
588 | |||
589 | $suppress = $wpdb->suppress_errors(); |
||
590 | foreach ( $tables as $notes_table ) { |
||
591 | $sql = $wpdb->prepare( " |
||
592 | DELETE FROM $notes_table |
||
593 | WHERE ( |
||
594 | `note_type` = 'gravityview' OR |
||
595 | `value` = %s OR |
||
596 | `value` = %s |
||
597 | ); |
||
598 | ", $approved, $disapproved ); |
||
599 | $wpdb->query( $sql ); |
||
600 | } |
||
601 | |||
602 | $wpdb->suppress_errors( $suppress ); |
||
603 | |||
604 | /** |
||
605 | * Capabilities. |
||
606 | */ |
||
607 | \GravityView_Roles_Capabilities::get_instance()->remove_caps(); |
||
608 | |||
609 | /** |
||
610 | * Options. |
||
611 | */ |
||
612 | delete_option( 'gravityview_cache_blacklist' ); |
||
613 | delete_option( 'gv_version_upgraded_from' ); |
||
614 | delete_transient( 'gravityview_edd-activate_valid' ); |
||
615 | delete_transient( 'gravityview_edd-deactivate_valid' ); |
||
616 | delete_transient( 'gravityview_dismissed_notices' ); |
||
617 | delete_site_transient( 'gravityview_related_plugins' ); |
||
618 | } |
||
619 | |||
620 | private function __clone() { } |
||
621 | |||
622 | private function __wakeup() { } |
||
623 | } |
||
624 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: