Completed
Push — master ( eafc43...486814 )
by Stephanie
04:48 queued 01:05
created

FrmAppController::get_os()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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