Completed
Pull Request — master (#100)
by Stephanie
03:13
created

FrmSettingsController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 167
Duplicated Lines 2.99 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 5
loc 167
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A menu() 0 6 1
A license_box() 0 4 1
A display_form() 0 15 1
B get_settings_tabs() 0 26 6
A load_settings_tab() 5 19 4
B process_form() 0 33 6
A route() 0 9 4
A settings_cta() 0 26 2
A settings_cta_dismiss() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class FrmSettingsController {
4
5
    public static function menu() {
6
		// Make sure admins can see the menu items
7
		FrmAppHelper::force_capability( 'frm_change_settings' );
8
9
        add_submenu_page( 'formidable', 'Formidable | ' . __( 'Global Settings', 'formidable' ), __( 'Global Settings', 'formidable' ), 'frm_change_settings', 'formidable-settings', 'FrmSettingsController::route' );
10
    }
11
12
    public static function license_box() {
13
		$a = FrmAppHelper::simple_get( 't', 'sanitize_title', 'general_settings' );
14
        include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/license_box.php' );
15
    }
16
17
    public static function display_form( $errors = array(), $message = '' ) {
18
        global $frm_vars;
19
20
        $frm_settings = FrmAppHelper::get_settings();
21
        $frm_roles = FrmAppHelper::frm_capabilities();
22
23
        $uploads = wp_upload_dir();
24
        $target_path = $uploads['basedir'] . '/formidable/css';
25
26
		$sections = self::get_settings_tabs();
27
28
		$captcha_lang = FrmAppHelper::locales( 'captcha' );
29
30
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/form.php' );
31
	}
32
33
	private static function get_settings_tabs() {
34
		$sections = array();
35
		if ( apply_filters( 'frm_include_addon_page', false ) ) {
36
			// if no addons need a license, skip this page
37
			$show_licenses = false;
38
			$installed_addons = apply_filters( 'frm_installed_addons', array() );
39
			foreach ( $installed_addons as $installed_addon ) {
40
				if ( ! $installed_addon->is_parent_licence && $installed_addon->plugin_name != 'Formidable Pro' ) {
41
					$show_licenses = true;
42
					break;
43
				}
44
			}
45
46
			if ( $show_licenses ) {
47
				$sections['licenses'] = array(
48
					'class'    => 'FrmAddonsController',
49
					'function' => 'license_settings',
50
					'name'     => __( 'Plugin Licenses', 'formidable' ),
51
					'ajax'     => true,
52
				);
53
			}
54
		}
55
		$sections = apply_filters( 'frm_add_settings_section', $sections );
56
57
		return $sections;
58
	}
59
60
	public static function load_settings_tab() {
61
		FrmAppHelper::permission_check( 'frm_change_settings' );
62
		check_ajax_referer( 'frm_ajax', 'nonce' );
63
64
		$section = FrmAppHelper::get_post_param( 'tab', '', 'sanitize_text_field' );
65
		$sections = self::get_settings_tabs();
66
		if ( ! isset( $sections[ $section ] ) ) {
67
			wp_die();
68
		}
69
70
		$section = $sections[ $section ];
71
72 View Code Duplication
		if ( isset( $section['class'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
			call_user_func( array( $section['class'], $section['function'] ) );
74
		} else {
75
			call_user_func( ( isset( $section['function'] ) ? $section['function'] : $section ) );
76
		}
77
		wp_die();
78
	}
79
80
    public static function process_form( $stop_load = false ) {
81
        global $frm_vars;
82
83
        $frm_settings = FrmAppHelper::get_settings();
84
85
		$process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' );
86
		if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) {
87
			wp_die( esc_html( $frm_settings->admin_permission ) );
88
        }
89
90
        $errors = array();
91
        $message = '';
92
93
        if ( ! isset( $frm_vars['settings_routed'] ) || ! $frm_vars['settings_routed'] ) {
94
            $errors = $frm_settings->validate( $_POST, array() );
95
96
            $frm_settings->update( stripslashes_deep( $_POST ) );
97
98
            if ( empty( $errors ) ) {
99
                $frm_settings->store();
100
                $message = __( 'Settings Saved', 'formidable' );
101
            }
102
        } else {
103
            $message = __( 'Settings Saved', 'formidable' );
104
        }
105
106
		if ( $stop_load == 'stop_load' ) {
107
            $frm_vars['settings_routed'] = true;
108
            return;
109
        }
110
111
        self::display_form( $errors, $message );
112
    }
113
114
    public static function route( $stop_load = false ) {
115
        $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
116
		$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
117
        if ( $action == 'process-form' ) {
118
			self::process_form( $stop_load );
119
        } else if ( $stop_load != 'stop_load' ) {
120
			self::display_form();
121
        }
122
    }
123
124
	/**
125
	 * Add CTA to the bottom on the plugin settings pages.
126
	 *
127
	 * @since 3.04.02
128
	 */
129
	public static function settings_cta( $view ) {
130
131
		if ( get_option( 'frm_lite_settings_upgrade', false ) ) {
132
			return;
133
		}
134
135
		$features = array(
136
			__( 'Extra form features like file uploads, pagination, etc', 'formidable' ),
137
			__( 'Repeaters & cascading fields for advanced forms', 'formidable' ),
138
			__( 'Flexibly view, search, edit, and delete entries anywhere', 'formidable' ),
139
			__( 'Display entries with virtually limitless Formidable views', 'formidable' ),
140
			__( 'Create surveys & polls', 'formidable' ),
141
			__( 'WordPress user registration and login forms', 'formidable' ),
142
			__( 'Create Stripe, PayPal or Authorize.net payment forms', 'formidable' ),
143
			__( 'Powerful conditional logic for smart forms', 'formidable' ),
144
			__( 'Integrations with 1000+ marketing & payment services', 'formidable' ),
145
			__( 'Collect digital signatures', 'formidable' ),
146
			__( 'Accept user-submitted content with Post submissions', 'formidable' ),
147
			__( 'Email routing', 'formidable' ),
148
			__( 'Create calculator forms', 'formidable' ),
149
			__( 'Save draft entries and return later', 'formidable' ),
150
			__( 'Analyze form data with graphs & stats', 'formidable' ),
151
		);
152
153
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/settings_cta.php' );
154
	}
155
156
	/**
157
	 * Dismiss upgrade notice at the bottom on the plugin settings pages.
158
	 *
159
	 * @since 3.04.02
160
	 */
161
	public static function settings_cta_dismiss() {
162
		FrmAppHelper::permission_check( 'frm_change_settings' );
163
164
		update_option( 'frm_lite_settings_upgrade', time(), 'no' );
165
166
		wp_send_json_success();
167
	}
168
169
}
170