Completed
Push — master ( e46f5a...6a21ee )
by Stephanie
03:04
created

FrmAppController::get_menu_position()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 10
rs 9.4285
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
		$count = count( get_post_types( array(
17
			'show_ui'      => true,
18
			'_builtin'     => false,
19
			'show_in_menu' => true,
20
		) ) );
21
		$pos = $count ? '22.7' : '29.3';
22
		$pos = apply_filters( 'frm_menu_position', $pos );
23
		return $pos;
24
	}
25
26
	/**
27
	 * @since 3.0
28
	 */
29
	public static function add_admin_class( $classes ) {
30
		if ( self::is_white_page() ) {
31
			$classes .= ' frm-white-body ';
32
		}
33
		return $classes;
34
	}
35
36
	/**
37
	 * @since 3.0
38
	 */
39
	private static function is_white_page() {
40
		$is_white_page = ( FrmAppHelper::is_admin_page( 'formidable' ) || FrmAppHelper::is_admin_page( 'formidable-entries' ) || FrmAppHelper::is_admin_page( 'formidable-pro-upgrade' ) );
41
		if ( ! $is_white_page ) {
42
			$screen = get_current_screen();
43
			$is_white_page = ( $screen && $screen->id === 'edit-frm_display' );
44
		}
45
46
		return $is_white_page;
47
	}
48
49
    public static function load_wp_admin_style() {
50
        FrmAppHelper::load_font_style();
51
    }
52
53
	public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
54
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
55
        if ( empty( $show_nav ) || ! $form ) {
56
            return;
57
        }
58
59
		FrmForm::maybe_get_form( $form );
60
		if ( ! is_object( $form ) ) {
61
			return;
62
		}
63
64
		$id = $form->id;
65
		$current_page = self::get_current_page();
66
		$nav_items = self::get_form_nav_items( $form );
67
68
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
69
	}
70
71
	private static function get_current_page() {
72
		global $pagenow;
73
74
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
75
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
76
		$current_page = isset( $_GET['page'] ) ? $page : $post_type;
77
		if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) {
78
			$current_page = 'frm_display';
79
		}
80
81
		return $current_page;
82
	}
83
84
	private static function get_form_nav_items( $form ) {
85
		$id = $form->parent_form_id ? $form->parent_form_id : $form->id;
86
87
		$nav_items = array(
88
			array(
89
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $id ) ),
90
				'label'   => __( 'Build', 'formidable' ),
91
				'current' => array( 'edit', 'new', 'duplicate' ),
92
				'page'    => 'formidable',
93
				'permission' => 'frm_edit_forms',
94
			),
95
			array(
96
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
97
				'label'   => __( 'Settings', 'formidable' ),
98
				'current' => array( 'settings' ),
99
				'page'    => 'formidable',
100
				'permission' => 'frm_edit_forms',
101
			),
102
			array(
103
				'link'    => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ),
104
				'label'   => __( 'Entries', 'formidable' ),
105
				'current' => array(),
106
				'page'    => 'formidable-entries',
107
				'permission' => 'frm_view_entries',
108
			),
109
		);
110
111
		$nav_items = apply_filters( 'frm_form_nav_list', $nav_items, array(
112
			'form_id' => $id,
113
			'form'    => $form,
114
		) );
115
		return $nav_items;
116
	}
117
118
    // Adds a settings link to the plugins page
119
    public static function settings_link( $links ) {
120
		$settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable' ) ) . '">' . __( 'Build a Form', 'formidable' ) . '</a>';
121
        array_unshift( $links, $settings );
122
123
        return $links;
124
    }
125
126
    public static function pro_get_started_headline() {
127
		self::maybe_show_upgrade_bar();
128
129
        // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message
130
        if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) {
131
            return;
132
        }
133
134
		$pro_installed = is_dir( WP_PLUGIN_DIR . '/formidable-pro' );
135
136
		if ( get_site_option( 'frmpro-authorized' ) && ! is_callable( 'load_formidable_pro' ) ) {
137
			FrmAppHelper::load_admin_wide_js();
138
139
			// user is authorized, but running free version
140
141
			if ( $pro_installed ) {
142
				// if pro version is installed, include link to activate it
143
				$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' );
144
			} else {
145
				$inst_install_url = 'https://formidableforms.com/knowledgebase/install-formidable-forms/';
146
			}
147
        ?>
148
<div class="error" class="frm_previous_install">
149
		<?php
150
		echo apply_filters( 'frm_pro_update_msg',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
151
			sprintf(
152
				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' ),
153
				'<br/><a href="' . esc_url( $inst_install_url ) . '" target="_blank">', '</a>',
154
				'<a href="#" class="frm_deauthorize_link">', '</a>'
155
			), esc_url( $inst_install_url )
156
		);
157
		?>
158
</div>
159
<?php
160
        }
