Completed
Push — master ( ed722f...d2e1e2 )
by Stephanie
04:24
created

FrmAppController::maybe_show_upgrade_bar()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 22
rs 8.9197
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( 'show_ui' => true, '_builtin' => false, 'show_in_menu' => true ) ) );
17
		$pos = $count ? '22.7' : '29.3';
18
		$pos = apply_filters( 'frm_menu_position', $pos );
19
		return $pos;
20
	}
21
22
    public static function load_wp_admin_style() {
23
        FrmAppHelper::load_font_style();
24
    }
25
26
	public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) {
27
        global $frm_vars;
28
29
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
30
        if ( empty( $show_nav || ! $form ) ) {
31
            return;
32
        }
33
34
		FrmForm::maybe_get_form( $form );
35
		if ( ! is_object( $form ) ) {
36
			return;
37
		}
38
39
		$id = $form->id;
40
		$current_page = self::get_current_page();
41
		$nav_items = self::get_form_nav_items( $form );
42
43
		include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' );
44
	}
45
46
	private static function get_current_page() {
47
		global $pagenow;
48
49
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
50
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' );
51
		$current_page = isset( $_GET['page'] ) ? $page : $post_type;
1 ignored issue
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
52
		if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) {
53
			$current_page = 'frm_display';
54
		}
55
56
		return $current_page;
57
	}
58
59
	private static function get_form_nav_items( $form ) {
60
		$id = $form->parent_form_id ? $form->parent_form_id : $form->id;
61
62
		$nav_items = array(
63
			array(
64
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $id ) ),
65
				'label'   => __( 'Build', 'formidable' ),
66
				'current' => array( 'edit', 'new', 'duplicate' ),
67
				'page'    => 'formidable',
68
				'permission' => 'frm_edit_forms',
69
			),
70
			array(
71
				'link'    => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ),
72
				'label'   => __( 'Settings', 'formidable' ),
73
				'current' => array( 'settings' ),
74
				'page'    => 'formidable',
75
				'permission' => 'frm_edit_forms',
76
			),
77
			array(
78
				'link'    => admin_url( 'admin.php?page=formidable-entries&frm_action=list&form=' . absint( $id ) ),
79
				'label'   => __( 'Entries', 'formidable' ),
80
				'current' => array(),
81
				'page'    => 'formidable-entries',
82
				'permission' => 'frm_view_entries',
83
			),
84
		);
85
86
		$nav_items = apply_filters( 'frm_form_nav_list', $nav_items, array( 'form_id' => $id, 'form' => $form ) );
87
		return $nav_items;
88
	}
89
90
    // Adds a settings link to the plugins page
91
    public static function settings_link( $links ) {
92
		$settings = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings' ) ) . '">' . __( 'Settings', 'formidable' ) . '</a>';
93
        array_unshift( $links, $settings );
94
95
        return $links;
96
    }
97
98
    public static function pro_get_started_headline() {
99
		self::maybe_show_upgrade_bar();
100
101
        // Don't display this error as we're upgrading the thing, or if the user shouldn't see the message
102
        if ( 'upgrade-plugin' == FrmAppHelper::simple_get( 'action', 'sanitize_title' ) || ! current_user_can( 'update_plugins' ) ) {
103
            return;
104
        }
105
106
		if ( get_site_option( 'frmpro-authorized' ) && ! file_exists( FrmAppHelper::plugin_path() . '/pro/formidable-pro.php' ) ) {
107
            FrmAppHelper::load_admin_wide_js();
108
109
            // user is authorized, but running free version
110
            $inst_install_url = 'https://formidablepro.com/knowledgebase/install-formidable-forms/';
111
        ?>
112
<div class="error" class="frm_previous_install">
113
		<?php
114
		echo wp_kses_post( apply_filters( 'frm_pro_update_msg',
115
			sprintf(
116
				__( 'This site has been previously authorized to run Formidable Forms.<br/>%1$sInstall Formidable Pro%2$s or %3$sdeauthorize%4$s this site to continue running the free version and remove this message.', 'formidable' ),
117
				'<a href="' . esc_url( $inst_install_url ) . '" target="_blank">', '</a>',
118
				'<a href="#" class="frm_deauthorize_link">', '</a>'
119
			), esc_url( $inst_install_url )
120
		) ); ?>
121
</div>
122
<?php
123
        }
124
    }
