Completed
Push — master ( 4ef3e4...2cdabf )
by Stephanie
03:15
created

FrmAppController::network_upgrade_site()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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