Completed
Push — master ( 4c8d3f...c44858 )
by Stephanie
02:48 queued 11s
created

FrmAppController   F

Complexity

Total Complexity 104

Size/Duplication

Total Lines 707
Duplicated Lines 4.38 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 31
loc 707
rs 1.893
c 0
b 0
f 0
wmc 104
lcom 1
cbo 10

43 Methods

Rating   Name   Duplication   Size   Complexity  
A get_menu_position() 0 3 1
A menu_icon() 0 11 1
B add_admin_class() 0 25 7
A get_os() 0 12 4
A menu() 0 9 2
A is_white_page() 0 24 3
A load_wp_admin_style() 0 3 1
A get_form_nav() 0 17 4
A get_current_page() 0 11 3
B get_form_nav_items() 24 63 5
A settings_link() 0 6 1
A pro_get_started_headline() 0 4 1
A review_request() 0 4 1
A dismiss_review() 0 7 1
A include_upgrade_overlay() 0 6 1
A upgrade_overlay_html() 3 17 3
B new_form_overlay_html() 0 47 6
A include_info_overlay() 0 6 1
A info_overlay_html() 0 3 1
A remove_upsells() 0 11 2
A install_js_fallback() 0 4 1
A needs_update() 0 15 2
A compare_for_update() 0 14 3
A admin_init() 4 17 6
D admin_js() 0 80 16
A load_lang() 0 3 1
A maybe_update_styles() 0 5 2
A create_rest_routes() 0 17 1
A can_update_db() 0 3 1
A network_upgrade_site() 0 20 3
A maybe_add_wp_site_health() 0 8 3
A api_install() 0 13 4
A ajax_install() 0 4 1
A install() 0 4 1
A uninstall() 0 14 1
A drop_tables() 0 9 1
A deauthorize() 0 10 1
A set_footer_text() 0 7 2
A get_form_shortcode() 0 3 1
A widget_text_filter() 0 3 1
A front_head() 0 3 1
A activation_install() 0 3 1
A page_route() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmAppController {
7
8
	public static function menu() {
9
		FrmAppHelper::maybe_add_permissions();
10
		if ( ! current_user_can( 'frm_view_forms' ) ) {
11
			return;
12
		}
13
14
		$menu_name = FrmAppHelper::get_menu_name();
15
		add_menu_page( 'Formidable', $menu_name, 'frm_view_forms', 'formidable', 'FrmFormsController::route', self::menu_icon(), self::get_menu_position() );
16
	}
17
18
	private static function get_menu_position() {
19
		return apply_filters( 'frm_menu_position', '29.3' );
20
	}
21
22
	/**
23
	 * @since 3.05
24
	 */
25
	private static function menu_icon() {
26
		$icon = FrmAppHelper::svg_logo(
27
			array(
28
				'fill'   => '#a0a5aa',
29
				'orange' => '#a0a5aa',
30
			)
31
		);
32
		$icon = 'data:image/svg+xml;base64,' . base64_encode( $icon );
33
34
		return apply_filters( 'frm_icon', $icon );
35
	}
36
37
	/**
38
	 * @since 3.0
39
	 */
40
	public static function add_admin_class( $classes ) {
41
		if ( self::is_white_page() ) {
42
			$classes .= ' frm-white-body ';
43
			$classes .= self::get_os();
44
45
			$page = str_replace( 'formidable-', '', FrmAppHelper::simple_get( 'page', 'sanitize_title' ) );
46
			if ( empty( $page ) || $page === 'formidable' ) {
47
				$action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
48
				if ( in_array( $action, array( 'settings', 'edit', 'list' ) ) ) {
49
					$page .= $action;
50
				} else {
51
					$page = $action;
52
				}
53
			}
54
			if ( ! empty( $page ) ) {
55
				$classes .= ' frm-admin-page-' . $page;
56
			}
57
		}
58
59
		if ( FrmAppHelper::is_full_screen() ) {
60
			$classes .= apply_filters( 'frm_admin_full_screen_class', ' frm-full-screen folded' );
61
		}
62
63
		return $classes;
64
	}
65
66
	/**
67
	 * @since 4.0
68
	 */
69
	private static function get_os() {
70
		$agent = strtolower( FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ) );
71
		$os    = '';
72
		if ( strpos( $agent, 'mac' ) !== false ) {
73
			$os = ' osx';
74
		} elseif ( strpos( $agent, 'linux' ) !== false ) {
75
			$os = ' linux';
76
		} elseif ( strpos( $agent, 'windows' ) !== false ) {
77
			$os = ' windows';
78
		}
79
		return $os;
80
	}
