Completed
Push — develop ( 4d830b...72e688 )
by Zack
06:12
created

View_Settings::defaults()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 397

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 223
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 2
dl 0
loc 397
ccs 223
cts 223
cp 1
crap 13
rs 5.2933
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The View Settings class.
11
 */
12
class View_Settings extends Settings {
13
	/**
14
	 * Retrieve an instance of the settings with default values.
15
	 * @param bool $detailed Whether to return detailed setting meta information or just the value.
16
	 *
17
	 * @api
18
	 * @since 2.0
19
	 *
20
	 * @return \GV\View_Settings
21
	 */
22
	public static function with_defaults( $detailed = false ) {
23
		$settings = new self();
24
		$settings->update( self::defaults( $detailed ) );
25
		return $settings;
26
	}
27
28
	/**
29
	 * Retrieve the default View settings.
30
	 *
31
	 * @param bool $detailed Whether to return detailed setting meta information or just the value.
32
	 * @param string $group Retrieve settings of a particular group.
33
	 *
34
	 * @api
35
	 * @since 2.0
36
	 *
37
	 * @return array The default settings along with their values.
38
	 *      @param[out] string $label Setting label shown in admin
39
	 *      @param[out] string $type Gravity Forms field type
40
	 *      @param[out] string $group The field group the setting is associated with. Default: "default"
41
	 *      @param[out] mixed  $value The default value for the setting
42
	 *      @param[out] string $tooltip Tooltip displayed for the setting
43
	 *      @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal
44
	 *      @param[out] array  $options Array of values to use when generating select, multiselect, radio, or checkboxes fields
45
	 *      @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
46
	 */
47 187
	public static function defaults( $detailed = false, $group = null ) {
48
49 187
		$default_settings = array_merge( array(
50 187
			'id' => array(
51 187
				'label'             => __( 'View ID', 'gravityview' ),
52 187
				'type'              => 'number',
53 187
				'group'             => 'default',
54
				'value'             => NULL,
55
				'tooltip'           => NULL,
56
				'show_in_shortcode' => false,
57
			),
58
			'page_size' => array(
59 187
				'label'             => __( 'Number of entries per page', 'gravityview' ),
60 187
				'type'              => 'number',
61 187
				'class'             => 'small-text',
62 187
				'group'             => 'default',
63 187
				'value'             => 25,
64
				'show_in_shortcode' => true,
65
			),
66
			'offset' => array(
67 187
				'label'             => __( 'Offset entries starting from', 'gravityview' ),
68 187
				'type'              => 'number',
69 187
				'class'             => 'small-text',
70 187
				'group'             => 'default',
71 187
				'value'             => 0,
72
				'show_in_shortcode' => true,
73
			),
74
			'lightbox' => array(
75 187
				'label'             => __( 'Enable lightbox for images', 'gravityview' ),
76 187
				'type'              => 'checkbox',
77 187
				'group'             => 'default',
78 187
				'value'             => 1,
79
				'tooltip'           => NULL,
80
				'show_in_shortcode' => true,
81
			),
82
			'show_only_approved'    => array(
83 187
				'label'             => __( 'Show only approved entries', 'gravityview' ),
84 187
				'type'              => 'checkbox',
85 187
				'group'             => 'default',
86 187
				'value'             => 0,
87
				'show_in_shortcode' => true,
88
			),
89
			'admin_show_all_statuses' => array(
90 187
				'label'             => __( 'Show all entries to administrators', 'gravityview' ),
91 187
				'desc'              => __( 'Administrators will be able to see entries with any approval status.', 'gravityview' ),
92 187
				'tooltip'           => __( 'Logged-out visitors and non-administrators will only see approved entries, while administrators will see entries with all statuses. This makes it easier for administrators to moderate entries from a View.', 'gravityview' ),
93 187
				'requires'          => 'show_only_approved',
94 187
				'type'              => 'checkbox',
95 187
				'group'             => 'default',
96 187
				'value'             => 0,
97
				'show_in_shortcode' => false,
98
			),
99
			'hide_until_searched' => array(
100 187
				'label'             => __( 'Hide View data until search is performed', 'gravityview' ),
101 187
				'type'              => 'checkbox',
102 187
				'group'             => 'default',
103 187
				'tooltip'           => __( 'When enabled it will only show any View entries after a search is performed.', 'gravityview' ),
104 187
				'value'             => 0,
105
				'show_in_shortcode' => false,
106
			),
107
			'hide_empty' => array(
108 187
				'label'             => __( 'Hide empty fields', 'gravityview' ),
109 187
				'group'             => 'default',
110 187
				'type'              => 'checkbox',
111 187
				'value'             => 1,
112
				'show_in_shortcode' => false,
113
			),
114
			'hide_empty_single' => array(
115 187
				'label'             => __( 'Hide empty fields', 'gravityview' ),
116 187
				'group'             => 'default',
117 187
				'type'              => 'checkbox',
118 187
				'value'             => 1,
119
				'show_in_shortcode' => false,
120
			),
121
			'edit_feeds' => array(
122 187
				'label'             => __( 'Feeds', 'gravityview' ),
123 187
				'group'             => 'default',
124 187
				'type'              => 'checkbox',
125
				'value'             => array(),
126
				'show_in_shortcode' => false,
127
			),
128
			'user_edit' => array(
129 187
				'label'             => __( 'Allow User Edit', 'gravityview' ), 'group'             => 'default',
130 187
				'desc'              => __( 'Allow logged-in users to edit entries they created.', 'gravityview' ) . ' ' . sprintf( __( 'Administrators are able to %s regardless of this setting.', 'gravityview' ), _x( 'edit entries', 'an action that admins can perform', 'gravityview' ) ),
131 187
				'value'             => 0,
132 187
				'tooltip'           => __( 'Display "Edit Entry" fields to non-administrator users if they created the entry. Edit Entry fields will always be displayed to site administrators.', 'gravityview' ),
133 187
				'type'              => 'checkbox',
134
				'show_in_shortcode' => true,
135
			),
136
			'unapprove_edit' => array(
137 187
				'label'             => __( 'Unapprove Entries After Edit', 'gravityview' ),
138 187
				'group'             => 'default',
139 187
				'requires'          => 'user_edit',
140 187
				'desc'              => __( 'When an entry is edited by a non-administrator, reset the approval status to "Unapproved".', 'gravityview' ),
141 187
				'tooltip'           => __( 'If the "Show only approved entries" setting is enabled, the entry will need to be re-approved by an administrator before it is shown in the View.', 'gravityview' ),
142 187
				'value'             => 0,
143 187
				'type'              => 'checkbox',
144
				'show_in_shortcode' => true,
145
			),
146
			'user_delete' => array(
147 187
				'label'             => __( 'Allow User Delete', 'gravityview' ),
148 187
				'group'             => 'default',
149 187
				'desc'              => __( 'Allow logged-in users to delete entries they created.', 'gravityview' ) . ' ' . sprintf( __( 'Administrators are able to %s regardless of this setting.', 'gravityview' ), _x( 'delete entries', 'an action that admins can perform', 'gravityview' ) ),
150 187
				'value'             => 0,
151 187
				'tooltip'           => __( 'Display "Delete Entry" fields to non-administrator users if they created the entry. Delete Entry fields will always be displayed to site administrators.', 'gravityview' ),
152 187
				'type'              => 'checkbox',
153
				'show_in_shortcode' => true,
154
			),
155
			'user_duplicate' => array(
156 187
				'label'             => __( 'Allow User Duplicate', 'gravityview' ),
157 187
				'group'             => 'default',
158 187
				'desc'              => __( 'Allow logged-in users to duplicate entries they created.', 'gravityview' ) . ' ' . sprintf( __( 'Administrators are able to %s regardless of this setting.', 'gravityview' ), _x( 'duplicate entries', 'an action that admins can perform', 'gravityview' ) ),
159 187
				'value'             => 0,
160 187
				'tooltip'           => __( 'Display "Duplicate Entry" fields to non-administrator users if they created the entry. Duplicate Entry fields will always be displayed to site administrators.', 'gravityview' ),
161 187
				'type'              => 'checkbox',
162
				'show_in_shortcode' => true,
163
			),
164
			'sort_field' => array(
165 187
				'label'             => __( 'Sort by field', 'gravityview' ),
166 187
				'type'              => 'select',
167 187
				'desc'              => __( 'By default, entries are sorted by Entry ID.', 'gravityview' ),
168 187
				'value'             => '',
169 187
				'group'             => 'sort',
170
				'options'           => array(
171 187
										''             => __( 'Default', 'gravityview' ),
172 187
										'date_created' => __( 'Date Created', 'gravityview' ),
173
									),
174
				'show_in_shortcode' => true,
175
			),
176
			'sort_direction' => array(
177 187
				'label'             => __( 'Sort direction', 'gravityview' ),
178 187
				'type'              => 'select',
179 187
				'value'             => 'ASC',
180 187
				'group'             => 'sort',
181
				'options'           => array(
182 187
										'ASC'  => __( 'ASC', 'gravityview' ),
183 187
										'DESC' => __( 'DESC', 'gravityview' ),
184
				),
185
				'show_in_shortcode' => true,
186
			),
187
			'sort_field_2' => array(
188 187
				'label'             => __( 'Sort by secondary field', 'gravityview' ),
189 187
				'type'              => 'select',
190 187
				'value'             => '',
191 187
				'group'             => 'sort',
192
				'options'           => array(
193 187
					''             => __( 'Default', 'gravityview' ),
194 187
					'date_created' => __( 'Date Created', 'gravityview' ),
195
				),
196 187
				'requires_not'          => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
197
				'show_in_shortcode' => true,
198
			),
199
			'sort_direction_2' => array(
200 187
				'label'             => __( 'Secondary sort direction', 'gravityview' ),
201 187
				'type'              => 'select',
202 187
				'value'             => 'ASC',
203 187
				'group'             => 'sort',
204
				'options'           => array(
205 187
					'ASC'  => __( 'ASC', 'gravityview' ),
206 187
					'DESC' => __( 'DESC', 'gravityview' ),
207
				),
208 187
				'requires_not'      => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
209
				'show_in_shortcode' => true,
210
			),
211
			'sort_columns' => array(
212 187
				'label'             => __( 'Enable sorting by column', 'gravityview' ),
213 187
				'left_label'        => __( 'Column Sorting', 'gravityview' ),
214 187
				'type'              => 'checkbox',
215
				'value'             => false,
216 187
				'group'             => 'sort',
217
				'tooltip'           => NULL,
218
				'show_in_shortcode' => true,
219
				'show_in_template'  => array( 'default_table', 'preset_business_data', 'preset_issue_tracker', 'preset_resume_board', 'preset_job_board' ),
220
			),
221
			'start_date' => array(
222 187
				'label'             => __( 'Filter by Start Date', 'gravityview' ),
223 187
				'class'             => 'gv-datepicker',
224 187
				'desc'              => __( 'Show entries submitted after this date. Supports relative dates, such as "-1 week" or "-1 month".', 'gravityview' ),
225 187
				'type'              => 'text',
226 187
				'value'             => '',
227 187
				'group'             => 'filter',
228
				'show_in_shortcode' => true,
229
			),
230
			'end_date' => array(
231 187
				'label'             => __( 'Filter by End Date', 'gravityview' ),
232 187
				'class'             => 'gv-datepicker',
233 187
				'desc'              => __( 'Show entries submitted before this date. Supports relative dates, such as "now" or "-3 days".', 'gravityview' ),
234 187
				'type'              => 'text',
235 187
				'value'             => '',
236 187
				'group'             => 'filter',
237
				'show_in_shortcode' => true,
238
			),
239
			'class' => array(
240 187
				'label'             => __( 'CSS Class', 'gravityview' ),
241 187
				'desc'              => __( 'CSS class to add to the wrapping HTML container.', 'gravityview' ),
242 187
				'group'             => 'default',
243 187
				'type'              => 'text',
244 187
				'value'             => '',
245
				'show_in_shortcode' => false,
246
			),
247
			'search_value' => array(
248 187
				'label'             => __( 'Search Value', 'gravityview' ),
249 187
				'desc'              => __( 'Define a default search value for the View', 'gravityview' ),
250 187
				'type'              => 'text',
251 187
				'value'             => '',
252 187
				'group'             => 'filter',
253
				'show_in_shortcode' => false,
254
			),
255
			'search_field' => array(
256 187
				'label'             => __( 'Search Field', 'gravityview' ),
257 187
				'desc'              => __( 'If Search Value is set, you can define a specific field to search in. Otherwise, all fields will be searched.', 'gravityview' ),
258 187
				'type'              => 'text',
259 187
				'value'             => '',
260 187
				'group'             => 'filter',
261
				'show_in_shortcode' => false,
262
			),
263
			'search_operator' => array(
264 187
				'label'             => __( 'Search Operator', 'gravityview' ),
265 187
				'type'              => 'operator',
266 187
				'value'             => 'contains',
267 187
				'group'             => 'filter',
268
				'show_in_shortcode' => false,
269
			),
270
			'single_title' => array(
271 187
				'label'             => __( 'Single Entry Title', 'gravityview' ),
272 187
				'type'              => 'text',
273 187
				'desc'              => __( 'When viewing a single entry, change the title of the page to this setting. Otherwise, the title will not change between the Multiple Entries and Single Entry views.', 'gravityview' ),
274 187
				'group'             => 'default',
275 187
				'value'             => '',
276
				'show_in_shortcode' => false,
277
				'full_width'        => true,
278
			),
279
			'back_link_label' => array(
280 187
				'label'             => __( 'Back Link Label', 'gravityview' ),
281 187
				'group'             => 'default',
282 187
				'desc'              => __( 'The text of the link that returns to the multiple entries view.', 'gravityview' ),
283 187
				'type'              => 'text',
284 187
				'value'             => '',
285
				'show_in_shortcode' => false,
286
				'full_width'        => true,
287
			),
288
			'edit_redirect' => array(
289 187
				'label'             => __( 'Redirect After Editing', 'gravityview' ),
290 187
				'group'             => 'default',
291 187
				'desc'              => __( 'The page to redirect to after editing an entry.', 'gravityview' ),
292 187
				'type'              => 'select',
293 187
				'value'             => '',
294
				'options'           => array(
295 187
					'' => __( 'Stay on Edit Entry', 'gravityview' ),
296 187
					'0'  => __( 'Redirect to Single Entry', 'gravityview' ),
297 187
					'1' => __( 'Redirect to Multiple Entries', 'gravityview' ),
298 187
					'2' => __( 'Redirect to URL', 'gravityview' ),
299
				)
300
			),
301
			'edit_return_context' => array(
302 187
				'label'             => __( 'Editing Returns To&hellip;', 'gravityview' ),
303 187
				'type'              => 'radio',
304 187
				'desc'              => __( 'After editing an entry or clicking Cancel, where should the user be sent?', 'gravityview' ),
305 187
				'group'             => 'default',
306 187
				'value'             => 'single',
307
				'options'           => array(
308 187
					'multiple' => __( 'Multiple Entries', 'gravityview' ),
309 187
					'single'   => __( 'Single Entry', 'gravityview' ),
310 187
					'custom'   => __( 'Other URL', 'gravityview' ),
311
				),
312
				'show_in_shortcode' => false,
313
				'full_width'        => true,
314
			),
315
			'edit_redirect_url' => array(
316 187
				'label'             => __( 'Edit Entry Redirect URL', 'gravityview' ),
317 187
				'group'             => 'default',
318 187
				'desc'              => __( 'After editing an entry, the user will be taken to this URL.', 'gravityview' ),
319 187
				'type'              => 'text',
320 187
				'class'             => 'code widefat',
321 187
				'value'             => '',
322 187
				'requires'          => 'edit_redirect=2',
323 187
				'merge_tags'        => 'force',
324
			),
325
			'edit_locking' => array(
326 187
				'label'             => __( 'Enable Edit Locking', 'gravityview' ),
327 187
				'group'             => 'default',
328 187
				'desc'              => __( 'Prevent multiple users from editing the same entry at the same time.', 'gravityview' ),
329 187
				'type'              => 'checkbox',
330
				'full_width'        => true,
331 187
				'class'             => 'code widefat',
332
				'value'             => true,
333
			),
334
			'embed_only' => array(
335 187
				'label'             => __( 'Prevent Direct Access', 'gravityview' ),
336 187
				'group'             => 'default',
337 187
				'desc'              => __( 'Only allow access to this View when embedded using the shortcode.', 'gravityview' ),
338 187
				'type'              => 'checkbox',
339 187
				'value'             => '',
340
				'tooltip'           => false,
341
				'show_in_shortcode' => false,
342
				'full_width'        => true,
343
			),
344 187
		), ( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) === '1' ) ) ?
345
			array(
346 187
				'rest_disable'          => array(
347 187
					'label'             => __( 'Prevent REST Access', 'gravityview' ),
348 187
					'group'             => 'default',
349 187
					'desc'              => __( 'Disable REST access to this View.', 'gravityview' ),
350 187
					'type'              => 'checkbox',
351 187
					'value'             => '',
352
					'tooltip'           => false,
353
					'show_in_shortcode' => false,
354
					'full_width'        => true,
355
				),
356 187
			) : array(),
357 187
		( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) !== '1' ) ) ?
