Completed
Push — develop ( 87b48c...882b48 )
by Gennady
23:47 queued 03:49
created

View_Settings::defaults()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 369

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 205
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 2
dl 0
loc 369
ccs 205
cts 205
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 182
	public static function defaults( $detailed = false, $group = null ) {
48
49 182
		$default_settings = array_merge( array(
50 182
			'id' => array(
51 182
				'label'             => __( 'View ID', 'gravityview' ),
52 182
				'type'              => 'number',
53 182
				'group'             => 'default',
54
				'value'             => NULL,
55
				'tooltip'           => NULL,
56
				'show_in_shortcode' => false,
57
			),
58
			'page_size' => array(
59 182
				'label'             => __( 'Number of entries per page', 'gravityview' ),
60 182
				'type'              => 'number',
61 182
				'class'             => 'small-text',
62 182
				'group'             => 'default',
63 182
				'value'             => 25,
64
				'show_in_shortcode' => true,
65
			),
66
			'offset' => array(
67 182
				'label'             => __( 'Offset entries starting from', 'gravityview' ),
68 182
				'type'              => 'number',
69 182
				'class'             => 'small-text',
70 182
				'group'             => 'default',
71 182
				'value'             => 0,
72
				'show_in_shortcode' => true,
73
			),
74
			'lightbox' => array(
75 182
				'label'             => __( 'Enable lightbox for images', 'gravityview' ),
76 182
				'type'              => 'checkbox',
77 182
				'group'             => 'default',
78 182
				'value'             => 1,
79
				'tooltip'           => NULL,
80
				'show_in_shortcode' => true,
81
			),
82
			'show_only_approved'    => array(
83 182
				'label'             => __( 'Show only approved entries', 'gravityview' ),
84 182
				'type'              => 'checkbox',
85 182
				'group'             => 'default',
86 182
				'value'             => 0,
87
				'show_in_shortcode' => true,
88
			),
89
			'admin_show_all_statuses' => array(
90 182
				'label'             => __( 'Show all entries to administrators', 'gravityview' ),
91 182
				'desc'              => __( 'Administrators will be able to see entries with any approval status.', 'gravityview' ),
92 182
				'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 182
				'requires'          => 'show_only_approved',
94 182
				'type'              => 'checkbox',
95 182
				'group'             => 'default',
96 182
				'value'             => 0,
97
				'show_in_shortcode' => false,
98
			),
99
			'hide_until_searched' => array(
100 182
				'label'             => __( 'Hide View data until search is performed', 'gravityview' ),
101 182
				'type'              => 'checkbox',
102 182
				'group'             => 'default',
103 182
				'tooltip'           => __( 'When enabled it will only show any View entries after a search is performed.', 'gravityview' ),
104 182
				'value'             => 0,
105
				'show_in_shortcode' => false,
106
			),
107
			'hide_empty' => array(
108 182
				'label'             => __( 'Hide empty fields', 'gravityview' ),
109 182
				'group'             => 'default',
110 182
				'type'              => 'checkbox',
111 182
				'value'             => 1,
112
				'show_in_shortcode' => false,
113
			),
114
			'edit_feeds' => array(
115 182
				'label'             => __( 'Feeds', 'gravityview' ),
116 182
				'group'             => 'default',
117 182
				'type'              => 'checkbox',
118
				'value'             => array(),
119
				'show_in_shortcode' => false,
120
			),
121
			'user_edit' => array(
122 182
				'label'             => __( 'Allow User Edit', 'gravityview' ), 'group'             => 'default',
123 182
				'desc'              => __( 'Allow logged-in users to edit entries they created.', 'gravityview' ),
124 182
				'value'             => 0,
125 182
				'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' ),
126 182
				'type'              => 'checkbox',
127
				'show_in_shortcode' => true,
128
			),
129
			'unapprove_edit' => array(
130 182
				'label'             => __( 'Unapprove Entries After Edit', 'gravityview' ),
131 182
				'group'             => 'default',
132 182
				'requires'          => 'user_edit',
133 182
				'desc'              => __( 'When an entry is edited by a non-administrator, reset the approval status to "Unapproved".', 'gravityview' ),
134 182
				'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' ),
135 182
				'value'             => 0,
136 182
				'type'              => 'checkbox',
137
				'show_in_shortcode' => true,
138
			),
139
			'user_delete' => array(
140 182
				'label'             => __( 'Allow User Delete', 'gravityview' ),
141 182
				'group'             => 'default',
142 182
				'desc'              => __( 'Allow logged-in users to delete entries they created.', 'gravityview' ),
143 182
				'value'             => 0,
144 182
				'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' ),
145 182
				'type'              => 'checkbox',
146
				'show_in_shortcode' => true,
147
			),
