| Total Complexity | 95 |
| Total Lines | 440 |
| 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 | $credentials = array(); |
||
| 87 | |||
| 88 | if ( ! defined( 'FS_METHOD' ) ) { |
||
| 89 | define( 'FS_METHOD', 'direct' ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $method = defined( 'FS_METHOD' ) ? FS_METHOD : false; |
||
| 93 | |||
| 94 | if ( 'ftpext' === $method ) { |
||
| 95 | // If defined, set it to that, Else, set to NULL. |
||
| 96 | $credentials['hostname'] = defined( 'FTP_HOST' ) ? preg_replace( '|\w+://|', '', FTP_HOST ) : null; |
||
|
|
|||
| 97 | $credentials['username'] = defined( 'FTP_USER' ) ? FTP_USER : null; |
||
| 98 | $credentials['password'] = defined( 'FTP_PASS' ) ? FTP_PASS : null; |
||
| 99 | |||
| 100 | // Set FTP port. |
||
| 101 | if ( strpos( $credentials['hostname'], ':' ) && null !== $credentials['hostname'] ) { |
||
| 102 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
||
| 103 | if ( ! is_numeric( $credentials['port'] ) ) { |
||
| 104 | unset( $credentials['port'] ); |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | unset( $credentials['port'] ); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Set connection type. |
||
| 111 | if ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $method ) { |
||
| 112 | $credentials['connection_type'] = 'ftps'; |
||
| 113 | } elseif ( ! array_filter( $credentials ) ) { |
||
| 114 | $credentials['connection_type'] = null; |
||
| 115 | } else { |
||
| 116 | $credentials['connection_type'] = 'ftp'; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // The WordPress filesystem. |
||
| 121 | global $wp_filesystem; |
||
| 122 | |||
| 123 | if ( empty( $wp_filesystem ) ) { |
||
| 124 | require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' ); |
||
| 125 | WP_Filesystem( $credentials ); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $wp_filesystem; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the attachment object |
||
| 133 | * |
||
| 134 | * @static |
||
| 135 | * @access public |
||
| 136 | * @see https://pippinsplugins.com/retrieve-attachment-id-from-image-url/ |
||
| 137 | * @param string $url URL to the image. |
||
| 138 | * @return int|string Numeric ID of the attachement. |
||
| 139 | */ |
||
| 140 | public static function get_image_id( $url ) { |
||
| 141 | global $wpdb; |
||
| 142 | if ( empty( $url ) ) { |
||
| 143 | return 0; |
||
| 144 | } |
||
| 145 | |||
| 146 | $attachment = wp_cache_get( 'kirki_image_id_' . md5( $url ), null ); |
||
| 147 | if ( false === $attachment ) { |
||
| 148 | // @codingStandardsIgnoreLine WordPress.VIP.DirectDatabaseQuery.DirectQuery |
||
| 149 | $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid = %s;", $url ) ); // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery |
||
| 150 | wp_cache_add( 'kirki_image_id_' . md5( $url ), $attachment, null ); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ( ! empty( $attachment ) ) { |
||
| 154 | return $attachment[0]; |
||
| 155 | } |
||
| 156 | return 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns an array of the attachment's properties. |
||
| 161 | * |
||
| 162 | * @param string $url URL to the image. |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public static function get_image_from_url( $url ) { |
||
| 166 | |||
| 167 | $image_id = self::get_image_id( $url ); |
||
| 168 | $image = wp_get_attachment_image_src( $image_id, 'full' ); |
||
| 169 | |||
| 170 | return array( |
||
| 171 | 'url' => $image[0], |
||
| 172 | 'width' => $image[1], |
||
| 173 | 'height' => $image[2], |
||
| 174 | 'thumbnail' => $image[3], |
||
| 175 | ); |
||
| 176 | |||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get an array of posts. |
||
| 181 | * |
||
| 182 | * @static |
||
| 183 | * @access public |
||
| 184 | * @param array $args Define arguments for the get_posts function. |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public static function get_posts( $args ) { |
||
| 188 | |||
| 189 | if ( is_string( $args ) ) { |
||
| 190 | $args = add_query_arg( |
||
| 191 | array( |
||
| 192 | 'suppress_filters' => false, |
||
| 193 | ) |
||
| 194 | ); |
||
| 195 | } elseif ( is_array( $args ) && ! isset( $args['suppress_filters'] ) ) { |
||
| 196 | $args['suppress_filters'] = false; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Get the posts. |
||
| 200 | // TODO: WordPress.VIP.RestrictedFunctions.get_posts_get_posts. |
||
| 201 | $posts = get_posts( $args ); |
||
| 202 | |||
| 203 | // Properly format the array. |
||
| 204 | $items = array(); |
||
| 205 | foreach ( $posts as $post ) { |
||
| 206 | $items[ $post->ID ] = $post->post_title; |
||
| 207 | } |
||
| 208 | wp_reset_postdata(); |
||
| 209 | |||
| 210 | return $items; |
||
| 211 | |||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get an array of publicly-querable taxonomies. |
||
| 216 | * |
||
| 217 | * @static |
||
| 218 | * @access public |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public static function get_taxonomies() { |
||
| 222 | |||
| 223 | $items = array(); |
||
| 224 | |||
| 225 | // Get the taxonomies. |
||
| 226 | $taxonomies = get_taxonomies( |
||
| 227 | array( |
||
| 228 | 'public' => true, |
||
| 229 | ) |
||
| 230 | ); |
||
| 231 | |||
| 232 | // Build the array. |
||
| 233 | foreach ( $taxonomies as $taxonomy ) { |
||
| 234 | $id = $taxonomy; |
||
| 235 | $taxonomy = get_taxonomy( $taxonomy ); |
||
| 236 | $items[ $id ] = $taxonomy->labels->name; |
||
| 237 | } |
||
| 238 | |||
| 239 | return $items; |
||
| 240 | |||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get an array of publicly-querable post-types. |
||
| 245 | * |
||
| 246 | * @static |
||
| 247 | * @access public |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public static function get_post_types() { |
||
| 251 | |||
| 252 | $items = array(); |
||
| 253 | |||
| 254 | // Get the post types. |
||
| 255 | $post_types = get_post_types( |
||
| 256 | array( |
||
| 257 | 'public' => true, |
||
| 258 | ), 'objects' |
||
| 259 | ); |
||
| 260 | |||
| 261 | // Build the array. |
||
| 262 | foreach ( $post_types as $post_type ) { |
||
| 263 | $items[ $post_type->name ] = $post_type->labels->name; |
||
| 264 | } |
||
| 265 | |||
| 266 | return $items; |
||
| 267 | |||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get an array of terms from a taxonomy |
||
| 272 | * |
||
| 273 | * @static |
||
| 274 | * @access public |
||
| 275 | * @param string|array $taxonomies See https://developer.wordpress.org/reference/functions/get_terms/ for details. |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public static function get_terms( $taxonomies ) { |
||
| 279 | |||
| 280 | $items = array(); |
||
| 281 | |||
| 282 | // Get the post types. |
||
| 283 | $terms = get_terms( $taxonomies ); |
||
| 284 | |||
| 285 | // Build the array. |
||
| 286 | foreach ( $terms as $term ) { |
||
| 287 | $items[ $term->term_id ] = $term->name; |
||
| 288 | } |
||
| 289 | |||
| 290 | return $items; |
||
| 291 | |||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets an array of material-design colors. |
||
| 296 | * |
||
| 297 | * @static |
||
| 298 | * @access public |
||
| 299 | * @param string $context Allows us to get subsets of the palette. |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public static function get_material_design_colors( $context = 'primary' ) { |
||
| 303 | |||
| 304 | $colors = array( |
||
| 305 | 'primary' => array( '#FFFFFF', '#000000', '#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B' ), |
||
| 306 | 'red' => array( '#FFEBEE', '#FFCDD2', '#EF9A9A', '#E57373', '#EF5350', '#F44336', '#E53935', '#D32F2F', '#C62828', '#B71C1C', '#FF8A80', '#FF5252', '#FF1744', '#D50000' ), |
||
| 307 | 'pink' => array( '#FCE4EC', '#F8BBD0', '#F48FB1', '#F06292', '#EC407A', '#E91E63', '#D81B60', '#C2185B', '#AD1457', '#880E4F', '#FF80AB', '#FF4081', '#F50057', '#C51162' ), |
||
| 308 | 'purple' => array( '#F3E5F5', '#E1BEE7', '#CE93D8', '#BA68C8', '#AB47BC', '#9C27B0', '#8E24AA', '#7B1FA2', '#6A1B9A', '#4A148C', '#EA80FC', '#E040FB', '#D500F9', '#AA00FF' ), |
||
| 309 | 'deep-purple' => array( '#EDE7F6', '#D1C4E9', '#B39DDB', '#9575CD', '#7E57C2', '#673AB7', '#5E35B1', '#512DA8', '#4527A0', '#311B92', '#B388FF', '#7C4DFF', '#651FFF', '#6200EA' ), |
||
| 310 | 'indigo' => array( '#E8EAF6', '#C5CAE9', '#9FA8DA', '#7986CB', '#5C6BC0', '#3F51B5', '#3949AB', '#303F9F', '#283593', '#1A237E', '#8C9EFF', '#536DFE', '#3D5AFE', '#304FFE' ), |
||
| 311 | 'blue' => array( '#E3F2FD', '#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3', '#1E88E5', '#1976D2', '#1565C0', '#0D47A1', '#82B1FF', '#448AFF', '#2979FF', '#2962FF' ), |
||
| 312 | 'light-blue' => array( '#E1F5FE', '#B3E5FC', '#81D4fA', '#4fC3F7', '#29B6FC', '#03A9F4', '#039BE5', '#0288D1', '#0277BD', '#01579B', '#80D8FF', '#40C4FF', '#00B0FF', '#0091EA' ), |
||
| 313 | 'cyan' => array( '#E0F7FA', '#B2EBF2', '#80DEEA', '#4DD0E1', '#26C6DA', '#00BCD4', '#00ACC1', '#0097A7', '#00838F', '#006064', '#84FFFF', '#18FFFF', '#00E5FF', '#00B8D4' ), |
||
| 314 | 'teal' => array( '#E0F2F1', '#B2DFDB', '#80CBC4', '#4DB6AC', '#26A69A', '#009688', '#00897B', '#00796B', '#00695C', '#004D40', '#A7FFEB', '#64FFDA', '#1DE9B6', '#00BFA5' ), |
||
| 315 | 'green' => array( '#E8F5E9', '#C8E6C9', '#A5D6A7', '#81C784', '#66BB6A', '#4CAF50', '#43A047', '#388E3C', '#2E7D32', '#1B5E20', '#B9F6CA', '#69F0AE', '#00E676', '#00C853' ), |
||
| 316 | 'light-green' => array( '#F1F8E9', '#DCEDC8', '#C5E1A5', '#AED581', '#9CCC65', '#8BC34A', '#7CB342', '#689F38', '#558B2F', '#33691E', '#CCFF90', '#B2FF59', '#76FF03', '#64DD17' ), |
||
| 317 | 'lime' => array( '#F9FBE7', '#F0F4C3', '#E6EE9C', '#DCE775', '#D4E157', '#CDDC39', '#C0CA33', '#A4B42B', '#9E9D24', '#827717', '#F4FF81', '#EEFF41', '#C6FF00', '#AEEA00' ), |
||
| 318 | 'yellow' => array( '#FFFDE7', '#FFF9C4', '#FFF590', '#FFF176', '#FFEE58', '#FFEB3B', '#FDD835', '#FBC02D', '#F9A825', '#F57F17', '#FFFF82', '#FFFF00', '#FFEA00', '#FFD600' ), |
||
| 319 | 'amber' => array( '#FFF8E1', '#FFECB3', '#FFE082', '#FFD54F', '#FFCA28', '#FFC107', '#FFB300', '#FFA000', '#FF8F00', '#FF6F00', '#FFE57F', '#FFD740', '#FFC400', '#FFAB00' ), |
||
| 320 | 'orange' => array( '#FFF3E0', '#FFE0B2', '#FFCC80', '#FFB74D', '#FFA726', '#FF9800', '#FB8C00', '#F57C00', '#EF6C00', '#E65100', '#FFD180', '#FFAB40', '#FF9100', '#FF6D00' ), |
||
| 321 | 'deep-orange' => array( '#FBE9A7', '#FFCCBC', '#FFAB91', '#FF8A65', '#FF7043', '#FF5722', '#F4511E', '#E64A19', '#D84315', '#BF360C', '#FF9E80', '#FF6E40', '#FF3D00', '#DD2600' ), |
||
| 322 | 'brown' => array( '#EFEBE9', '#D7CCC8', '#BCAAA4', '#A1887F', '#8D6E63', '#795548', '#6D4C41', '#5D4037', '#4E342E', '#3E2723' ), |
||
| 323 | 'grey' => array( '#FAFAFA', '#F5F5F5', '#EEEEEE', '#E0E0E0', '#BDBDBD', '#9E9E9E', '#757575', '#616161', '#424242', '#212121', '#000000', '#ffffff' ), |
||
| 324 | 'blue-grey' => array( '#ECEFF1', '#CFD8DC', '#B0BBC5', '#90A4AE', '#78909C', '#607D8B', '#546E7A', '#455A64', '#37474F', '#263238' ), |
||
| 325 | ); |
||
| 326 | |||
| 327 | switch ( $context ) { |
||
| 328 | |||
| 329 | case '50': |
||
| 330 | case '100': |
||
| 331 | case '200': |
||
| 332 | case '300': |
||
| 333 | case '400': |
||
| 334 | case '500': |
||
| 335 | case '600': |
||
| 336 | case '700': |
||
| 337 | case '800': |
||
| 338 | case '900': |
||
| 339 | case 'A100': |
||
| 340 | case 'A200': |
||
| 341 | case 'A400': |
||
| 342 | case 'A700': |
||
| 343 | $key = absint( $context ) / 100; |
||
| 344 | if ( 'A100' === $context ) { |
||
| 345 | $key = 10; |
||
| 346 | unset( $colors['grey'] ); |
||
| 347 | } elseif ( 'A200' === $context ) { |
||
| 348 | $key = 11; |
||
| 349 | unset( $colors['grey'] ); |
||
| 350 | } elseif ( 'A400' === $context ) { |
||
| 351 | $key = 12; |
||
| 352 | unset( $colors['grey'] ); |
||
| 353 | } elseif ( 'A700' === $context ) { |
||
| 354 | $key = 13; |
||
| 355 | unset( $colors['grey'] ); |
||
| 356 | } |
||
| 357 | unset( $colors['primary'] ); |
||
| 358 | $position_colors = array(); |
||
| 359 | foreach ( $colors as $color_family ) { |
||
| 360 | if ( isset( $color_family[ $key ] ) ) { |
||
| 361 | $position_colors[] = $color_family[ $key ]; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | return $position_colors; |
||
| 365 | case 'all': |
||
| 366 | unset( $colors['primary'] ); |
||
| 367 | $all_colors = array(); |
||
| 368 | foreach ( $colors as $color_family ) { |
||
| 369 | foreach ( $color_family as $color ) { |
||
| 370 | $all_colors[] = $color; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | return $all_colors; |
||
| 374 | case 'primary': |
||
| 375 | return $colors['primary']; |
||
| 376 | default: |
||
| 377 | if ( isset( $colors[ $context ] ) ) { |
||
| 378 | return $colors[ $context ]; |
||
| 379 | } |
||
| 380 | return $colors['primary']; |
||
| 381 | } // End switch(). |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get an array of all available dashicons. |
||
| 386 | * |
||
| 387 | * @static |
||
| 388 | * @access public |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public static function get_dashicons() { |
||
| 409 | ); |
||
| 410 | |||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Compares the 2 values given the condition |
||
| 415 | * |
||
| 416 | * @param mixed $value1 The 1st value in the comparison. |
||
| 417 | * @param mixed $value2 The 2nd value in the comparison. |
||
| 418 | * @param string $operator The operator we'll use for the comparison. |
||
| 419 | * @return boolean whether The comparison has succeded (true) or failed (false). |
||
| 420 | */ |
||
| 421 | public static function compare_values( $value1, $value2, $operator ) { |
||
| 461 | } |
||
| 462 | } |
||
| 463 |