Completed
Push — develop ( 3f13ed...151ba0 )
by Zack
30:06 queued 21:30
created

View_Settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 399
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.02%

Importance

Changes 0
Metric Value
dl 0
loc 399
ccs 198
cts 202
cp 0.9802
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

3 Methods

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