Completed
Push — master ( 45ba98...c3b0cd )
by Stephanie
04:13
created

FrmAppController::load_wp_admin_style()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 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
        $frm_settings = FrmAppHelper::get_settings();
12
        add_menu_page( 'Formidable', $frm_settings->menu, 'frm_view_forms', 'formidable', 'FrmFormsController::route', FrmAppHelper::plugin_url() . '/images/form_16.png', 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 $pagenow, $frm_vars;
28
29
		$show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' );
1 ignored issue
show
Documentation introduced by
$show_nav is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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