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() { |
||
| 42 | |||
| 43 | public static function menu() { |
||
| 46 | |||
| 47 | public static function admin_init() { |
||
| 48 | if ( ! FrmAppHelper::is_admin_page('formidable-styles') ) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | self::load_pro_hooks(); |
||
| 53 | |||
| 54 | $style_tab = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' ); |
||
| 55 | if ( $style_tab == 'manage' || $style_tab == 'custom_css' ) { |
||
| 56 | // we only need to load these styles/scripts on the styler page |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | wp_enqueue_script('jquery-ui-datepicker'); |
||
| 61 | |||
| 62 | $version = FrmAppHelper::plugin_version(); |
||
| 63 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 64 | wp_enqueue_script( 'jquery-frm-themepicker', FrmAppHelper::plugin_url() . '/js/jquery/jquery-ui-themepicker' . $suffix . '.js', array( 'jquery' ), $version ); |
||
| 65 | |||
| 66 | wp_enqueue_style( 'frm-custom-theme', admin_url('admin-ajax.php?action=frmpro_css' ) ); |
||
| 67 | |||
| 68 | $style = apply_filters('frm_style_head', false); |
||
| 69 | if ( $style ) { |
||
| 70 | wp_enqueue_style( 'frm-single-custom-theme', admin_url( 'admin-ajax.php?action=frmpro_load_css&flat=1' ) . '&' . http_build_query( $style->post_content ) ); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public static function enqueue_css( $register = 'enqueue' ) { |
||
| 75 | global $frm_vars; |
||
| 76 | |||
| 77 | $register_css = ( $register == 'register' ); |
||
| 78 | |||
| 79 | if ( ( $frm_vars['load_css'] || $register_css ) && ! FrmAppHelper::is_admin() ) { |
||
| 80 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 81 | if ( $frm_settings->load_style == 'none' ) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | $css = apply_filters( 'get_frm_stylesheet', self::custom_stylesheet() ); |
||
| 86 | |||
| 87 | if ( ! empty( $css ) ) { |
||
| 88 | $version = FrmAppHelper::plugin_version(); |
||
| 89 | |||
| 90 | foreach ( (array) $css as $css_key => $file ) { |
||
| 91 | if ( $register_css ) { |
||
| 92 | $this_version = self::get_css_version( $css_key, $version ); |
||
| 93 | wp_register_style( $css_key, $file, array(), $this_version ); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( 'all' == $frm_settings->load_style || $register != 'register' ) { |
||
| 97 | wp_enqueue_style( $css_key ); |
||
| 98 | } |
||
| 99 | unset( $css_key, $file ); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ( $frm_settings->load_style == 'all' ) { |
||
| 103 | $frm_vars['css_loaded'] = true; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | unset( $css ); |
||
| 107 | |||
| 108 | add_filter( 'style_loader_tag', 'FrmStylesController::add_tags_to_css', 10, 2 ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | public static function custom_stylesheet() { |
||
| 113 | global $frm_vars; |
||
| 114 | $stylesheet_urls = array(); |
||
| 115 | self::maybe_enqueue_jquery_css(); |
||
| 116 | |||
| 117 | if ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) { |
||
| 118 | //include css in head |
||
| 119 | self::get_url_to_custom_style( $stylesheet_urls ); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $stylesheet_urls; |
||
| 123 | } |
||
| 124 | |||
| 125 | private static function get_url_to_custom_style( &$stylesheet_urls ) { |
||
| 126 | $uploads = FrmStylesHelper::get_upload_base(); |
||
| 127 | $saved_css_path = '/formidable/css/formidablepro.css'; |
||
| 128 | if ( is_readable( $uploads['basedir'] . $saved_css_path ) ) { |
||
| 129 | $url = $uploads['baseurl'] . $saved_css_path; |
||
| 130 | } else { |
||
| 131 | $url = admin_url( 'admin-ajax.php?action=frmpro_css' ); |
||
| 132 | } |
||
| 133 | $stylesheet_urls['formidable'] = $url; |
||
| 134 | } |
||
| 135 | |||
| 136 | private static function get_css_version( $css_key, $version ) { |
||
| 137 | if ( 'formidable' == $css_key ) { |
||
| 138 | $this_version = get_option( 'frm_last_style_update' ); |
||
| 139 | if ( ! $this_version ) { |
||
| 140 | $this_version = $version; |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $this_version = $version; |
||
| 144 | } |
||
| 145 | return $this_version; |
||
| 146 | } |
||
| 147 | |||
| 148 | private static function maybe_enqueue_jquery_css() { |
||
| 149 | global $frm_vars; |
||
| 150 | if ( isset( $frm_vars['datepicker_loaded'] ) && ! empty( $frm_vars['datepicker_loaded'] ) ) { |
||
| 151 | FrmStylesHelper::enqueue_jquery_css(); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | public static function add_tags_to_css( $tag, $handle ) { |
||
| 156 | if ( ( 'formidable' == $handle || 'jquery-theme' == $handle ) && strpos( $tag, ' property=' ) === false ) { |
||
| 157 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 158 | if ( $frm_settings->use_html ) { |
||
| 159 | $tag = str_replace( ' type="', ' property="stylesheet" type="', $tag ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | return $tag; |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function new_style( $return = '' ) { |
||
|
|
|||
| 166 | self::load_styler('default'); |
||
| 167 | } |
||
| 168 | |||
| 169 | public static function duplicate() { |
||
| 170 | self::load_styler('default'); |
||
| 171 | } |
||
| 172 | |||
| 173 | public static function edit( $style_id = false, $message = '' ) { |
||
| 174 | if ( ! $style_id ) { |
||
| 175 | $style_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 176 | if ( empty($style_id) ) { |
||
| 177 | $style_id = 'default'; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | if ( 'default' == $style_id ) { |
||
| 182 | $style = 'default'; |
||
| 183 | } else { |
||
| 184 | $frm_style = new FrmStyle($style_id); |
||
| 185 | $style = $frm_style->get_one(); |
||
| 186 | $style = $style->ID; |
||
| 187 | } |
||
| 188 | |||
| 189 | self::load_styler($style, $message); |
||
| 190 | } |
||
| 191 | |||
| 192 | public static function save() { |
||
| 193 | $frm_style = new FrmStyle(); |
||
| 194 | $message = ''; |
||
| 195 | $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_title' ); |
||
| 196 | $style_nonce = FrmAppHelper::get_post_param( 'frm_style', '', 'sanitize_text_field' ); |
||
| 197 | |||
| 198 | if ( $post_id !== false && wp_verify_nonce( $style_nonce, 'frm_style_nonce' ) ) { |
||
| 199 | $id = $frm_style->update($post_id); |
||
| 200 | if ( empty($post_id) && ! empty($id) ) { |
||
| 201 | // set the post id to the new style so it will be loaded for editing |
||
| 202 | $post_id = reset($id); |
||
| 203 | } |
||
| 204 | // include the CSS that includes this style |
||
| 205 | echo '<link href="' . esc_url( admin_url( 'admin-ajax.php?action=frmpro_css' ) ) . '" type="text/css" rel="Stylesheet" class="frm-custom-theme" />'; |
||
| 206 | $message = __( 'Your styling settings have been saved.', 'formidable' ); |
||
| 207 | } |
||
| 208 | |||
| 209 | return self::edit($post_id, $message); |
||
| 210 | } |
||
| 211 | |||
| 212 | public static function load_styler( $style, $message = '' ) { |
||
| 213 | global $frm_settings; |
||
| 214 | |||
| 215 | $frm_style = new FrmStyle(); |
||
| 216 | $styles = $frm_style->get_all(); |
||
| 217 | |||
| 218 | if ( is_numeric($style) ) { |
||
| 219 | $style = $styles[ $style ]; |
||
| 220 | } else if ( 'default' == $style ) { |
||
| 221 | $style = $frm_style->get_default_style($styles); |
||
| 222 | } |
||
| 223 | |||
| 224 | self::add_meta_boxes(); |
||
| 225 | |||
| 226 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/show.php' ); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $message |
||
| 231 | * @param array|object $forms |
||
| 232 | */ |
||
| 233 | private static function manage( $message = '', $forms = array() ) { |
||
| 234 | $frm_style = new FrmStyle(); |
||
| 235 | $styles = $frm_style->get_all(); |
||
| 236 | $default_style = $frm_style->get_default_style($styles); |
||
| 237 | |||
| 238 | if ( empty($forms) ) { |
||
| 239 | $forms = FrmForm::get_published_forms(); |
||
| 240 | } |
||
| 241 | |||
| 242 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/manage.php' ); |
||
| 243 | } |
||
| 244 | |||
| 245 | private static function manage_styles() { |
||
| 246 | $style_nonce = FrmAppHelper::get_post_param( 'frm_manage_style', '', 'sanitize_text_field' ); |
||
| 247 | if ( ! $_POST || ! isset( $_POST['style'] ) || ! wp_verify_nonce( $style_nonce, 'frm_manage_style_nonce' ) ) { |
||
| 248 | return self::manage(); |
||
| 249 | } |
||
| 250 | |||
| 251 | global $wpdb; |
||
| 252 | |||
| 253 | $forms = FrmForm::get_published_forms(); |
||
| 254 | foreach ( $forms as $form ) { |
||
| 255 | if ( $_POST['style'][ $form->id ] == $_POST['prev_style'][ $form->id ] ) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | |||
| 259 | $form->options['custom_style'] = $_POST['style'][ $form->id ]; |
||
| 260 | |||
| 261 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $form->options ) ), array( 'id' => $form->id ) ); |
||
| 262 | unset($form); |
||
| 263 | } |
||
| 264 | |||
| 265 | $message = __( 'Your form styles have been saved.', 'formidable' ); |
||
| 266 | return self::manage($message, $forms); |
||
| 267 | } |
||
| 268 | |||
| 269 | public static function custom_css( $message = '', $style = null ) { |
||
| 270 | wp_enqueue_style('codemirror', FrmAppHelper::plugin_url() . '/css/codemirror.css'); |
||
| 271 | wp_enqueue_script('codemirror', FrmAppHelper::plugin_url() . '/js/codemirror/codemirror.js', array(), '4.7'); |
||
| 272 | wp_enqueue_script( 'codemirror-css', FrmAppHelper::plugin_url() . '/js/codemirror/css.js', array( 'codemirror' ), '4.7' ); |
||
| 273 | |||
| 274 | if ( ! isset($style) ) { |
||
| 275 | $frm_style = new FrmStyle(); |
||
| 276 | $style = $frm_style->get_default_style(); |
||
| 277 | } |
||
| 278 | |||
| 279 | include( FrmAppHelper::plugin_path() . '/classes/views/styles/custom_css.php' ); |
||
| 280 | } |
||
| 281 | |||
| 282 | public static function save_css() { |
||
| 283 | $frm_style = new FrmStyle(); |
||
| 284 | |||
| 285 | $message = ''; |
||
| 286 | $post_id = FrmAppHelper::get_post_param( 'ID', false, 'sanitize_text_field' ); |
||
| 287 | $nonce = FrmAppHelper::get_post_param( 'frm_custom_css', '', 'sanitize_text_field' ); |
||
| 288 | if ( wp_verify_nonce( $nonce, 'frm_custom_css_nonce' ) ) { |
||
| 289 | $frm_style->update($post_id); |
||
| 290 | $message = __( 'Your styling settings have been saved.', 'formidable' ); |
||
| 291 | } |
||
| 292 | |||
| 293 | return self::custom_css($message); |
||
| 294 | } |
||
| 295 | |||
| 296 | public static function route() { |
||
| 297 | $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' ); |
||
| 298 | |||
| 299 | switch ( $action ) { |
||
| 300 | case 'edit': |
||
| 301 | case 'save': |
||
| 302 | case 'manage': |
||
| 303 | case 'manage_styles': |
||
| 304 | case 'custom_css': |
||
| 305 | case 'save_css': |
||
| 306 | return self::$action(); |
||
| 307 | default: |
||
| 308 | do_action( 'frm_style_action_route', $action ); |
||
| 309 | if ( apply_filters( 'frm_style_stop_action_route', false, $action ) ) { |
||
| 310 | return; |
||
| 311 | } |
||
| 312 | |||
| 313 | if ( 'new_style' == $action || 'duplicate' == $action ) { |
||
| 314 | return self::$action(); |
||
| 315 | } |
||
| 316 | |||
| 317 | return self::edit(); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | View Code Duplication | public static function reset_styling() { |
|
| 322 | FrmAppHelper::permission_check('frm_change_settings'); |
||
| 323 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 324 | |||
| 325 | $frm_style = new FrmStyle(); |
||
| 326 | $defaults = $frm_style->get_defaults(); |
||
| 327 | |||
| 328 | echo json_encode( $defaults ); |
||
| 329 | wp_die(); |
||
| 330 | } |
||
| 331 | |||
| 332 | public static function change_styling() { |
||
| 333 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 334 | |||
| 335 | $frm_style = new FrmStyle(); |
||
| 336 | $defaults = $frm_style->get_defaults(); |
||
| 337 | $style = ''; |
||
| 338 | |||
| 339 | // remove the # from the colors |
||
| 340 | foreach ( $_GET['frm_style_setting']['post_content'] as $k => $v ) { |
||
| 341 | if ( ! is_array($v) && strpos($v, '#') === 0 ) { |
||
| 342 | $_GET['frm_style_setting']['post_content'][ $k ] = str_replace( '#', '', $v ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | echo '<style type="text/css">'; |
||
| 347 | include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' ); |
||
| 348 | echo '</style>'; |
||
| 349 | wp_die(); |
||
| 350 | } |
||
| 351 | |||
| 352 | private static function add_meta_boxes() { |
||
| 353 | |||
| 354 | // setup meta boxes |
||
| 355 | $meta_boxes = array( |
||
| 356 | 'general' => __( 'General', 'formidable' ), |
||
| 357 | 'form-title' => __( 'Form Title', 'formidable' ), |
||
| 358 | 'form-description' => __( 'Form Description', 'formidable' ), |
||
| 359 | 'field-labels' => __( 'Field Labels', 'formidable' ), |
||
| 360 | 'field-description' => __( 'Field Description', 'formidable' ), |
||
| 361 | 'field-colors' => __( 'Field Colors', 'formidable' ), |
||
| 362 | 'field-sizes' => __( 'Field Settings', 'formidable' ), |
||
| 363 | 'check-box-radio-fields' => __( 'Check Box & Radio Fields', 'formidable' ), |
||
| 364 | 'buttons' => __( 'Buttons', 'formidable' ), |
||
| 365 | 'form-messages' => __( 'Form Messages', 'formidable' ), |
||
| 366 | ); |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Add custom boxes to the styling settings |
||
| 370 | * @since 2.3 |
||
| 371 | */ |
||
| 372 | $meta_boxes = apply_filters( 'frm_style_boxes', $meta_boxes ); |
||
| 373 | |||
| 374 | foreach ( $meta_boxes as $nicename => $name ) { |
||
| 375 | add_meta_box( $nicename . '-style', $name, 'FrmStylesController::include_style_section', self::$screen, 'side', 'default', $nicename ); |
||
| 376 | unset($nicename, $name); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | public static function include_style_section( $atts, $sec ) { |
||
| 396 | |||
| 397 | View Code Duplication | public static function load_css() { |
|
| 398 | header('Content-type: text/css'); |
||
| 399 | |||
| 400 | $frm_style = new FrmStyle(); |
||
| 401 | $defaults = $frm_style->get_defaults(); |
||
| 402 | $style = ''; |
||
| 403 | |||
| 404 | include( FrmAppHelper::plugin_path() . '/css/_single_theme.css.php' ); |
||
| 405 | wp_die(); |
||
| 406 | } |
||
| 407 | |||
| 408 | public static function load_saved_css() { |
||
| 409 | $css = get_transient( 'frmpro_css' ); |
||
| 410 | |||
| 411 | include( FrmAppHelper::plugin_path() . '/css/custom_theme.css.php' ); |
||
| 412 | wp_die(); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Check if the Formidable styling should be loaded, |
||
| 417 | * then enqueue it for the footer |
||
| 418 | * @since 2.0 |
||
| 419 | */ |
||
| 420 | public static function enqueue_style() { |
||
| 434 | |||
| 435 | // Get the stylesheets for the form settings page |
||
| 436 | public static function get_style_opts() { |
||
| 442 | |||
| 443 | public static function get_form_style( $form = 'default' ) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $class |
||
| 456 | * @param string $style |
||
| 457 | */ |
||
| 458 | public static function get_form_style_class( $class, $style ) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param string $val |
||
| 475 | */ |
||
| 476 | public static function get_style_val( $val, $form = 'default' ) { |
||
| 482 | |||
| 483 | public static function show_entry_styles( $default_styles ) { |
||
| 504 | |||
| 505 | public static function &important_style( $important, $field ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Fallback for WP < 3.6 |
||
| 512 | */ |
||
| 513 | public static function do_accordion_sections( $screen, $context, $object ) { |
||
| 570 | } |
||
| 571 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.