Completed
Push — master ( 46b435...c317c9 )
by Stephanie
02:52
created

FrmAppController::maybe_show_upgrade_bar()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 18
rs 9.6666
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.04.04
21
	 */
22
	private static function menu_icon() {
23
		$icon = FrmAppHelper::svg_logo( array( 'fill' => '#a0a5aa', 'orange' => '#a0a5aa' ) );
24
		$icon = 'data:image/svg+xml;base64,' . base64_encode( $icon );
25
		return apply_filters( 'frm_icon', $icon );
26
	}
27
28
	/**
29
	 * @since 3.0
30
	 */
31
	public static function add_admin_class( $classes ) {
32
		if ( self::is_white_page() ) {
33
			$classes .= ' frm-white-body ';
34
		}
35
		return $classes;
36
	}
37
38
	/**
39
	 * @since 3.0
40
	 */
41
	private static function is_white_page() {
42
		$is_white_page = ( FrmAppHelper::is_admin_page( 'formidable' ) || FrmAppHelper::is_admin_page( 'formidable-entries' ) || FrmAppHelper::is_admin_page( 'formidable-pro-upgrade' ) || FrmAppHelper::is_admin_page( 'formidable-addons' ) );
43
		if ( ! $is_white_page ) {
44
			$screen = get_current_screen();
45
			$is_white_page = ( $screen && $screen->id === 'edit-frm_display' );
46
		}
47
48
		return $is_white_page;
49
	}
50
51
    public static function load_wp_admin_style() {
52
        FrmAppHelper::load_font_style();
53
    }
54
55
	public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
56
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
57
        if ( empty( $show_nav ) || ! $form ) {
58
            return;
59
        }
60
61
		FrmForm::maybe_get_form( $form );
62
		if ( ! is_object( $form ) ) {
63
			return;
64
		}
65
66
		$id = $form->id;
67
		$current_page = self::get_current_page();
68
		$nav_items = self::get_form_nav_items( $form );
69
70
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
71
	}
72
73
	private static function get_current_page() {
74
		global $pagenow;
75
76
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
77
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
78
		$current_page = isset( $_GET['page'] ) ? $page : $post_type;
79
		if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) {
80
			$current_page = 'frm_display';
81
		}
82
83
		return $current_page;
84
	}
85
86
	private static function get_form_nav_items( $form ) {
87
		$id = $form->parent_form_id ? $form->parent_form_id : $form->id;
88
89
		$nav_items = array(
90
			array(
91
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $id ) ),
92
				'label'   => __( 'Build', 'formidable' ),
93
				'current' => array( 'edit', 'new', 'duplicate' ),
94
				'page'    => 'formidable',
95
				'permission' => 'frm_edit_forms',
96
			),
97
			array(
98
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
99
				'label'   => __( 'Settings', 'formidable' ),
100
				'current' => array( 'settings' ),
101
				'page'    => 'formidable',
102
				'permission' => 'frm_edit_forms',
103
			),
104
			array(
105
				'link'    => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ),
106
				'label'   => __( 'Entries', 'formidable' ),
107
				'current' => array(),
108
				'page'    => 'formidable-entries',
109
				'permission' => 'frm_view_entries',
110
			),
111
		);
112
113
		$nav_args = array(
114
			'form_id' => $id,
115
			'form'    => $form,
116
		);
117
		return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args );
118
	}
119
120
    // Adds a settings link to the plugins page
121
    public static function settings_link( $links ) {
122
		$settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>';
123
        array_unshift( $links, $settings );
124
125
        return $links;
126
    }
127
128
    public static function pro_get_started_headline() {
129
		self::maybe_show_upgrade_bar();
130
		self::review_request();
131
132
        // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message
133
        if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) {
134
            return;
135
        }
136
137
		$pro_installed = is_dir( WP_PLUGIN_DIR . '/formidable-pro' );