161
    }
162
163
	private static function maybe_show_upgrade_bar() {
164
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
165
		if ( strpos( $page, 'formidable' ) !== 0 ) {
166
			return;
167
		}
168
169
		if ( FrmAppHelper::pro_is_installed() ) {
170
			return;
171
		}
172
173
		$affiliate = FrmAppHelper::get_affiliate();
174
		if ( ! empty( $affiliate ) ) {
175
			$tip = FrmTipsHelper::get_banner_tip();
176
?>
177
<div class="update-nag frm-update-to-pro">
178
	<?php echo FrmAppHelper::kses( $tip['tip'] ); ?>
179
	<span><?php echo FrmAppHelper::kses( $tip['call'] ); ?></span>
180
	<a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url('https://formidableforms.com?banner=1&tip=' . absint( $tip['num'] ) ) ); ?>" class="button">Upgrade to Pro</a>
181
</div>
182
<?php
183
		}
184
	}
185
186
	/**
187
	 * If there are CURL problems on this server, wp_remote_post won't work for installing
188
	 * Use a javascript fallback instead.
189
	 *
190
	 * @since 2.0.3
191
	 */
192
	public static function install_js_fallback() {
193
		FrmAppHelper::load_admin_wide_js();
194
		echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>';
195
	}
196
197
	/**
198
	 * Check if the database is outdated
199
	 *
200
	 * @since 2.0.1
201
	 * @return boolean
202
	 */
203
	public static function needs_update() {
204
		$needs_upgrade = self::compare_for_update( array(
205
			'option'             => 'frm_db_version',
206
			'new_db_version'     => FrmAppHelper::$db_version,
207
			'new_plugin_version' => FrmAppHelper::plugin_version(),
208
		) );
209
210
		if ( ! $needs_upgrade ) {
211
			$needs_upgrade = apply_filters( 'frm_db_needs_upgrade', $needs_upgrade );
212
		}
213
		return $needs_upgrade;
214
	}
215
216
	/**
217
	 * Check both version number and DB number for changes
218
	 *
219
	 * @since 3.0.04
220
	 */
221
	public static function compare_for_update( $atts ) {
222
		$needs_upgrade = false;
0 ignored issues
show
Unused Code introduced by
$needs_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
		$db_version = get_option( $atts['option'] );
224
225
		if ( strpos( $db_version, '-' ) === false ) {
226
			$needs_upgrade = true;
227
		} else {
228
			$last_upgrade = explode( '-', $db_version );
229
			$needs_db_upgrade = (int) $last_upgrade[1] < (int) $atts['new_db_version'];
230
			$new_version = version_compare( $last_upgrade[0], $atts['new_plugin_version'], '<' );
231
			$needs_upgrade = $needs_db_upgrade || $new_version;
232
		}
233
234
		return $needs_upgrade;
235
	}
236
237
	/**
238
	 * Check for database update and trigger js loading
239
	 *
240
	 * @since 2.0.1
241
	 */
242
	public static function admin_init() {
243
		if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
244
			self::network_upgrade_site();
245
		}
246
247
		$action = FrmAppHelper::simple_get( 'action', 'sanitize_title' );
248
		if ( ! FrmAppHelper::doing_ajax() || $action == 'frm_import_choices' ) {
249
			// don't continue during ajax calls
250
			self::load_tour();
251
			self::admin_js();
252
		}
253
	}
254
255
	/**
256
	 * See if we should start our tour.
257
	 * @since 2.0.20
258
	 */
259
	private static function load_tour() {
260
		$restart_tour = filter_input( INPUT_GET, 'frm_restart_tour' );
261
		if ( $restart_tour ) {
262
			delete_user_meta( get_current_user_id(), 'frm_ignore_tour' );
263
		}
264
		self::ignore_tour();
265
266
		if ( ! self::has_ignored_tour() ) {
267
			add_action( 'admin_enqueue_scripts', array( 'FrmPointers', 'get_instance' ) );
268
		}
269
	}
270
271
	/**
272
	 * Returns the value of the ignore tour.
273
	 *
274
	 * @return bool
275
	 */
276
	private static function has_ignored_tour() {
277
		$user_meta = get_user_meta( get_current_user_id(), 'frm_ignore_tour' );
278
279
		return ! empty( $user_meta );
280
	}