148
			'user_duplicate' => array(
149 182
				'label'             => __( 'Allow User Duplicate', 'gravityview' ),
150 182
				'group'             => 'default',
151 182
				'desc'              => __( 'Allow logged-in users to duplicate entries they created.', 'gravityview' ),
152 182
				'value'             => 0,
153 182
				'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' ),
154 182
				'type'              => 'checkbox',
155
				'show_in_shortcode' => true,
156
			),
157
			'sort_field' => array(
158 182
				'label'             => __( 'Sort by field', 'gravityview' ),
159 182
				'type'              => 'select',
160 182
				'desc'              => __( 'By default, entries are sorted by Entry ID.', 'gravityview' ),
161 182
				'value'             => '',
162 182
				'group'             => 'sort',
163
				'options'           => array(
164 182
										''             => __( 'Default', 'gravityview' ),
165 182
										'date_created' => __( 'Date Created', 'gravityview' ),
166
									),
167
				'show_in_shortcode' => true,
168
			),
169
			'sort_direction' => array(
170 182
				'label'             => __( 'Sort direction', 'gravityview' ),
171 182
				'type'              => 'select',
172 182
				'value'             => 'ASC',
173 182
				'group'             => 'sort',
174
				'options'           => array(
175 182
										'ASC'  => __( 'ASC', 'gravityview' ),
176 182
										'DESC' => __( 'DESC', 'gravityview' ),
177
				),
178
				'show_in_shortcode' => true,
179
			),
180
			'sort_field_2' => array(
181 182
				'label'             => __( 'Sort by secondary field', 'gravityview' ),
182 182
				'type'              => 'select',
183 182
				'value'             => '',
184 182
				'group'             => 'sort',
185
				'options'           => array(
186 182
					''             => __( 'Default', 'gravityview' ),
187 182
					'date_created' => __( 'Date Created', 'gravityview' ),
188
				),
189 182
				'requires_not'          => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
190
				'show_in_shortcode' => true,
191
			),
192
			'sort_direction_2' => array(
193 182
				'label'             => __( 'Secondary sort direction', 'gravityview' ),
194 182
				'type'              => 'select',
195 182
				'value'             => 'ASC',
196 182
				'group'             => 'sort',
197
				'options'           => array(
198 182
					'ASC'  => __( 'ASC', 'gravityview' ),
199 182
					'DESC' => __( 'DESC', 'gravityview' ),
200
				),
201 182
				'requires_not'      => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
202
				'show_in_shortcode' => true,
203
			),
204
			'sort_columns' => array(
205 182
				'label'             => __( 'Enable sorting by column', 'gravityview' ),
206 182
				'left_label'        => __( 'Column Sorting', 'gravityview' ),
207 182
				'type'              => 'checkbox',
208
				'value'             => false,
209 182
				'group'             => 'sort',
210
				'tooltip'           => NULL,
211
				'show_in_shortcode' => true,
212
				'show_in_template'  => array( 'default_table', 'preset_business_data', 'preset_issue_tracker', 'preset_resume_board', 'preset_job_board' ),
213
			),
214
			'start_date' => array(
215 182
				'label'             => __( 'Filter by Start Date', 'gravityview' ),
216 182
				'class'             => 'gv-datepicker',
217 182
				'desc'              => __( 'Show entries submitted after this date. Supports relative dates, such as "-1 week" or "-1 month".', 'gravityview' ),
218 182
				'type'              => 'text',
219 182
				'value'             => '',
220 182
				'group'             => 'filter',
221
				'show_in_shortcode' => true,
222
			),
223
			'end_date' => array(
224 182
				'label'             => __( 'Filter by End Date', 'gravityview' ),
225 182
				'class'             => 'gv-datepicker',
226 182
				'desc'              => __( 'Show entries submitted before this date. Supports relative dates, such as "now" or "-3 days".', 'gravityview' ),
227 182
				'type'              => 'text',
228 182
				'value'             => '',
229 182
				'group'             => 'filter',
230
				'show_in_shortcode' => true,
231
			),