81
82
	/**
83
	 * @since 3.0
84
	 */
85
	private static function is_white_page() {
86
		$white_pages = array(
87
			'formidable',
88
			'formidable-entries',
89
			'formidable-views',
90
			'formidable-pro-upgrade',
91
			'formidable-addons',
92
			'formidable-import',
93
			'formidable-settings',
94
			'formidable-styles',
95
			'formidable-styles2',
96
			'formidable-inbox',
97
		);
98
99
		$get_page      = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
100
		$is_white_page = in_array( $get_page, $white_pages );
101
102
		if ( ! $is_white_page ) {
103
			$screen        = get_current_screen();
104
			$is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false );
105
		}
106
107
		return $is_white_page;
108
	}
109
110
	public static function load_wp_admin_style() {
111
		FrmAppHelper::load_font_style();
112
	}
113
114
	public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
115
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
116
		if ( empty( $show_nav ) || ! $form ) {
117
			return;
118
		}
119
120
		FrmForm::maybe_get_form( $form );
121
		if ( ! is_object( $form ) ) {
122
			return;
123
		}
124
125
		$id           = $form->id;
126
		$current_page = self::get_current_page();
127
		$nav_items    = self::get_form_nav_items( $form );
128
129
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
130
	}
131
132
	private static function get_current_page() {
133
		$page         = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
134
		$post_type    = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
135
		$current_page = isset( $_GET['page'] ) ? $page : $post_type;
136
137
		if ( FrmAppHelper::is_view_builder_page() ) {
138
			$current_page = 'frm_display';
139
		}
140
141
		return $current_page;
142
	}
143
144
	private static function get_form_nav_items( $form ) {
145
		$id = $form->parent_form_id ? $form->parent_form_id : $form->id;
146
147
		$nav_items = array(
148
			array(
149
				'link'       => FrmForm::get_edit_link( $id ),
150
				'label'      => __( 'Build', 'formidable' ),
151
				'current'    => array( 'edit', 'new', 'duplicate' ),
152
				'page'       => 'formidable',
153
				'permission' => 'frm_edit_forms',
154
			),
155
			array(
156
				'link'       => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
157
				'label'      => __( 'Settings', 'formidable' ),
158
				'current'    => array( 'settings' ),
159
				'page'       => 'formidable',
160
				'permission' => 'frm_edit_forms',
161
			),
162
			array(
163
				'link'       => admin_url( 'admin.php?page=formidable-entries&frm-full=1&frm_action=list&form=' . absint( $id ) ),
164
				'label'      => __( 'Entries', 'formidable' ),
165
				'current'    => array(),
166
				'page'       => 'formidable-entries',
167
				'permission' => 'frm_view_entries',
168
			),
169
		);
170
171
		$views_installed = is_callable( 'FrmProAppHelper::views_is_installed' ) ? FrmProAppHelper::views_is_installed() : FrmAppHelper::pro_is_installed();
172
173 View Code Duplication
		if ( ! $views_installed ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
			$nav_items[] = array(
175
				'link'    => admin_url( 'admin.php?page=formidable-views&frm-full=1&form=' . absint( $id ) ),
176
				'label'   => __( 'Views', 'formidable' ),
177
				'current' => array(),
178
				'page'    => 'formidable-views',
179
				'permission' => 'frm_view_entries',
180
				'atts'    => array(
181
					'class' => 'frm_noallow',
182
				),
183
			);
184
		}
185
186
		// Let people know reports and views exist.
187 View Code Duplication
		if ( ! FrmAppHelper::pro_is_installed() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
			$nav_items[] = array(
189
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=lite-reports&frm-full=1&form=' . absint( $id ) ),
190
				'label'   => __( 'Reports', 'formidable' ),
191
				'current' => array( 'reports' ),
192
				'page'    => 'formidable',
193
				'permission' => 'frm_view_entries',
194
				'atts'    => array(
195
					'class' => 'frm_noallow',
196
				),
197
			);
198
		}
199
200
		$nav_args = array(
201
			'form_id' => $id,
202
			'form'    => $form,
203
		);
204
205
		return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args );