138
		$authorized    = get_site_option( 'frmpro-authorized' ) && ! is_callable( 'load_formidable_pro' );
139
		if ( ! $authorized ) {
140
			return;
141
		}
142
143
		FrmAppHelper::load_admin_wide_js();
144
145
		// user is authorized, but running free version
146
		$download_url = '';
147
		if ( $pro_installed ) {
148
			// if pro version is installed, include link to activate it
149
			$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' );
150
		} else {
151
			$inst_install_url = '#';
152
			$download_url = FrmAddonsController::get_pro_download_url();
153
154
			if ( empty( $download_url ) ) {
155
				$inst_install_url = 'https://formidableforms.com/knowledgebase/install-formidable-forms/?utm_source=WordPress&utm_medium=get-started&utm_campaign=liteplugin';
156
			}
157
		}
158
        ?>
159
<div class="error frm_previous_install">
160
		<?php
161
		echo apply_filters( // WPCS: XSS ok.
162
			'frm_pro_update_msg',
163
			sprintf(
164
				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' ),
165
				'<br/><a href="' . esc_url( $inst_install_url ) . '" id="frm_install_link" target="_blank" data-prourl="' . esc_url( $download_url ) . '">',
166
				'</a>',
167
				'<a href="#" class="frm_deauthorize_link">',
168
				'</a>'
169
			),
170
			esc_url( $inst_install_url )
171
		);
172
		?>
173
	<div id="frm_install_message" class="hidden frm_hidden"></div>
174
</div>
175
<?php
176
    }
177
178
	private static function maybe_show_upgrade_bar() {
179
		if ( ! FrmAppHelper::is_formidable_admin() || FrmAppHelper::pro_is_installed() ) {
180
			return;
181
		}
182
183
		$affiliate = FrmAppHelper::get_affiliate();
184
		if ( ! empty( $affiliate ) ) {
185
			$tip = FrmTipsHelper::get_banner_tip();
186
			$link = FrmAppHelper::admin_upgrade_link( 'banner' );
187
?>
188
<div class="update-nag frm-update-to-pro">
189
	<?php echo FrmAppHelper::kses( $tip['tip'] ); // WPCS: XSS ok. ?>
190
	<span><?php echo FrmAppHelper::kses( $tip['call'] ); // WPCS: XSS ok. ?></span>
191
	<a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url( $link ) ); ?>" class="button">Upgrade to Pro</a>
192
</div>
193
<?php
194
		}
195
	}
196
197
	/**
198
	 * Add admin notices as needed for reviews
199
	 *
200
	 * @since 3.04.03
201
	 */
202
	private static function review_request() {
203
		$reviews = new FrmReviews();
204
		$reviews->review_request();
205
	}
206
207
	/**
208
	 * Save the request to hide the review
209
	 *
210
	 * @since 3.04.03
211
	 */
212
	public static function dismiss_review() {
213
		FrmAppHelper::permission_check( 'frm_change_settings' );
214
		check_ajax_referer( 'frm_ajax', 'nonce' );
215
216
		$reviews = new FrmReviews();
217
		$reviews->dismiss_review();
218
	}
219
220
	/**
221
	 * @since 3.04.02
222
	 */
223
	public static function include_upgrade_overlay() {
224
		$is_pro = FrmAppHelper::pro_is_installed();
225
		wp_enqueue_script( 'jquery-ui-dialog' );
226
		wp_enqueue_style( 'jquery-ui-dialog' );
227
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/upgrade_overlay.php' );
228
	}
229
230
	/**
231
	 * @since 3.04.02
232
	 */
233
	public static function remove_upsells() {
234
		remove_action( 'frm_before_settings', 'FrmSettingsController::license_box' );
235
		remove_action( 'frm_after_settings', 'FrmSettingsController::settings_cta' );
236
	}
237
238
	/**
239
	 * Don't nag people to install WPForms
240
	 *
241
	 * @since 3.04.04
242
	 */
