Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmStylesController 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 FrmStylesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmStylesController { |
||
| 4 | public static $post_type = 'frm_styles'; |
||
| 5 | public static $screen = 'formidable_page_formidable-styles'; |
||
| 6 | |||
| 7 | public static function load_pro_hooks() { |
||
| 12 | |||
| 13 | public static function register_post_types() { |
||
| 45 | |||
| 46 | public static function menu() { |
||
| 47 | add_submenu_page( 'formidable', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Styles', 'formidable' ), 'frm_change_settings', 'formidable-styles', 'FrmStylesController::route' ); |
||
| 48 | add_submenu_page( 'themes.php', 'Formidable | ' . __( 'Styles', 'formidable' ), __( 'Forms', 'formidable' ), 'frm_change_settings', 'formidable-styles2', 'FrmStylesController::route' ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public static function admin_init() { |
||
| 52 | if ( ! FrmAppHelper::is_admin_page( 'formidable-styles' ) && ! FrmAppHelper::is_admin_page( 'formidable-styles2' ) ) { |
||
| 53 | return; |
||
| 54 | } |
||
| 55 | |||
| 56 | self::load_pro_hooks(); |
||
| 57 | |||
| 58 | $style_tab = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' ); |
||
| 59 | if ( $style_tab == 'manage' || $style_tab == 'custom_css' ) { |
||
| 60 | // we only need to load these styles/scripts on the styler page |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $version = FrmAppHelper::plugin_version(); |
||
| 65 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
||
| 66 | wp_enqueue_style( 'wp-color-picker' ); |
||
| 67 | wp_enqueue_style( 'frm-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_css' ), array(), $version ); |
||
| 68 | |||
| 69 | $style = apply_filters( 'frm_style_head', false ); |
||
| 70 | if ( $style ) { |
||
| 71 | wp_enqueue_style( 'frm-single-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_load_css&flat=1' ) . '&' . http_build_query( $style->post_content ), array(), $version ); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $register Either 'enqueue' or 'register'. |
||
| 77 | * @param bool $force True to enqueue/register the style if a form has not been loaded. |
||
| 78 | */ |
||
| 79 | public static function enqueue_css( $register = 'enqueue', $force = false ) { |
||
| 80 | global $frm_vars; |
||
| 81 | |||
| 82 | $register_css = ( $register == 'register' ); |
||
| 83 | $should_load = $force || ( ( $frm_vars['load_css'] || $register_css ) && ! FrmAppHelper::is_admin() ); |
||
| 84 | |||
| 85 | if ( ! $should_load ) { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 90 | if ( $frm_settings->load_style == 'none' ) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | $css = apply_filters( 'get_frm_stylesheet', self::custom_stylesheet() ); |
||
| 95 | |||
| 96 | if ( ! empty( $css ) ) { |
||
| 97 | $css = (array) $css; |
||
| 98 | |||
| 99 | $version = FrmAppHelper::plugin_version(); |
||
| 100 | |||
| 101 | foreach ( $css as $css_key => $file ) { |
||
| 102 | if ( $register_css ) { |
||
| 103 | $this_version = self::get_css_version( $css_key, $version ); |
||
| 104 | wp_register_style( $css_key, $file, array(), $this_version ); |
||
| 105 | } |
||
| 106 | |||
| 107 | $load_on_all = ! FrmAppHelper::is_admin() && 'all' == $frm_settings->load_style; |
||
| 108 | if ( $load_on_all || $register != 'register' ) { |
||
| 109 | wp_enqueue_style( $css_key ); |
||
| 110 | } |
||
| 111 | unset( $css_key, $file ); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ( $frm_settings->load_style == 'all' ) { |
||
| 115 | $frm_vars['css_loaded'] = true; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | unset( $css ); |
||
| 119 | |||
| 120 | add_filter( 'style_loader_tag', 'FrmStylesController::add_tags_to_css', 10, 2 ); |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function custom_stylesheet() { |
||
| 124 | global $frm_vars; |
||
| 125 | $stylesheet_urls = array(); |
||
| 126 | |||
| 127 | if ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) { |
||
| 128 | //include css in head |
||
| 129 | self::get_url_to_custom_style( $stylesheet_urls ); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $stylesheet_urls; |
||
| 133 | } |
||
| 134 | |||
| 135 | private static function get_url_to_custom_style( &$stylesheet_urls ) { |
||
| 136 | $file_name = '/css/' . self::get_file_name(); |
||
| 137 | if ( is_readable( FrmAppHelper::plugin_path() . $file_name ) ) { |
||
| 138 | $url = FrmAppHelper::plugin_url() . $file_name; |
||
| 139 | } else { |
||
| 140 | $url = admin_url( 'admin-ajax.php?action=frmpro_css' ); |
||
| 141 | } |
||
| 142 | $stylesheet_urls['formidable'] = $url; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Use a different stylesheet per site in a multisite install |
||
| 147 | * |
||
| 148 | * @since 3.0.03 |
||
| 149 | */ |
||
| 150 | public static function get_file_name() { |
||
| 151 | if ( is_multisite() ) { |
||
| 152 | $blog_id = get_current_blog_id(); |
||
| 153 | $name = 'formidableforms' . absint( $blog_id ) . '.css'; |
||
| 154 | } else { |
||
| 155 | $name = 'formidableforms.css'; |
||
| 156 | } |
||
| 157 | |||
| 158 | return $name; |
||
| 159 | } |
||
| 160 | |||
| 161 | private static function get_css_version( $css_key, $version ) { |
||
| 162 | if ( 'formidable' == $css_key ) { |
||
| 163 | $this_version = get_option( 'frm_last_style_update' ); |
||
| 164 | if ( ! $this_version ) { |
||
| 165 | $this_version = $version; |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | $this_version = $version; |
||
| 169 | } |
||
| 170 | |||
| 171 | return $this_version; |
||
| 172 | } |
||
| 173 | |||
| 174 | public static function add_tags_to_css( $tag, $handle ) { |
||
| 175 | if ( ( 'formidable' == $handle || 'jquery-theme' == $handle ) && strpos( $tag, ' property=' ) === false ) { |
||
| 176 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 177 | if ( $frm_settings->use_html ) { |
||
| 178 | $tag = str_replace( ' type="', ' property="stylesheet" type="', $tag ); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | return $tag; |
||
| 183 | } |
||
| 184 | |||
| 185 | public static function new_style( $return = '' ) { |
||
| 186 | self::load_styler( 'default' ); |
||
| 187 | } |
||
| 188 | |||
| 189 | public static function duplicate() { |
||
| 190 | self::load_styler( 'default' ); |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function edit( $style_id = false, $message = '' ) { |
||
| 194 | if ( ! $style_id ) { |
||
| 195 | $style_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 196 | if ( empty( $style_id ) ) { |
||
| 197 | $style_id = 'default'; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | if ( 'default' == $style_id ) { |
||
| 202 | $style = 'default'; |
||
| 203 | } else { |
||
| 204 | $frm_style = new FrmStyle( $style_id ); |
||
| 205 | $style = $frm_style->get_one(); |
||
| 206 | $style = $style->ID; |
||
| 207 | } |
||
| 208 | |||
| 209 | self::load_styler( $style, $message ); |
||
| 210 | } |
||
| 211 | |||
| 212 | public static function save() { |
||
| 213 | $frm_style = new FrmStyle(); |
||
| 214 | $message = ''; |
||
| 215 | $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' ); |
||
| 216 | $style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' ); |
||
| 217 | |||
| 218 | if ( $post_id !== false && wp_verify_nonce( $style_nonce, 'frm_style_nonce' ) ) { |
||
| 219 | $id = $frm_style->update( $post_id ); |
||
|
|
|||
| 220 | if ( empty( $post_id ) && ! empty( $id ) ) { |
||
| 221 | // set the post id to the new style so it will be loaded for editing |
||
| 222 | $post_id = reset( $id ); |
||
| 223 | } |
||
| 224 | // include the CSS that includes this style |
||
| 225 | echo '<link href="' . esc_url( admin_url( 'admin-ajax.php?action=frmpro_css' ) ) . '" type="text/css" rel="Stylesheet" class="frm-custom-theme" />'; |
||
| 226 | $message = __( 'Your styling settings have been saved.', 'formidable' ); |
||
| 227 | } |
||
| 228 | |||
| 229 | return self::edit( $post_id, $message ); |
||
| 230 | } |
||
| 231 | |||
| 232 | public static function load_styler( $style, $message = '' ) { |
||
| 233 | global $frm_settings; |
||
| 234 | |||
| 235 | $frm_style = new FrmStyle(); |
||
| 236 | $styles = $frm_style->get_all(); |
||
| 237 | |||
| 238 | if ( is_numeric( $style ) ) { |
||
| 239 | $style = $styles[ $style ]; |
||
| 240 | } elseif ( 'default' == $style ) { |
||
| 241 | $style = $frm_style->get_default_style( $styles ); |
||
| 242 | } |
||
| 243 | |||
| 244 | self::add_meta_boxes(); |
||
| 245 | |||
| 246 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/show.php' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $message |
||
| 251 | * @param array|object $forms |
||
| 252 | */ |
||
| 253 | private static function manage( $message = '', $forms = array() ) { |
||
| 254 | $frm_style = new FrmStyle(); |
||
| 255 | $styles = $frm_style->get_all(); |
||
| 256 | $default_style = $frm_style->get_default_style( $styles ); |
||
| 257 | |||
| 258 | if ( empty( $forms ) ) { |
||
| 259 | $forms = FrmForm::get_published_forms(); |
||
| 260 | } |
||
| 261 | |||
| 262 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/manage.php' ); |
||
| 263 | } |
||
| 264 | |||
| 265 | private static function manage_styles() { |
||
| 266 | $style_nonce = FrmAppHelper::get_post_param( 'frm_manage_style', '', 'sanitize_text_field' ); |
||
| 267 | if ( ! $_POST || ! isset( $_POST['style'] ) || ! wp_verify_nonce( $style_nonce, 'frm_manage_style_nonce' ) ) { |
||
| 268 | return self::manage(); |
||
| 269 | } |
||
| 270 | |||
| 271 | global $wpdb; |
||
| 272 | |||
| 273 | $forms = FrmForm::get_published_forms(); |
||
| 274 | foreach ( $forms as $form ) { |
||
| 275 | $new_style = ( isset( $_POST['style'] ) && isset( $_POST['style'][ $form->id ] ) ) ? sanitize_text_field( wp_unslash( $_POST['style'][ $form->id ] ) ) : ''; |
||
| 276 | $previous_style = ( isset( $_POST['prev_style'] ) && isset( $_POST['prev_style'][ $form->id ] ) ) ? sanitize_text_field( wp_unslash( $_POST['prev_style'][ $form->id ] ) ) : ''; |
||
| 277 | if ( $new_style == $previous_style ) { |
||
| 278 | continue; |
||
| 279 | } |
||
| 280 | |||
| 281 | $form->options['custom_style'] = $new_style; |
||
| 282 | |||
| 283 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) ); |
||
| 284 | unset( $form ); |
||
| 285 | } |
||
| 286 | |||
| 287 | $message = __( 'Your form styles have been saved.', 'formidable' ); |
||
| 288 | |||
| 289 | return self::manage( $message, $forms ); |
||
| 290 | } |
||
| 291 | |||
| 292 | public static function custom_css( $message = '', $style = null ) { |
||
| 293 | if ( function_exists( 'wp_enqueue_code_editor' ) ) { |
||
| 294 | $id = 'frm_codemirror_box'; |
||
| 295 | $settings = wp_enqueue_code_editor( |
||
| 296 | array( |
||
| 297 | 'type' => 'text/css', |
||
| 298 | 'codemirror' => array( |
||
| 299 | 'indentUnit' => 2, |
||
| 300 | 'tabSize' => 2, |
||
| 301 | ), |
||
| 302 | ) |
||
| 303 | ); |
||
| 304 | } else { |
||
| 305 | _deprecated_function( 'Codemirror v4.7', 'WordPress 4.9', 'Update WordPress' ); |
||
| 306 | $id = 'frm_custom_css_box'; |
||
| 307 | $settings = array(); |
||
| 308 | $codemirror = '4.7'; |
||
| 309 | wp_enqueue_style( 'codemirror', FrmAppHelper::plugin_url() . '/css/codemirror.css', array(), $codemirror ); |
||
| 310 | wp_enqueue_script( 'codemirror', FrmAppHelper::plugin_url() . '/js/codemirror/codemirror.js', array(), $codemirror ); |
||
| 311 | wp_enqueue_script( 'codemirror-css', FrmAppHelper::plugin_url() . '/js/codemirror/css.js', array( 'codemirror' ), $codemirror ); |
||
| 312 | } |
||
| 313 | |||
| 314 | if ( ! isset( $style ) ) { |
||
| 315 | $frm_style = new FrmStyle(); |
||
| 316 | $style = $frm_style->get_default_style(); |
||
| 317 | } |
||
| 318 | |||
| 319 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php' ); |
||
| 320 | } |
||
| 321 | |||
| 322 | public static function save_css() { |
||
| 323 | $frm_style = new FrmStyle(); |
||
| 324 | |||
| 325 | $message = ''; |
||
| 326 | $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' ); |
||
| 327 | $nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' ); |
||
| 328 | if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) { |
||
| 329 | $frm_style->update( $post_id ); |
||
| 330 | $message = __( 'Your styling settings have been saved.', 'formidable' ); |
||
| 331 | } |
||
| 332 | |||
| 333 | return self::custom_css( $message ); |
||
| 334 | } |
||
| 335 | |||
| 336 | public static function route() { |
||
| 337 | $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' ); |
||
| 338 | FrmAppHelper::include_svg(); |
||
| 339 | |||
| 340 | switch ( $action ) { |
||
| 341 | case 'edit': |
||
| 342 | case 'save': |
||
| 343 | case 'manage': |
||
| 344 | case 'manage_styles': |
||
| 345 | case 'custom_css': |
||
| 346 | case 'save_css': |
||
| 347 | return self::$action(); |
||
| 348 | default: |
||
| 349 | do_action( 'frm_style_action_route', $action ); |
||
| 350 | if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) { |
||
| 351 | return; |
||
| 352 | } |
||
| 353 | |||
| 354 | if ( 'new_style' == $action || 'duplicate' == $action ) { |
||
| 355 | return self::$action(); |
||
| 356 | } |
||
| 357 | |||
| 358 | return self::edit(); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | View Code Duplication | public static function reset_styling() { |
|
| 363 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
||
| 364 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 365 | |||
| 366 | $frm_style = new FrmStyle(); |
||
| 367 | $defaults = $frm_style->get_defaults(); |
||
| 368 | |||
| 369 | echo json_encode( $defaults ); |
||
| 370 | wp_die(); |
||
| 371 | } |
||
| 372 | |||
| 373 | public static function change_styling() { |
||
| 374 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 375 | |||
| 376 | $frm_style = new FrmStyle(); |
||
| 377 | $defaults = $frm_style->get_defaults(); |
||
| 378 | $style = ''; |
||
| 379 | |||
| 380 | echo '<style type="text/css">'; |
||
| 381 | include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' ); |
||
| 382 | echo '</style>'; |
||
| 383 | wp_die(); |
||
| 384 | } |
||
| 385 | |||
| 386 | private static function add_meta_boxes() { |
||
| 387 | |||
| 388 | // setup meta boxes |
||
| 389 | $meta_boxes = array( |
||
| 390 | 'general' => __( 'General', 'formidable' ), |
||
| 391 | 'form-title' => __( 'Form Title', 'formidable' ), |
||
| 392 | 'form-description' => __( 'Form Description', 'formidable' ), |
||
| 393 | 'field-labels' => __( 'Field Labels', 'formidable' ), |
||
| 394 | 'field-description' => __( 'Field Description', 'formidable' ), |
||
| 395 | 'field-colors' => __( 'Field Colors', 'formidable' ), |
||
| 396 | 'field-sizes' => __( 'Field Settings', 'formidable' ), |
||
| 397 | 'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ), |
||
| 398 | 'buttons' => __( 'Buttons', 'formidable' ), |
||
| 399 | 'form-messages' => __( 'Form Messages', 'formidable' ), |
||
| 400 | ); |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Add custom boxes to the styling settings |
||
| 404 | * |
||
| 405 | * @since 2.3 |
||
| 406 | */ |
||
| 407 | $meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes ); |
||
| 408 | |||
| 409 | foreach ( $meta_boxes as $nicename => $name ) { |
||
| 410 | add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename ); |
||
| 411 | unset( $nicename, $name ); |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | public static function include_style_section( $atts, $sec ) { |
||
| 416 | extract( $atts ); |
||
| 417 | $style = $atts['style']; |
||
| 418 | FrmStylesHelper::prepare_color_output( $style->post_content, false ); |
||
| 419 | |||
| 420 | $current_tab = FrmAppHelper::simple_get( 'page-tab', 'sanitize_title', 'default' ); |
||
| 421 | $file_name = FrmAppHelper::plugin_path() . '/classes/views/styles/_' . $sec['args'] . '.php'; |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set the location of custom styling settings right before |
||
| 425 | * loading onto the page. If your style box was named "progress", |
||
| 426 | * this hook name will be frm_style_settings_progress. |
||
| 427 | * |
||
| 428 | * @since 2.3 |
||
| 429 | */ |
||
| 430 | $file_name = apply_filters( 'frm_style_settings_' . $sec['args'], $file_name ); |
||
| 431 | |||
| 432 | echo '<div class="frm_grid_container">'; |
||
| 433 | include( $file_name ); |
||
| 434 | echo '</div>'; |
||
| 435 | } |
||
| 436 | |||
| 437 | View Code Duplication | public static function load_css() { |
|
| 447 | |||
| 448 | public static function load_saved_css() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Check if the Formidable styling should be loaded, |
||
| 457 | * then enqueue it for the footer |
||
| 458 | * |
||
| 459 | * @since 2.0 |
||
| 460 | */ |
||
| 461 | public static function enqueue_style() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the stylesheets for the form settings page |
||
| 478 | */ |
||
| 479 | public static function get_style_opts() { |
||
| 480 | $frm_style = new FrmStyle(); |
||
| 481 | $styles = $frm_style->get_all(); |
||
| 482 | |||
| 483 | return $styles; |
||
| 484 | } |
||
| 485 | |||
| 486 | public static function get_form_style( $form = 'default' ) { |
||
| 487 | $style = FrmFormsHelper::get_form_style( $form ); |
||
| 488 | |||
| 489 | if ( empty( $style ) || 1 == $style ) { |
||
| 490 | $style = 'default'; |
||
| 491 | } |
||
| 492 | |||
| 493 | $frm_style = new FrmStyle( $style ); |
||
| 494 | |||
| 495 | return $frm_style->get_one(); |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $class |
||
| 500 | * @param string $style |
||
| 501 | */ |
||
| 502 | public static function get_form_style_class( $class, $style ) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param object $style |
||
| 520 | * @param string $class |
||
| 521 | * |
||
| 522 | * @since 3.0 |
||
| 523 | */ |
||
| 524 | private static function maybe_add_rtl_class( $style, &$class ) { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param string $val |
||
| 533 | */ |
||
| 534 | public static function get_style_val( $val, $form = 'default' ) { |
||
| 540 | |||
| 541 | public static function show_entry_styles( $default_styles ) { |
||
| 562 | |||
| 563 | public static function &important_style( $important, $field ) { |
||
| 568 | |||
| 569 | public static function do_accordion_sections( $screen, $context, $object ) { |
||
| 572 | } |
||
| 573 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.