206
	}
207
208
	// Adds a settings link to the plugins page
209
	public static function settings_link( $links ) {
210
		$settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>';
211
		array_unshift( $links, $settings );
212
213
		return $links;
214
	}
215
216
	public static function pro_get_started_headline() {
217
		self::review_request();
218
		FrmAppHelper::min_pro_version_notice( '4.0' );
219
	}
220
221
	/**
222
	 * Add admin notices as needed for reviews
223
	 *
224
	 * @since 3.04.03
225
	 */
226
	private static function review_request() {
227
		$reviews = new FrmReviews();
228
		$reviews->review_request();
229
	}
230
231
	/**
232
	 * Save the request to hide the review
233
	 *
234
	 * @since 3.04.03
235
	 */
236
	public static function dismiss_review() {
237
		FrmAppHelper::permission_check( 'frm_change_settings' );
238
		check_ajax_referer( 'frm_ajax', 'nonce' );
239
240
		$reviews = new FrmReviews();
241
		$reviews->dismiss_review();
242
	}
243
244
	/**
245
	 * @since 3.04.02
246
	 */
247
	public static function include_upgrade_overlay() {
248
		wp_enqueue_script( 'jquery-ui-dialog' );
249
		wp_enqueue_style( 'jquery-ui-dialog' );
250
251
		add_action( 'admin_footer', 'FrmAppController::upgrade_overlay_html' );
252
	}
253
254
	/**
255
	 * @since 3.06.03
256
	 */
257
	public static function upgrade_overlay_html() {
258
		$is_pro       = FrmAppHelper::pro_is_installed();
259
		$upgrade_link = array(
260
			'medium'  => 'builder',
261
			'content' => 'upgrade',
262
		);
263
		$default_link = FrmAppHelper::admin_upgrade_link( $upgrade_link );
264
		$plugin_path  = FrmAppHelper::plugin_path();
265
		$shared_path  = $plugin_path . '/classes/views/shared/';
266
267
		include $shared_path . 'upgrade_overlay.php';
268
		include $shared_path . 'confirm-overlay.php';
269
270 View Code Duplication
		if ( FrmAppHelper::is_admin_page( 'formidable' ) && in_array( FrmAppHelper::get_param( 'frm_action' ), array( '', 'list', 'trash' ), true ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
			self::new_form_overlay_html();
272
		}
273
	}
274
275
	private static function new_form_overlay_html() {
276
		FrmFormsController::before_list_templates();
277
278
		$plugin_path      = FrmAppHelper::plugin_path();
279
		$path             = $plugin_path . '/classes/views/frm-forms/';
280
		$expired          = FrmFormsController::expired();
281
		$expiring         = FrmAddonsController::is_license_expiring();
282
		$user             = wp_get_current_user(); // $user used in leave-email.php to determine a default value for field
283
		$view_path        = $path . 'new-form-overlay/';
284
		$modal_class      = '';
285
		$upgrade_link     = FrmAppHelper::admin_upgrade_link(
286
			array(
287
				'medium'  => 'new-template',
288
				'content' => 'upgrade',
289
			)
290
		);
291
		$renew_link       = FrmAppHelper::admin_upgrade_link(
292
			array(
293
				'medium'  => 'new-template',
294
				'content' => 'renew',
295
			)
296
		);
297
		$blocks_to_render = array();
298
299
		if ( ! FrmAppHelper::pro_is_installed() ) {
300
			// avoid rendering the email and code blocks for users who have upgraded or have a free license already
301
			$api = new FrmFormTemplateApi();
302
			if ( ! $api->has_free_access() ) {
303
				array_push( $blocks_to_render, 'email', 'code' );
304
			}
305
		}
306
307
		// avoid rendering the upgrade block for users with elite
308
		if ( 'elite' !== FrmAddonsController::license_type() ) {
309
			$blocks_to_render[] = 'upgrade';
310
		}
311
312
		// avoid rendering the renew block for users who are not currently expired
313
		if ( $expired ) {
314
			$blocks_to_render[] = 'renew';
315
			$modal_class        = 'frm-expired';
316
		} elseif ( $expiring ) {
317
			$modal_class = 'frm-expiring';
318
		}
319
320
		include $path . 'new-form-overlay.php';
321
	}
322
323
	public static function include_info_overlay() {
324
		wp_enqueue_script( 'jquery-ui-dialog' );
325
		wp_enqueue_style( 'jquery-ui-dialog' );
326
327
		add_action( 'admin_footer', 'FrmAppController::info_overlay_html' );
328
	}
329
330
	public static function info_overlay_html() {
331
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/info-overlay.php' );
332
	}
333
334
	/**
335
	 * @since 3.04.02
336
	 */
337
	public static function remove_upsells() {
338
		remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' );
339
		remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' );
340
		remove_action( 'frm_add_form_style_tab_options', 'FrmFormsController::add_form_style_tab_options' );
341
		remove_action( 'frm_after_field_options', 'FrmFormsController::logic_tip' );
342
343
		if ( is_callable( 'FrmProAddonsController::renewal_message' ) ) {
344
			// These functions moved to Pro in 4.09.01
345
			remove_action( 'frm_page_footer', 'FrmAppHelper::renewal_message' );
346
		}
347
	}
348
349
	/**
350
	 * If there are CURL problems on this server, wp_remote_post won't work for installing
351
	 * Use a javascript fallback instead.
352
	 *
353
	 * @since 2.0.3
354
	 */
355
	public static function install_js_fallback() {
356
		FrmAppHelper::load_admin_wide_js();
357
		echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>';
358
	}
359
360
	/**
361
	 * Check if the database is outdated
362
	 *
363
	 * @since 2.0.1
364
	 * @return boolean
365
	 */
366
	public static function needs_update() {
367
		$needs_upgrade = self::compare_for_update(
368
			array(
369
				'option'             => 'frm_db_version',
370
				'new_db_version'     => FrmAppHelper::$db_version,
371
				'new_plugin_version' => FrmAppHelper::plugin_version(),
372
			)
373
		);
374
375
		if ( ! $needs_upgrade ) {
376
			$needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade );
377
		}
378
379
		return $needs_upgrade;
380
	}