125
126
	private static function maybe_show_upgrade_bar() {
127
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
128
		if ( strpos( $page, 'formidable' ) !== 0 ) {
129
			return;
130
		}
131
132
		if ( FrmAppHelper::pro_is_installed() ) {
133
			return;
134
		}
135
136
		$affiliate = FrmAppHelper::get_affiliate();
137
		if ( ! empty( $affiliate ) ) {
138
			$tip = FrmTipsHelper::get_banner_tip();
139
?>
140
<div class="update-nag frm-update-to-pro">
141
	<?php echo FrmAppHelper::kses( $tip['tip'] ) ?>
142
	<span><?php echo FrmAppHelper::kses( $tip['call'] ) ?></span>
143
	<a href="<?php echo esc_url( FrmAppHelper::make_affiliate_url('https://formidablepro.com?banner=1&tip=' . absint( $tip['num'] ) ) ) ?>" class="button">Upgrade to Pro</a>
144
</div>
145
<?php
146
		}
147
	}
148
149
	/**
150
	 * If there are CURL problems on this server, wp_remote_post won't work for installing
151
	 * Use a javascript fallback instead.
152
	 *
153
	 * @since 2.0.3
154
	 */
155
	public static function install_js_fallback() {
156
		FrmAppHelper::load_admin_wide_js();
157
		echo '<div id="hidden frm_install_message"></div><script type="text/javascript">jQuery(document).ready(function(){frm_install_now();});</script>';
158
	}
159
160
	/**
161
	 * Check if the database is outdated
162
	 *
163
	 * @since 2.0.1
164
	 * @return boolean
165
	 */
166
	public static function needs_update() {
167
		$db_version = (int) get_option( 'frm_db_version' );
168
		$pro_db_version = FrmAppHelper::pro_is_installed() ? get_option( 'frmpro_db_version' ) : false;
169
		return ( ( $db_version < FrmAppHelper::$db_version ) || ( FrmAppHelper::pro_is_installed() && (int) $pro_db_version < FrmAppHelper::$pro_db_version ) );
170
	}
171
172
	/**
173
	 * Check for database update and trigger js loading
174
	 *
175
	 * @since 2.0.1
176
	 */
177
	public static function admin_init() {
178
		if ( ! FrmAppHelper::doing_ajax() && self::needs_update() ) {
179
			self::network_upgrade_site();
180
		}
181
182
		$action = FrmAppHelper::simple_get( 'action', 'sanitize_title' );
183
		if ( ! FrmAppHelper::doing_ajax() || $action == 'frm_import_choices' ) {
184
			// don't continue during ajax calls
185
			self::load_tour();
186
			self::admin_js();
187
		}
188
	}
189
190
	/**
191
	 * See if we should start our tour.
192
	 * @since 2.0.20
193
	 */
194
	private static function load_tour() {
195
		$restart_tour = filter_input( INPUT_GET, 'frm_restart_tour' );
196
		if ( $restart_tour ) {
197
			delete_user_meta( get_current_user_id(), 'frm_ignore_tour' );
1 ignored issue
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
198
		}
199
		self::ignore_tour();
200
201
		if ( ! self::has_ignored_tour() ) {
202
			add_action( 'admin_enqueue_scripts', array( 'FrmPointers', 'get_instance' ) );
203
		}
204
	}
205
206
	/**
207
	 * Returns the value of the ignore tour.
208
	 *
209
	 * @return bool
210
	 */
211
	private static function has_ignored_tour() {
212
		$user_meta = get_user_meta( get_current_user_id(), 'frm_ignore_tour' );
1 ignored issue
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
213
214
		return ! empty( $user_meta );
215
	}
216
217
	/**
218
	 * Listener for the ignore tour GET value. If this one is set, just set the user meta to true.
219
	 */
220
	private static function ignore_tour() {
221
		if ( filter_input( INPUT_GET, 'frm_ignore_tour' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'frm-ignore-tour' ) ) {
222
			update_user_meta( get_current_user_id(), 'frm_ignore_tour', true );
1 ignored issue
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
223
		}
224
	}
