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