381
382
	/**
383
	 * Check both version number and DB number for changes
384
	 *
385
	 * @since 3.0.04
386
	 */
387
	public static function compare_for_update( $atts ) {
388
		$db_version = get_option( $atts['option'] );
389
390
		if ( strpos( $db_version, '-' ) === false ) {
391
			$needs_upgrade = true;
392
		} else {
393
			$last_upgrade     = explode( '-', $db_version );
394
			$needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version'];
395
			$new_version      = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' );
396
			$needs_upgrade    = $needs_db_upgrade || $new_version;
397
		}
398
399
		return $needs_upgrade;
400
	}
401
402
	/**
403
	 * Check for database update and trigger js loading
404
	 *
405
	 * @since 2.0.1
406
	 */
407
	public static function admin_init() {
408
		new FrmPersonalData(); // register personal data hooks
409
410
		if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
411
			self::network_upgrade_site();
412
		}
413
414
		if ( ! FrmAppHelper::doing_ajax() ) {
415
			// don't continue during ajax calls
416
			self::admin_js();
417
		}
418
419 View Code Duplication
		if ( FrmAppHelper::is_admin_page( 'formidable' ) && in_array( FrmAppHelper::get_param( 'frm_action' ), array( 'add_new', 'list_templates' ), true ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420
			wp_safe_redirect( admin_url( 'admin.php?page=formidable&triggerNewFormModal=1' ) );
421
			exit;
422
		}
423
	}
424
425
	public static function admin_js() {
426
		$version = FrmAppHelper::plugin_version();
427
		FrmAppHelper::load_admin_wide_js();
428
429
		$dependecies = array(
430
			'formidable_admin_global',
431
			'jquery',
432
			'jquery-ui-core',
433
			'jquery-ui-draggable',
434
			'jquery-ui-sortable',
435
			'bootstrap_tooltip',
436
			'bootstrap-multiselect',
437
		);
438
439
		if ( FrmAppHelper::is_admin_page( 'formidable-styles' ) || FrmAppHelper::is_admin_page( 'formidable-styles2' ) ) {
440
			$dependecies[] = 'wp-color-picker';
441
		}
442
443
		wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', $dependecies, $version, true );
444
		wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version );
445
		wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' );
446
		wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version );
447
448
		// load multselect js
449
		$depends_on = array( 'jquery', 'bootstrap_tooltip' );
450
		wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', $depends_on, '0.9.8', true );
451
452
		$page      = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
453
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
454
455
		global $pagenow;
