Completed
Push — develop ( 73d878...fd8780 )
by Zack
22:43 queued 18:42
created

View_Settings::with_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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