Completed
Push — develop ( a060f1...c1e96d )
by Zack
24:07 queued 18:16
created

View_Settings::defaults()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 359

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 198
CRAP Score 13

Importance

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