243
	public static function remove_wpforms_nag( $upsell ) {
244
		if ( is_array( $upsell ) ) {
245
			foreach ( $upsell as $k => $plugin ) {
246
				if ( strpos( $plugin['slug'], 'wpforms' ) !== false ) {
247
					unset( $upsell[ $k ] );
248
				}
249
			}
250
		}
251
		return $upsell;
252
	}
253
254
	/**
255
	 * If there are CURL problems on this server, wp_remote_post won't work for installing
256
	 * Use a javascript fallback instead.
257
	 *
258
	 * @since 2.0.3
259
	 */
260
	public static function install_js_fallback() {
261
		FrmAppHelper::load_admin_wide_js();
262
		echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>';
263
	}
264
265
	/**
266
	 * Check if the database is outdated
267
	 *
268
	 * @since 2.0.1
269
	 * @return boolean
270
	 */
271
	public static function needs_update() {
272
		$needs_upgrade = self::compare_for_update(
273
			array(
274
				'option'             => 'frm_db_version',
275
				'new_db_version'     => FrmAppHelper::$db_version,
276
				'new_plugin_version' => FrmAppHelper::plugin_version(),
277
			)
278
		);
279
280
		if ( ! $needs_upgrade ) {
281
			$needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade );
282
		}
283
		return $needs_upgrade;
284
	}
285
286
	/**
287
	 * Check both version number and DB number for changes
288
	 *
289
	 * @since 3.0.04
290
	 */
291
	public static function compare_for_update( $atts ) {
292
		$db_version = get_option( $atts['option'] );
293
294
		if ( strpos( $db_version, '-' ) === false ) {
295
			$needs_upgrade = true;
296
		} else {
297
			$last_upgrade = explode( '-', $db_version );
298
			$needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version'];
299
			$new_version = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' );
300
			$needs_upgrade = $needs_db_upgrade || $new_version;
301
		}
302
303
		return $needs_upgrade;
304
	}
305
306
	/**
307
	 * Check for database update and trigger js loading
308
	 *
309
	 * @since 2.0.1
310
	 */
311
	public static function admin_init() {
312
		new FrmPersonalData(); // register personal data hooks
313
314
		if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
315
			self::network_upgrade_site();
316
		}
317
318
		$action = FrmAppHelper::simple_get( 'action', 'sanitize_title' );
319
		if ( ! FrmAppHelper::doing_ajax() || $action == 'frm_import_choices' ) {
320
			// don't continue during ajax calls
321
			self::admin_js();
322
		}
323
	}
324
325
    public static function admin_js() {
326
		$version = FrmAppHelper::plugin_version();
327
		FrmAppHelper::load_admin_wide_js( false );
328
329
		$dependecies = array(
330
			'formidable_admin_global',
331
			'formidable',
332
			'jquery',
333
			'jquery-ui-core',
334
			'jquery-ui-draggable',
335
			'jquery-ui-sortable',
336
			'bootstrap_tooltip',
337
			'bootstrap-multiselect',
338
		);
339
340
		if ( FrmAppHelper::is_admin_page( 'formidable-styles' ) ) {
341
			$dependecies[] = 'wp-color-picker';
342
		}
343
344
		wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', $dependecies, $version, true );
345
		wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version );
346
        wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' );
347
		wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version );
348
349
		// load multselect js
350
		wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip' ), '0.9.8', true );
351
352
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
353
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
354
355
		global $pagenow;
