Completed
Push — develop ( 2f7818...6928d3 )
by Gennady
12:35
created

View_Settings::defaults()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 347

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 180
CRAP Score 13.0412

Importance

Changes 0
Metric Value
cc 13
nc 12
nop 2
dl 0
loc 347
ccs 180
cts 192
cp 0.9375
crap 13.0412
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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