232
			'class' => array(
233 182
				'label'             => __( 'CSS Class', 'gravityview' ),
234 182
				'desc'              => __( 'CSS class to add to the wrapping HTML container.', 'gravityview' ),
235 182
				'group'             => 'default',
236 182
				'type'              => 'text',
237 182
				'value'             => '',
238
				'show_in_shortcode' => false,
239
			),
240
			'search_value' => array(
241 182
				'label'             => __( 'Search Value', 'gravityview' ),
242 182
				'desc'              => __( 'Define a default search value for the View', 'gravityview' ),
243 182
				'type'              => 'text',
244 182
				'value'             => '',
245 182
				'group'             => 'filter',
246
				'show_in_shortcode' => false,
247
			),
248
			'search_field' => array(
249 182
				'label'             => __( 'Search Field', 'gravityview' ),
250 182
				'desc'              => __( 'If Search Value is set, you can define a specific field to search in. Otherwise, all fields will be searched.', 'gravityview' ),
251 182
				'type'              => 'text',
252 182
				'value'             => '',
253 182
				'group'             => 'filter',
254
				'show_in_shortcode' => false,
255
			),
256
			'search_operator' => array(
257 182
				'label'             => __( 'Search Operator', 'gravityview' ),
258 182
				'type'              => 'operator',
259 182
				'value'             => 'contains',
260 182
				'group'             => 'filter',
261
				'show_in_shortcode' => false,
262
			),
263
			'single_title' => array(
264 182
				'label'             => __( 'Single Entry Title', 'gravityview' ),
265 182
				'type'              => 'text',
266 182
				'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' ),
267 182
				'group'             => 'default',
268 182
				'value'             => '',
269
				'show_in_shortcode' => false,
270
				'full_width'        => true,
271
			),
272
			'back_link_label' => array(
273 182
				'label'             => __( 'Back Link Label', 'gravityview' ),
274 182
				'group'             => 'default',
275 182
				'desc'              => __( 'The text of the link that returns to the multiple entries view.', 'gravityview' ),
276 182
				'type'              => 'text',
277 182
				'value'             => '',
278
				'show_in_shortcode' => false,
279
				'full_width'        => true,
280
			),
281
			'edit_redirect' => array(
282 182
				'label'             => __( 'Redirect After Editing', 'gravityview' ),
283 182
				'group'             => 'default',
284 182
				'desc'              => __( 'The page to redirect to after editing an entry.', 'gravityview' ),
285 182
				'type'              => 'select',
286 182
				'value'             => '',
287
				'options'           => array(
288 182
					'' => __( 'Stay on Edit Entry', 'gravityview' ),
289 182
					'0'  => __( 'Redirect to Single Entry', 'gravityview' ),
290 182
					'1' => __( 'Redirect to Multiple Entries', 'gravityview' ),
291 182
					'2' => __( 'Redirect to URL', 'gravityview' ),
292
				),
293
				'show_in_shortcode' => false,
294
				'full_width'        => true,
295
			),
296
			'edit_redirect_url' => array(
297 182
				'label'             => __( 'Edit Entry Redirect URL', 'gravityview' ),
298 182
				'group'             => 'default',
299 182
				'desc'              => __( 'After editing an entry, the user will be taken to this URL.', 'gravityview' ),
300 182
				'type'              => 'text',
301 182
				'class'             => 'code widefat',
302 182
				'value'             => '',
303 182
				'requires'          => 'edit_redirect=2',
304 182
				'merge_tags'        => 'force',
305
			),
306
			'embed_only' => array(
307 182
				'label'             => __( 'Prevent Direct Access', 'gravityview' ),
308 182
				'group'             => 'default',
309 182
				'desc'              => __( 'Only allow access to this View when embedded using the shortcode.', 'gravityview' ),
310 182
				'type'              => 'checkbox',
311 182
				'value'             => '',
312
				'tooltip'           => false,
313
				'show_in_shortcode' => false,
314
				'full_width'        => true,
315
			),
316 182
		), ( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) === '1' ) ) ?
