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() { |
||
| 23 | $icon = FrmAppHelper::svg_logo( |
||
| 24 | array( |
||
| 25 | 'fill' => '#a0a5aa', |
||
| 26 | 'orange' => '#a0a5aa', |
||
| 27 | ) |
||
| 28 | ); |
||
| 29 | $icon = 'data:image/svg+xml;base64,' . base64_encode( $icon ); |
||
| 30 | |||
| 31 | return apply_filters( 'frm_icon', $icon ); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @since 3.0 |
||
| 36 | */ |
||
| 37 | public static function add_admin_class( $classes ) { |
||
| 38 | if ( self::is_white_page() ) { |
||
| 39 | $classes .= ' frm-white-body '; |
||
| 40 | $classes .= self::get_os(); |
||
| 41 | |||
| 42 | $page = str_replace( 'formidable-', '', FrmAppHelper::simple_get( 'page', 'sanitize_title' ) ); |
||
| 43 | if ( empty( $page ) || $page === 'formidable' ) { |
||
| 44 | $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
||
| 45 | if ( in_array( $action, array( 'settings', 'edit', 'list' ) ) ) { |
||
| 46 | $page .= $action; |
||
| 47 | } else { |
||
| 48 | $page = $action; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | if ( ! empty( $page ) ) { |
||
| 52 | $classes .= ' frm-admin-page-' . $page; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if ( FrmAppHelper::is_full_screen() ) { |
||
| 57 | $classes .= ' frm-full-screen folded'; |
||
| 58 | } |
||
| 59 | |||
| 60 | return $classes; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @since 4.0 |
||
| 65 | */ |
||
| 66 | private static function get_os() { |
||
| 67 | $agent = strtolower( FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ) ); |
||
| 68 | $os = ''; |
||
| 69 | if ( strpos( $agent, 'mac' ) !== false ) { |
||
| 70 | $os = ' osx'; |
||
| 71 | } elseif ( strpos( $agent, 'linux' ) !== false ) { |
||
| 72 | $os = ' linux'; |
||
| 73 | } elseif ( strpos( $agent, 'windows' ) !== false ) { |
||
| 74 | $os = ' windows'; |
||
| 75 | } |
||
| 76 | return $os; |
||
| 77 | } |
||
| 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 | ); |
||
| 92 | |||
| 93 | $get_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 94 | $is_white_page = in_array( $get_page, $white_pages ); |
||
| 95 | |||
| 96 | if ( ! $is_white_page ) { |
||
| 97 | $screen = get_current_screen(); |
||
| 98 | $is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false ); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $is_white_page; |
||
| 102 | } |
||
| 103 | |||
| 104 | public static function load_wp_admin_style() { |
||
| 105 | FrmAppHelper::load_font_style(); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 109 | $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); |
||
| 110 | if ( empty( $show_nav ) || ! $form ) { |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | FrmForm::maybe_get_form( $form ); |
||
| 115 | if ( ! is_object( $form ) ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $id = $form->id; |
||
| 120 | $current_page = self::get_current_page(); |
||
| 121 | $nav_items = self::get_form_nav_items( $form ); |
||
| 122 | |||
| 123 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' ); |
||
| 124 | } |
||
| 125 | |||
| 126 | private static function get_current_page() { |
||
| 127 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 128 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' ); |
||
| 129 | $current_page = isset( $_GET['page'] ) ? $page : $post_type; |
||
| 130 | |||
| 131 | if ( FrmAppHelper::is_view_builder_page() ) { |
||
| 132 | $current_page = 'frm_display'; |
||
| 133 | } |
||
| 134 | |||
| 135 | return $current_page; |
||
| 136 | } |
||
| 137 | |||
| 138 | private static function get_form_nav_items( $form ) { |
||
| 139 | $id = $form->parent_form_id ? $form->parent_form_id : $form->id; |
||
| 140 | |||
| 141 | $nav_items = array( |
||
| 142 | array( |
||
| 143 | 'link' => FrmForm::get_edit_link( $id ), |
||
| 144 | 'label' => __( 'Build', 'formidable' ), |
||
| 145 | 'current' => array( 'edit', 'new', 'duplicate' ), |
||
| 146 | 'page' => 'formidable', |
||
| 147 | 'permission' => 'frm_edit_forms', |
||
| 148 | ), |
||
| 149 | array( |
||
| 150 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ), |
||
| 151 | 'label' => __( 'Settings', 'formidable' ), |
||
| 152 | 'current' => array( 'settings' ), |
||
| 153 | 'page' => 'formidable', |
||
| 154 | 'permission' => 'frm_edit_forms', |
||
| 155 | ), |
||
| 156 | array( |
||
| 157 | 'link' => admin_url( 'admin.php?page=formidable-entries&frm-full=1&frm_action=list&form=' . absint( $id ) ), |
||
| 158 | 'label' => __( 'Entries', 'formidable' ), |
||
| 159 | 'current' => array(), |
||
| 160 | 'page' => 'formidable-entries', |
||
| 161 | 'permission' => 'frm_view_entries', |
||
| 162 | ), |
||
| 163 | ); |
||
| 164 | |||
| 165 | // Let people know reports and views exist. |
||
| 166 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 167 | $nav_items[] = array( |
||
| 168 | 'link' => '', |
||
| 169 | 'label' => __( 'Views', 'formidable' ), |
||
| 170 | 'current' => array(), |
||
| 171 | 'page' => '', |
||
| 172 | 'permission' => 'frm_view_entries', |
||
| 173 | 'atts' => array( |
||
| 174 | 'class' => 'frm_show_upgrade frm_noallow', |
||
| 175 | 'data-upgrade' => __( 'Views', 'formidable' ), |
||
| 176 | 'data-medium' => 'views-nav', |
||
| 177 | ), |
||
| 178 | ); |
||
| 179 | $nav_items[] = array( |
||
| 180 | 'link' => '', |
||
| 181 | 'label' => __( 'Reports', 'formidable' ), |
||
| 182 | 'current' => array(), |
||
| 183 | 'page' => '', |
||
| 184 | 'permission' => 'frm_view_entries', |
||
| 185 | 'atts' => array( |
||
| 186 | 'class' => 'frm_show_upgrade frm_noallow', |
||
| 187 | 'data-upgrade' => __( 'Reports', 'formidable' ), |
||
| 188 | 'data-medium' => 'reports-nav', |
||
| 189 | ), |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | $nav_args = array( |
||
| 194 | 'form_id' => $id, |
||
| 195 | 'form' => $form, |
||
| 196 | ); |
||
| 197 | |||
| 198 | return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args ); |
||
| 199 | } |
||
| 200 | |||
| 201 | // Adds a settings link to the plugins page |
||
| 202 | public static function settings_link( $links ) { |
||
| 203 | $settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>'; |
||
| 204 | array_unshift( $links, $settings ); |
||
| 205 | |||
| 206 | return $links; |
||
| 207 | } |
||
| 208 | |||
| 209 | public static function pro_get_started_headline() { |
||
| 210 | self::maybe_show_upgrade_bar(); |
||
| 211 | self::review_request(); |
||
| 212 | FrmAppHelper::min_pro_version_notice( '4.0' ); |
||
| 213 | |||
| 214 | // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message |
||
| 215 | if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) { |
||
| 216 | return; |
||
| 217 | } |
||
| 218 | |||
| 219 | $pro_installed = is_dir( WP_PLUGIN_DIR . '/formidable-pro' ); |
||
| 220 | $authorized = get_site_option( 'frmpro-authorized' ) && ! is_callable( 'load_formidable_pro' ); |
||
| 221 | if ( ! $authorized ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | FrmAppHelper::load_admin_wide_js(); |
||
| 226 | |||
| 227 | // user is authorized, but running free version |
||
| 228 | $download_url = ''; |
||
| 229 | if ( $pro_installed ) { |
||
| 230 | // if pro version is installed, include link to activate it |
||
| 231 | $inst_install_url = wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=formidable-pro/formidable-pro.php' ), 'activate-plugin_formidable-pro/formidable-pro.php' ); |
||
| 232 | } else { |
||
| 233 | $inst_install_url = '#'; |
||
| 234 | $download_url = FrmAddonsController::get_pro_download_url(); |
||
| 235 | |||
| 236 | if ( empty( $download_url ) ) { |
||
| 237 | $inst_install_url = 'https://formidableforms.com/knowledgebase/install-formidable-forms/?utm_source=WordPress&utm_medium=get-started&utm_campaign=liteplugin'; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | ?> |
||
| 241 | <div class="error frm_previous_install"> |
||
| 242 | <?php |
||
| 243 | echo apply_filters( // WPCS: XSS ok. |
||
| 244 | 'frm_pro_update_msg', |
||
| 245 | sprintf( |
||
| 246 | /* translators: %1$s: Start link HTML, %2$s: End link HTML, %3$s: Start link HTML, %4$s: End link HTML */ |
||
| 247 | esc_html__( 'This site has been previously authorized to run Formidable Forms. %1$sInstall Formidable Pro%2$s or %3$sdeauthorize%4$s this site to continue running the free version and remove this message.', 'formidable' ), |
||
| 248 | '<br/><a href="' . esc_url( $inst_install_url ) . '" id="frm_install_link" target="_blank" data-prourl="' . esc_url( $download_url ) . '">', |
||
| 249 | '</a>', |
||
| 250 | '<a href="#" class="frm_deauthorize_link">', |
||
| 251 | '</a>' |
||
| 252 | ), |
||
| 253 | esc_url( $inst_install_url ) |
||
| 254 | ); |
||
| 255 | ?> |
||
| 256 | <div id="frm_install_message" class="hidden frm_hidden"></div> |
||
| 257 | </div> |
||
| 258 | <?php |
||
| 259 | } |
||
| 260 | |||
| 261 | private static function maybe_show_upgrade_bar() { |
||
| 262 | if ( ! FrmAppHelper::is_formidable_admin() || FrmAppHelper::pro_is_installed() ) { |
||
| 263 | return; |
||
| 264 | } |
||
| 265 | |||
| 266 | $affiliate = FrmAppHelper::get_affiliate(); |
||
| 267 | if ( ! empty( $affiliate ) ) { |
||
| 268 | $tip = FrmTipsHelper::get_banner_tip(); |
||
| 269 | $link = FrmAppHelper::admin_upgrade_link( 'banner' ); |
||
| 270 | ?> |
||
| 271 | <div class="update-nag frm-update-to-pro"> |
||
| 272 | <?php echo FrmAppHelper::kses( $tip['tip'] ); // WPCS: XSS ok. ?> |
||
| 273 | <span><?php echo FrmAppHelper::kses( $tip['call'] ); // WPCS: XSS ok. ?></span> |
||
| 274 | <a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url( $link ) ); ?>" class="button"> |
||
| 275 | Upgrade to Pro |
||
| 276 | </a> |
||
| 277 | </div> |
||
| 278 | <?php |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Add admin notices as needed for reviews |
||
| 284 | * |
||
| 285 | * @since 3.04.03 |
||
| 286 | */ |
||
| 287 | private static function review_request() { |
||
| 288 | $reviews = new FrmReviews(); |
||
| 289 | $reviews->review_request(); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Save the request to hide the review |
||
| 294 | * |
||
| 295 | * @since 3.04.03 |
||
| 296 | */ |
||
| 297 | public static function dismiss_review() { |
||
| 298 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
||
| 299 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 300 | |||
| 301 | $reviews = new FrmReviews(); |
||
| 302 | $reviews->dismiss_review(); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @since 3.04.02 |
||
| 307 | */ |
||
| 308 | public static function include_upgrade_overlay() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @since 3.06.03 |
||
| 317 | */ |
||
| 318 | public static function upgrade_overlay_html() { |
||
| 319 | $is_pro = FrmAppHelper::pro_is_installed(); |
||
| 320 | $upgrade_link = array( |
||
| 321 | 'medium' => 'builder', |
||
| 322 | 'content' => 'upgrade', |
||
| 323 | ); |
||
| 324 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/upgrade_overlay.php' ); |
||
| 325 | |||
| 326 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/confirm-overlay.php' ); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @since 3.04.02 |
||
| 331 | */ |
||
| 332 | public static function remove_upsells() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Don't nag people to install WPForms |
||
| 339 | * |
||
| 340 | * @since 3.05 |
||
| 341 | */ |
||
| 342 | public static function remove_wpforms_nag( $upsell ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 356 | * Use a javascript fallback instead. |
||
| 357 | * |
||
| 358 | * @since 2.0.3 |
||
| 359 | */ |
||
| 360 | public static function install_js_fallback() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if the database is outdated |
||
| 367 | * |
||
| 368 | * @since 2.0.1 |
||
| 369 | * @return boolean |
||
| 370 | */ |
||
| 371 | public static function needs_update() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Check both version number and DB number for changes |
||
| 389 | * |
||
| 390 | * @since 3.0.04 |
||
| 391 | */ |
||
| 392 | public static function compare_for_update( $atts ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Check for database update and trigger js loading |
||
| 409 | * |
||
| 410 | * @since 2.0.1 |
||
| 411 | */ |
||
| 412 | public static function admin_init() { |
||
| 424 | |||
| 425 | public static function admin_js() { |
||
| 502 | |||
| 503 | public static function load_lang() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Check if the styles are updated when a form is loaded on the front-end |
||
| 509 | * |
||
| 510 | * @since 3.0.1 |
||
| 511 | */ |
||
| 512 | public static function maybe_update_styles() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @since 3.0 |
||
| 520 | */ |
||
| 521 | public static function create_rest_routes() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 531 | * Update database settings for all sites in a network during network upgrade process. |
||
| 532 | * |
||
| 533 | * @since 2.0.1 |
||
| 534 | * |
||
| 535 | * @param int $blog_id Blog ID. |
||
| 536 | */ |
||
| 537 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @since 3.0 |
||
| 557 | */ |
||
| 558 | public static function api_install() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Silent database upgrade (no redirect). |
||
| 573 | * Called via ajax request during network upgrade process. |
||
| 574 | * |
||
| 575 | * @since 2.0.1 |
||
| 576 | */ |
||
| 577 | public static function ajax_install() { |
||
| 581 | |||
| 582 | public static function install() { |
||
| 586 | |||
| 587 | public static function uninstall() { |
||
| 600 | |||
| 601 | public static function drop_tables( $tables ) { |
||
| 610 | |||
| 611 | public static function deauthorize() { |
||
| 621 | |||
| 622 | public static function set_footer_text( $text ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @deprecated 1.07.05 |
||
| 632 | * @codeCoverageIgnore |
||
| 633 | */ |
||
| 634 | public static function get_form_shortcode( $atts ) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @deprecated 2.5.4 |
||
| 640 | * @codeCoverageIgnore |
||
| 641 | */ |
||
| 642 | public static function widget_text_filter( $content ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Deprecated in favor of wpmu_upgrade_site |
||
| 648 | * |
||
| 649 | * @deprecated 2.3 |
||
| 650 | * @codeCoverageIgnore |
||
| 651 | */ |
||
| 652 | public static function front_head() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @deprecated 3.0.04 |
||
| 658 | * @codeCoverageIgnore |
||
| 659 | */ |
||
| 660 | public static function activation_install() { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @deprecated 3.0 |
||
| 666 | * @codeCoverageIgnore |
||
| 667 | */ |
||
| 668 | public static function page_route( $content ) { |
||
| 671 | } |
||
| 672 |