Completed
Pull Request — master (#165)
by Stephanie
02:25
created

FrmAppController::get_notice_count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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