Completed
Pull Request — develop (#1144)
by Zack
16:40
created

View_Settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.47%

Importance

Changes 0
Metric Value
dl 0
loc 324
ccs 154
cts 158
cp 0.9747
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 272 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
			'single_title' => array(
217 83
				'label'             => __( 'Single Entry Title', 'gravityview' ),
218 83
				'type'              => 'text',
219 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' ),
220 83
				'group'             => 'default',
221 83
				'value'             => '',
222
				'show_in_shortcode' => false,
223
				'full_width'        => true,
224
			),
225
			'back_link_label' => array(
226 83
				'label'             => __( 'Back Link Label', 'gravityview' ),
227 83
				'group'             => 'default',
228 83
				'desc'              => __( 'The text of the link that returns to the multiple entries view.', 'gravityview' ),
229 83
				'type'              => 'text',
230 83
				'value'             => '',
231
				'show_in_shortcode' => false,
232
				'full_width'        => true,
233
			),
234
			'embed_only' => array(
235 83
				'label'             => __( 'Prevent Direct Access', 'gravityview' ),
236 83
				'group'             => 'default',
237 83
				'desc'              => __( 'Only allow access to this View when embedded using the shortcode.', 'gravityview' ),
238 83
				'type'              => 'checkbox',
239 83
				'value'             => '',
240
				'tooltip'           => false,
241
				'show_in_shortcode' => false,
242
				'full_width'        => true,
243
			),
244 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...
245
			array(
246 83
				'rest_disable'          => array(
247 83
					'label'             => __( 'Prevent REST Access', 'gravityview' ),
248 83
					'group'             => 'default',
249 83
					'desc'              => __( 'Disable REST access to this View.', 'gravityview' ),
250 83
					'type'              => 'checkbox',
251 83
					'value'             => '',
252
					'tooltip'           => false,
253
					'show_in_shortcode' => false,
254
					'full_width'        => true,
255
				),
256 83
			) : array(),
257 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...
258
			array(
259 1
				'rest_enable'           => array(
260 1
					'label'             => __( 'Allow REST Access', 'gravityview' ),
261 1
					'group'             => 'default',
262 1
					'desc'              => __( 'Enable  REST access to this View.', 'gravityview' ),
263 1
					'type'              => 'checkbox',
264 1
					'value'             => '',
265
					'tooltip'           => false,
266
					'show_in_shortcode' => false,
267
					'full_width'        => true,
268
				),
269 83
			) : array(),
270
		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...
271 83
			'post_id' => array(
272
				'type'              => 'number',
273
				'value'             => '',
274
				'show_in_shortcode' => false,
275
			),
276
		) );
277
278 83
		if ( version_compare( \GFCommon::$version, '2.3-beta-4', '>=' ) ) {
279 83
			$default_settings['sort_direction']['options']['RAND'] = __( 'Random', 'gravityview' );
280
		}
281
282
		/**
283
		 * @filter `gravityview_default_args` Modify the default settings for new Views
284
		 * @param[in,out] array $default_args Array of default args.
285
		 * @deprecated
286
		 * @see filter `gravityview/view/settings/defaults`
287
		 */
288 83
		$default_settings = apply_filters( 'gravityview_default_args', $default_settings );
289
290
		/**
291
		 * @filter `gravityview/view/defaults` Modify the default settings for new Views
292
		 * @param[in,out] array $default_settings Array of default settings.
293
		 */
294 83
		$default_settings = apply_filters( 'gravityview/view/settings/defaults', $default_settings );
295
296
		// By default, we only want the key => value pairing, not the whole array.
297 83
		if ( ! $detailed ) {
298 83
			$defaults = array();
299 83
			foreach( $default_settings as $key => $value ) {
300 83
				$defaults[ $key ] = $value['value'];
301
			}
302 83
			return $defaults;
303
304
		// But sometimes, we want all the details.
305
		} else {
306 7
			foreach ( $default_settings as $key => $value ) {
307
308
				// If the $group argument is set for the method,
309
				// ignore any settings that aren't in that group.
310 7
				if ( ! empty( $group ) && is_string( $group ) ) {
311 1
					if ( empty( $value['group'] ) || $value['group'] !== $group ) {
312 7
						unset( $default_settings[ $key ] );
313
					}
314
				}
315
			}
316 7
			return $default_settings;
317
		}
318
	}
319
320
	/**
321
	 * Turn to an $atts array as used around the old codebase.
322
	 *
323
	 * @internal
324
	 * @deprecated
325
	 *
326
	 * @return array
327
	 */
328 44
	public function as_atts() {
329 44
		$defaults = array_keys( self::defaults() );
330 44
		$_this = &$this;
331 44
		return array_combine( $defaults, array_map( function( $key ) use ( $_this ) {
332 44
			return $_this->get( $key );
333 44
		}, $defaults ) );
334
	}
335
}
336