358
			array(
359 1
				'rest_enable'           => array(
360 1
					'label'             => __( 'Allow REST Access', 'gravityview' ),
361 1
					'group'             => 'default',
362 1
					'desc'              => __( 'Enable REST access to this View.', 'gravityview' ),
363 1
					'type'              => 'checkbox',
364 1
					'value'             => '',
365
					'tooltip'           => false,
366
					'show_in_shortcode' => false,
367
					'full_width'        => true,
368
				),
369 187
			) : array(),
370
		array(
371 187
			'csv_enable'            => array(
372 187
				'label'             => __( 'Allow CSV Access', 'gravityview' ),
373 187
				'group'             => 'default',
374 187
				'desc'              => __( 'Enable CSV access to this View.', 'gravityview' ),
375 187
				'type'              => 'checkbox',
376 187
				'value'             => '',
377 187
				'tooltip'           => __( 'If enabled, entries can be exported for this View by adding "/csv/" to the View URL. Each configured field will be a column in the exported CSV.', 'gravityview' ),
378
				'show_in_shortcode' => false,
379
				'full_width'        => true,
380
			),
381
		),
382
		array(
383 187
			'csv_nolimit'           => array(
384 187
				'label'             => __( 'Show all in CSV', 'gravityview' ),
385 187
				'group'             => 'default',
386 187
				'requires'          => 'csv_enable',
387 187
				'desc'              => __( 'Do not limit the number of entries output in the CSV.', 'gravityview' ),
388 187
				'type'              => 'checkbox',
389 187
				'value'             => '',
390
				'tooltip'           => false,
391
				'show_in_shortcode' => false,
392
				'full_width'        => true,
393
			),
394
		),
