Total Complexity | 40 |
Total Lines | 188 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 | add_filter( 'http_request_args', array( $this, 'http_request' ), 10, 2 ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Determine if Kirki is installed as a plugin. |
||
30 | * |
||
31 | * @static |
||
32 | * @access public |
||
33 | * @since 3.0.0 |
||
34 | * @return bool |
||
35 | */ |
||
36 | public static function is_plugin() { |
||
37 | $is_plugin = false; |
||
38 | if ( ! function_exists( 'get_plugins' ) ) { |
||
39 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
40 | } |
||
41 | |||
42 | // Get all plugins. |
||
43 | $plugins = get_plugins(); |
||
44 | $_plugin = ''; |
||
45 | foreach ( $plugins as $plugin => $args ) { |
||
46 | if ( ! $is_plugin && isset( $args['Name'] ) && ( 'Kirki' === $args['Name'] || 'Kirki Toolkit' === $args['Name'] ) ) { |
||
47 | $is_plugin = true; |
||
48 | $_plugin = $plugin; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | // No need to proceed any further if Kirki wasn't found in the list of plugins. |
||
53 | if ( ! $is_plugin ) { |
||
54 | return false; |
||
55 | } |
||
56 | |||
57 | // Make sure the is_plugins_loaded function is loaded. |
||
58 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
59 | |||
60 | // Extra logic in case the plugin is installed but not activated. |
||
61 | if ( $_plugin && is_plugin_inactive( $_plugin ) ) { |
||
62 | return false; |
||
63 | } |
||
64 | return $is_plugin; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Build the variables. |
||
69 | * |
||
70 | * @static |
||
71 | * @access public |
||
72 | * @since 3.0.9 |
||
73 | * @return array Formatted as array( 'variable-name' => value ). |
||
74 | */ |
||
75 | public static function get_variables() { |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * HTTP Request injection. |
||
112 | * |
||
113 | * @access public |
||
114 | * @since 3.0.0 |
||
115 | * @param array $request The request params. |
||
116 | * @param string $url The request URL. |
||
117 | * @return array |
||
118 | */ |
||
119 | public function http_request( $request = array(), $url = '' ) { |
||
120 | |||
121 | // Early exit if installed as a plugin or not a request to wordpress.org, |
||
122 | // or finally if we don't have everything we need. |
||
123 | if ( |
||
124 | self::is_plugin() || |
||
125 | false === strpos( $url, 'wordpress.org' ) || ( |
||
126 | ! isset( $request['body'] ) || |
||
127 | ! isset( $request['body']['plugins'] ) || |
||
128 | ! isset( $request['body']['translations'] ) || |
||
129 | ! isset( $request['body']['locale'] ) || |
||
130 | ! isset( $request['body']['all'] ) |
||
131 | ) |
||
132 | ) { |
||
133 | return $request; |
||
134 | } |
||
135 | |||
136 | $plugins = json_decode( $request['body']['plugins'], true ); |
||
137 | if ( ! isset( $plugins['plugins'] ) ) { |
||
138 | return $request; |
||
139 | } |
||
140 | $exists = false; |
||
141 | foreach ( $plugins['plugins'] as $plugin ) { |
||
142 | if ( isset( $plugin['Name'] ) && 'Kirki Toolkit' === $plugin['Name'] ) { |
||
143 | $exists = true; |
||
144 | } |
||
145 | } |
||
146 | // Inject data. |
||
147 | if ( ! $exists && defined( 'KIRKI_PLUGIN_FILE' ) ) { |
||
148 | $plugins['plugins']['kirki/kirki.php'] = get_plugin_data( KIRKI_PLUGIN_FILE ); |
||
149 | } |
||
150 | $request['body']['plugins'] = wp_json_encode( $plugins ); |
||
151 | return $request; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns the $wp_version. |
||
156 | * |
||
157 | * @static |
||
158 | * @access public |
||
159 | * @since 3.0.12 |
||
160 | * @param string $context Use 'minor' or 'major'. |
||
161 | * @return int|string Returns integer when getting the 'major' version. |
||
162 | * Returns string when getting the 'minor' version. |
||
163 | */ |
||
164 | public static function get_wp_version( $context = 'minor' ) { |
||
165 | global $wp_version; |
||
166 | |||
167 | // We only need the major version. |
||
168 | if ( 'major' === $context ) { |
||
169 | $version_parts = explode( '.', $wp_version ); |
||
170 | return $version_parts[0]; |
||
171 | } |
||
172 | |||
173 | return $wp_version; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns the $wp_version, only numeric value. |
||
178 | * |
||
179 | * @static |
||
180 | * @access public |
||
181 | * @since 3.0.12 |
||
182 | * @param string $context Use 'minor' or 'major'. |
||
183 | * @param bool $only_numeric Whether we wwant to return numeric value or include beta/alpha etc. |
||
184 | * @return int|float Returns integer when getting the 'major' version. |
||
185 | * Returns float when getting the 'minor' version. |
||
186 | */ |
||
187 | public static function get_wp_version_numeric( $context = 'minor', $only_numeric = true ) { |
||
204 | } |
||
205 | } |
||
206 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.