Completed
Push — develop ( bc44a9...0eb617 )
by Zack
55:36 queued 53:23
created

View_Settings::defaults()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 360

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 193
CRAP Score 13.0046

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 2
dl 0
loc 360
ccs 193
cts 199
cp 0.9698
crap 13.0046
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
			'sort_field' => array(
149 182
				'label'             => __( 'Sort by field', 'gravityview' ),
150 182
				'type'              => 'select',
151 182
				'desc'              => __( 'By default, entries are sorted by Entry ID.', 'gravityview' ),
152 182
				'value'             => '',
153 182
				'group'             => 'sort',
154
				'options'           => array(
155 182
										''             => __( 'Default', 'gravityview' ),
156 182
										'date_created' => __( 'Date Created', 'gravityview' ),
157
									),
158
				'show_in_shortcode' => true,
159
			),
160
			'sort_direction' => array(
161 182
				'label'             => __( 'Sort direction', 'gravityview' ),
162 182
				'type'              => 'select',
163 182
				'value'             => 'ASC',
164 182
				'group'             => 'sort',
165
				'options'           => array(
166 182
										'ASC'  => __( 'ASC', 'gravityview' ),
167 182
										'DESC' => __( 'DESC', 'gravityview' ),
168
				),
169
				'show_in_shortcode' => true,
170
			),
171
			'sort_field_2' => array(
172 182
				'label'             => __( 'Sort by secondary field', 'gravityview' ),
173 182
				'type'              => 'select',
174 182
				'value'             => '',
175 182
				'group'             => 'sort',
176
				'options'           => array(
177 182
					''             => __( 'Default', 'gravityview' ),
178 182
					'date_created' => __( 'Date Created', 'gravityview' ),
179
				),
180 182
				'requires_not'          => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
181
				'show_in_shortcode' => true,
182
			),
183
			'sort_direction_2' => array(
184 182
				'label'             => __( 'Secondary sort direction', 'gravityview' ),
185 182
				'type'              => 'select',
186 182
				'value'             => 'ASC',
187 182
				'group'             => 'sort',
188
				'options'           => array(
189 182
					'ASC'  => __( 'ASC', 'gravityview' ),
190 182
					'DESC' => __( 'DESC', 'gravityview' ),
191
				),
192 182
				'requires_not'      => 'sort_direction][=RAND', // ][ is for toggleRequired, so it ends in []
193
				'show_in_shortcode' => true,
194
			),
195
			'sort_columns' => array(
196 182
				'label'             => __( 'Enable sorting by column', 'gravityview' ),
197 182
				'left_label'        => __( 'Column Sorting', 'gravityview' ),
198 182
				'type'              => 'checkbox',
199
				'value'             => false,
200 182
				'group'             => 'sort',
201
				'tooltip'           => NULL,
202
				'show_in_shortcode' => true,
203
				'show_in_template'  => array( 'default_table', 'preset_business_data', 'preset_issue_tracker', 'preset_resume_board', 'preset_job_board' ),
204
			),
205
			'start_date' => array(
206 182
				'label'             => __( 'Filter by Start Date', 'gravityview' ),
207 182
				'class'             => 'gv-datepicker',
208 182
				'desc'              => __( 'Show entries submitted after this date. Supports relative dates, such as "-1 week" or "-1 month".', 'gravityview' ),
209 182
				'type'              => 'text',
210 182
				'value'             => '',
211 182
				'group'             => 'filter',
212
				'show_in_shortcode' => true,
213
			),
214
			'end_date' => array(
215 182
				'label'             => __( 'Filter by End Date', 'gravityview' ),
216 182
				'class'             => 'gv-datepicker',
217 182
				'desc'              => __( 'Show entries submitted before this date. Supports relative dates, such as "now" or "-3 days".', 'gravityview' ),
218 182
				'type'              => 'text',
219 182
				'value'             => '',
220 182
				'group'             => 'filter',
221
				'show_in_shortcode' => true,
222
			),
223
			'class' => array(
224 182
				'label'             => __( 'CSS Class', 'gravityview' ),
225 182
				'desc'              => __( 'CSS class to add to the wrapping HTML container.', 'gravityview' ),
226 182
				'group'             => 'default',
227 182
				'type'              => 'text',
228 182
				'value'             => '',
229
				'show_in_shortcode' => false,
230
			),