356
		if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) {
357
358
            wp_enqueue_script( 'admin-widgets' );
359
            wp_enqueue_style( 'widgets' );
360
            wp_enqueue_script( 'formidable' );
361
            wp_enqueue_script( 'formidable_admin' );
362
			FrmAppHelper::localize_script( 'admin' );
363
364
			wp_enqueue_style( 'formidable-admin' );
365
			if ( 'formidable-styles' !== $page ) {
366
				wp_enqueue_style( 'formidable-grids' );
367
				wp_enqueue_style( 'formidable-dropzone' );
368
				add_thickbox();
369
			} else {
370
				$settings = FrmAppHelper::get_settings();
371
				if ( empty( $settings->old_css ) ) {
372
					wp_enqueue_style( 'formidable-grids' );
373
				}
374
			}
375
376
            wp_register_script( 'formidable-editinplace', FrmAppHelper::plugin_url() . '/js/jquery/jquery.editinplace.packed.js', array( 'jquery' ), '2.3.0' );
377
378
			do_action( 'frm_enqueue_builder_scripts' );
379
        } else if ( $pagenow == 'post.php' || ( $pagenow == 'post-new.php' && $post_type == 'frm_display' ) ) {
380
            if ( isset( $_REQUEST['post_type'] ) ) {
381
                $post_type = sanitize_title( $_REQUEST['post_type'] );
382
			} else if ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
383
				$post = get_post( absint( $_REQUEST['post'] ) );
384
                if ( ! $post ) {
385
                    return;
386
                }
387
                $post_type = $post->post_type;
388
            } else {
389
                return;
390
            }
391
392
            if ( $post_type == 'frm_display' ) {
393
                wp_enqueue_script( 'jquery-ui-draggable' );
394
                wp_enqueue_script( 'formidable_admin' );
395
                wp_enqueue_style( 'formidable-admin' );
396
				FrmAppHelper::localize_script( 'admin' );
397
            }
398
        } else if ( $pagenow == 'widgets.php' ) {
399
            FrmAppHelper::load_admin_wide_js();
400
        }
401
    }
402
403
    public static function load_lang() {
404
        load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
405
    }
406
407
	/**
408
	 * Check if the styles are updated when a form is loaded on the front-end
409
	 *
410
	 * @since 3.0.1
411
	 */
412
	public static function maybe_update_styles() {
413
		if ( self::needs_update() ) {
414
			self::network_upgrade_site();
415
		}
416
	}
417
418
	/**
419
	 * @since 3.0
420
	 */
421
	public static function create_rest_routes() {
422
		$args = array(
423
			'methods'  => 'GET',
424
			'callback' => 'FrmAppController::api_install',
425
		);
426
		register_rest_route( 'frm-admin/v1', '/install', $args );
427
	}
428
429
	/**
430
	 * Run silent upgrade on each site in the network during a network upgrade.
431
	 * Update database settings for all sites in a network during network upgrade process.
432
	 *
433
	 * @since 2.0.1
434
	 *
435
	 * @param int $blog_id Blog ID.
436
	 */
437
	public static function network_upgrade_site( $blog_id = 0 ) {
438
439
		$request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' );
440
441
		if ( $blog_id ) {
442
			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...
443
			$response = rest_do_request( $request );
444
			restore_current_blog();
445
		} else {
446
			$response = rest_do_request( $request );
447
		}
448
449
		if ( $response->is_error() ) {
450
			// if the remove post fails, use javascript instead
451
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
452
		}
453
	}
454
455
	/**
456
	 * @since 3.0
457
	 */
458
	public static function api_install() {
459
		if ( self::needs_update() ) {
460
			$running = get_option( 'frm_install_running' );
461
			if ( false === $running || $running < strtotime( '-5 minutes' ) ) {
462
				update_option( 'frm_install_running', time(), 'no' );
463
				self::install();
464
				delete_option( 'frm_install_running' );
465
			}
466
		}
467
		return true;
468
	}
469
470
	/**
471
	 * Silent database upgrade (no redirect).
472
	 * Called via ajax request during network upgrade process.
473
	 *
474
	 * @since 2.0.1
475
	 */
476
	public static function ajax_install() {
477
		self::api_install();
478
		wp_die();
479
	}