317
			array(
318 182
				'rest_disable'          => array(
319 182
					'label'             => __( 'Prevent REST Access', 'gravityview' ),
320 182
					'group'             => 'default',
321 182
					'desc'              => __( 'Disable REST access to this View.', 'gravityview' ),
322 182
					'type'              => 'checkbox',
323 182
					'value'             => '',
324
					'tooltip'           => false,
325
					'show_in_shortcode' => false,
326
					'full_width'        => true,
327
				),
328 182
			) : array(),
329 182
		( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) !== '1' ) ) ?
330
			array(
331 1
				'rest_enable'           => array(
332 1
					'label'             => __( 'Allow REST Access', 'gravityview' ),
333 1
					'group'             => 'default',
334 1
					'desc'              => __( 'Enable REST access to this View.', 'gravityview' ),
335 1
					'type'              => 'checkbox',
336 1
					'value'             => '',
337
					'tooltip'           => false,
338
					'show_in_shortcode' => false,
339
					'full_width'        => true,
340
				),
341 182
			) : array(),
342
		array(
343 182
			'csv_enable'            => array(
344 182
				'label'             => __( 'Allow CSV Access', 'gravityview' ),
345 182
				'group'             => 'default',
346 182
				'desc'              => __( 'Enable CSV access to this View.', 'gravityview' ),
347 182
				'type'              => 'checkbox',
348 182
				'value'             => '',
349
				'tooltip'           => false,
350
				'show_in_shortcode' => false,
351
				'full_width'        => true,
352
			),
353
		),
354
		array(
355 182
			'csv_nolimit'           => array(
356 182
				'label'             => __( 'Show all in CSV', 'gravityview' ),
357 182
				'group'             => 'default',
358 182
				'requires'          => 'csv_enable',
359 182
				'desc'              => __( 'Do not limit the number of entries output in the CSV.', 'gravityview' ),
360 182
				'type'              => 'checkbox',
361 182
				'value'             => '',
362
				'tooltip'           => false,
363
				'show_in_shortcode' => false,
364
				'full_width'        => true,
365
			),
366
		),
367
		array(
368 182
			'post_id' => array(
369
				'type'              => 'number',
370
				'value'             => '',
371
				'show_in_shortcode' => false,
372
			),
373
		) );
374
375 182
		if ( version_compare( \GFCommon::$version, '2.3-beta-4', '>=' ) ) {
376 182
			$default_settings['sort_direction']['options']['RAND'] = __( 'Random', 'gravityview' );
377
		}
378
379
		/**
380
		 * @filter `gravityview_default_args` Modify the default settings for new Views
381
		 * @param[in,out] array $default_args Array of default args.
382
		 * @deprecated
383
		 * @see filter `gravityview/view/settings/defaults`
384
		 */
385 182
		$default_settings = apply_filters( 'gravityview_default_args', $default_settings );
386
387
		/**
388
		 * @filter `gravityview/view/defaults` Modify the default settings for new Views
389
		 * @param[in,out] array $default_settings Array of default settings.
390
		 */
391 182
		$default_settings = apply_filters( 'gravityview/view/settings/defaults', $default_settings );
392
393
		// By default, we only want the key => value pairing, not the whole array.
394 182
		if ( ! $detailed ) {
395 182
			$defaults = array();
396 182
			foreach( $default_settings as $key => $value ) {
397 182
				$defaults[ $key ] = $value['value'];
398
			}
399 182
			return $defaults;
400
401
		// But sometimes, we want all the details.
402
		} else {
403 10
			foreach ( $default_settings as $key => $value ) {
404
405
				// If the $group argument is set for the method,
406
				// ignore any settings that aren't in that group.
407 10
				if ( ! empty( $group ) && is_string( $group ) ) {
408 1
					if ( empty( $value['group'] ) || $value['group'] !== $group ) {
409 1
						unset( $default_settings[ $key ] );
410
					}
411
				}
412
			}
413 10
			return $default_settings;
414
		}
415
	}
416
417
	/**
418
	 * Turn to an $atts array as used around the old codebase.
419
	 *
420
	 * @internal
421
	 * @deprecated
422
	 *
423
	 * @return array
424
	 */
425 101
	public function as_atts() {
426 101
		$defaults = array_keys( self::defaults() );
427 101
		$_this = &$this;
428 101
		return array_combine( $defaults, array_map( function( $key ) use ( $_this ) {
429 101
			return $_this->get( $key );
430 101
		}, $defaults ) );
431
	}
432
}
433