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() { |
||
| 83 | $white_pages = array( |
||
| 84 | 'formidable', |
||
| 85 | 'formidable-entries', |
||
| 86 | 'formidable-pro-upgrade', |
||
| 87 | 'formidable-addons', |
||
| 88 | 'formidable-import', |
||
| 89 | 'formidable-settings', |
||
| 90 | 'formidable-styles', |
||
| 91 | 'formidable-styles2', |
||
| 92 | 'formidable-inbox', |
||
| 93 | ); |
||
| 94 | |||
| 95 | $get_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 96 | $is_white_page = in_array( $get_page, $white_pages ); |
||
| 97 | |||
| 98 | if ( ! $is_white_page ) { |
||
| 99 | $screen = get_current_screen(); |
||
| 100 | $is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false ); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $is_white_page; |
||
| 104 | } |
||
| 105 | |||
| 106 | public static function load_wp_admin_style() { |
||
| 107 | FrmAppHelper::load_font_style(); |
||
| 108 | } |
||
| 109 | |||
| 110 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 111 | $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); |
||
| 112 | if ( empty( $show_nav ) || ! $form ) { |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | FrmForm::maybe_get_form( $form ); |
||
| 117 | if ( ! is_object( $form ) ) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | $id = $form->id; |
||
| 122 | $current_page = self::get_current_page(); |
||
| 123 | $nav_items = self::get_form_nav_items( $form ); |
||
| 124 | |||
| 125 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' ); |
||
| 126 | } |
||
| 127 | |||
| 128 | private static function get_current_page() { |
||
| 129 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 130 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' ); |
||
| 131 | $current_page = isset( $_GET['page'] ) ? $page : $post_type; |
||
| 132 | |||
| 133 | if ( FrmAppHelper::is_view_builder_page() ) { |
||
| 134 | $current_page = 'frm_display'; |
||
| 135 | } |
||
| 136 | |||
| 137 | return $current_page; |
||
| 138 | } |
||
| 139 | |||
| 140 | private static function get_form_nav_items( $form ) { |
||
| 141 | $id = $form->parent_form_id ? $form->parent_form_id : $form->id; |
||
| 142 | |||
| 143 | $nav_items = array( |
||
| 144 | array( |
||
| 145 | 'link' => FrmForm::get_edit_link( $id ), |
||
| 146 | 'label' => __( 'Build', 'formidable' ), |
||
| 147 | 'current' => array( 'edit', 'new', 'duplicate' ), |
||
| 148 | 'page' => 'formidable', |
||
| 149 | 'permission' => 'frm_edit_forms', |
||
| 150 | ), |
||
| 151 | array( |
||
| 152 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ), |
||
| 153 | 'label' => __( 'Settings', 'formidable' ), |
||
| 154 | 'current' => array( 'settings' ), |
||
| 155 | 'page' => 'formidable', |
||
| 156 | 'permission' => 'frm_edit_forms', |
||
| 157 | ), |
||
| 158 | array( |
||
| 159 | 'link' => admin_url( 'admin.php?page=formidable-entries&frm-full=1&frm_action=list&form=' . absint( $id ) ), |
||
| 160 | 'label' => __( 'Entries', 'formidable' ), |
||
| 161 | 'current' => array(), |
||
| 162 | 'page' => 'formidable-entries', |
||
| 163 | 'permission' => 'frm_view_entries', |
||
| 164 | ), |
||
| 165 | ); |
||
| 166 | |||
| 167 | // Let people know reports and views exist. |
||
| 168 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 169 | $nav_items[] = array( |
||
| 170 | 'link' => '', |
||
| 171 | 'label' => __( 'Views', 'formidable' ), |
||
| 172 | 'current' => array(), |
||
| 173 | 'page' => '', |
||
| 174 | 'permission' => 'frm_view_entries', |
||
| 175 | 'atts' => array( |
||
| 176 | 'class' => 'frm_show_upgrade frm_noallow', |
||
| 177 | 'data-upgrade' => __( 'Views', 'formidable' ), |
||
| 178 | 'data-medium' => 'views-nav', |
||
| 179 | ), |
||
| 180 | ); |
||
| 181 | $nav_items[] = array( |
||
| 182 | 'link' => '', |
||
| 183 | 'label' => __( 'Reports', 'formidable' ), |
||
| 184 | 'current' => array(), |
||
| 185 | 'page' => '', |
||
| 186 | 'permission' => 'frm_view_entries', |
||
| 187 | 'atts' => array( |
||
| 188 | 'class' => 'frm_show_upgrade frm_noallow', |
||
| 189 | 'data-upgrade' => __( 'Reports', 'formidable' ), |
||
| 190 | 'data-medium' => 'reports-nav', |
||
| 191 | ), |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | $nav_args = array( |
||
| 196 | 'form_id' => $id, |
||
| 197 | 'form' => $form, |
||
| 198 | ); |
||
| 199 | |||
| 200 | return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args ); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Adds a settings link to the plugins page |
||
| 204 | public static function settings_link( $links ) { |
||
| 205 | $settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>'; |
||
| 206 | array_unshift( $links, $settings ); |
||
| 207 | |||
| 208 | return $links; |
||
| 209 | } |
||
| 210 | |||
| 211 | public static function pro_get_started_headline() { |
||
| 212 | self::review_request(); |
||
| 213 | FrmAppHelper::min_pro_version_notice( '4.0' ); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Add admin notices as needed for reviews |
||
| 218 | * |
||
| 219 | * @since 3.04.03 |
||
| 220 | */ |
||
| 221 | private static function review_request() { |
||
| 222 | $reviews = new FrmReviews(); |
||
| 223 | $reviews->review_request(); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Save the request to hide the review |
||
| 228 | * |
||
| 229 | * @since 3.04.03 |
||
| 230 | */ |
||
| 231 | public static function dismiss_review() { |
||
| 232 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
||
| 233 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 234 | |||
| 235 | $reviews = new FrmReviews(); |
||
| 236 | $reviews->dismiss_review(); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @since 3.04.02 |
||
| 241 | */ |
||
| 242 | public static function include_upgrade_overlay() { |
||
| 243 | wp_enqueue_script( 'jquery-ui-dialog' ); |
||
| 244 | wp_enqueue_style( 'jquery-ui-dialog' ); |
||
| 245 | |||
| 246 | add_action( 'admin_footer', 'FrmAppController::upgrade_overlay_html' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @since 3.06.03 |
||
| 251 | */ |
||
| 252 | public static function upgrade_overlay_html() { |
||
| 253 | $is_pro = FrmAppHelper::pro_is_installed(); |
||
| 254 | $upgrade_link = array( |
||
| 255 | 'medium' => 'builder', |
||
| 256 | 'content' => 'upgrade', |
||
| 257 | ); |
||
| 258 | $default_link = FrmAppHelper::admin_upgrade_link( $upgrade_link ); |
||
| 259 | |||
| 260 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/upgrade_overlay.php' ); |
||
| 261 | |||
| 262 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/confirm-overlay.php' ); |
||
| 263 | } |
||
| 264 | |||
| 265 | public static function include_info_overlay() { |
||
| 266 | wp_enqueue_script( 'jquery-ui-dialog' ); |
||
| 267 | wp_enqueue_style( 'jquery-ui-dialog' ); |
||
| 268 | |||
| 269 | add_action( 'admin_footer', 'FrmAppController::info_overlay_html' ); |
||
| 270 | } |
||
| 271 | |||
| 272 | public static function info_overlay_html() { |
||
| 273 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/info-overlay.php' ); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @since 3.04.02 |
||
| 278 | */ |
||
| 279 | public static function remove_upsells() { |
||
| 280 | remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' ); |
||
| 281 | remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' ); |
||
| 282 | remove_action( 'frm_add_form_style_tab_options', 'FrmFormsController::add_form_style_tab_options' ); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 287 | * Use a javascript fallback instead. |
||
| 288 | * |
||
| 289 | * @since 2.0.3 |
||
| 290 | */ |
||
| 291 | public static function install_js_fallback() { |
||
| 292 | FrmAppHelper::load_admin_wide_js(); |
||
| 293 | echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>'; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Check if the database is outdated |
||
| 298 | * |
||
| 299 | * @since 2.0.1 |
||
| 300 | * @return boolean |
||
| 301 | */ |
||
| 302 | public static function needs_update() { |
||
| 303 | $needs_upgrade = self::compare_for_update( |
||
| 304 | array( |
||
| 305 | 'option' => 'frm_db_version', |
||
| 306 | 'new_db_version' => FrmAppHelper::$db_version, |
||
| 307 | 'new_plugin_version' => FrmAppHelper::plugin_version(), |
||
| 308 | ) |
||
| 309 | ); |
||
| 310 | |||
| 311 | if ( ! $needs_upgrade ) { |
||
| 312 | $needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade ); |
||
| 313 | } |
||
| 314 | |||
| 315 | return $needs_upgrade; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Check both version number and DB number for changes |
||
| 320 | * |
||
| 321 | * @since 3.0.04 |
||
| 322 | */ |
||
| 323 | public static function compare_for_update( $atts ) { |
||
| 324 | $db_version = get_option( $atts['option'] ); |
||
| 325 | |||
| 326 | if ( strpos( $db_version, '-' ) === false ) { |
||
| 327 | $needs_upgrade = true; |
||
| 328 | } else { |
||
| 329 | $last_upgrade = explode( '-', $db_version ); |
||
| 330 | $needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version']; |
||
| 331 | $new_version = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' ); |
||
| 332 | $needs_upgrade = $needs_db_upgrade || $new_version; |
||
| 333 | } |
||
| 334 | |||
| 335 | return $needs_upgrade; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Check for database update and trigger js loading |
||
| 340 | * |
||
| 341 | * @since 2.0.1 |
||
| 342 | */ |
||
| 343 | public static function admin_init() { |
||
| 344 | new FrmPersonalData(); // register personal data hooks |
||
| 345 | |||
| 346 | if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) { |
||
| 347 | self::network_upgrade_site(); |
||
| 348 | } |
||
| 349 | |||
| 350 | if ( ! FrmAppHelper::doing_ajax() ) { |
||
| 351 | // don't continue during ajax calls |
||
| 352 | self::admin_js(); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | public static function admin_js() { |
||
| 357 | $version = FrmAppHelper::plugin_version(); |
||
| 358 | FrmAppHelper::load_admin_wide_js(); |
||
| 359 | |||
| 360 | $dependecies = array( |
||
| 361 | 'formidable_admin_global', |
||
| 362 | 'jquery', |
||
| 363 | 'jquery-ui-core', |
||
| 364 | 'jquery-ui-draggable', |
||
| 365 | 'jquery-ui-sortable', |
||
| 366 | 'bootstrap_tooltip', |
||
| 367 | 'bootstrap-multiselect', |
||
| 368 | ); |
||
| 369 | |||
| 370 | if ( FrmAppHelper::is_admin_page( 'formidable-styles' ) || FrmAppHelper::is_admin_page( 'formidable-styles2' ) ) { |
||
| 371 | $dependecies[] = 'wp-color-picker'; |
||
| 372 | } |
||
| 373 | |||
| 374 | wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', $dependecies, $version, true ); |
||
| 375 | wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version ); |
||
| 376 | wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' ); |
||
| 377 | wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version ); |
||
| 378 | |||
| 379 | // load multselect js |
||
| 380 | $depends_on = array( 'jquery', 'bootstrap_tooltip' ); |
||
| 381 | wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', $depends_on, '0.9.8', true ); |
||
| 382 | |||
| 383 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 384 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' ); |
||
| 385 | |||
| 386 | global $pagenow; |
||
| 387 | if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) { |
||
| 388 | |||
| 389 | wp_enqueue_script( 'admin-widgets' ); |
||
| 390 | wp_enqueue_style( 'widgets' ); |
||
| 391 | wp_enqueue_script( 'formidable_admin' ); |
||
| 392 | FrmAppHelper::localize_script( 'admin' ); |
||
| 393 | |||
| 394 | wp_enqueue_style( 'formidable-admin' ); |
||
| 395 | if ( 'formidable-styles' !== $page && 'formidable-styles2' !== $page ) { |
||
| 396 | wp_enqueue_style( 'formidable-grids' ); |
||
| 397 | wp_enqueue_style( 'formidable-dropzone' ); |
||
| 398 | } else { |
||
| 399 | $settings = FrmAppHelper::get_settings(); |
||
| 400 | if ( empty( $settings->old_css ) ) { |
||
| 401 | wp_enqueue_style( 'formidable-grids' ); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | if ( 'formidable-entries' === $page ) { |
||
| 406 | // Load front end js for entries. |
||
| 407 | wp_enqueue_script( 'formidable' ); |
||
| 408 | } |
||
| 409 | |||
| 410 | do_action( 'frm_enqueue_builder_scripts' ); |
||
| 411 | self::include_upgrade_overlay(); |
||
| 412 | self::include_info_overlay(); |
||
| 413 | } elseif ( FrmAppHelper::is_view_builder_page() ) { |
||
| 414 | if ( isset( $_REQUEST['post_type'] ) ) { |
||
| 415 | $post_type = sanitize_title( wp_unslash( $_REQUEST['post_type'] ) ); |
||
| 416 | } elseif ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) { |
||
| 417 | $post = get_post( absint( wp_unslash( $_REQUEST['post'] ) ) ); |
||
| 418 | if ( ! $post ) { |
||
| 419 | return; |
||
| 420 | } |
||
| 421 | $post_type = $post->post_type; |
||
| 422 | } else { |
||
| 423 | return; |
||
| 424 | } |
||
| 425 | |||
| 426 | if ( $post_type == 'frm_display' ) { |
||
| 427 | wp_enqueue_style( 'formidable-grids' ); |
||
| 428 | wp_enqueue_script( 'jquery-ui-draggable' ); |
||
| 429 | wp_enqueue_script( 'formidable_admin' ); |
||
| 430 | wp_enqueue_style( 'formidable-admin' ); |
||
| 431 | FrmAppHelper::localize_script( 'admin' ); |
||
| 432 | self::include_info_overlay(); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | public static function load_lang() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Check if the styles are updated when a form is loaded on the front-end |
||
| 443 | * |
||
| 444 | * @since 3.0.1 |
||
| 445 | */ |
||
| 446 | public static function maybe_update_styles() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @since 3.0 |
||
| 454 | */ |
||
| 455 | public static function create_rest_routes() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 465 | * Update database settings for all sites in a network during network upgrade process. |
||
| 466 | * |
||
| 467 | * @since 2.0.1 |
||
| 468 | * |
||
| 469 | * @param int $blog_id Blog ID. |
||
| 470 | */ |
||
| 471 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @since 3.0 |
||
| 491 | */ |
||
| 492 | public static function api_install() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Silent database upgrade (no redirect). |
||
| 507 | * Called via ajax request during network upgrade process. |
||
| 508 | * |
||
| 509 | * @since 2.0.1 |
||
| 510 | */ |
||
| 511 | public static function ajax_install() { |
||
| 515 | |||
| 516 | public static function install() { |
||
| 520 | |||
| 521 | public static function uninstall() { |
||
| 535 | |||
| 536 | public static function drop_tables( $tables ) { |
||
| 545 | |||
| 546 | public static function deauthorize() { |
||
| 556 | |||
| 557 | public static function set_footer_text( $text ) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @deprecated 1.07.05 |
||
| 567 | * @codeCoverageIgnore |
||
| 568 | */ |
||
| 569 | public static function get_form_shortcode( $atts ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @deprecated 2.5.4 |
||
| 575 | * @codeCoverageIgnore |
||
| 576 | */ |
||
| 577 | public static function widget_text_filter( $content ) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Deprecated in favor of wpmu_upgrade_site |
||
| 583 | * |
||
| 584 | * @deprecated 2.3 |
||
| 585 | * @codeCoverageIgnore |
||
| 586 | */ |
||
| 587 | public static function front_head() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @deprecated 3.0.04 |
||
| 593 | * @codeCoverageIgnore |
||
| 594 | */ |
||
| 595 | public static function activation_install() { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @deprecated 3.0 |
||
| 601 | * @codeCoverageIgnore |
||
| 602 | */ |
||
| 603 | public static function page_route( $content ) { |
||
| 606 | } |
||
| 607 |