Completed
Push — master ( 881903...6c57e3 )
by Stephanie
15s queued 11s
created

FrmSettingsController::inbox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
22
		$uploads     = wp_upload_dir();
23
		$target_path = $uploads['basedir'] . '/formidable/css';
24
25
		$sections = self::get_settings_tabs();
26
		$current  = FrmAppHelper::simple_get( 't', 'sanitize_title', 'general_settings' );
27
28
		require( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/form.php' );
29
	}
30
31
	private static function get_settings_tabs() {
32
		$sections = array(
33
			'general' => array(
34
				'class'    => __CLASS__,
35
				'function' => 'general_settings',
36
				'name'     => __( 'General Settings', 'formidable' ),
37
				'icon'     => 'frm_icon_font frm_settings_icon',
38
			),
39
			'messages' => array(
40
				'class'    => __CLASS__,
41
				'function' => 'message_settings',
42
				'name'     => __( 'Message Defaults', 'formidable' ),
43
				'icon'     => 'frm_icon_font frm_stamp_icon',
44
			),
45
			'permissions' => array(
46
				'class'    => __CLASS__,
47
				'function' => 'permission_settings',
48
				'name'     => __( 'Permissions', 'formidable' ),
49
				'icon'     => 'frm_icon_font frm_lock_icon',
50
			),
51
			'recaptcha' => array(
52
				'class'    => __CLASS__,
53
				'function' => 'recaptcha_settings',
54
				'name'     => __( 'reCaptcha', 'formidable' ),
55
				'icon'     => 'frm_icon_font frm_shield_check_icon',
56
			),
57
			'white_label' => array(
58
				'name'       => __( 'White Labeling', 'formidable' ),
59
				'icon'       => 'frm_icon_font frm_ghost_icon',
60
				'html_class' => 'frm_show_upgrade frm_noallow',
61
				'data'       => array(
62
					'medium'  => 'white-label',
63
					'upgrade' => __( 'White labeling options', 'formidable' ),
64
				),
65
			),
66
		);
67
68
		if ( apply_filters( 'frm_include_addon_page', false ) ) {
69
			// if no addons need a license, skip this page
70
			$show_licenses    = false;
71
			$installed_addons = apply_filters( 'frm_installed_addons', array() );
72
			foreach ( $installed_addons as $installed_addon ) {
73
				if ( ! $installed_addon->is_parent_licence && $installed_addon->plugin_name != 'Formidable Pro' ) {
74
					$show_licenses = true;
75
					break;
76
				}
77
			}
78
79
			if ( $show_licenses ) {
80
				$sections['licenses'] = array(
81
					'class'    => 'FrmAddonsController',
82
					'function' => 'license_settings',
83
					'name'     => __( 'Plugin Licenses', 'formidable' ),
84
					'icon'     => 'frm_icon_font frm_keyalt_icon',
85
					'ajax'     => true,
86
				);
87
			}
88
		}
89
		$sections = apply_filters( 'frm_add_settings_section', $sections );
90
91
		$sections['misc'] = array(
92
			'name'     => __( 'Miscellaneous', 'formidable' ),
93
			'icon'     => 'frm_icon_font frm_shuffle_icon',
94
			'class'    => __CLASS__,
95
			'function' => 'misc_settings',
96
		);
97
98
		foreach ( $sections as $key => $section ) {
99
			$original = $section;
100
			$defaults = array(
101
				'html_class' => '',
102
				'name'       => ucfirst( $key ),
103
				'icon'       => 'frm_icon_font frm_settings_icon',
104
				'anchor'     => $key . '_settings',
105
				'data'       => array(),
106
			);
107
108
			$section = array_merge( $defaults, $section );
109
110
			if ( isset( $section['ajax'] ) && ! isset( $section['data']['frmajax'] ) ) {
111
				$section['data']['frmajax'] = $section['ajax'];
112
			}
113
114
			// For reverse compatibility.
115
			if ( ! isset( $section['function'] ) && ( ! is_array( $original ) || ! isset( $original['name'] ) ) ) {
116
				$section['function'] = $original;
117
			}
118
119
			$sections[ $key ] = $section;
120
		}
121
122
		return $sections;
123
	}
124
125
	public static function load_settings_tab() {
126
		FrmAppHelper::permission_check( 'frm_change_settings' );
127
		check_ajax_referer( 'frm_ajax', 'nonce' );
128
129
		$section  = FrmAppHelper::get_post_param( 'tab', '', 'sanitize_text_field' );
130
		$sections = self::get_settings_tabs();
131
		if ( ! isset( $sections[ $section ] ) ) {
132
			wp_die();
133
		}
134
135
		$section = $sections[ $section ];
136
137 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...
138
			call_user_func( array( $section['class'], $section['function'] ) );
139
		} else {
140
			call_user_func( ( isset( $section['function'] ) ? $section['function'] : $section ) );
141
		}
142
		wp_die();
143
	}
144
145
	/**
146
	 * @since 4.0
147
	 */
148
	public static function general_settings() {
149
		$frm_settings = FrmAppHelper::get_settings();
150
151
		$uploads     = wp_upload_dir();
152
		$target_path = $uploads['basedir'] . '/formidable/css';
153
154
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/general.php' );
155
	}
156
157
	/**
158
	 * @since 4.0
159
	 */
160
	public static function message_settings() {
161
		$frm_settings = FrmAppHelper::get_settings();
162
163
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/messages.php' );
164
	}
165
166
	/**
167
	 * @since 4.0
168
	 */