395
		array(
396 187
			'post_id' => array(
397
				'type'              => 'number',
398
				'value'             => '',
399
				'show_in_shortcode' => false,
400
			),
401
		) );
402
403 187
		if ( version_compare( \GFCommon::$version, '2.3-beta-4', '>=' ) ) {
404 187
			$default_settings['sort_direction']['options']['RAND'] = __( 'Random', 'gravityview' );
405
		}
406
407
		/**
408
		 * @filter `gravityview_default_args` Modify the default settings for new Views
409
		 * @param[in,out] array $default_args Array of default args.
410
		 * @deprecated
411
		 * @see filter `gravityview/view/settings/defaults`
412
		 */
413 187
		$default_settings = apply_filters( 'gravityview_default_args', $default_settings );
414
415
		/**
416
		 * @filter `gravityview/view/defaults` Modify the default settings for new Views
417
		 * @param[in,out] array $default_settings Array of default settings.
418
		 */
419 187
		$default_settings = apply_filters( 'gravityview/view/settings/defaults', $default_settings );
420
421
		// By default, we only want the key => value pairing, not the whole array.
422 187
		if ( ! $detailed ) {
423 187
			$defaults = array();
424 187
			foreach( $default_settings as $key => $value ) {
425 187
				$defaults[ $key ] = $value['value'];
426
			}
427 187
			return $defaults;
428
429
		// But sometimes, we want all the details.
430
		} else {
431 11
			foreach ( $default_settings as $key => $value ) {
432
433
				// If the $group argument is set for the method,
434
				// ignore any settings that aren't in that group.
435 11
				if ( ! empty( $group ) && is_string( $group ) ) {
436 1
					if ( empty( $value['group'] ) || $value['group'] !== $group ) {
437 1
						unset( $default_settings[ $key ] );
438
					}
439
				}
440
			}
441 11
			return $default_settings;
442
		}
443
	}
444
445
	/**
446
	 * Turn to an $atts array as used around the old codebase.
447
	 *
448
	 * @internal
449
	 * @deprecated
450
	 *
451
	 * @return array
452
	 */
453 108
	public function as_atts() {
454 108
		$defaults = array_keys( self::defaults() );
455 108
		$_this = &$this;
456 108
		return array_combine( $defaults, array_map( function( $key ) use ( $_this ) {
457 108
			return $_this->get( $key );
458 108
		}, $defaults ) );
459
	}
460
}
461