Complex classes like FrmStylesHelper 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 FrmStylesHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmStylesHelper { |
||
| 4 | |||
| 5 | public static function get_upload_base() { |
||
| 6 | $uploads = wp_upload_dir(); |
||
| 7 | if ( is_ssl() && ! preg_match( '/^https:\/\/.*\..*$/', $uploads['baseurl'] ) ) { |
||
| 8 | $uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] ); |
||
| 9 | } |
||
| 10 | |||
| 11 | return $uploads; |
||
| 12 | } |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @since 4.0 |
||
| 16 | */ |
||
| 17 | public static function get_style_menu( $active = '' ) { |
||
| 18 | ob_start(); |
||
| 19 | self::style_menu( $active ); |
||
| 20 | $menu = ob_get_contents(); |
||
| 21 | ob_end_clean(); |
||
| 22 | |||
| 23 | return $menu; |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function style_menu( $active = '' ) { |
||
| 27 | ?> |
||
| 28 | <ul class="frm_form_nav"> |
||
| 29 | <li> |
||
| 30 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles' ) ); ?>" |
||
| 31 | class="<?php echo ( '' == $active ) ? 'current_page' : ''; ?>"> |
||
| 32 | <?php esc_html_e( 'Edit Styles', 'formidable' ); ?> |
||
| 33 | </a> |
||
| 34 | </li> |
||
| 35 | <li> |
||
| 36 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=manage' ) ); ?>" |
||
| 37 | class="<?php echo ( 'manage' == $active ) ? 'current_page' : ''; ?>"> |
||
| 38 | <?php esc_html_e( 'Manage Styles', 'formidable' ); ?> |
||
| 39 | </a> |
||
| 40 | </li> |
||
| 41 | <li> |
||
| 42 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=custom_css' ) ); ?>" |
||
| 43 | class="<?php echo ( 'custom_css' == $active ) ? 'current_page' : ''; ?>"> |
||
| 44 | <?php esc_html_e( 'Custom CSS', 'formidable' ); ?> |
||
| 45 | </a> |
||
| 46 | </li> |
||
| 47 | </ul> |
||
| 48 | <?php |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @since 4.0 |
||
| 53 | */ |
||
| 54 | public static function styler_save_button( $atts ) { |
||
| 55 | $style = $atts['style']; |
||
| 56 | if ( ! empty( $style->ID ) && empty( $style->menu_order ) ) { |
||
| 57 | $delete_link = admin_url( 'admin.php?page=formidable-styles&frm_action=destroy&id=' . $style->ID ); |
||
| 58 | } |
||
| 59 | |||
| 60 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/header-buttons.php' ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Called from the admin header. |
||
| 65 | * |
||
| 66 | * @since 4.0 |
||
| 67 | */ |
||
| 68 | public static function save_button() { |
||
| 69 | ?> |
||
| 70 | <input type="submit" name="submit" class="button button-primary frm-button-primary" value="<?php esc_attr_e( 'Update', 'formidable' ); ?>" /> |
||
| 71 | <?php |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Either get the heading or the style switcher. |
||
| 76 | * |
||
| 77 | * @since 4.0 |
||
| 78 | */ |
||
| 79 | public static function styler_switcher( $atts ) { |
||
| 80 | if ( has_action( 'frm_style_switcher_heading' ) ) { |
||
| 81 | do_action( 'frm_style_switcher_heading', $atts ); |
||
| 82 | } else { |
||
| 83 | ?> |
||
| 84 | <div class="frm_top_left"> |
||
| 85 | <h1> |
||
| 86 | <?php echo esc_html( $atts['style']->post_title ); ?> |
||
| 87 | </h1> |
||
| 88 | </div> |
||
| 89 | <?php |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @since 2.05 |
||
| 95 | */ |
||
| 96 | public static function get_css_label_positions() { |
||
| 97 | return array( |
||
| 98 | 'none' => __( 'top', 'formidable' ), |
||
| 99 | 'left' => __( 'left', 'formidable' ), |
||
| 100 | 'right' => __( 'right', 'formidable' ), |
||
| 101 | 'no_label' => __( 'none', 'formidable' ), |
||
| 102 | 'inside' => __( 'inside', 'formidable' ), |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | public static function get_single_label_positions() { |
||
| 107 | return array( |
||
| 108 | 'top' => __( 'Top', 'formidable' ), |
||
| 109 | 'left' => __( 'Left', 'formidable' ), |
||
| 110 | 'right' => __( 'Right', 'formidable' ), |
||
| 111 | 'inline' => __( 'Inline (left without a set width)', 'formidable' ), |
||
| 112 | 'none' => __( 'None', 'formidable' ), |
||
| 113 | 'hidden' => __( 'Hidden (but leave the space)', 'formidable' ), |
||
| 114 | 'inside' => __( 'Placeholder inside the field', 'formidable' ), |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | public static function minus_icons() { |
||
| 119 | return array( |
||
| 120 | 0 => array( |
||
| 121 | '-' => '62e', |
||
| 122 | '+' => '62f', |
||
| 123 | ), |
||
| 124 | 1 => array( |
||
| 125 | '-' => '600', |
||
| 126 | '+' => '602', |
||
| 127 | ), |
||
| 128 | 2 => array( |
||
| 129 | '-' => '604', |
||
| 130 | '+' => '603', |
||
| 131 | ), |
||
| 132 | 3 => array( |
||
| 133 | '-' => '633', |
||
| 134 | '+' => '632', |
||
| 135 | ), |
||
| 136 | 4 => array( |
||
| 137 | '-' => '613', |
||
| 138 | '+' => '60f', |
||
| 139 | ), |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function arrow_icons() { |
||
| 144 | $minus_icons = self::minus_icons(); |
||
| 145 | |||
| 146 | return array( |
||
| 147 | 6 => array( |
||
| 148 | '-' => '62d', |
||
| 149 | '+' => '62a', |
||
| 150 | ), |
||
| 151 | 0 => array( |
||
| 152 | '-' => '60d', |
||
| 153 | '+' => '609', |
||
| 154 | ), |
||
| 155 | 1 => array( |
||
| 156 | '-' => '60e', |
||
| 157 | '+' => '60c', |
||
| 158 | ), |
||
| 159 | 2 => array( |
||
| 160 | '-' => '630', |
||
| 161 | '+' => '631', |
||
| 162 | ), |
||
| 163 | 3 => array( |
||
| 164 | '-' => '62b', |
||
| 165 | '+' => '628', |
||
| 166 | ), |
||
| 167 | 4 => array( |
||
| 168 | '-' => '62c', |
||
| 169 | '+' => '629', |
||
| 170 | ), |
||
| 171 | 5 => array( |
||
| 172 | '-' => '635', |
||
| 173 | '+' => '634', |
||
| 174 | ), |
||
| 175 | 'p0' => $minus_icons[0], |
||
| 176 | 'p1' => $minus_icons[1], |
||
| 177 | 'p2' => $minus_icons[2], |
||
| 178 | 'p3' => $minus_icons[3], |
||
| 179 | 'p4' => $minus_icons[4], |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @since 2.0 |
||
| 185 | * @return The class for this icon |
||
| 186 | */ |
||
| 187 | public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) { |
||
| 188 | if ( 'arrow' == $type && is_numeric( $key ) ) { |
||
| 189 | //frm_arrowup6_icon |
||
| 190 | $arrow = array( |
||
| 191 | '-' => 'down', |
||
| 192 | '+' => 'up', |
||
| 193 | ); |
||
| 194 | $class = 'frm_arrow' . $arrow[ $icon ]; |
||
| 195 | } else { |
||
| 196 | //frm_minus1_icon |
||
| 197 | $key = str_replace( 'p', '', $key ); |
||
| 198 | $plus = array( |
||
| 199 | '-' => 'minus', |
||
| 200 | '+' => 'plus', |
||
| 201 | ); |
||
| 202 | $class = 'frm_' . $plus[ $icon ]; |
||
| 203 | } |
||
| 204 | |||
| 205 | if ( $key ) { |
||
| 206 | $class .= $key; |
||
| 207 | } |
||
| 208 | $class .= '_icon'; |
||
| 209 | |||
| 210 | return $class; |
||
| 211 | } |
||
| 212 | |||
| 213 | public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) { |
||
| 214 | $function_name = $type . '_icons'; |
||
| 215 | $icons = self::$function_name(); |
||
| 216 | unset( $function_name ); |
||
| 217 | |||
| 218 | $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon'; |
||
| 219 | ?> |
||
| 220 | <div class="btn-group" id="frm_<?php echo esc_attr( $name ); ?>_select"> |
||
| 221 | <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button"> |
||
| 222 | <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ); ?> |
||
| 223 | <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ); ?> |
||
| 224 | <b class="caret"></b> |
||
| 225 | </button> |
||
| 226 | <ul class="multiselect-container frm-dropdown-menu"> |
||
| 227 | <?php foreach ( $icons as $key => $icon ) { ?> |
||
| 228 | <li <?php echo ( $style->post_content['collapse_icon'] == $key ) ? 'class="active"' : ''; ?>> |
||
| 229 | <a href="javascript:void(0);"> |
||
| 230 | <label> |
||
| 231 | <input type="radio" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $frm_style->get_field_name( $name ) ); ?>" <?php checked( $style->post_content[ $name ], $key ); ?>/> |
||
| 232 | <span> |
||
| 233 | <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '+', $type ) ); ?> |
||
| 234 | <?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '-', $type ) ); ?> |
||
| 235 | </span> |
||
| 236 | </label> |
||
| 237 | </a> |
||
| 238 | </li> |
||
| 239 | <?php } ?> |
||
| 240 | </ul> |
||
| 241 | </div> |
||
| 242 | <?php |
||
| 243 | } |
||
| 244 | |||
| 245 | public static function hex2rgb( $hex ) { |
||
| 246 | $hex = str_replace( '#', '', $hex ); |
||
| 247 | |||
| 248 | list( $r, $g, $b ) = sscanf( $hex, '%02x%02x%02x' ); |
||
| 249 | |||
| 250 | $rgb = array( $r, $g, $b ); |
||
| 251 | |||
| 252 | return implode( ',', $rgb ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @since 4.0 |
||
| 257 | */ |
||
| 258 | public static function hex2rgba( $hex, $a ) { |
||
| 259 | $rgb = self::hex2rgb( $hex ); |
||
| 260 | |||
| 261 | return 'rgba(' . $rgb . ',' . $a . ')'; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param $hex string - The original color in hex format #ffffff |
||
| 266 | * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter |
||
| 267 | * |
||
| 268 | * @since 2.3 |
||
| 269 | */ |
||
| 270 | public static function adjust_brightness( $hex, $steps ) { |
||
| 271 | $steps = max( - 255, min( 255, $steps ) ); |
||
| 272 | |||
| 273 | // Normalize into a six character long hex string |
||
| 274 | $hex = str_replace( '#', '', $hex ); |
||
| 275 | if ( strlen( $hex ) == 3 ) { |
||
| 276 | $hex = str_repeat( substr( $hex, 0, 1 ), 2 ); |
||
| 277 | $hex .= str_repeat( substr( $hex, 1, 1 ), 2 ); |
||
| 278 | $hex .= str_repeat( substr( $hex, 2, 1 ), 2 ); |
||
| 279 | } |
||
| 280 | |||
| 281 | // Split into three parts: R, G and B |
||
| 282 | $color_parts = str_split( $hex, 2 ); |
||
| 283 | $return = '#'; |
||
| 284 | |||
| 285 | foreach ( $color_parts as $color ) { |
||
| 286 | $color = hexdec( $color ); // Convert to decimal |
||
| 287 | $color = max( 0, min( 255, $color + $steps ) ); // Adjust color |
||
| 288 | $return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code |
||
| 289 | } |
||
| 290 | |||
| 291 | return $return; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @since 2.3 |
||
| 296 | */ |
||
| 297 | public static function get_settings_for_output( $style ) { |
||
| 298 | if ( self::previewing_style() ) { |
||
| 299 | |||
| 300 | if ( isset( $_POST['frm_style_setting'] ) ) { |
||
| 301 | // Sanitizing is done later. |
||
| 302 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
||
| 303 | $posted = wp_unslash( $_POST['frm_style_setting'] ); |
||
| 304 | if ( ! is_array( $posted ) ) { |
||
| 305 | $posted = json_decode( $posted, true ); |
||
| 306 | FrmAppHelper::format_form_data( $posted ); |
||
| 307 | $settings = $posted['frm_style_setting']['post_content']; |
||
| 308 | $style_name = sanitize_title( $posted['style_name'] ); |
||
| 309 | } else { |
||
| 310 | $settings = $posted['post_content']; |
||
| 311 | $style_name = FrmAppHelper::get_post_param( 'style_name', '', 'sanitize_title' ) ); |
||
|
|
|||
| 312 | } |
||
| 313 | } else { |
||
| 314 | $settings = $_GET; |
||
| 315 | $style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' ); |
||
| 316 | } |
||
| 317 | |||
| 318 | FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings ); |
||
| 319 | |||
| 320 | $settings['style_class'] = ''; |
||
| 321 | if ( ! empty( $style_name ) ) { |
||
| 322 | $settings['style_class'] = $style_name . '.'; |
||
| 323 | } |
||
| 324 | } else { |
||
| 325 | $settings = $style->post_content; |
||
| 326 | $settings['style_class'] = 'frm_style_' . $style->post_name . '.'; |
||
| 327 | } |
||
| 328 | |||
| 329 | $settings['style_class'] .= 'with_frm_style'; |
||
| 330 | $settings['font'] = stripslashes( $settings['font'] ); |
||
| 331 | $settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] ); |
||
| 332 | |||
| 333 | $checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' ); |
||
| 334 | foreach ( $checkbox_opts as $opt ) { |
||
| 335 | if ( ! isset( $settings[ $opt ] ) ) { |
||
| 336 | $settings[ $opt ] = 0; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | self::prepare_color_output( $settings ); |
||
| 341 | |||
| 342 | return $settings; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @since 2.3 |
||
| 347 | */ |
||
| 348 | public static function prepare_color_output( &$settings, $allow_transparent = true ) { |
||
| 349 | $colors = self::allow_color_override(); |
||
| 350 | foreach ( $colors as $css => $opts ) { |
||
| 351 | if ( $css === 'transparent' && ! $allow_transparent ) { |
||
| 352 | $css = ''; |
||
| 353 | } |
||
| 354 | foreach ( $opts as $opt ) { |
||
| 355 | self::get_color_output( $css, $settings[ $opt ] ); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @since 2.3 |
||
| 362 | */ |
||
| 363 | private static function allow_color_override() { |
||
| 364 | $frm_style = new FrmStyle(); |
||
| 365 | $colors = $frm_style->get_color_settings(); |
||
| 366 | |||
| 367 | $transparent = array( |
||
| 368 | 'fieldset_color', |
||
| 369 | 'fieldset_bg_color', |
||
| 370 | 'bg_color', |
||
| 371 | 'section_bg_color', |
||
| 372 | 'error_bg', |
||
| 373 | 'success_bg_color', |
||
| 374 | 'progress_bg_color', |
||
| 375 | 'progress_active_bg_color', |
||
| 376 | ); |
||
| 377 | |||
| 378 | return array( |
||
| 379 | 'transparent' => $transparent, |
||
| 380 | '' => array_diff( $colors, $transparent ), |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @since 2.3 |
||
| 386 | */ |
||
| 387 | private static function get_color_output( $default, &$color ) { |
||
| 388 | $color = trim( $color ); |
||
| 389 | if ( empty( $color ) ) { |
||
| 390 | $color = $default; |
||
| 391 | } elseif ( strpos( $color, '#' ) === false ) { |
||
| 392 | $color = '#' . $color; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * If left/right label is over a certain size, |
||
| 398 | * adjust the field description margin at a different screen size |
||
| 399 | * |
||
| 400 | * @since 2.3 |
||
| 401 | */ |
||
| 402 | private static function description_margin_for_screensize( $width ) { |
||
| 403 | $temp_label_width = str_replace( 'px', '', $width ); |
||
| 404 | $change_margin = false; |
||
| 405 | if ( $temp_label_width >= 230 ) { |
||
| 406 | $change_margin = '800px'; |
||
| 407 | } elseif ( $width >= 215 ) { |
||
| 408 | $change_margin = '700px'; |
||
| 409 | } elseif ( $width >= 180 ) { |
||
| 410 | $change_margin = '650px'; |
||
| 411 | } |
||
| 412 | |||
| 413 | return $change_margin; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @since 2.3 |
||
| 418 | */ |
||
| 419 | public static function previewing_style() { |
||
| 420 | $ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] ); |
||
| 421 | |||
| 422 | return $ajax_change || isset( $_GET['flat'] ); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @deprecated 3.01 |
||
| 427 | * @codeCoverageIgnore |
||
| 428 | */ |
||
| 429 | public static function get_sigle_label_postitions() { |
||
| 430 | return FrmDeprecated::get_sigle_label_postitions(); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @deprecated 3.02.03 |
||
| 435 | * @codeCoverageIgnore |
||
| 436 | */ |
||
| 437 | public static function jquery_themes() { |
||
| 438 | return FrmDeprecated::jquery_themes(); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @deprecated 3.02.03 |
||
| 443 | * @codeCoverageIgnore |
||
| 444 | */ |
||
| 445 | public static function jquery_css_url( $theme_css ) { |
||
| 446 | return FrmDeprecated::jquery_css_url( $theme_css ); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @deprecated 3.02.03 |
||
| 451 | * @codeCoverageIgnore |
||
| 452 | */ |
||
| 453 | public static function enqueue_jquery_css() { |
||
| 454 | FrmDeprecated::enqueue_jquery_css(); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @deprecated 3.02.03 |
||
| 459 | * @codeCoverageIgnore |
||
| 460 | */ |
||
| 461 | public static function get_form_for_page() { |
||
| 462 | return FrmDeprecated::get_form_for_page(); |
||
| 463 | } |
||
| 464 | } |
||
| 465 |