281
282
	/**
283
	 * Listener for the ignore tour GET value. If this one is set, just set the user meta to true.
284
	 */
285
	private static function ignore_tour() {
286
		if ( filter_input( INPUT_GET, 'frm_ignore_tour' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'frm-ignore-tour' ) ) {
287
			update_user_meta( get_current_user_id(), 'frm_ignore_tour', true );
288
		}
289
	}
290
291
    public static function admin_js() {
292
		$version = FrmAppHelper::plugin_version();
293
		FrmAppHelper::load_admin_wide_js( false );
294
295
		wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', array(
296
			'formidable_admin_global',
297
			'formidable',
298
			'jquery',
299
			'jquery-ui-core',
300
			'jquery-ui-draggable',
301
			'jquery-ui-sortable',
302
			'bootstrap_tooltip',
303
			'bootstrap-multiselect',
304
		), $version, true );
305
		wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version );
306
        wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' );
307
		wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version );
308
309
		// load multselect js
310
		wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip' ), '0.9.8', true );
311
312
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
313
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
314
315
		global $pagenow;
316
		if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) {
317
318
            wp_enqueue_script( 'admin-widgets' );
319
            wp_enqueue_style( 'widgets' );
320
            wp_enqueue_script( 'formidable' );
321
            wp_enqueue_script( 'formidable_admin' );
322
			FrmAppHelper::localize_script( 'admin' );
323
324
            wp_enqueue_style( 'formidable-admin' );
325
			wp_enqueue_style( 'formidable-grids' );
326
			wp_enqueue_style( 'formidable-dropzone' );
327
            add_thickbox();
328
329
            wp_register_script( 'formidable-editinplace', FrmAppHelper::plugin_url() . '/js/jquery/jquery.editinplace.packed.js', array( 'jquery' ), '2.3.0' );
330
331
			do_action( 'frm_enqueue_builder_scripts' );
332
        } else if ( $pagenow == 'post.php' || ( $pagenow == 'post-new.php' && $post_type == 'frm_display' ) ) {
333
            if ( isset( $_REQUEST['post_type'] ) ) {
334
                $post_type = sanitize_title( $_REQUEST['post_type'] );
335
			} else if ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
336
				$post = get_post( absint( $_REQUEST['post'] ) );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
337
                if ( ! $post ) {
338
                    return;
339
                }
340
                $post_type = $post->post_type;
341
            } else {
342
                return;
343
            }
344
345
            if ( $post_type == 'frm_display' ) {
346
                wp_enqueue_script( 'jquery-ui-draggable' );
347
                wp_enqueue_script( 'formidable_admin' );
348
                wp_enqueue_style( 'formidable-admin' );
349
				FrmAppHelper::localize_script( 'admin' );
350
            }
351
        } else if ( $pagenow == 'widgets.php' ) {
352
            FrmAppHelper::load_admin_wide_js();
353
        }
354
    }
355
356
    public static function load_lang() {
357
        load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
358
    }
359
360
    /**
361
     * Filter shortcodes in text widgets
362
     */
363
	public static function widget_text_filter( $content ) {
364
		_deprecated_function( __METHOD__, '2.5.4' );
365
		$regex = '/\[\s*(formidable|display-frm-data|frm-stats|frm-graph|frm-entry-links|formresults|frm-search)\s+.*\]/';
366
		return preg_replace_callback( $regex, 'FrmAppHelper::widget_text_filter_callback', $content );
367
	}
368
369
	/**
370
	 * Deprecated in favor of wpmu_upgrade_site
371
	 */
372
	public static function front_head() {
373
		_deprecated_function( __FUNCTION__, '2.3' );
374
		if ( is_multisite() && self::needs_update() ) {
375
			self::install();
376
		}
377
	}
378
379
	/**
380
	 * Check if the styles are updated when a form is loaded on the front-end
381
	 *
382
	 * @since 3.0.1
383
	 */
384
	public static function maybe_update_styles() {
385
		if ( self::needs_update() ) {
386
			self::network_upgrade_site();
387
		}
388
	}
389
390
	/**
391
	 * @since 3.0
392
	 */
393
	public static function create_rest_routes() {
394
		register_rest_route( 'frm-admin/v1', '/install', array(
395
			'methods'  => 'GET',
396
			'callback' => 'FrmAppController::api_install',
397
		) );
398
	}
