Completed
Push — develop ( aad3f3...839a66 )
by Zack
15:14 queued 02:19
created

View_Settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.35%

Importance

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