Completed
Push — master ( 649509...5121e2 )
by Stephanie
03:07
created

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