456
		if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) {
457
458
			wp_enqueue_script( 'admin-widgets' );
459
			wp_enqueue_style( 'widgets' );
460
			wp_enqueue_script( 'formidable_admin' );
461
			FrmAppHelper::localize_script( 'admin' );
462
463
			wp_enqueue_style( 'formidable-admin' );
464
			if ( 'formidable-styles' !== $page && 'formidable-styles2' !== $page ) {
465
				wp_enqueue_style( 'formidable-grids' );
466
				wp_enqueue_style( 'formidable-dropzone' );
467
			} else {
468
				$settings = FrmAppHelper::get_settings();
469
				if ( empty( $settings->old_css ) ) {
470
					wp_enqueue_style( 'formidable-grids' );
471
				}
472
			}
473
474
			if ( 'formidable-entries' === $page ) {
475
				// Load front end js for entries.
476
				wp_enqueue_script( 'formidable' );
477
			}
478
479
			do_action( 'frm_enqueue_builder_scripts' );
480
			self::include_upgrade_overlay();
481
			self::include_info_overlay();
482
		} elseif ( FrmAppHelper::is_view_builder_page() ) {
483
			if ( isset( $_REQUEST['post_type'] ) ) {
484
				$post_type = sanitize_title( wp_unslash( $_REQUEST['post_type'] ) );
485
			} elseif ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
486
				$post = get_post( absint( wp_unslash( $_REQUEST['post'] ) ) );
487
				if ( ! $post ) {
488
					return;
489
				}
490
				$post_type = $post->post_type;
491
			} else {
492
				return;
493
			}
494
495
			if ( $post_type == 'frm_display' ) {
496
				wp_enqueue_style( 'formidable-grids' );
497
				wp_enqueue_script( 'jquery-ui-draggable' );
498
				wp_enqueue_script( 'formidable_admin' );
499
				wp_enqueue_style( 'formidable-admin' );
500
				FrmAppHelper::localize_script( 'admin' );
501
				self::include_info_overlay();
502
			}
503
		}
504
	}
505
506
	public static function load_lang() {
507
		load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
508
	}
509
510
	/**
511
	 * Check if the styles are updated when a form is loaded on the front-end
512
	 *
513
	 * @since 3.0.1
514
	 */
515
	public static function maybe_update_styles() {
516
		if ( self::needs_update() ) {
517
			self::network_upgrade_site();
518
		}
519
	}
520
521
	/**
522
	 * @since 3.0
523
	 */
524
	public static function create_rest_routes() {
525
		$args = array(
526
			'methods'  => 'GET',
527
			'callback' => 'FrmAppController::api_install',
528
			'permission_callback' => __CLASS__ . '::can_update_db',
529
		);
530
531
		register_rest_route( 'frm-admin/v1', '/install', $args );
532
533
		$args = array(
534
			'methods'  => 'GET',
535
			'callback' => 'FrmAddonsController::install_addon_api',
536
			'permission_callback' => 'FrmAddonsController::can_install_addon_api',
537
		);
538
539
		register_rest_route( 'frm-admin/v1', '/install-addon', $args );
540
	}
541
542
	/**
543
	 * Make sure the install is only being run when we tell it to.
544
	 * We don't want to run manually by people calling the API.
545
	 *
546
	 * @since 4.06.02
547
	 */
548
	public static function can_update_db() {
549
		return get_transient( 'frm_updating_api' );
550
	}
551
552
	/**
553
	 * Run silent upgrade on each site in the network during a network upgrade.
554
	 * Update database settings for all sites in a network during network upgrade process.
555
	 *
556
	 * @since 2.0.1
557
	 *
558
	 * @param int $blog_id Blog ID.
559
	 */
560
	public static function network_upgrade_site( $blog_id = 0 ) {
561
		// Flag to check if install is happening as intended.
562
		set_transient( 'frm_updating_api', true, MINUTE_IN_SECONDS );
563
		$request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' );
564
565
		self::maybe_add_wp_site_health();
566
567
		if ( $blog_id ) {
568
			switch_to_blog( $blog_id );
569
			$response = rest_do_request( $request );
570
			restore_current_blog();
571
		} else {
572
			$response = rest_do_request( $request );
573
		}
574
575
		if ( $response->is_error() ) {
576
			// if the remove post fails, use javascript instead
577
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
578
		}
579
	}
