Completed
Push — master ( b5bee3...18e7f5 )
by Stephanie
03:41 queued 01:06
created

FrmAppController::drop_tables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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::menu_icon(), self::get_menu_position() );
13
	}
14
15
	private static function get_menu_position() {
16
		return apply_filters( 'frm_menu_position', '29.3' );
17
	}
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 .= apply_filters( 'frm_admin_full_screen_class', ' 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
			'formidable-styles2',
92
			'formidable-inbox',
93
		);
94
95
		$get_page      = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
96
		$is_white_page = in_array( $get_page, $white_pages );
97
98
		if ( ! $is_white_page ) {
99
			$screen        = get_current_screen();
100
			$is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false );
101
		}
102
103
		return $is_white_page;
104
	}
105
106
	public static function load_wp_admin_style() {
107
		FrmAppHelper::load_font_style();
108
	}
109
110
	public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
111
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
112
		if ( empty( $show_nav ) || ! $form ) {
113
			return;
114
		}
115
116
		FrmForm::maybe_get_form( $form );
117
		if ( ! is_object( $form ) ) {
118
			return;
119
		}
120
121
		$id           = $form->id;
122
		$current_page = self::get_current_page();
123
		$nav_items    = self::get_form_nav_items( $form );
124
125
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
126
	}
127
128
	private static function get_current_page() {
129
		$page         = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
130
		$post_type    = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
131
		$current_page = isset( $_GET['page'] ) ? $page : $post_type;
132
133
		if ( FrmAppHelper::is_view_builder_page() ) {
134
			$current_page = 'frm_display';
135
		}
136
137
		return $current_page;
138
	}
139
140
	private static function get_form_nav_items( $form ) {
141
		$id = $form->parent_form_id ? $form->parent_form_id : $form->id;
142
143
		$nav_items = array(
144
			array(
145
				'link'       => FrmForm::get_edit_link( $id ),
146
				'label'      => __( 'Build', 'formidable' ),
147
				'current'    => array( 'edit', 'new', 'duplicate' ),
148
				'page'       => 'formidable',
149
				'permission' => 'frm_edit_forms',
150
			),
151
			array(
152
				'link'       => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
153
				'label'      => __( 'Settings', 'formidable' ),
154
				'current'    => array( 'settings' ),
155
				'page'       => 'formidable',
156
				'permission' => 'frm_edit_forms',
157
			),
158
			array(
159
				'link'       => admin_url( 'admin.php?page=formidable-entries&frm-full=1&frm_action=list&form=' . absint( $id ) ),
160
				'label'      => __( 'Entries', 'formidable' ),
161
				'current'    => array(),
162
				'page'       => 'formidable-entries',
163
				'permission' => 'frm_view_entries',
164
			),
165
		);
166
167
		// Let people know reports and views exist.
168
		if ( ! FrmAppHelper::pro_is_installed() ) {
169
			$nav_items[] = array(
170
				'link'    => '',
171
				'label'   => __( 'Views', 'formidable' ),
172
				'current' => array(),
173
				'page'    => '',
174
				'permission' => 'frm_view_entries',
175
				'atts'    => array(
176
					'class'        => 'frm_show_upgrade frm_noallow',
177
					'data-upgrade' => __( 'Views', 'formidable' ),
178
					'data-medium'  => 'views-nav',
179
				),
180
			);
181
			$nav_items[] = array(
182
				'link'    => '',
183
				'label'   => __( 'Reports', 'formidable' ),
184
				'current' => array(),
185
				'page'    => '',
186
				'permission' => 'frm_view_entries',
187
				'atts'    => array(
188
					'class'        => 'frm_show_upgrade frm_noallow',
189
					'data-upgrade' => __( 'Reports', 'formidable' ),
190
					'data-medium'  => 'reports-nav',
191
				),
192
			);
193
		}
194
195
		$nav_args = array(
196
			'form_id' => $id,
197
			'form'    => $form,
198
		);
199
200
		return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args );
201
	}
202
203
	// Adds a settings link to the plugins page
204
	public static function settings_link( $links ) {
205
		$settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>';
206
		array_unshift( $links, $settings );
207
208
		return $links;
209
	}
210
211
	public static function pro_get_started_headline() {
212
		self::review_request();
213
		FrmAppHelper::min_pro_version_notice( '4.0' );
214
	}
