Completed
Push — develop ( 98d972...52c683 )
by Zack
28:40 queued 12:29
created

View_Settings::as_atts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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