399
400
	/**
401
	 * Run silent upgrade on each site in the network during a network upgrade.
402
	 * Update database settings for all sites in a network during network upgrade process.
403
	 *
404
	 * @since 2.0.1
405
	 *
406
	 * @param int $blog_id Blog ID.
407
	 */
408
	public static function network_upgrade_site( $blog_id = 0 ) {
409
410
		$request = new WP_REST_Request( 'GET', '/frm-admin/v1/install' );
411
412
		if ( $blog_id ) {
413
			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...
414
			$response = rest_do_request( $request );
415
			restore_current_blog();
416
		} else {
417
			$response = rest_do_request( $request );
418
		}
419
420
		if ( $response->is_error() ) {
421
			// if the remove post fails, use javascript instead
422
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
423
		}
424
	}
425
426
	/**
427
	 * @since 3.0
428
	 */
429
	public static function api_install() {
430
		if ( self::needs_update() ) {
431
			$running = get_option( 'frm_install_running' );
432
			if ( false === $running ) {
433
				update_option( 'frm_install_running', true );
434
				self::install();
435
				delete_option( 'frm_install_running' );
436
			}
437
		}
438
		return true;
439
	}
440
441
	/**
442
	 * Silent database upgrade (no redirect).
443
	 * Called via ajax request during network upgrade process.
444
	 *
445
	 * @since 2.0.1
446
	 */
447
	public static function ajax_install() {
448
		self::api_install();
449
		wp_die();
450
	}
451
452
    public static function activation_install() {
453
		_deprecated_function( __METHOD__, '3.0.04', 'FrmAppController::install' );
454
        FrmDb::delete_cache_and_transient( 'frm_plugin_version' );
455
        FrmFormActionsController::actions_init();
456
        self::install();
457
    }
458
459
    public static function install( $old_db_version = false ) {
460
        $frmdb = new FrmMigrate();
461
        $frmdb->upgrade( $old_db_version );
462
    }
463
464
    public static function uninstall() {
465
		FrmAppHelper::permission_check('administrator');
466
        check_ajax_referer( 'frm_ajax', 'nonce' );
467
468
		$frmdb = new FrmMigrate();
469
		$frmdb->uninstall();
470
471
		//disable the plugin and redirect after uninstall so the tables don't get added right back
472
		deactivate_plugins( FrmAppHelper::plugin_folder() . '/formidable.php', false, false );
473
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
474
475
        wp_die();
476
    }
477
478
    public static function drop_tables( $tables ) {
479
        global $wpdb;
480
        $tables[] = $wpdb->prefix . 'frm_fields';
481
        $tables[] = $wpdb->prefix . 'frm_forms';
482
        $tables[] = $wpdb->prefix . 'frm_items';
483
        $tables[] = $wpdb->prefix . 'frm_item_metas';
484
        return $tables;
485
    }
486
487
    // Routes for wordpress pages -- we're just replacing content here folks.
488
	public static function page_route( $content ) {
489
		_deprecated_function( __METHOD__, '3.0' );
490
		global $post;
491
492
		if ( $post && isset( $_GET['form'] ) ) {
493
			$content = FrmFormsController::page_preview();
494
		}
495
496
		return $content;
497
	}
498
499
    public static function deauthorize() {
500
		FrmAppHelper::permission_check('frm_change_settings');
501
        check_ajax_referer( 'frm_ajax', 'nonce' );
502
503
        delete_option( 'frmpro-credentials' );
504
        delete_option( 'frmpro-authorized' );
505
        delete_site_option( 'frmpro-credentials' );
506
        delete_site_option( 'frmpro-authorized' );
507
        wp_die();
508
    }
509
510
	public static function set_footer_text( $text ) {
511
		if ( FrmAppHelper::is_formidable_admin() ) {
512
			$text = sprintf(
513
				__( 'Help us spread the %1$sFormidable Forms%2$s love with %3$s %5$s on WordPress.org%4$s. Thank you heaps!', 'formidable' ),
514
				'<a href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidableforms.com' ) ) . '" target="_blank">',
515
				'</a>',
516
				'<a href="https://wordpress.org/support/plugin/formidable/reviews/?filter=5#new-post" target="_blank">',
517
				'</a>',
518
				'&#9733;&#9733;&#9733;&#9733;&#9733;'
519
			);
520
			$text = '<span id="footer-thankyou">' . $text . '</span>';
521
		}
522
		return $text;
523
	}
524
525
    public static function get_form_shortcode( $atts ) {
526
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form_shortcode()' );
527
        return FrmFormsController::get_form_shortcode( $atts );
528
    }
529
}
530