215
216
	/**
217
	 * Add admin notices as needed for reviews
218
	 *
219
	 * @since 3.04.03
220
	 */
221
	private static function review_request() {
222
		$reviews = new FrmReviews();
223
		$reviews->review_request();
224
	}
225
226
	/**
227
	 * Save the request to hide the review
228
	 *
229
	 * @since 3.04.03
230
	 */
231
	public static function dismiss_review() {
232
		FrmAppHelper::permission_check( 'frm_change_settings' );
233
		check_ajax_referer( 'frm_ajax', 'nonce' );
234
235
		$reviews = new FrmReviews();
236
		$reviews->dismiss_review();
237
	}
238
239
	/**
240
	 * @since 3.04.02
241
	 */
242
	public static function include_upgrade_overlay() {
243
		wp_enqueue_script( 'jquery-ui-dialog' );
244
		wp_enqueue_style( 'jquery-ui-dialog' );
245
246
		add_action( 'admin_footer', 'FrmAppController::upgrade_overlay_html' );
247
	}
248
249
	/**
250
	 * @since 3.06.03
251
	 */
252
	public static function upgrade_overlay_html() {
253
		$is_pro       = FrmAppHelper::pro_is_installed();
254
		$upgrade_link = array(
255
			'medium'  => 'builder',
256
			'content' => 'upgrade',
257
		);
258
		$default_link = FrmAppHelper::admin_upgrade_link( $upgrade_link );
259
260
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/upgrade_overlay.php' );
261
262
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/confirm-overlay.php' );
263
	}
264
265
	public static function include_info_overlay() {
266
		wp_enqueue_script( 'jquery-ui-dialog' );
267
		wp_enqueue_style( 'jquery-ui-dialog' );
268
269
		add_action( 'admin_footer', 'FrmAppController::info_overlay_html' );
270
	}
271
272
	public static function info_overlay_html() {
273
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/info-overlay.php' );
274
	}
275
276
	/**
277
	 * @since 3.04.02
278
	 */
279
	public static function remove_upsells() {
280
		remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' );
281
		remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' );
282
		remove_action( 'frm_add_form_style_tab_options', 'FrmFormsController::add_form_style_tab_options' );
283
	}
284
285
	/**
286
	 * If there are CURL problems on this server, wp_remote_post won't work for installing
287
	 * Use a javascript fallback instead.
288
	 *
289
	 * @since 2.0.3
290
	 */
291
	public static function install_js_fallback() {
292
		FrmAppHelper::load_admin_wide_js();
293
		echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>';
294
	}
295
296
	/**
297
	 * Check if the database is outdated
298
	 *
299
	 * @since 2.0.1
300
	 * @return boolean
301
	 */
302
	public static function needs_update() {
303
		$needs_upgrade = self::compare_for_update(
304
			array(
305
				'option'             => 'frm_db_version',
306
				'new_db_version'     => FrmAppHelper::$db_version,
307
				'new_plugin_version' => FrmAppHelper::plugin_version(),
308
			)
309
		);
310
311
		if ( ! $needs_upgrade ) {
312
			$needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade );
313
		}
314
315
		return $needs_upgrade;
316
	}
317
318
	/**
319
	 * Check both version number and DB number for changes
320
	 *
321
	 * @since 3.0.04
322
	 */
323
	public static function compare_for_update( $atts ) {
324
		$db_version = get_option( $atts['option'] );
325
326
		if ( strpos( $db_version, '-' ) === false ) {
327
			$needs_upgrade = true;
328
		} else {
329
			$last_upgrade     = explode( '-', $db_version );
330
			$needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version'];
331
			$new_version      = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' );
332
			$needs_upgrade    = $needs_db_upgrade || $new_version;
333
		}
334
335
		return $needs_upgrade;
336
	}
337
338
	/**
339
	 * Check for database update and trigger js loading
340
	 *
341
	 * @since 2.0.1
342
	 */
343
	public static function admin_init() {
344
		new FrmPersonalData(); // register personal data hooks
345
346
		if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
347
			self::network_upgrade_site();
348
		}
349
350
		if ( ! FrmAppHelper::doing_ajax() ) {
351
			// don't continue during ajax calls
352
			self::admin_js();
353
		}
354
	}
