Completed
Push — master ( fff5c6...454e54 )
by Stephanie
05:25
created

FrmAppController::pro_get_started_headline()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

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