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() { |
||
| 6 | FrmAppHelper::maybe_add_permissions(); |
||
| 7 | if ( ! current_user_can( 'frm_view_forms' ) ) { |
||
| 8 | return; |
||
| 9 | } |
||
| 10 | |||
| 11 | $menu_name = FrmAppHelper::get_menu_name(); |
||
| 12 | add_menu_page( 'Formidable', $menu_name, 'frm_view_forms', 'formidable', 'FrmFormsController::route', '', self::get_menu_position() ); |
||
| 13 | } |
||
| 14 | |||
| 15 | private static function get_menu_position() { |
||
| 16 | $count = count( get_post_types( array( 'show_ui' => true, '_builtin' => false, 'show_in_menu' => true ) ) ); |
||
| 17 | $pos = $count ? '22.7' : '29.3'; |
||
| 18 | $pos = apply_filters( 'frm_menu_position', $pos ); |
||
| 19 | return $pos; |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function load_wp_admin_style() { |
||
| 23 | FrmAppHelper::load_font_style(); |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 27 | global $pagenow, $frm_vars; |
||
| 28 | |||
| 29 | $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); |
||
| 30 | if ( empty( $show_nav ) ) { |
||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | $current_page = isset( $_GET['page'] ) ? FrmAppHelper::simple_get( 'page', 'sanitize_title' ) : FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' ); |
||
|
|
|||
| 35 | if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) { |
||
| 36 | $current_page = 'frm_display'; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ( $form ) { |
||
| 40 | FrmForm::maybe_get_form( $form ); |
||
| 41 | |||
| 42 | if ( is_object( $form ) ) { |
||
| 43 | $id = $form->id; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | if ( ! isset( $id ) ) { |
||
| 48 | $form = $id = false; |
||
| 49 | } |
||
| 50 | |||
| 51 | $nav_items = self::get_form_nav_items( $id ); |
||
| 52 | |||
| 53 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' ); |
||
| 54 | } |
||
| 55 | |||
| 56 | private static function get_form_nav_items( $id ) { |
||
| 57 | $nav_items = array( |
||
| 58 | array( |
||
| 59 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $id ) ), |
||
| 60 | 'label' => __( 'Build', 'formidable' ), |
||
| 61 | 'current' => array( 'edit', 'new', 'duplicate' ), |
||
| 62 | 'page' => 'formidable', |
||
| 63 | 'permission' => 'frm_edit_forms', |
||
| 64 | ), |
||
| 65 | array( |
||
| 66 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ), |
||
| 67 | 'label' => __( 'Settings', 'formidable' ), |
||
| 68 | 'current' => array( 'settings' ), |
||
| 69 | 'page' => 'formidable', |
||
| 70 | 'permission' => 'frm_edit_forms', |
||
| 71 | ), |
||
| 72 | array( |
||
| 73 | 'link' => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ), |
||
| 74 | 'label' => __( 'Entries', 'formidable' ), |
||
| 75 | 'current' => array(), |
||
| 76 | 'page' => 'formidable-entries', |
||
| 77 | 'permission' => 'frm_view_entries', |
||
| 78 | ), |
||
| 79 | ); |
||
| 80 | |||
| 81 | $nav_items = apply_filters( 'frm_form_nav_list', $nav_items, array( 'form_id' => $id ) ); |
||
| 82 | return $nav_items; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Adds a settings link to the plugins page |
||
| 86 | public static function settings_link( $links ) { |
||
| 87 | $settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">' . __( 'Settings', 'formidable' ) . '</a>'; |
||
| 88 | array_unshift( $links, $settings ); |
||
| 89 | |||
| 90 | return $links; |
||
| 91 | } |
||
| 92 | |||
| 93 | public static function pro_get_started_headline() { |
||
| 94 | self::maybe_show_upgrade_bar(); |
||
| 95 | |||
| 96 | // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message |
||
| 97 | if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ( get_site_option( 'frmpro-authorized' ) && ! file_exists( FrmAppHelper::plugin_path() . '/pro/formidable-pro.php' ) ) { |
||
| 102 | FrmAppHelper::load_admin_wide_js(); |
||
| 103 | |||
| 104 | // user is authorized, but running free version |
||
| 105 | $inst_install_url = 'https://formidablepro.com/knowledgebase/install-formidable-forms/'; |
||
| 106 | ?> |
||
| 107 | <div class="error" class="frm_previous_install"> |
||
| 108 | <?php |
||
| 109 | echo wp_kses_post( apply_filters( 'frm_pro_update_msg', |
||
| 110 | sprintf( |
||
| 111 | __( 'This site has been previously authorized to run Formidable Forms.<br/>%1$sInstall Formidable Pro%2$s or %3$sdeauthorize%4$s this site to continue running the free version and remove this message.', 'formidable' ), |
||
| 112 | '<a href="' . esc_url( $inst_install_url ) . '" target="_blank">', '</a>', |
||
| 113 | '<a href="#" class="frm_deauthorize_link">', '</a>' |
||
| 114 | ), esc_url( $inst_install_url ) |
||
| 115 | ) ); ?> |
||
| 116 | </div> |
||
| 117 | <?php |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | private static function maybe_show_upgrade_bar() { |
||
| 122 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 123 | if ( strpos( $page, 'formidable' ) !== 0 ) { |
||
| 124 | return; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | $affiliate = FrmAppHelper::get_affiliate(); |
||
| 132 | if ( ! empty( $affiliate ) ) { |
||
| 133 | $tip = FrmTipsHelper::get_banner_tip(); |
||
| 134 | ?> |
||
| 135 | <div class="update-nag frm-update-to-pro"> |
||
| 136 | <?php echo FrmAppHelper::kses( $tip['tip'] ) ?> |
||
| 137 | <span><?php echo FrmAppHelper::kses( $tip['call'] ) ?></span> |
||
| 138 | <a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url('https://formidablepro.com?banner=1&tip=' . absint( $tip['num'] ) ) ) ?>" class="button">Upgrade to Pro</a> |
||
| 139 | </div> |
||
| 140 | <?php |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 146 | * Use a javascript fallback instead. |
||
| 147 | * |
||
| 148 | * @since 2.0.3 |
||
| 149 | */ |
||
| 150 | public static function install_js_fallback() { |
||
| 151 | FrmAppHelper::load_admin_wide_js(); |
||
| 152 | echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>'; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Check if the database is outdated |
||
| 157 | * |
||
| 158 | * @since 2.0.1 |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | public static function needs_update() { |
||
| 162 | $db_version = (int) get_option( 'frm_db_version' ); |
||
| 163 | $pro_db_version = FrmAppHelper::pro_is_installed() ? get_option( 'frmpro_db_version' ) : false; |
||
| 164 | return ( ( $db_version < FrmAppHelper::$db_version ) || ( FrmAppHelper::pro_is_installed() && (int) $pro_db_version < FrmAppHelper::$pro_db_version ) ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Check for database update and trigger js loading |
||
| 169 | * |
||
| 170 | * @since 2.0.1 |
||
| 171 | */ |
||
| 172 | public static function admin_init() { |
||
| 173 | if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) { |
||
| 174 | self::network_upgrade_site(); |
||
| 175 | } |
||
| 176 | |||
| 177 | $action = FrmAppHelper::simple_get( 'action', 'sanitize_title' ); |
||
| 178 | if ( ! FrmAppHelper::doing_ajax() || $action == 'frm_import_choices' ) { |
||
| 179 | // don't continue during ajax calls |
||
| 180 | self::load_tour(); |
||
| 181 | self::admin_js(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * See if we should start our tour. |
||
| 187 | * @since 2.0.20 |
||
| 188 | */ |
||
| 189 | private static function load_tour() { |
||
| 190 | $restart_tour = filter_input( INPUT_GET, 'frm_restart_tour' ); |
||
| 191 | if ( $restart_tour ) { |
||
| 192 | delete_user_meta( get_current_user_id(), 'frm_ignore_tour' ); |
||
|
1 ignored issue
–
show
|
|||
| 193 | } |
||
| 194 | self::ignore_tour(); |
||
| 195 | |||
| 196 | if ( ! self::has_ignored_tour() ) { |
||
| 197 | add_action( 'admin_enqueue_scripts', array( 'FrmPointers', 'get_instance' ) ); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns the value of the ignore tour. |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | private static function has_ignored_tour() { |
||
| 207 | $user_meta = get_user_meta( get_current_user_id(), 'frm_ignore_tour' ); |
||
|
1 ignored issue
–
show
|
|||
| 208 | |||
| 209 | return ! empty( $user_meta ); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Listener for the ignore tour GET value. If this one is set, just set the user meta to true. |
||
| 214 | */ |
||
| 215 | private static function ignore_tour() { |
||
| 216 | if ( filter_input( INPUT_GET, 'frm_ignore_tour' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'frm-ignore-tour' ) ) { |
||
| 217 | update_user_meta( get_current_user_id(), 'frm_ignore_tour', true ); |
||
|
1 ignored issue
–
show
|
|||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | public static function admin_js() { |
||
| 281 | |||
| 282 | public static function load_lang() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Filter shortcodes in text widgets |
||
| 288 | */ |
||
| 289 | public static function widget_text_filter( $content ) { |
||
| 293 | |||
| 294 | public static function front_head() { |
||
| 295 | if ( is_multisite() ) { |
||
| 296 | $old_db_version = get_option( 'frm_db_version' ); |
||
| 297 | $pro_db_version = FrmAppHelper::pro_is_installed() ? get_option( 'frmpro_db_version' ) : false; |
||
| 298 | if ( ( (int) $old_db_version < (int) FrmAppHelper::$db_version ) || |
||
| 299 | ( FrmAppHelper::pro_is_installed() && (int) $pro_db_version < (int) FrmAppHelper::$pro_db_version ) ) { |
||
| 300 | self::install( $old_db_version ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | public static function localize_script( $location ) { |
||
| 306 | _deprecated_function( __FUNCTION__, '2.0.9', 'FrmAppHelper::localize_script' ); |
||
| 307 | return FrmAppHelper::localize_script( $location ); |
||
| 308 | } |
||
| 309 | |||
| 310 | public static function custom_stylesheet() { |
||
| 314 | |||
| 315 | public static function load_css() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 322 | * Update database settings for all sites in a network during network upgrade process. |
||
| 323 | * |
||
| 324 | * @since 2.0.1 |
||
| 325 | * |
||
| 326 | * @param int $blog_id Blog ID. |
||
| 327 | */ |
||
| 328 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Silent database upgrade (no redirect). |
||
| 347 | * Called via ajax request during network upgrade process. |
||
| 348 | * |
||
| 349 | * @since 2.0.1 |
||
| 350 | */ |
||
| 351 | public static function ajax_install() { |
||
| 357 | |||
| 358 | public static function activation_install() { |
||
| 363 | |||
| 364 | public static function install( $old_db_version = false ) { |
||
| 368 | |||
| 369 | public static function uninstall() { |
||
| 382 | |||
| 383 | public static function drop_tables( $tables ) { |
||
| 391 | |||
| 392 | // Routes for wordpress pages -- we're just replacing content here folks. |
||
| 393 | public static function page_route( $content ) { |
||
| 403 | |||
| 404 | public static function deauthorize() { |
||
| 414 | |||
| 415 | public static function get_form_shortcode( $atts ) { |
||
| 419 | } |
||
| 420 |