480
481
    public static function install() {
482
        $frmdb = new FrmMigrate();
483
        $frmdb->upgrade();
484
    }
485
486
    public static function uninstall() {
487
		FrmAppHelper::permission_check( 'administrator' );
488
        check_ajax_referer( 'frm_ajax', 'nonce' );
489
490
		$frmdb = new FrmMigrate();
491
		$frmdb->uninstall();
492
493
		//disable the plugin and redirect after uninstall so the tables don't get added right back
494
		deactivate_plugins( FrmAppHelper::plugin_folder() . '/formidable.php', false, false );
495
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
496
497
        wp_die();
498
    }
499
500
    public static function drop_tables( $tables ) {
501
        global $wpdb;
502
        $tables[] = $wpdb->prefix . 'frm_fields';
503
        $tables[] = $wpdb->prefix . 'frm_forms';
504
        $tables[] = $wpdb->prefix . 'frm_items';
505
        $tables[] = $wpdb->prefix . 'frm_item_metas';
506
        return $tables;
507
    }
508
509
    public static function deauthorize() {
510
		FrmAppHelper::permission_check( 'frm_change_settings' );
511
        check_ajax_referer( 'frm_ajax', 'nonce' );
512
513
        delete_option( 'frmpro-credentials' );
514
        delete_option( 'frmpro-authorized' );
515
        delete_site_option( 'frmpro-credentials' );
516
        delete_site_option( 'frmpro-authorized' );
517
        wp_die();
518
    }
519
520
	public static function set_footer_text( $text ) {
521
		if ( FrmAppHelper::is_formidable_admin() ) {
522
			$link = FrmAppHelper::admin_upgrade_link( 'footer' );
523
			$text = sprintf(
524
				__( 'Help us spread the %1$sFormidable Forms%2$s love with %3$s %5$s on WordPress.org%4$s. Thank you heaps!', 'formidable' ),
525
				'<a href="' . esc_url( FrmAppHelper::make_affiliate_url( $link ) ) . '" target="_blank">',
526
				'</a>',
527
				'<a href="https://wordpress.org/support/plugin/formidable/reviews/?filter=5#new-post" target="_blank">',
528
				'</a>',
529
				'&#9733;&#9733;&#9733;&#9733;&#9733;'
530
			);
531
			$text = '<span id="footer-thankyou">' . $text . '</span>';
532
		}
533
		return $text;
534
	}
535
536
	/**
537
	 * @deprecated 1.07.05
538
	 * @codeCoverageIgnore
539
	 */
540
    public static function get_form_shortcode( $atts ) {
541
        return FrmDeprecated::get_form_shortcode( $atts );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::get_form_shortcode() has been deprecated with message: 1.07.05

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
542
    }
543
544
	/**
545
	 * @deprecated 2.5.4
546
	 * @codeCoverageIgnore
547
	 */
548
	public static function widget_text_filter( $content ) {
549
		return FrmDeprecated::widget_text_filter( $content );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::widget_text_filter() has been deprecated with message: 2.5.4

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
550
	}
551
552
	/**
553
	 * Deprecated in favor of wpmu_upgrade_site
554
	 *
555
	 * @deprecated 2.3
556
	 * @codeCoverageIgnore
557
	 */
558
	public static function front_head() {
559
		FrmDeprecated::front_head();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::front_head() has been deprecated with message: 2.3

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
560
	}
561
562
563
	/**
564
	 * @deprecated 3.0.04
565
	 * @codeCoverageIgnore
566
	 */
567
	public static function activation_install() {
568
		FrmDeprecated::activation_install();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::activation_install() has been deprecated with message: 3.0.04

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
569
	}
570
571
	/**
572
	 * @deprecated 3.0
573
	 * @codeCoverageIgnore
574
	 */
575
	public static function page_route( $content ) {
576
		return FrmDeprecated::page_route( $content );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::page_route() has been deprecated with message: 3.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
577
	}
578
}
579