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() { |
||
| 21 | |||
| 22 | public static function load_wp_admin_style() { |
||
| 25 | |||
| 26 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 27 | $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); |
||
| 28 | if ( empty( $show_nav ) || ! $form ) { |
||
| 29 | return; |
||
| 30 | } |
||
| 31 | |||
| 32 | FrmForm::maybe_get_form( $form ); |
||
| 33 | if ( ! is_object( $form ) ) { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | $id = $form->id; |
||
| 38 | $current_page = self::get_current_page(); |
||
| 39 | $nav_items = self::get_form_nav_items( $form ); |
||
| 40 | |||
| 41 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' ); |
||
| 42 | } |
||
| 43 | |||
| 44 | private static function get_current_page() { |
||
| 45 | global $pagenow; |
||
| 46 | |||
| 47 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 48 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' ); |
||
| 49 | $current_page = isset( $_GET['page'] ) ? $page : $post_type; |
||
| 50 | if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) { |
||
| 51 | $current_page = 'frm_display'; |
||
| 52 | } |
||
| 53 | |||
| 54 | return $current_page; |
||
| 55 | } |
||
| 56 | |||
| 57 | private static function get_form_nav_items( $form ) { |
||
| 58 | $id = $form->parent_form_id ? $form->parent_form_id : $form->id; |
||
| 59 | |||
| 60 | $nav_items = array( |
||
| 61 | array( |
||
| 62 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $id ) ), |
||
| 63 | 'label' => __( 'Build', 'formidable' ), |
||
| 64 | 'current' => array( 'edit', 'new', 'duplicate' ), |
||
| 65 | 'page' => 'formidable', |
||
| 66 | 'permission' => 'frm_edit_forms', |
||
| 67 | ), |
||
| 68 | array( |
||
| 69 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ), |
||
| 70 | 'label' => __( 'Settings', 'formidable' ), |
||
| 71 | 'current' => array( 'settings' ), |
||
| 72 | 'page' => 'formidable', |
||
| 73 | 'permission' => 'frm_edit_forms', |
||
| 74 | ), |
||
| 75 | array( |
||
| 76 | 'link' => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ), |
||
| 77 | 'label' => __( 'Entries', 'formidable' ), |
||
| 78 | 'current' => array(), |
||
| 79 | 'page' => 'formidable-entries', |
||
| 80 | 'permission' => 'frm_view_entries', |
||
| 81 | ), |
||
| 82 | ); |
||
| 83 | |||
| 84 | $nav_items = apply_filters( 'frm_form_nav_list', $nav_items, array( 'form_id' => $id, 'form' => $form ) ); |
||
| 85 | return $nav_items; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Adds a settings link to the plugins page |
||
| 89 | public static function settings_link( $links ) { |
||
| 90 | $settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">' . __( 'Settings', 'formidable' ) . '</a>'; |
||
| 91 | array_unshift( $links, $settings ); |
||
| 92 | |||
| 93 | return $links; |
||
| 94 | } |
||
| 95 | |||
| 96 | public static function pro_get_started_headline() { |
||
| 97 | self::maybe_show_upgrade_bar(); |
||
| 98 | |||
| 99 | // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message |
||
| 100 | if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) { |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ( get_site_option( 'frmpro-authorized' ) && ! file_exists( FrmAppHelper::plugin_path() . '/pro/formidable-pro.php' ) ) { |
||
| 105 | FrmAppHelper::load_admin_wide_js(); |
||
| 106 | |||
| 107 | // user is authorized, but running free version |
||
| 108 | $inst_install_url = 'https://formidableforms.com/knowledgebase/install-formidable-forms/'; |
||
| 109 | ?> |
||
| 110 | <div class="error" class="frm_previous_install"> |
||
| 111 | <?php |
||
| 112 | echo wp_kses_post( apply_filters( 'frm_pro_update_msg', |
||
| 113 | sprintf( |
||
| 114 | __( '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' ), |
||
| 115 | '<a href="' . esc_url( $inst_install_url ) . '" target="_blank">', '</a>', |
||
| 116 | '<a href="#" class="frm_deauthorize_link">', '</a>' |
||
| 117 | ), esc_url( $inst_install_url ) |
||
| 118 | ) ); ?> |
||
| 119 | </div> |
||
| 120 | <?php |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | private static function maybe_show_upgrade_bar() { |
||
| 125 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 126 | if ( strpos( $page, 'formidable' ) !== 0 ) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | |||
| 134 | $affiliate = FrmAppHelper::get_affiliate(); |
||
| 135 | if ( ! empty( $affiliate ) ) { |
||
| 136 | $tip = FrmTipsHelper::get_banner_tip(); |
||
| 137 | ?> |
||
| 138 | <div class="update-nag frm-update-to-pro"> |
||
| 139 | <?php echo FrmAppHelper::kses( $tip['tip'] ) ?> |
||
| 140 | <span><?php echo FrmAppHelper::kses( $tip['call'] ) ?></span> |
||
| 141 | <a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url('https://formidableforms.com?banner=1&tip=' . absint( $tip['num'] ) ) ) ?>" class="button">Upgrade to Pro</a> |
||
| 142 | </div> |
||
| 143 | <?php |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 149 | * Use a javascript fallback instead. |
||
| 150 | * |
||
| 151 | * @since 2.0.3 |
||
| 152 | */ |
||
| 153 | public static function install_js_fallback() { |
||
| 154 | FrmAppHelper::load_admin_wide_js(); |
||
| 155 | echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>'; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Check if the database is outdated |
||
| 160 | * |
||
| 161 | * @since 2.0.1 |
||
| 162 | * @return boolean |
||
| 163 | */ |
||
| 164 | public static function needs_update() { |
||
| 165 | $db_version = (int) get_option( 'frm_db_version' ); |
||
| 166 | $needs_upgrade = ( (int) $db_version < (int) FrmAppHelper::$db_version ); |
||
| 167 | if ( ! $needs_upgrade ) { |
||
| 168 | $needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade ); |
||
| 169 | } |
||
| 170 | return $needs_upgrade; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check for database update and trigger js loading |
||
| 175 | * |
||
| 176 | * @since 2.0.1 |
||
| 177 | */ |
||
| 178 | public static function admin_init() { |
||
| 179 | if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) { |
||
| 180 | self::network_upgrade_site(); |
||
| 181 | } |
||
| 182 | |||
| 183 | $action = FrmAppHelper::simple_get( 'action', 'sanitize_title' ); |
||
| 184 | if ( ! FrmAppHelper::doing_ajax() || $action == 'frm_import_choices' ) { |
||
| 185 | // don't continue during ajax calls |
||
| 186 | self::load_tour(); |
||
| 187 | self::admin_js(); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * See if we should start our tour. |
||
| 193 | * @since 2.0.20 |
||
| 194 | */ |
||
| 195 | private static function load_tour() { |
||
| 196 | $restart_tour = filter_input( INPUT_GET, 'frm_restart_tour' ); |
||
| 197 | if ( $restart_tour ) { |
||
| 198 | delete_user_meta( get_current_user_id(), 'frm_ignore_tour' ); |
||
| 199 | } |
||
| 200 | self::ignore_tour(); |
||
| 201 | |||
| 202 | if ( ! self::has_ignored_tour() ) { |
||
| 203 | add_action( 'admin_enqueue_scripts', array( 'FrmPointers', 'get_instance' ) ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the value of the ignore tour. |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | private static function has_ignored_tour() { |
||
| 213 | $user_meta = get_user_meta( get_current_user_id(), 'frm_ignore_tour' ); |
||
| 214 | |||
| 215 | return ! empty( $user_meta ); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Listener for the ignore tour GET value. If this one is set, just set the user meta to true. |
||
| 220 | */ |
||
| 221 | private static function ignore_tour() { |
||
| 222 | if ( filter_input( INPUT_GET, 'frm_ignore_tour' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'frm-ignore-tour' ) ) { |
||
| 223 | update_user_meta( get_current_user_id(), 'frm_ignore_tour', true ); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | public static function admin_js() { |
||
| 228 | $version = FrmAppHelper::plugin_version(); |
||
| 229 | FrmAppHelper::load_admin_wide_js( false ); |
||
| 230 | |||
| 231 | wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', array( |
||
| 232 | 'formidable_admin_global', 'formidable', 'jquery', |
||
| 233 | 'jquery-ui-core', 'jquery-ui-draggable', |
||
| 234 | 'jquery-ui-sortable', |
||
| 235 | 'bootstrap_tooltip', 'bootstrap-multiselect', |
||
| 236 | ), $version, true ); |
||
| 237 | wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version ); |
||
| 238 | wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' ); |
||
| 239 | wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version ); |
||
| 240 | |||
| 241 | // load multselect js |
||
| 242 | wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip' ), '0.9.8', true ); |
||
| 243 | |||
| 244 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 245 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' ); |
||
| 246 | |||
| 247 | global $pagenow; |
||
| 248 | if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) { |
||
| 249 | |||
| 250 | wp_enqueue_script( 'admin-widgets' ); |
||
| 251 | wp_enqueue_style( 'widgets' ); |
||
| 252 | wp_enqueue_script( 'formidable' ); |
||
| 253 | wp_enqueue_script( 'formidable_admin' ); |
||
| 254 | FrmAppHelper::localize_script( 'admin' ); |
||
| 255 | |||
| 256 | wp_enqueue_style( 'formidable-admin' ); |
||
| 257 | wp_enqueue_style( 'formidable-grids' ); |
||
| 258 | wp_enqueue_style( 'formidable-dropzone' ); |
||
| 259 | add_thickbox(); |
||
| 260 | |||
| 261 | wp_register_script( 'formidable-editinplace', FrmAppHelper::plugin_url() . '/js/jquery/jquery.editinplace.packed.js', array( 'jquery' ), '2.3.0' ); |
||
| 262 | |||
| 263 | } else if ( $pagenow == 'post.php' || ( $pagenow == 'post-new.php' && $post_type == 'frm_display' ) ) { |
||
| 264 | if ( isset( $_REQUEST['post_type'] ) ) { |
||
| 265 | $post_type = sanitize_title( $_REQUEST['post_type'] ); |
||
| 266 | } else if ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) { |
||
| 267 | $post = get_post( absint( $_REQUEST['post'] ) ); |
||
|
|
|||
| 268 | if ( ! $post ) { |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | $post_type = $post->post_type; |
||
| 272 | } else { |
||
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 276 | if ( $post_type == 'frm_display' ) { |
||
| 277 | wp_enqueue_script( 'jquery-ui-draggable' ); |
||
| 278 | wp_enqueue_script( 'formidable_admin' ); |
||
| 279 | wp_enqueue_style( 'formidable-admin' ); |
||
| 280 | FrmAppHelper::localize_script( 'admin' ); |
||
| 281 | } |
||
| 282 | } else if ( $pagenow == 'widgets.php' ) { |
||
| 283 | FrmAppHelper::load_admin_wide_js(); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | public static function load_lang() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Filter shortcodes in text widgets |
||
| 293 | */ |
||
| 294 | public static function widget_text_filter( $content ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Deprecated in favor of wpmu_upgrade_site |
||
| 301 | */ |
||
| 302 | public static function front_head() { |
||
| 303 | _deprecated_function( __FUNCTION__, '2.3' ); |
||
| 304 | if ( is_multisite() && self::needs_update() ) { |
||
| 305 | self::install(); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | public static function localize_script( $location ) { |
||
| 313 | |||
| 314 | public static function custom_stylesheet() { |
||
| 318 | |||
| 319 | public static function load_css() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 326 | * Update database settings for all sites in a network during network upgrade process. |
||
| 327 | * |
||
| 328 | * @since 2.0.1 |
||
| 329 | * |
||
| 330 | * @param int $blog_id Blog ID. |
||
| 331 | */ |
||
| 332 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Silent database upgrade (no redirect). |
||
| 351 | * Called via ajax request during network upgrade process. |
||
| 352 | * |
||
| 353 | * @since 2.0.1 |
||
| 354 | */ |
||
| 355 | public static function ajax_install() { |
||
| 361 | |||
| 362 | public static function activation_install() { |
||
| 367 | |||
| 368 | public static function install( $old_db_version = false ) { |
||
| 372 | |||
| 373 | public static function uninstall() { |
||
| 386 | |||
| 387 | public static function drop_tables( $tables ) { |
||
| 395 | |||
| 396 | // Routes for wordpress pages -- we're just replacing content here folks. |
||
| 397 | public static function page_route( $content ) { |
||
| 407 | |||
| 408 | public static function deauthorize() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Add a filter to shorten the EDD filename for Formidable plugin, and add-on, updates |
||
| 421 | * |
||
| 422 | * @since 2.03.08 |
||
| 423 | * |
||
| 424 | * @param boolean $return |
||
| 425 | * @param string $package |
||
| 426 | * |
||
| 427 | * @return boolean |
||
| 428 | */ |
||
| 429 | public static function add_shorten_edd_filename_filter( $return, $package ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Shorten the EDD filename for automatic updates |
||
| 439 | * Decreases size of file path so file path limit is not hit on Windows servers |
||
| 440 | * |
||
| 441 | * @since 2.03.08 |
||
| 442 | * |
||
| 443 | * @param string $filename |
||
| 444 | * @param string $ext |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public static function shorten_edd_filename( $filename, $ext ) { |
||
| 454 | |||
| 455 | public static function get_form_shortcode( $atts ) { |
||
| 459 | } |
||
| 460 |