169
	public static function recaptcha_settings() {
170
		$frm_settings = FrmAppHelper::get_settings();
171
		$captcha_lang = FrmAppHelper::locales( 'captcha' );
172
173
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/recaptcha.php' );
174
	}
175
176
	/**
177
	 * @since 4.0
178
	 */
179
	public static function permission_settings() {
180
		$frm_settings = FrmAppHelper::get_settings();
181
		$frm_roles    = FrmAppHelper::frm_capabilities();
182
183
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/permissions.php' );
184
	}
185
186
	/**
187
	 * @since 4.0
188
	 */
189
	public static function misc_settings() {
190
		$frm_settings = FrmAppHelper::get_settings();
191
192
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/misc.php' );
193
	}
194
195
	public static function process_form( $stop_load = false ) {
196
		global $frm_vars;
197
198
		$frm_settings = FrmAppHelper::get_settings();
199
200
		$process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' );
201
		if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) {
202
			wp_die( esc_html( $frm_settings->admin_permission ) );
203
		}
204
205
		$errors  = array();
206
		$message = '';
207
208
		if ( ! isset( $frm_vars['settings_routed'] ) || ! $frm_vars['settings_routed'] ) {
209
			$errors = $frm_settings->validate( $_POST, array() );
210
211
			$frm_settings->update( wp_unslash( $_POST ) );
212
213
			if ( empty( $errors ) ) {
214
				$frm_settings->store();
215
				$message = __( 'Settings Saved', 'formidable' );
216
			}
217
		} else {
218
			$message = __( 'Settings Saved', 'formidable' );
219
		}
220
221
		if ( $stop_load == 'stop_load' ) {
222
			$frm_vars['settings_routed'] = true;
223
224
			return;
225
		}
226
227
		self::display_form( $errors, $message );
228
	}
229
230
	/**
231
	 * Include the Update button on the global settings page.
232
	 *
233
	 * @since 4.0.02
234
	 */
235
	public static function save_button() {
236
		echo '<input class="button-primary frm-button-primary" type="submit"
237
			value="' . esc_attr__( 'Update', 'formidable' ) . '"/>';
238
	}
239
240
	public static function route( $stop_load = false ) {
241
		$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action';
242
		$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' );
243
		FrmAppHelper::include_svg();
244
245
		if ( $action == 'process-form' ) {
246
			self::process_form( $stop_load );
247
		} elseif ( $stop_load != 'stop_load' ) {
248
			self::display_form();
249
		}
250
	}
251
252
	/**
253
	 * Add CTA to the bottom on the plugin settings pages.
254
	 *
255
	 * @since 3.04.02
256
	 */
257
	public static function settings_cta( $view ) {
258
259
		if ( get_option( 'frm_lite_settings_upgrade', false ) ) {
260
			return;
261
		}
262
263
		$features = array(
264
			__( 'Extra form features like file uploads, pagination, etc', 'formidable' ),
265
			__( 'Repeaters & cascading fields for advanced forms', 'formidable' ),
266
			__( 'Flexibly view, search, edit, and delete entries anywhere', 'formidable' ),
267
			__( 'Display entries with virtually limitless Formidable views', 'formidable' ),
268
			__( 'Create surveys & polls', 'formidable' ),
269
			__( 'WordPress user registration and login forms', 'formidable' ),
270
			__( 'Create Stripe, PayPal or Authorize.net payment forms', 'formidable' ),
271
			__( 'Powerful conditional logic for smart forms', 'formidable' ),
272
			__( 'Integrations with 1000+ marketing & payment services', 'formidable' ),
273
			__( 'Collect digital signatures', 'formidable' ),
274
			__( 'Accept user-submitted content with Post submissions', 'formidable' ),
275
			__( 'Email routing', 'formidable' ),
276
			__( 'Create calculator forms', 'formidable' ),
277
			__( 'Save draft entries and return later', 'formidable' ),
278
			__( 'Analyze form data with graphs & stats', 'formidable' ),
279
		);
280
281
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-settings/settings_cta.php' );
282
	}
283
284
	/**
285
	 * Dismiss upgrade notice at the bottom on the plugin settings pages.
286
	 *
287
	 * @since 3.04.02
288
	 */
289
	public static function settings_cta_dismiss() {
290
		FrmAppHelper::permission_check( 'frm_change_settings' );
291
292
		update_option( 'frm_lite_settings_upgrade', time(), 'no' );
293
294
		wp_send_json_success();
295
	}
296
297
	/**
298
	 * Autocomplete page admin ajax endpoint
299
	 *
300
	 * @since 4.03.06
301
	 */
302
	public static function page_search() {
303
		FrmAppHelper::permission_check( 'frm_edit_forms' );
304
		check_ajax_referer( 'frm_ajax', 'nonce' );
305
306
		global $wpdb;
307
308
		$term = FrmAppHelper::get_param( 'term', '', 'get', 'sanitize_text_field' );
309
310
		$where = array(
311
			'post_status'     => 'publish',
312
			'post_type'       => 'page',
313
			'post_title LIKE' => $term,
314
		);
315
316
		$atts = array(
317
			'limit'    => 25,
318
			'order_by' => 'post_title',
319
		);
320
321
		$pages = FrmDb::get_results( $wpdb->posts, $where, 'ID, post_title', $atts );
322
323
		$results = array();
324
		foreach ( $pages as $page ) {
0 ignored issues
show
Bug introduced by
The expression $pages of type array|null|string|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
325
			$results[] = array(
326
				'value' => $page->ID,
327
				'label' => $page->post_title,
328
			);
329
		}
330
331
		wp_send_json( $results );
332
	}
333
}
334