231
			'search_value' => array(
232 182
				'label'             => __( 'Search Value', 'gravityview' ),
233 182
				'desc'              => __( 'Define a default search value for the View', 'gravityview' ),
234 182
				'type'              => 'text',
235 182
				'value'             => '',
236 182
				'group'             => 'filter',
237
				'show_in_shortcode' => false,
238
			),
239
			'search_field' => array(
240 182
				'label'             => __( 'Search Field', 'gravityview' ),
241 182
				'desc'              => __( 'If Search Value is set, you can define a specific field to search in. Otherwise, all fields will be searched.', 'gravityview' ),
242 182
				'type'              => 'text',
243 182
				'value'             => '',
244 182
				'group'             => 'filter',
245
				'show_in_shortcode' => false,
246
			),
247
			'search_operator' => array(
248 182
				'label'             => __( 'Search Operator', 'gravityview' ),
249 182
				'type'              => 'operator',
250 182
				'value'             => 'contains',
251 182
				'group'             => 'filter',
252
				'show_in_shortcode' => false,
253
			),
254
			'single_title' => array(
255 182
				'label'             => __( 'Single Entry Title', 'gravityview' ),
256 182
				'type'              => 'text',
257 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' ),
258 182
				'group'             => 'default',
259 182
				'value'             => '',
260
				'show_in_shortcode' => false,
261
				'full_width'        => true,
262
			),
263
			'back_link_label' => array(
264 182
				'label'             => __( 'Back Link Label', 'gravityview' ),
265 182
				'group'             => 'default',
266 182
				'desc'              => __( 'The text of the link that returns to the multiple entries view.', 'gravityview' ),
267 182
				'type'              => 'text',
268 182
				'value'             => '',
269
				'show_in_shortcode' => false,
270
				'full_width'        => true,
271
			),
272
			'edit_redirect' => array(
273 182
				'label'             => __( 'Redirect After Editing', 'gravityview' ),
274 182
				'group'             => 'default',
275 182
				'desc'              => __( 'The page to redirect to after editing an entry.', 'gravityview' ),
276 182
				'type'              => 'select',
277 182
				'value'             => '',
278
				'options'           => array(
279 182
					'' => __( 'Stay on Edit Entry', 'gravityview' ),
280 182
					'0'  => __( 'Redirect to Single Entry', 'gravityview' ),
281 182
					'1' => __( 'Redirect to Multiple Entries', 'gravityview' ),
282 182
					'2' => __( 'Redirect to URL', 'gravityview' ),
283
				),
284
				'show_in_shortcode' => false,
285
				'full_width'        => true,
286
			),
287
			'edit_redirect_url' => array(
288 182
				'label'             => __( 'Edit Entry Redirect URL', 'gravityview' ),
289 182
				'group'             => 'default',
290 182
				'desc'              => __( 'After editing an entry, the user will be taken to this URL.', 'gravityview' ),
291 182
				'type'              => 'text',
292 182
				'class'             => 'code widefat',
293 182
				'value'             => '',
294 182
				'requires'          => 'edit_redirect=2',
295 182
				'merge_tags'        => 'force',
296
			),
297
			'embed_only' => array(
298 182
				'label'             => __( 'Prevent Direct Access', 'gravityview' ),
299 182
				'group'             => 'default',
300 182
				'desc'              => __( 'Only allow access to this View when embedded using the shortcode.', 'gravityview' ),
301 182
				'type'              => 'checkbox',
302 182
				'value'             => '',
303
				'tooltip'           => false,
304
				'show_in_shortcode' => false,
305
				'full_width'        => true,
306
			),
307 182
		), ( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) === '1' ) ) ?
308
			array(
309 182
				'rest_disable'          => array(
310 182
					'label'             => __( 'Prevent REST Access', 'gravityview' ),
311 182
					'group'             => 'default',
312 182
					'desc'              => __( 'Disable REST access to this View.', 'gravityview' ),
313 182
					'type'              => 'checkbox',
314 182
					'value'             => '',
315
					'tooltip'           => false,
316
					'show_in_shortcode' => false,
317
					'full_width'        => true,
318
				),
319 182
			) : array(),
