Complex classes like FrmAppController 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 FrmAppController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmAppController { |
||
| 4 | |||
| 5 | public static function menu() { |
||
| 14 | |||
| 15 | private static function get_menu_position() { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @since 3.05 |
||
| 21 | */ |
||
| 22 | private static function menu_icon() { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @since 3.0 |
||
| 36 | */ |
||
| 37 | public static function add_admin_class( $classes ) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @since 4.0 |
||
| 65 | */ |
||
| 66 | private static function get_os() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @since 3.0 |
||
| 81 | */ |
||
| 82 | private static function is_white_page() { |
||
| 103 | |||
| 104 | public static function load_wp_admin_style() { |
||
| 107 | |||
| 108 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 125 | |||
| 126 | private static function get_current_page() { |
||
| 137 | |||
| 138 | private static function get_form_nav_items( $form ) { |
||
| 200 | |||
| 201 | // Adds a settings link to the plugins page |
||
| 202 | public static function settings_link( $links ) { |
||
| 208 | |||
| 209 | public static function pro_get_started_headline() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Add admin notices as needed for reviews |
||
| 216 | * |
||
| 217 | * @since 3.04.03 |
||
| 218 | */ |
||
| 219 | private static function review_request() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Save the request to hide the review |
||
| 226 | * |
||
| 227 | * @since 3.04.03 |
||
| 228 | */ |
||
| 229 | public static function dismiss_review() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @since 3.04.02 |
||
| 239 | */ |
||
| 240 | public static function include_upgrade_overlay() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @since 3.06.03 |
||
| 249 | */ |
||
| 250 | public static function upgrade_overlay_html() { |
||
| 262 | |||
| 263 | public static function include_info_overlay() { |
||
| 264 | wp_enqueue_script( 'jquery-ui-dialog' ); |
||
| 265 | wp_enqueue_style( 'jquery-ui-dialog' ); |
||
| 266 | |||
| 267 | add_action( 'admin_footer', 'FrmAppController::info_overlay_html' ); |
||
| 268 | } |
||
| 269 | |||
| 270 | public static function info_overlay_html() { |
||
| 271 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/info-overlay.php' ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @since 3.04.02 |
||
| 276 | */ |
||
| 277 | public static function remove_upsells() { |
||
| 278 | remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' ); |
||
| 279 | remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' ); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 284 | * Use a javascript fallback instead. |
||
| 285 | * |
||
| 286 | * @since 2.0.3 |
||
| 287 | */ |
||
| 288 | public static function install_js_fallback() { |
||
| 289 | FrmAppHelper::load_admin_wide_js(); |
||
| 290 | echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>'; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Check if the database is outdated |
||
| 295 | * |
||
| 296 | * @since 2.0.1 |
||
| 297 | * @return boolean |
||
| 298 | */ |
||
| 299 | public static function needs_update() { |
||
| 300 | $needs_upgrade = self::compare_for_update( |
||
| 301 | array( |
||
| 302 | 'option' => 'frm_db_version', |
||
| 303 | 'new_db_version' => FrmAppHelper::$db_version, |
||
| 304 | 'new_plugin_version' => FrmAppHelper::plugin_version(), |
||
| 305 | ) |
||
| 306 | ); |
||
| 307 | |||
| 308 | if ( ! $needs_upgrade ) { |
||
| 309 | $needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade ); |
||
| 310 | } |
||
| 311 | |||
| 312 | return $needs_upgrade; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Check both version number and DB number for changes |
||
| 317 | * |
||
| 318 | * @since 3.0.04 |
||
| 319 | */ |
||
| 320 | public static function compare_for_update( $atts ) { |
||
| 321 | $db_version = get_option( $atts['option'] ); |
||
| 322 | |||
| 323 | if ( strpos( $db_version, '-' ) === false ) { |
||
| 324 | $needs_upgrade = true; |
||
| 325 | } else { |
||
| 326 | $last_upgrade = explode( '-', $db_version ); |
||
| 327 | $needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version']; |
||
| 328 | $new_version = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' ); |
||
| 329 | $needs_upgrade = $needs_db_upgrade || $new_version; |
||
| 330 | } |
||
| 331 | |||
| 332 | return $needs_upgrade; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check for database update and trigger js loading |
||
| 337 | * |
||
| 338 | * @since 2.0.1 |
||
| 339 | */ |
||
| 340 | public static function admin_init() { |
||
| 341 | new FrmPersonalData(); // register personal data hooks |
||
| 342 | |||
| 343 | if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) { |
||
| 344 | self::network_upgrade_site(); |
||
| 345 | } |
||
| 346 | |||
| 347 | if ( ! FrmAppHelper::doing_ajax() ) { |
||
| 348 | // don't continue during ajax calls |
||
| 349 | self::admin_js(); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | public static function admin_js() { |
||
| 354 | $version = FrmAppHelper::plugin_version(); |
||
| 355 | FrmAppHelper::load_admin_wide_js( false ); |
||
| 356 | |||
| 357 | $dependecies = array( |
||
| 358 | 'formidable_admin_global', |
||
| 359 | 'jquery', |
||
| 360 | 'jquery-ui-core', |
||
| 361 | 'jquery-ui-draggable', |
||
| 362 | 'jquery-ui-sortable', |
||
| 363 | 'bootstrap_tooltip', |
||
| 364 | 'bootstrap-multiselect', |
||
| 365 | ); |
||
| 366 | |||
| 367 | if ( FrmAppHelper::is_admin_page( 'formidable-styles' ) ) { |
||
| 368 | $dependecies[] = 'wp-color-picker'; |
||
| 369 | } |
||
| 370 | |||
| 371 | wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', $dependecies, $version, true ); |
||
| 372 | wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version ); |
||
| 373 | wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' ); |
||
| 374 | wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version ); |
||
| 375 | |||
| 376 | // load multselect js |
||
| 377 | $depends_on = array( 'jquery', 'bootstrap_tooltip' ); |
||
| 378 | wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', $depends_on, '0.9.8', true ); |
||
| 379 | |||
| 380 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 381 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' ); |
||
| 382 | |||
| 383 | global $pagenow; |
||
| 384 | if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) { |
||
| 385 | |||
| 386 | wp_enqueue_script( 'admin-widgets' ); |
||
| 387 | wp_enqueue_style( 'widgets' ); |
||
| 388 | wp_enqueue_script( 'formidable_admin' ); |
||
| 389 | FrmAppHelper::localize_script( 'admin' ); |
||
| 390 | |||
| 391 | wp_enqueue_style( 'formidable-admin' ); |
||
| 392 | if ( 'formidable-styles' !== $page ) { |
||
| 393 | wp_enqueue_style( 'formidable-grids' ); |
||
| 394 | wp_enqueue_style( 'formidable-dropzone' ); |
||
| 395 | } else { |
||
| 396 | $settings = FrmAppHelper::get_settings(); |
||
| 397 | if ( empty( $settings->old_css ) ) { |
||
| 398 | wp_enqueue_style( 'formidable-grids' ); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | if ( 'formidable-entries' === $page ) { |
||
| 403 | // Load front end js for entries. |
||
| 404 | wp_enqueue_script( 'formidable' ); |
||
| 405 | } |
||
| 406 | |||
| 407 | do_action( 'frm_enqueue_builder_scripts' ); |
||
| 408 | self::include_upgrade_overlay(); |
||
| 409 | self::include_info_overlay(); |
||
| 410 | } elseif ( FrmAppHelper::is_view_builder_page() ) { |
||
| 411 | if ( isset( $_REQUEST['post_type'] ) ) { |
||
| 412 | $post_type = sanitize_title( wp_unslash( $_REQUEST['post_type'] ) ); |
||
| 413 | } elseif ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) { |
||
| 414 | $post = get_post( absint( wp_unslash( $_REQUEST['post'] ) ) ); |
||
| 415 | if ( ! $post ) { |
||
| 416 | return; |
||
| 417 | } |
||
| 418 | $post_type = $post->post_type; |
||
| 419 | } else { |
||
| 420 | return; |
||
| 421 | } |
||
| 422 | |||
| 423 | if ( $post_type == 'frm_display' ) { |
||
| 424 | wp_enqueue_style( 'formidable-grids' ); |
||
| 425 | wp_enqueue_script( 'jquery-ui-draggable' ); |
||
| 426 | wp_enqueue_script( 'formidable_admin' ); |
||
| 427 | wp_enqueue_style( 'formidable-admin' ); |
||
| 428 | FrmAppHelper::localize_script( 'admin' ); |
||
| 429 | self::include_info_overlay(); |
||
| 430 | } |
||
| 431 | } elseif ( $pagenow == 'widgets.php' ) { |
||
| 432 | FrmAppHelper::load_admin_wide_js(); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | public static function load_lang() { |
||
| 437 | load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' ); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Check if the styles are updated when a form is loaded on the front-end |
||
| 442 | * |
||
| 443 | * @since 3.0.1 |
||
| 444 | */ |
||
| 445 | public static function maybe_update_styles() { |
||
| 446 | if ( self::needs_update() ) { |
||
| 447 | self::network_upgrade_site(); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @since 3.0 |
||
| 453 | */ |
||
| 454 | public static function create_rest_routes() { |
||
| 455 | $args = array( |
||
| 456 | 'methods' => 'GET', |
||
| 457 | 'callback' => 'FrmAppController::api_install', |
||
| 458 | ); |
||
| 459 | register_rest_route( 'frm-admin/v1', '/install', $args ); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 464 | * Update database settings for all sites in a network during network upgrade process. |
||
| 465 | * |
||
| 466 | * @since 2.0.1 |
||
| 467 | * |
||
| 468 | * @param int $blog_id Blog ID. |
||
| 469 | */ |
||
| 470 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 471 | |||
| 472 | $request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' ); |
||
| 473 | |||
| 474 | if ( $blog_id ) { |
||
| 475 | switch_to_blog( $blog_id ); |
||
| 476 | $response = rest_do_request( $request ); |
||
| 477 | restore_current_blog(); |
||
| 478 | } else { |
||
| 479 | $response = rest_do_request( $request ); |
||
| 480 | } |
||
| 481 | |||
| 482 | if ( $response->is_error() ) { |
||
| 483 | // if the remove post fails, use javascript instead |
||
| 484 | add_action( 'admin_notices', 'FrmAppController::install_js_fallback' ); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @since 3.0 |
||
| 490 | */ |
||
| 491 | public static function api_install() { |
||
| 492 | if ( self::needs_update() ) { |
||
| 493 | $running = get_option( 'frm_install_running' ); |
||
| 494 | if ( false === $running || $running < strtotime( '-5 minutes' ) ) { |
||
| 495 | update_option( 'frm_install_running', time(), 'no' ); |
||
| 496 | self::install(); |
||
| 497 | delete_option( 'frm_install_running' ); |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | return true; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Silent database upgrade (no redirect). |
||
| 506 | * Called via ajax request during network upgrade process. |
||
| 507 | * |
||
| 508 | * @since 2.0.1 |
||
| 509 | */ |
||
| 510 | public static function ajax_install() { |
||
| 511 | self::api_install(); |
||
| 512 | wp_die(); |
||
| 513 | } |
||
| 514 | |||
| 515 | public static function install() { |
||
| 516 | $frmdb = new FrmMigrate(); |
||
| 517 | $frmdb->upgrade(); |
||
| 518 | } |
||
| 519 | |||
| 520 | public static function uninstall() { |
||
| 521 | FrmAppHelper::permission_check( 'administrator' ); |
||
| 522 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 523 | |||
| 524 | $frmdb = new FrmMigrate(); |
||
| 525 | $frmdb->uninstall(); |
||
| 526 | |||
| 527 | //disable the plugin and redirect after uninstall so the tables don't get added right back |
||
| 528 | $plugins = array( FrmAppHelper::plugin_folder() . '/formidable.php', 'formidable-pro/formidable-pro.php' ); |
||
| 529 | deactivate_plugins( $plugins, false, false ); |
||
| 530 | echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) ); |
||
| 531 | |||
| 532 | wp_die(); |
||
| 533 | } |
||
| 534 | |||
| 535 | public static function drop_tables( $tables ) { |
||
| 544 | |||
| 545 | public static function deauthorize() { |
||
| 555 | |||
| 556 | public static function set_footer_text( $text ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @deprecated 1.07.05 |
||
| 566 | * @codeCoverageIgnore |
||
| 567 | */ |
||
| 568 | public static function get_form_shortcode( $atts ) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @deprecated 2.5.4 |
||
| 574 | * @codeCoverageIgnore |
||
| 575 | */ |
||
| 576 | public static function widget_text_filter( $content ) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Deprecated in favor of wpmu_upgrade_site |
||
| 582 | * |
||
| 583 | * @deprecated 2.3 |
||
| 584 | * @codeCoverageIgnore |
||
| 585 | */ |
||
| 586 | public static function front_head() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @deprecated 3.0.04 |
||
| 592 | * @codeCoverageIgnore |
||
| 593 | */ |
||
| 594 | public static function activation_install() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @deprecated 3.0 |
||
| 600 | * @codeCoverageIgnore |
||
| 601 | */ |
||
| 602 | public static function page_route( $content ) { |
||
| 605 | } |
||
| 606 |