355
356
	public static function admin_js() {
357
		$version = FrmAppHelper::plugin_version();
358
		FrmAppHelper::load_admin_wide_js();
359
360
		$dependecies = array(
361
			'formidable_admin_global',
362
			'jquery',
363
			'jquery-ui-core',
364
			'jquery-ui-draggable',
365
			'jquery-ui-sortable',
366
			'bootstrap_tooltip',
367
			'bootstrap-multiselect',
368
		);
369
370
		if ( FrmAppHelper::is_admin_page( 'formidable-styles' ) || FrmAppHelper::is_admin_page( 'formidable-styles2' ) ) {
371
			$dependecies[] = 'wp-color-picker';
372
		}
373
374
		wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', $dependecies, $version, true );
375
		wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version );
376
		wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' );
377
		wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version );
378
379
		// load multselect js
380
		$depends_on = array( 'jquery', 'bootstrap_tooltip' );
381
		wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', $depends_on, '0.9.8', true );
382
383
		$page      = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
384
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
385
386
		global $pagenow;
387
		if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) {
388
389
			wp_enqueue_script( 'admin-widgets' );
390
			wp_enqueue_style( 'widgets' );
391
			wp_enqueue_script( 'formidable_admin' );
392
			FrmAppHelper::localize_script( 'admin' );
393
394
			wp_enqueue_style( 'formidable-admin' );
395
			if ( 'formidable-styles' !== $page && 'formidable-styles2' !== $page ) {
396
				wp_enqueue_style( 'formidable-grids' );
397
				wp_enqueue_style( 'formidable-dropzone' );
398
			} else {
399
				$settings = FrmAppHelper::get_settings();
400
				if ( empty( $settings->old_css ) ) {
401
					wp_enqueue_style( 'formidable-grids' );
402
				}
403
			}
404
405
			if ( 'formidable-entries' === $page ) {
406
				// Load front end js for entries.
407
				wp_enqueue_script( 'formidable' );
408
			}
409
410
			do_action( 'frm_enqueue_builder_scripts' );
411
			self::include_upgrade_overlay();
412
			self::include_info_overlay();
413
		} elseif ( FrmAppHelper::is_view_builder_page() ) {
414
			if ( isset( $_REQUEST['post_type'] ) ) {
415
				$post_type = sanitize_title( wp_unslash( $_REQUEST['post_type'] ) );
416
			} elseif ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
417
				$post = get_post( absint( wp_unslash( $_REQUEST['post'] ) ) );
418
				if ( ! $post ) {
419
					return;
420
				}
421
				$post_type = $post->post_type;
422
			} else {
423
				return;
424
			}
425
426
			if ( $post_type == 'frm_display' ) {
427
				wp_enqueue_style( 'formidable-grids' );
428
				wp_enqueue_script( 'jquery-ui-draggable' );
429
				wp_enqueue_script( 'formidable_admin' );
430
				wp_enqueue_style( 'formidable-admin' );
431
				FrmAppHelper::localize_script( 'admin' );
432
				self::include_info_overlay();
433
			}
434
		}
435
	}
436
437
	public static function load_lang() {
438
		load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
439
	}
440
441
	/**
442
	 * Check if the styles are updated when a form is loaded on the front-end
443
	 *
444
	 * @since 3.0.1
445
	 */
446
	public static function maybe_update_styles() {
447
		if ( self::needs_update() ) {
448
			self::network_upgrade_site();
449
		}
450
	}
451
452
	/**
453
	 * @since 3.0
454
	 */
455
	public static function create_rest_routes() {
456
		$args = array(
457
			'methods'  => 'GET',
458
			'callback' => 'FrmAppController::api_install',
459
			'permission_callback' => __CLASS__ . '::can_update_db',
460
		);
461
462
		register_rest_route( 'frm-admin/v1', '/install', $args );
463
	}
464
465
	/**
466
	 * Make sure the install is only being run when we tell it to.
467
	 * We don't want to run manually by people calling the API.
468
	 *
469
	 * @since 4.06.02
470
	 */
471
	public static function can_update_db() {
472
		return get_transient( 'frm_updating_api' );
473
	}
474
475
	/**
476
	 * Run silent upgrade on each site in the network during a network upgrade.
477
	 * Update database settings for all sites in a network during network upgrade process.
478
	 *
479
	 * @since 2.0.1
480
	 *
481
	 * @param int $blog_id Blog ID.
482
	 */
