Completed
Push — develop ( 936044...d510c0 )
by Zack
16:20
created

View_Settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.62%

Importance

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