Total Complexity | 40 |
Total Lines | 175 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Kirki_Util 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.
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 Kirki_Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Kirki_Util { |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | * |
||
21 | * @since 3.0.9 |
||
22 | * @access public |
||
23 | */ |
||
24 | public function __construct() { |
||
25 | |||
26 | add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
||
1 ignored issue
–
show
|
|||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Determine if Kirki is installed as a plugin. |
||
31 | * |
||
32 | * @static |
||
33 | * @access public |
||
34 | * @since 3.0.0 |
||
35 | * @return bool |
||
36 | */ |
||
37 | public static function is_plugin() { |
||
38 | |||
39 | $is_plugin = false; |
||
40 | if ( ! function_exists( 'get_plugins' ) ) { |
||
41 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
1 ignored issue
–
show
|
|||
42 | } |
||
43 | |||
44 | // Get all plugins. |
||
45 | $plugins = get_plugins(); |
||
46 | $_plugin = ''; |
||
47 | foreach ( $plugins as $plugin => $args ) { |
||
48 | if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
||
49 | $is_plugin = true; |
||
50 | $_plugin = $plugin; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | // No need to proceed any further if Kirki wasn't found in the list of plugins. |
||
55 | if ( ! $is_plugin ) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | // Make sure the is_plugins_loaded function is loaded. |
||
60 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
61 | |||
62 | // Extra logic in case the plugin is installed but not activated. |
||
63 | if ( $_plugin && ! is_plugin_active( $_plugin ) ) { |
||
1 ignored issue
–
show
|
|||
64 | return false; |
||
65 | } |
||
66 | return $is_plugin; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Build the variables. |
||
71 | * |
||
72 | * @static |
||
73 | * @access public |
||
74 | * @since 3.0.9 |
||
75 | * @return array Formatted as array( 'variable-name' => value ). |
||
76 | */ |
||
77 | public static function get_variables() { |
||
113 | |||
114 | } |
||
115 | |||
116 | /** |
||
117 | * HTTP Request injection. |
||
118 | * |
||
119 | * @access public |
||
120 | * @since 3.0.0 |
||
121 | * @param array $request The request params. |
||
122 | * @param string $url The request URL. |
||
123 | * @return array |
||
124 | */ |
||
125 | public function http_request( $request = array(), $url = '' ) { |
||
126 | // Early exit if installed as a plugin or not a request to wordpress.org, |
||
127 | // or finally if we don't have everything we need. |
||
128 | if ( |
||
129 | self::is_plugin() || |
||
130 | false === strpos( $url, 'wordpress.org' ) || ( |
||
131 | ! isset( $request['body'] ) || |
||
132 | ! isset( $request['body']['plugins'] ) || |
||
133 | ! isset( $request['body']['translations'] ) || |
||
134 | ! isset( $request['body']['locale'] ) || |
||
135 | ! isset( $request['body']['all'] ) |
||
136 | ) |
||
137 | ) { |
||
138 | return $request; |
||
139 | } |
||
140 | |||
141 | $plugins = json_decode( $request['body']['plugins'], true ); |
||
142 | if ( ! isset( $plugins['plugins'] ) ) { |
||
143 | return $request; |
||
144 | } |
||
145 | $exists = false; |
||
146 | foreach ( $plugins['plugins'] as $plugin ) { |
||
147 | if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
||
148 | $exists = true; |
||
149 | } |
||
150 | } |
||
151 | // Inject data. |
||
152 | if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
||
153 | $plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
||
1 ignored issue
–
show
|
|||
154 | } |
||
155 | $request['body']['plugins'] = wp_json_encode( $plugins ); |
||
1 ignored issue
–
show
|
|||
156 | return $request; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Returns the $wp_version. |
||
161 | * |
||
162 | * @static |
||
163 | * @access public |
||
164 | * @since 3.0.12 |
||
165 | * @param string $context Use 'minor' or 'major'. |
||
166 | * @param boolean $only_numeric Set to true if you want to skip the alpha/beta etc parts. |
||
167 | * @return int|float|string Returns integer when getting the 'major' version. |
||
168 | * Returns float when getting the 'minor' version with $only_numeric set to true. |
||
169 | * Returns string when getting the 'minor' version with $only_numeric set to false. |
||
170 | */ |
||
171 | public static function get_wp_version( $context = 'minor', $only_numeric = true ) { |
||
191 | } |
||
192 | } |
||
193 |