Completed
Push — master ( c4a6dd...038d54 )
by Jamie
03:10
created

FrmAppController   C

Complexity

Total Complexity 78

Size/Duplication

Total Lines 421
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 421
rs 5.4563
c 0
b 0
f 0
wmc 78
lcom 1
cbo 8

31 Methods

Rating   Name   Duplication   Size   Complexity  
A menu() 0 9 2
A get_menu_position() 0 6 2
A load_wp_admin_style() 0 3 1
A load_lang() 0 3 1
A widget_text_filter() 0 4 1
A get_form_nav() 0 17 4
A get_current_page() 0 12 4
B get_form_nav_items() 0 30 2
A settings_link() 0 6 1
B pro_get_started_headline() 0 27 5
B maybe_show_upgrade_bar() 0 22 4
A install_js_fallback() 0 4 1
A localize_script() 0 4 1
A custom_stylesheet() 0 4 1
A load_css() 0 4 1
B network_upgrade_site() 0 16 5
A ajax_install() 0 6 2
A activation_install() 0 5 1
A install() 0 4 1
A uninstall() 0 13 1
A drop_tables() 0 8 1
A page_route() 0 10 4
A deauthorize() 0 10 1
A get_form_shortcode() 0 4 1
A needs_update() 0 8 2
B admin_init() 0 12 5
A load_tour() 0 11 3
A has_ignored_tour() 0 5 1
A ignore_tour() 0 5 3
C admin_js() 0 59 13
A front_head() 0 6 3

How to fix   Complexity   

Complex Class

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
2
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
		$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'] ) );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
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() {
288
        load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
289
    }
290
291
    /**
292
     * Filter shortcodes in text widgets
293
     */
294
    public static function widget_text_filter( $content ) {
295
    	$regex = '/\[\s*(formidable|display-frm-data|frm-stats|frm-graph|frm-entry-links|formresults|frm-search)\s+.*\]/';
296
    	return preg_replace_callback( $regex, 'FrmAppHelper::widget_text_filter_callback', $content );
297
    }
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 ) {
310
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmAppHelper::localize_script' );
311
		return FrmAppHelper::localize_script( $location );
312
	}
313
314
	public static function custom_stylesheet() {
315
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmStylesController::custom_stylesheet' );
316
		return FrmStylesController::custom_stylesheet();
317
	}
318
319
	public static function load_css() {
320
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmStylesController::load_saved_css' );
321
		return FrmStylesController::load_saved_css();
322
	}
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 ) {
333
		if ( $blog_id ) {
334
			switch_to_blog( $blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
335
			$upgrade_url = admin_url( 'admin-ajax.php' );
336
			restore_current_blog();
337
		} else {
338
			$upgrade_url = admin_url( 'admin-ajax.php' );
339
		}
340
341
		$upgrade_url = add_query_arg( array( 'action' => 'frm_silent_upgrade' ), $upgrade_url );
342
		$r = wp_remote_get( esc_url_raw( $upgrade_url ) );
343
		if ( is_wp_error( $r ) || ! is_array( $r ) || ! empty( $r['body'] ) ) {
344
			// if the remove post fails, use javascript instead
345
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
346
		}
347
	}
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() {
356
		if ( self::needs_update() ) {
357
			self::install();
358
		}
359
		wp_die();
360
	}
361
362
    public static function activation_install() {
363
        FrmAppHelper::delete_cache_and_transient( 'frm_plugin_version' );
364
        FrmFormActionsController::actions_init();
365
        self::install();
366
    }
367
368
    public static function install( $old_db_version = false ) {
369
        $frmdb = new FrmDb();
370
        $frmdb->upgrade( $old_db_version );
371
    }
372
373
    public static function uninstall() {
374
		FrmAppHelper::permission_check('administrator');
375
        check_ajax_referer( 'frm_ajax', 'nonce' );
376
377
		$frmdb = new FrmDb();
378
		$frmdb->uninstall();
379
380
		//disable the plugin and redirect after uninstall so the tables don't get added right back
381
		deactivate_plugins( FrmAppHelper::plugin_folder() . '/formidable.php', false, false );
382
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
383
384
        wp_die();
385
    }
386
387
    public static function drop_tables( $tables ) {
388
        global $wpdb;
389
        $tables[] = $wpdb->prefix . 'frm_fields';
390
        $tables[] = $wpdb->prefix . 'frm_forms';
391
        $tables[] = $wpdb->prefix . 'frm_items';
392
        $tables[] = $wpdb->prefix . 'frm_item_metas';
393
        return $tables;
394
    }
395
396
    // Routes for wordpress pages -- we're just replacing content here folks.
397
    public static function page_route( $content ) {
398
        global $post;
399
400
        $frm_settings = FrmAppHelper::get_settings();
401
        if ( $post && $post->ID == $frm_settings->preview_page_id && isset( $_GET['form'] ) ) {
402
            $content = FrmFormsController::page_preview();
403
        }
404
405
        return $content;
406
    }
407
408
    public static function deauthorize() {
409
		FrmAppHelper::permission_check('frm_change_settings');
410
        check_ajax_referer( 'frm_ajax', 'nonce' );
411
412
        delete_option( 'frmpro-credentials' );
413
        delete_option( 'frmpro-authorized' );
414
        delete_site_option( 'frmpro-credentials' );
415
        delete_site_option( 'frmpro-authorized' );
416
        wp_die();
417
    }
418
419
    public static function get_form_shortcode( $atts ) {
420
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form_shortcode()' );
421
        return FrmFormsController::get_form_shortcode( $atts );
422
    }
423
}
424