Completed
Push — master ( 1466b8...74a3d9 )
by Stephanie
14s
created

FrmAppController::install_js_fallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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