| Total Complexity | 82 |
| Total Lines | 403 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 4 | Features | 0 |
Complex classes like Kirki_Helper 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_Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Kirki_Helper { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Recursive replace in arrays. |
||
| 25 | * |
||
| 26 | * @static |
||
| 27 | * @access public |
||
| 28 | * @param array $array The first array. |
||
| 29 | * @param array $array1 The second array. |
||
| 30 | * @return mixed |
||
| 31 | */ |
||
| 32 | public static function array_replace_recursive( $array, $array1 ) { |
||
| 33 | if ( function_exists( 'array_replace_recursive' ) ) { |
||
| 34 | // @codingStandardsIgnoreLine PHPCompatibility.PHP.NewFunctions.array_replace_recursiveFound |
||
| 35 | return array_replace_recursive( $array, $array1 ); // phpcs:ignore PHPCompatibility.PHP.NewFunctions.array_replace_recursiveFound |
||
| 36 | } |
||
| 37 | |||
| 38 | // Handle the arguments, merge one by one. |
||
| 39 | $args = func_get_args(); |
||
| 40 | $array = $args[0]; |
||
| 41 | if ( ! is_array( $array ) ) { |
||
| 42 | return $array; |
||
| 43 | } |
||
| 44 | $count = count( $args ); |
||
| 45 | for ( $i = 1; $i < $count; $i++ ) { |
||
| 46 | if ( is_array( $args[ $i ] ) ) { |
||
| 47 | $array = self::recurse( $array, $args[ $i ] ); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | return $array; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Helper method to be used from the array_replace_recursive method. |
||
| 55 | * |
||
| 56 | * @static |
||
| 57 | * @access public |
||
| 58 | * @param array $array The first array. |
||
| 59 | * @param array $array1 The second array. |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public static function recurse( $array, $array1 ) { |
||
| 63 | foreach ( $array1 as $key => $value ) { |
||
| 64 | // Create new key in $array, if it is empty or not an array. |
||
| 65 | if ( ! isset( $array[ $key ] ) || ( isset( $array[ $key ] ) && ! is_array( $array[ $key ] ) ) ) { |
||
| 66 | $array[ $key ] = array(); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Overwrite the value in the base array. |
||
| 70 | if ( is_array( $value ) ) { |
||
| 71 | $value = self::recurse( $array[ $key ], $value ); |
||
| 72 | } |
||
| 73 | $array[ $key ] = $value; |
||
| 74 | } |
||
| 75 | return $array; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initialize the WP_Filesystem |
||
| 80 | * |
||
| 81 | * @static |
||
| 82 | * @access public |
||
| 83 | * @return object WP_Filesystem |
||
| 84 | */ |
||
| 85 | public static function init_filesystem() { |
||
| 86 | global $wp_filesystem; |
||
| 87 | if ( empty( $wp_filesystem ) ) { |
||
| 88 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
||
| 89 | WP_Filesystem(); |
||
| 90 | } |
||
| 91 | return $wp_filesystem; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the attachment object |
||
| 96 | * |
||
| 97 | * @static |
||
| 98 | * @access public |
||
| 99 | * @see https://pippinsplugins.com/retrieve-attachment-id-from-image-url/ |
||
| 100 | * @param string $url URL to the image. |
||
| 101 | * @return int|string Numeric ID of the attachement. |
||
| 102 | */ |
||
| 103 | public static function get_image_id( $url ) { |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns an array of the attachment's properties. |
||
| 124 | * |
||
| 125 | * @param string $url URL to the image. |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public static function get_image_from_url( $url ) { |
||
| 129 | |||
| 130 | $image_id = self::get_image_id( $url ); |
||
| 131 | $image = wp_get_attachment_image_src( $image_id, 'full' ); |
||
| 132 | |||
| 133 | return array( |
||
| 134 | 'url' => $image[0], |
||
| 135 | 'width' => $image[1], |
||
| 136 | 'height' => $image[2], |
||
| 137 | 'thumbnail' => $image[3], |
||
| 138 | ); |
||
| 139 | |||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get an array of posts. |
||
| 144 | * |
||
| 145 | * @static |
||
| 146 | * @access public |
||
| 147 | * @param array $args Define arguments for the get_posts function. |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public static function get_posts( $args ) { |
||
| 151 | |||
| 152 | if ( is_string( $args ) ) { |
||
|
|
|||
| 153 | $args = add_query_arg( |
||
| 154 | array( |
||
| 155 | 'suppress_filters' => false, |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | } elseif ( is_array( $args ) && ! isset( $args['suppress_filters'] ) ) { |
||
| 159 | $args['suppress_filters'] = false; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Get the posts. |
||
| 163 | // TODO: WordPress.VIP.RestrictedFunctions.get_posts_get_posts. |
||
| 164 | $posts = get_posts( $args ); |
||
| 165 | |||
| 166 | // Properly format the array. |
||
| 167 | $items = array(); |
||
| 168 | foreach ( $posts as $post ) { |
||
| 169 | $items[ $post->ID ] = $post->post_title; |
||
| 170 | } |
||
| 171 | wp_reset_postdata(); |
||
| 172 | |||
| 173 | return $items; |
||
| 174 | |||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get an array of publicly-querable taxonomies. |
||
| 179 | * |
||
| 180 | * @static |
||
| 181 | * @access public |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public static function get_taxonomies() { |
||
| 185 | |||
| 186 | $items = array(); |
||
| 187 | |||
| 188 | // Get the taxonomies. |
||
| 189 | $taxonomies = get_taxonomies( |
||
| 190 | array( |
||
| 191 | 'public' => true, |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | |||
| 195 | // Build the array. |
||
| 196 | foreach ( $taxonomies as $taxonomy ) { |
||
| 197 | $id = $taxonomy; |
||
| 198 | $taxonomy = get_taxonomy( $taxonomy ); |
||
| 199 | $items[ $id ] = $taxonomy->labels->name; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $items; |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get an array of publicly-querable post-types. |
||
| 208 | * |
||
| 209 | * @static |
||
| 210 | * @access public |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public static function get_post_types() { |
||
| 214 | |||
| 215 | $items = array(); |
||
| 216 | |||
| 217 | // Get the post types. |
||
| 218 | $post_types = get_post_types( |
||
| 219 | array( |
||
| 220 | 'public' => true, |
||
| 221 | ), 'objects' |
||
| 222 | ); |
||
| 223 | |||
| 224 | // Build the array. |
||
| 225 | foreach ( $post_types as $post_type ) { |
||
| 226 | $items[ $post_type->name ] = $post_type->labels->name; |
||
| 227 | } |
||
| 228 | |||
| 229 | return $items; |
||
| 230 | |||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get an array of terms from a taxonomy |
||
| 235 | * |
||
| 236 | * @static |
||
| 237 | * @access public |
||
| 238 | * @param string|array $taxonomies See https://developer.wordpress.org/reference/functions/get_terms/ for details. |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public static function get_terms( $taxonomies ) { |
||
| 254 | |||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Gets an array of material-design colors. |
||
| 259 | * |
||
| 260 | * @static |
||
| 261 | * @access public |
||
| 262 | * @param string $context Allows us to get subsets of the palette. |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public static function get_material_design_colors( $context = 'primary' ) { |
||
| 344 | } // End switch(). |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get an array of all available dashicons. |
||
| 349 | * |
||
| 350 | * @static |
||
| 351 | * @access public |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public static function get_dashicons() { |
||
| 372 | ); |
||
| 373 | |||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Compares the 2 values given the condition |
||
| 378 | * |
||
| 379 | * @param mixed $value1 The 1st value in the comparison. |
||
| 380 | * @param mixed $value2 The 2nd value in the comparison. |
||
| 381 | * @param string $operator The operator we'll use for the comparison. |
||
| 382 | * @return boolean whether The comparison has succeded (true) or failed (false). |
||
| 383 | */ |
||
| 384 | public static function compare_values( $value1, $value2, $operator ) { |
||
| 426 |