483
	public static function network_upgrade_site( $blog_id = 0 ) {
484
		// Flag to check if install is happening as intended.
485
		set_transient( 'frm_updating_api', true, MINUTE_IN_SECONDS );
486
		$request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' );
487
488
		if ( $blog_id ) {
489
			switch_to_blog( $blog_id );
490
			$response = rest_do_request( $request );
491
			restore_current_blog();
492
		} else {
493
			$response = rest_do_request( $request );
494
		}
495
496
		if ( $response->is_error() ) {
497
			// if the remove post fails, use javascript instead
498
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
499
		}
500
	}
501
502
	/**
503
	 * @since 3.0
504
	 */
505
	public static function api_install() {
506
		delete_transient( 'frm_updating_api' );
507
		if ( self::needs_update() ) {
508
			$running = get_option( 'frm_install_running' );
509
			if ( false === $running || $running < strtotime( '-5 minutes' ) ) {
510
				update_option( 'frm_install_running', time(), 'no' );
511
				self::install();
512
				delete_option( 'frm_install_running' );
513
			}
514
		}
515
516
		return true;
517
	}
518
519
	/**
520
	 * Silent database upgrade (no redirect).
521
	 * Called via ajax request during network upgrade process.
522
	 *
523
	 * @since 2.0.1
524
	 */
525
	public static function ajax_install() {
526
		self::api_install();
527
		wp_die();
528
	}
529
530
	public static function install() {
531
		$frmdb = new FrmMigrate();
532
		$frmdb->upgrade();
533
	}
534
535
	public static function uninstall() {
536
		FrmAppHelper::permission_check( 'administrator' );
537
		check_ajax_referer( 'frm_ajax', 'nonce' );
538
539
		$frmdb = new FrmMigrate();
540
		$frmdb->uninstall();
541
542
		//disable the plugin and redirect after uninstall so the tables don't get added right back
543
		$plugins = array( FrmAppHelper::plugin_folder() . '/formidable.php', 'formidable-pro/formidable-pro.php' );
544
		deactivate_plugins( $plugins, false, false );
545
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
546
547
		wp_die();
548
	}
549
550
	public static function drop_tables( $tables ) {
551
		global $wpdb;
552
		$tables[] = $wpdb->prefix . 'frm_fields';
553
		$tables[] = $wpdb->prefix . 'frm_forms';
554
		$tables[] = $wpdb->prefix . 'frm_items';
555
		$tables[] = $wpdb->prefix . 'frm_item_metas';
556
557
		return $tables;
558
	}
559
560
	public static function deauthorize() {
561
		FrmAppHelper::permission_check( 'frm_change_settings' );
562
		check_ajax_referer( 'frm_ajax', 'nonce' );
563
564
		delete_option( 'frmpro-credentials' );
565
		delete_option( 'frmpro-authorized' );
566
		delete_site_option( 'frmpro-credentials' );
567
		delete_site_option( 'frmpro-authorized' );
568
		wp_die();
569
	}
570
571
	public static function set_footer_text( $text ) {
572
		if ( FrmAppHelper::is_formidable_admin() ) {
573
			$text = '';
574
		}
575
576
		return $text;
577
	}
578
579
	/**
580
	 * @deprecated 1.07.05
581
	 * @codeCoverageIgnore
582
	 */
583
	public static function get_form_shortcode( $atts ) {
584
		return FrmDeprecated::get_form_shortcode( $atts );
585
	}
586
587
	/**
588
	 * @deprecated 2.5.4
589
	 * @codeCoverageIgnore
590
	 */
591
	public static function widget_text_filter( $content ) {
592
		return FrmDeprecated::widget_text_filter( $content );
593
	}
594
595
	/**
596
	 * Deprecated in favor of wpmu_upgrade_site
597
	 *
598
	 * @deprecated 2.3
599
	 * @codeCoverageIgnore
600
	 */
601
	public static function front_head() {
602
		FrmDeprecated::front_head();
603
	}
604
605
	/**
606
	 * @deprecated 3.0.04
607
	 * @codeCoverageIgnore
608
	 */
609
	public static function activation_install() {
610
		FrmDeprecated::activation_install();
611
	}
612
613
	/**
614
	 * @deprecated 3.0
615
	 * @codeCoverageIgnore
616
	 */
617
	public static function page_route( $content ) {
618
		return FrmDeprecated::page_route( $content );
619
	}
620
}
621