225
226
    public static function admin_js() {
227
		$version = FrmAppHelper::plugin_version();
228
		FrmAppHelper::load_admin_wide_js( false );
229
230
		wp_register_script( 'formidable_admin', FrmAppHelper::plugin_url() . '/js/formidable_admin.js', array(
231
			'formidable_admin_global', 'formidable', 'jquery',
232
			'jquery-ui-core', 'jquery-ui-draggable',
233
			'jquery-ui-sortable',
234
			'bootstrap_tooltip', 'bootstrap-multiselect',
235
		), $version, true );
236
		wp_register_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css', array(), $version );
237
        wp_register_script( 'bootstrap_tooltip', FrmAppHelper::plugin_url() . '/js/bootstrap.min.js', array( 'jquery' ), '3.3.4' );
238
		wp_register_style( 'formidable-grids', FrmAppHelper::plugin_url() . '/css/frm_grids.css', array(), $version );
239
		wp_register_style( 'formidable-dropzone', FrmAppHelper::plugin_url() . '/css/dropzone.css', array(), $version );
240
241
		// load multselect js
242
		wp_register_script( 'bootstrap-multiselect', FrmAppHelper::plugin_url() . '/js/bootstrap-multiselect.js', array( 'jquery', 'bootstrap_tooltip' ), '0.9.8', true );
243
244
		$page = FrmAppHelper::simple_get( 'page', 'sanitize_title' );
245
		$post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title' );
246
247
		global $pagenow;
248
		if ( strpos( $page, 'formidable' ) === 0 || ( $pagenow == 'edit.php' && $post_type == 'frm_display' ) ) {
249
250
            wp_enqueue_script( 'admin-widgets' );
251
            wp_enqueue_style( 'widgets' );
252
            wp_enqueue_script( 'formidable' );
253
            wp_enqueue_script( 'formidable_admin' );
254
			FrmAppHelper::localize_script( 'admin' );
255
256
            wp_enqueue_style( 'formidable-admin' );
257
			wp_enqueue_style( 'formidable-grids' );
258
			wp_enqueue_style( 'formidable-dropzone' );
259
            add_thickbox();
260
261
            wp_register_script( 'formidable-editinplace', FrmAppHelper::plugin_url() . '/js/jquery/jquery.editinplace.packed.js', array( 'jquery' ), '2.3.0' );
262
263
        } else if ( $pagenow == 'post.php' || ( $pagenow == 'post-new.php' && $post_type == 'frm_display' ) ) {
264
            if ( isset( $_REQUEST['post_type'] ) ) {
265
                $post_type = sanitize_title( $_REQUEST['post_type'] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
266
			} else if ( isset( $_REQUEST['post'] ) && absint( $_REQUEST['post'] ) ) {
267
				$post = get_post( absint( $_REQUEST['post'] ) );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
268
                if ( ! $post ) {
269
                    return;
270
                }
271
                $post_type = $post->post_type;
272
            } else {
273
                return;
274
            }
275
276
            if ( $post_type == 'frm_display' ) {
277
                wp_enqueue_script( 'jquery-ui-draggable' );
278
                wp_enqueue_script( 'formidable_admin' );
279
                wp_enqueue_style( 'formidable-admin' );
280
				FrmAppHelper::localize_script( 'admin' );
281
            }
282
        } else if ( $pagenow == 'widgets.php' ) {
283
            FrmAppHelper::load_admin_wide_js();
284
        }
285
    }
286
287
    public static function load_lang() {
288
        load_plugin_textdomain( 'formidable', false, FrmAppHelper::plugin_folder() . '/languages/' );
289
    }
290
291
    /**
292
     * Filter shortcodes in text widgets
293
     */
294
    public static function widget_text_filter( $content ) {
295
    	$regex = '/\[\s*(formidable|display-frm-data|frm-stats|frm-graph|frm-entry-links|formresults|frm-search)\s+.*\]/';
296
    	return preg_replace_callback( $regex, 'FrmAppHelper::widget_text_filter_callback', $content );
297
    }
298
299
    public static function front_head() {
300
        if ( is_multisite() ) {
301
            $old_db_version = get_option( 'frm_db_version' );
302
            $pro_db_version = FrmAppHelper::pro_is_installed() ? get_option( 'frmpro_db_version' ) : false;
303
            if ( ( (int) $old_db_version < (int) FrmAppHelper::$db_version ) ||
304
                ( FrmAppHelper::pro_is_installed() && (int) $pro_db_version < (int) FrmAppHelper::$pro_db_version ) ) {
305
                self::install( $old_db_version );
306
            }
307
        }
308
    }
309
310
	public static function localize_script( $location ) {
311
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmAppHelper::localize_script' );
312
		return FrmAppHelper::localize_script( $location );
313
	}
314
315
	public static function custom_stylesheet() {
316
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmStylesController::custom_stylesheet' );
317
		return FrmStylesController::custom_stylesheet();
318
	}
319
320
	public static function load_css() {
321
		_deprecated_function( __FUNCTION__, '2.0.9', 'FrmStylesController::load_saved_css' );
322
		return FrmStylesController::load_saved_css();
323
	}
324
325
	/**
326
	 * Run silent upgrade on each site in the network during a network upgrade.
327
	 * Update database settings for all sites in a network during network upgrade process.
328
	 *
329
	 * @since 2.0.1
330
	 *
331
	 * @param int $blog_id Blog ID.
332
	 */
333
	public static function network_upgrade_site( $blog_id = 0 ) {
334
		if ( $blog_id ) {
335
			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...
336
			$upgrade_url = admin_url( 'admin-ajax.php' );
337
			restore_current_blog();
338
		} else {
339
			$upgrade_url = admin_url( 'admin-ajax.php' );
340
		}
341
342
		$upgrade_url = add_query_arg( array( 'action' => 'frm_silent_upgrade' ), $upgrade_url );
343
		$r = wp_remote_get( esc_url_raw( $upgrade_url ) );
344
		if ( is_wp_error( $r ) || ! is_array( $r ) || ! empty( $r['body'] ) ) {
345
			// if the remove post fails, use javascript instead
346
			add_action( 'admin_notices', 'FrmAppController::install_js_fallback' );
347
		}
348
	}
349
350
	/**
351
	 * Silent database upgrade (no redirect).
352
	 * Called via ajax request during network upgrade process.
353
	 *
354
	 * @since 2.0.1
355
	 */
356
	public static function ajax_install() {
357
		if ( self::needs_update() ) {
358
			self::install();
359
		}
360
		wp_die();
361
	}
362
363
    public static function activation_install() {
364
        FrmAppHelper::delete_cache_and_transient( 'frm_plugin_version' );
365
        FrmFormActionsController::actions_init();
366
        self::install();
367
    }
368
369
    public static function install( $old_db_version = false ) {
370
        $frmdb = new FrmDb();
371
        $frmdb->upgrade( $old_db_version );
372
    }
373
374
    public static function uninstall() {
375
		FrmAppHelper::permission_check('administrator');
376
        check_ajax_referer( 'frm_ajax', 'nonce' );
377
378
		$frmdb = new FrmDb();
379
		$frmdb->uninstall();
380
381
		//disable the plugin and redirect after uninstall so the tables don't get added right back
382
		deactivate_plugins( FrmAppHelper::plugin_folder() . '/formidable.php', false, false );
383
		echo esc_url_raw( admin_url( 'plugins.php?deactivate=true' ) );
384
385
        wp_die();
386
    }
387
388
    public static function drop_tables( $tables ) {
389
        global $wpdb;
390
        $tables[] = $wpdb->prefix . 'frm_fields';
391
        $tables[] = $wpdb->prefix . 'frm_forms';
392
        $tables[] = $wpdb->prefix . 'frm_items';
393
        $tables[] = $wpdb->prefix . 'frm_item_metas';
394
        return $tables;
395
    }
396
397
    // Routes for wordpress pages -- we're just replacing content here folks.
398
    public static function page_route( $content ) {
399
        global $post;
400
401
        $frm_settings = FrmAppHelper::get_settings();
402
        if ( $post && $post->ID == $frm_settings->preview_page_id && isset( $_GET['form'] ) ) {
403
            $content = FrmFormsController::page_preview();
404
        }
405
406
        return $content;
407
    }
408
409
    public static function deauthorize() {
410
		FrmAppHelper::permission_check('frm_change_settings');
411
        check_ajax_referer( 'frm_ajax', 'nonce' );
412
413
        delete_option( 'frmpro-credentials' );
414
        delete_option( 'frmpro-authorized' );
415
        delete_site_option( 'frmpro-credentials' );
416
        delete_site_option( 'frmpro-authorized' );
417
        wp_die();
418
    }
419
420
    public static function get_form_shortcode( $atts ) {
421
        _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::get_form_shortcode()' );
422
        return FrmFormsController::get_form_shortcode( $atts );
423
    }
424
}
425