580
581
	/**
582
	 * Make sure WP_Site_Health has been included because it is required when calling rest_do_request.
583
	 * Check first that the file exists because WP_Site_Health was only introduced in WordPress 5.2.
584
	 */
585
	private static function maybe_add_wp_site_health() {
586
		if ( ! class_exists( 'WP_Site_Health' ) ) {
587
			$wp_site_health_path = ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
588
			if ( file_exists( $wp_site_health_path ) ) {
589
				require_once $wp_site_health_path;
590
			}
591
		}
592
	}
593
594
	/**
595
	 * @since 3.0
596
	 */
597
	public static function api_install() {
598
		delete_transient( 'frm_updating_api' );
599
		if ( self::needs_update() ) {
600
			$running = get_option( 'frm_install_running' );
601
			if ( false === $running || $running < strtotime( '-5 minutes' ) ) {
602
				update_option( 'frm_install_running', time(), 'no' );
603
				self::install();
604
				delete_option( 'frm_install_running' );
605
			}
606
		}
607
608
		return true;
609
	}
610
611
	/**
612
	 * Silent database upgrade (no redirect).
613
	 * Called via ajax request during network upgrade process.
614
	 *
615
	 * @since 2.0.1
616
	 */
617
	public static function ajax_install() {
618
		self::api_install();
619
		wp_die();
620
	}
621
622
	public static function install() {
623
		$frmdb = new FrmMigrate();
624
		$frmdb->upgrade();
625
	}
626
627
	public static function uninstall() {
628
		FrmAppHelper::permission_check( 'administrator' );
629
		check_ajax_referer( 'frm_ajax', 'nonce' );
630
631
		$frmdb = new FrmMigrate();
632
		$frmdb->uninstall();
633
634
		//disable the plugin and redirect after uninstall so the tables don't get added right back
635
		$plugins = array( FrmAppHelper::plugin_folder() . '/formidable.php', 'formidable-pro/formidable-pro.php' );
636
		deactivate_plugins( $plugins, false, false );
637
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
638
639
		wp_die();
640
	}
641
642
	public static function drop_tables( $tables ) {
643
		global $wpdb;
644
		$tables[] = $wpdb->prefix . 'frm_fields';
645
		$tables[] = $wpdb->prefix . 'frm_forms';
646
		$tables[] = $wpdb->prefix . 'frm_items';
647
		$tables[] = $wpdb->prefix . 'frm_item_metas';
648
649
		return $tables;
650
	}
651
652
	public static function deauthorize() {
653
		FrmAppHelper::permission_check( 'frm_change_settings' );
654
		check_ajax_referer( 'frm_ajax', 'nonce' );
655
656
		delete_option( 'frmpro-credentials' );
657
		delete_option( 'frmpro-authorized' );
658
		delete_site_option( 'frmpro-credentials' );
659
		delete_site_option( 'frmpro-authorized' );
660
		wp_die();
661
	}
662
663
	public static function set_footer_text( $text ) {
664
		if ( FrmAppHelper::is_formidable_admin() ) {
665
			$text = '';
666
		}
667
668
		return $text;
669
	}
670
671
	/**
672
	 * @deprecated 1.07.05
673
	 * @codeCoverageIgnore
674
	 */
675
	public static function get_form_shortcode( $atts ) {
676
		return FrmDeprecated::get_form_shortcode( $atts );
677
	}
678
679
	/**
680
	 * @deprecated 2.5.4
681
	 * @codeCoverageIgnore
682
	 */
683
	public static function widget_text_filter( $content ) {
684
		return FrmDeprecated::widget_text_filter( $content );
685
	}
686
687
	/**
688
	 * Deprecated in favor of wpmu_upgrade_site
689
	 *
690
	 * @deprecated 2.3
691
	 * @codeCoverageIgnore
692
	 */
693
	public static function front_head() {
694
		FrmDeprecated::front_head();
695
	}
696
697
	/**
698
	 * @deprecated 3.0.04
699
	 * @codeCoverageIgnore
700
	 */
701
	public static function activation_install() {
702
		FrmDeprecated::activation_install();
703
	}
704
705
	/**
706
	 * @deprecated 3.0
707
	 * @codeCoverageIgnore
708
	 */
709
	public static function page_route( $content ) {
710
		return FrmDeprecated::page_route( $content );
711
	}
712
}
713