320 182
		( gravityview()->plugin->supports( Plugin::FEATURE_REST ) && ( gravityview()->plugin->settings->get( 'rest_api' ) !== '1' ) ) ?
321
			array(
322
				'rest_enable'           => array(
323
					'label'             => __( 'Allow REST Access', 'gravityview' ),
324
					'group'             => 'default',
325
					'desc'              => __( 'Enable REST access to this View.', 'gravityview' ),
326
					'type'              => 'checkbox',
327
					'value'             => '',
328
					'tooltip'           => false,
329
					'show_in_shortcode' => false,
330
					'full_width'        => true,
331
				),
332 182
			) : array(),
333
		array(
334 182
			'csv_enable'            => array(
335 182
				'label'             => __( 'Allow CSV Access', 'gravityview' ),
336 182
				'group'             => 'default',
337 182
				'desc'              => __( 'Enable CSV access to this View.', 'gravityview' ),
338 182
				'type'              => 'checkbox',
339 182
				'value'             => '',
340
				'tooltip'           => false,
341
				'show_in_shortcode' => false,
342
				'full_width'        => true,
343
			),
344
		),
345
		array(
346 182
			'csv_nolimit'           => array(
347 182
				'label'             => __( 'Show all in CSV', 'gravityview' ),
348 182
				'group'             => 'default',
349 182
				'requires'          => 'csv_enable',
350 182
				'desc'              => __( 'Do not limit the number of entries output in the CSV.', 'gravityview' ),
351 182
				'type'              => 'checkbox',
352 182
				'value'             => '',
353
				'tooltip'           => false,
354
				'show_in_shortcode' => false,
355
				'full_width'        => true,
356
			),
357
		),
358
		array(
359 182
			'post_id' => array(
360
				'type'              => 'number',
361
				'value'             => '',
362
				'show_in_shortcode' => false,
363
			),
364
		) );
365
366 182
		if ( version_compare( \GFCommon::$version, '2.3-beta-4', '>=' ) ) {
367 182
			$default_settings['sort_direction']['options']['RAND'] = __( 'Random', 'gravityview' );
368
		}
369
370
		/**
371
		 * @filter `gravityview_default_args` Modify the default settings for new Views
372
		 * @param[in,out] array $default_args Array of default args.
373
		 * @deprecated
374
		 * @see filter `gravityview/view/settings/defaults`
375
		 */
376 182
		$default_settings = apply_filters( 'gravityview_default_args', $default_settings );
377
378
		/**
379
		 * @filter `gravityview/view/defaults` Modify the default settings for new Views
380
		 * @param[in,out] array $default_settings Array of default settings.
381
		 */
382 182
		$default_settings = apply_filters( 'gravityview/view/settings/defaults', $default_settings );
383
384
		// By default, we only want the key => value pairing, not the whole array.
385 182
		if ( ! $detailed ) {
386 182
			$defaults = array();
387 182
			foreach( $default_settings as $key => $value ) {
388 182
				$defaults[ $key ] = $value['value'];
389
			}
390 182
			return $defaults;
391
392
		// But sometimes, we want all the details.
393
		} else {
394 10
			foreach ( $default_settings as $key => $value ) {
395
396
				// If the $group argument is set for the method,
397
				// ignore any settings that aren't in that group.
398 10
				if ( ! empty( $group ) && is_string( $group ) ) {
399 1
					if ( empty( $value['group'] ) || $value['group'] !== $group ) {
400 1
						unset( $default_settings[ $key ] );
401
					}
402
				}
403
			}
404 10
			return $default_settings;
405
		}
406
	}
407
408
	/**
409
	 * Turn to an $atts array as used around the old codebase.
410
	 *
411
	 * @internal
412
	 * @deprecated
413
	 *
414
	 * @return array
415
	 */
416 99
	public function as_atts() {
417 99
		$defaults = array_keys( self::defaults() );
418 99
		$_this = &$this;
419 99
		return array_combine( $defaults, array_map( function( $key ) use ( $_this ) {
420 99
			return $_this->get( $key );
421 99
		}, $defaults ) );
422
	}
423
}
424