Completed
Pull Request — develop (#1572)
by Zack
19:45
created

render_field_options()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 170

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
cc 24
nc 27464
nop 11
dl 0
loc 170
ccs 0
cts 55
cp 0
crap 600
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Renders field/widget options and view settings
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    GravityView <[email protected]>
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 *
11
 * @since 1.2
12
 */
13
14
class GravityView_Render_Settings {
15
16
	/**
17
	 * Get available field groups.
18
	 *
19
	 * @since 2.10
20
	 *
21
	 * @return array
22
	 */
23
	public static function get_field_groups() {
24
25
		return array(
26
			'field'      => _x( 'Field', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
27 2
			'display'    => _x( 'Display', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
28
			'label'      => _x( 'Label', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
29 2
			'visibility' => _x( 'Visibility', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
30
			'advanced'   => _x( 'Advanced', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
31 2
			'default'    => _x( 'Default', 'Denotes the name under which certain field settings are grouped', 'gravityview' ),
32
		);
33 2
	}
34
35
	/**
36
	 * Get the default options for a standard field.
37 2
	 *
38 2
	 * @since 2.10 added $grouped parameter
39 2
	 *
40 2
	 * @param  string      $field_type  Type of field options to render (`field` or `widget`)
41
	 * @param  string      $template_id Layout slug (`default_table`, `default_list`, `datatables_table`, etc.
42
	 * @param  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
43 2
	 * @param  string      $context     What context are we in? Example: `single` or `directory`
44 2
	 * @param  string      $input_type  (textarea, list, select, etc.)
45 2
	 * @param  int         $form_id     The form ID. @since develop
46 2
	 * @param  bool        $grouped     Whether to group the field settings by `group` key
47
	 *
48
	 * @return array       Array of field options with `label`, `value`, `type`, `default` keys
49
	 */
50 2
	public static function get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id, $grouped = false ) {
51 2
52 2
		$field_options = array();
53 2
54
		$is_table_layout = preg_match( '/table/ism', $template_id );
55 2
56 2
		if( 'field' === $field_type ) {
57
58
			// Default options - fields
59 2
			$field_options = array(
60 2
				'show_label' => array(
61 2
					'type' => 'checkbox',
62
					'label' => __( 'Show Label', 'gravityview' ),
63
					'value' => ! empty ( $is_table_layout ),
64 2
					'priority' => 1000,
65 2
					'group' => 'label',
66 2
				),
67 2
				'custom_label' => array(
68 2
					'type' => 'text',
69
					'label' => __( 'Custom Label:', 'gravityview' ),
70
					'value' => '',
71
					'merge_tags' => true,
72
					'class'      => 'widefat',
73 2
					'priority' => 1100,
74 1
					'requires' => 'show_label',
75 1
					'group' => 'label',
76 1
				),
77 1
				'custom_class' => array(
78 1
					'type'       => 'text',
79 1
					'label'      => __( 'Custom CSS Class:', 'gravityview' ),
80
					'desc'       => __( 'This class will be added to the field container', 'gravityview' ),
81
					'value'      => '',
82
					'merge_tags' => true,
83
					'tooltip'    => 'gv_css_merge_tags',
84
					'class'      => 'widefat code',
85
					'priority' => 5000,
86
					'group' => 'advanced',
87
				),
88
				'only_loggedin' => array(
89
					'type' => 'checkbox',
90
					'label' => __( 'Make visible only to logged-in users?', 'gravityview' ),
91
					'value' => '',
92
					'priority' => 4000,
93
					'group' => 'visibility',
94 2
				),
95
				'only_loggedin_cap' => array(
96
					'type' => 'select',
97
					'label' => __( 'Make visible for:', 'gravityview' ),
98
					'options' => self::get_cap_choices( $template_id, $field_id, $context, $input_type ),
99
					'class' => 'widefat',
100
					'value' => 'read',
101
					'priority' => 4100,
102
					'requires' => 'only_loggedin',
103
					'group' => 'visibility',
104
				),
105 2
			);
106
107 2
			// Match Table as well as DataTables
108
			if( $is_table_layout && 'directory' === $context ) {
109
				$field_options['width'] = array(
110
					'type' => 'number',
111
					'label' => __('Percent Width', 'gravityview'),
112
					'desc' => __( 'Leave blank for column width to be based on the field content.', 'gravityview'),
113
					'class' => 'code widefat',
114
					'value' => '',
115
					'priority' => 200,
116
					'group' => 'display',
117
				);
118
			}
119
120
		}
121 1
122
		// Remove suffix ":" from the labels to standardize style. Using trim() instead of rtrim() for i18n.
123
		foreach ( $field_options as $key => $field_option ) {
124 1
			$field_options[ $key ]['label'] = trim( $field_options[ $key ]['label'], ':' );
125 1
		}
126 1
127 1
		/**
128 1
		 * @filter `gravityview_template_{$field_type}_options` Filter the field options by field type. Filter names: `gravityview_template_field_options` and `gravityview_template_widget_options`
129 1
		 * @param[in,out] array    Array of field options with `label`, `value`, `type`, `default` keys
130
		 * @param[in]  string      $template_id Table slug
131
		 * @param[in]  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
132 1
		 * @param[in]  string      $context     What context are we in? Example: `single` or `directory`
133
		 * @param[in]  string      $input_type  (textarea, list, select, etc.)
134
		 * @param[in]  int         $form_id     The form ID. {@since 2.5}
135
		 */
136
		$field_options = apply_filters( "gravityview_template_{$field_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
137
138
		/**
139
		 * @filter `gravityview_template_{$input_type}_options` Filter the field options by input type (`$input_type` examples: `textarea`, `list`, `select`, etc.)
140
		 * @param[in,out] array    Array of field options with `label`, `value`, `type`, `default` keys
141
		 * @param[in]  string      $template_id Table slug
142
		 * @param[in]  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
143
		 * @param[in]  string      $context     What context are we in? Example: `single` or `directory`
144
		 * @param[in]  string      $input_type  (textarea, list, select, etc.)
145
		 * @param[in]  int         $form_id     The form ID. {@since 2.5}
146 1
		 */
147
		$field_options = apply_filters( "gravityview_template_{$input_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
148 1
149
		if ( $grouped ) {
150
151
			$option_groups = array();
152
153
			foreach ( $field_options as $key => $field_option ) {
154
155
				// TODO: Add filter to override instead of doing inline.
156
				switch ( $key ) {
157
					case 'show_as_link':
158
						$_group = 'display';
159
						$field_option['priority'] = 100;
160
						break;
161
					default:
162
						$_group = \GV\Utils::get( $field_option, 'group', 'display' );
163
						break;
164
				}
165
166
				$option_groups[ $_group ][ $key ] = $field_option;
167
			}
168
169
			foreach ( $option_groups as & $option_group ) {
170
				uasort( $option_group, array( __CLASS__, '_sort_by_priority' ) );
171 1
			}
172
173 1
			$field_options = array();
174
			foreach ( self::get_field_groups() as $group_key => $group_name  ) {
175
				$field_options[ $group_key ] = \GV\Utils::get( $option_groups, $group_key, array() );
176
			}
177
178
		} else {
179 1
			uasort( $field_options, array( __CLASS__, '_sort_by_priority' ) );
180
		}
181
182 1
		return $field_options;
183
	}
184
185 1
	/**
186 1
	 * Sort field settings by the `priority` key
187 1
	 *
188 1
	 * Default priority is 10001. Lower is higher.
189 1
	 *
190
	 * @since 3.0
191
	 * @internal
192
	 *
193 1
	 * @param array $a
194
	 * @param array $b
195
	 */
196
	static public function _sort_by_priority( $a, $b ) {
197
198
		$a_priority = \GV\Utils::get( $a, 'priority', 10001 );
199
		$b_priority = \GV\Utils::get( $b, 'priority', 10001 );
200
201 1
		if ( $a_priority === $b_priority ) {
202
			return 0;
203
		}
204
205
		return ( $a_priority < $b_priority ) ? - 1 : 1;
206 1
	}
207
208
	/**
209
	 * Get capabilities options for GravityView
210 1
	 *
211
	 * Parameters are only to pass to the filter.
212 1
	 *
213
	 * @param  string $template_id Optional. View slug
214 1
	 * @param  string $field_id    Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
215
	 * @param  string $context     Optional. What context are we in? Example: `single` or `directory`
216
	 * @param  string $input_type  Optional. (textarea, list, select, etc.)
217 1
	 * @return array Associative array, with the key being the capability and the value being the label shown.
218
	 */
219
	static public function get_cap_choices( $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
220
221 1
		$select_cap_choices = array(
222
			'read' => __( 'Any Logged-In User', 'gravityview' ),
223 1
			'publish_posts' => __( 'Author Or Higher', 'gravityview' ),
224
			'gravityforms_view_entries' => __( 'Can View Gravity Forms Entries', 'gravityview' ),
225
			'delete_others_posts' => __( 'Editor Or Higher', 'gravityview' ),
226
			'gravityforms_edit_entries' => __( 'Can Edit Gravity Forms Entries', 'gravityview' ),
227 1
			'manage_options' => __( 'Administrator', 'gravityview' ),
228
		);
229
230
		if( is_multisite() ) {
231
			$select_cap_choices['manage_network'] = __('Multisite Super Admin', 'gravityview' );
232 1
		}
233
234 1
		/**
235
		 * @filter `gravityview_field_visibility_caps` Modify the capabilities shown in the field dropdown
236
		 * @see https://docs.gravityview.co/article/96-how-to-modify-capabilities-shown-in-the-field-only-visible-to-dropdown
237
		 * @since  1.0.1
238
		 * @param  array $select_cap_choices Associative rray of role slugs with labels ( `manage_options` => `Administrator` )
239
		 * @param  string $template_id Optional. View slug
240
		 * @param  string $field_id    Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
241
		 * @param  string $context     Optional. What context are we in? Example: `single` or `directory`
242
		 * @param  string $input_type  Optional. (textarea, list, select, etc.)
243
		 */
244
		$select_cap_choices = apply_filters('gravityview_field_visibility_caps', $select_cap_choices, $template_id, $field_id, $context, $input_type );
245
246
		return $select_cap_choices;
247
	}
248
249
250
	/**
251
	 * Render Field Options html (shown through a dialog box)
252
	 *
253
	 * @see GravityView_Ajax::get_field_options
254
	 * @see GravityView_Admin_Views::render_active_areas
255
	 *
256
	 * @param string $form_id
257
	 * @param string $field_type field / widget
258
	 * @param string $template_id
259
	 * @param string $field_id
260
	 * @param string $field_label
261
	 * @param string $area
262
	 * @param string $uniqid (default: '')
263
	 * @param string $current (default: '')
264
	 * @param string $context (default: 'single')
265
	 * @param array $item Field or widget array that's being rendered
266
	 *
267
	 * @return string HTML of dialog box
268
	 */
269
	public static function render_field_options( $form_id, $field_type, $template_id, $field_id, $field_label, $area, $input_type = NULL, $uniqid = '', $current = '', $context = 'single', $item = array() ) {
270
271
		if( empty( $uniqid ) ) {
272
			//generate a unique field id
273
			$uniqid = uniqid('', false);
274
		}
275
276
		$grouped = ( 'field' === $field_type );
277
278
		// get field/widget options
279
		$option_groups = self::get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id, $grouped );
280
281
		if( ! $grouped ) {
282
			$option_groups = array( $option_groups );
283
		}
284
285
		$option_groups = array_filter( $option_groups );
286
287
		// two different post arrays, depending of the field type
288
		$name_prefix = $field_type .'s' .'['. $area .']['. $uniqid .']';
289
290
		// build output
291
		$hidden_fields  = '<input type="hidden" class="field-key" name="'. $name_prefix .'[id]" value="'. esc_attr( $field_id ) .'">';
292
		$hidden_fields .= '<input type="hidden" class="field-label" name="'. $name_prefix .'[label]" value="'. esc_attr( $field_label ) .'">';
293
294
		$form_title = '';
295
		if ( $form_id ) {
296
			$hidden_fields .= '<input type="hidden" class="field-form-id" name="'. $name_prefix .'[form_id]" value="'. esc_attr( $form_id ) .'">';
297
			$form = GVCommon::get_form( $form_id );
298
			$form_title = $form['title'];
299
		}
300
301
		// If there are no options, return what we got.
302
		if ( empty( $option_groups ) ) {
303
			return $hidden_fields . '<!-- No Options -->'; // The HTML comment is here for checking if the output is empty in render_label()
304
		}
305
306
		$settings_title = esc_attr( sprintf( __( '%s Settings', 'gravityview' ) , strip_tags( html_entity_decode( $field_label ) ) ) );
307
308
		$field_details = '';
309
310
		// Get the pretty name for the input type
311
		$gv_field = GravityView_Fields::get( $input_type );
312
313
		if( $gv_field ) {
314
			$input_type_label = $gv_field->label;
315
		} else {
316
			$input_type_label = $input_type;
317
		}
318
319
		$field_settings = '';
320
		foreach ( $option_groups as $group_key => $option_group ) {
321
322
			if ( empty( $option_group ) ) {
323
				continue;
324
			}
325
326
			if ( $grouped ) {
327
				$group_name     = rgar( self::get_field_groups(), $group_key, '' );
328
				$field_settings .= '<fieldset class="item-settings-group item-settings-group-' . esc_attr( $group_key ) . '">';
329
				$field_settings .= '<legend>' . esc_attr( $group_name ) . '</legend>';
330
			}
331
332
			foreach ( $option_group as $key => $option ) {
333
334
				$value = isset( $current[ $key ] ) ? $current[ $key ] : null;
335
336
				$field_output = self::render_field_option( $name_prefix . '[' . $key . ']', $option, $value );
337
338
				// The setting is empty
339
				if ( empty( $field_output ) ) {
340
					continue;
341
				}
342
343
				$show_if = '';
344
				if ( ! empty( $option['requires'] ) ) {
345
					$show_if .= sprintf( ' data-requires="%s"', $option['requires'] );
346
				}
347
348
				if ( ! empty( $option['requires_not'] ) ) {
349
					$show_if .= sprintf( ' data-requires-not="%s"', $option['requires_not'] );
350
				}
351
352
				switch ( $option['type'] ) {
353
					// Hide hidden fields
354
					case 'hidden':
355
						$field_settings .= '<div class="gv-setting-container gv-setting-container-' . esc_attr( $key ) . ' screen-reader-text">' . $field_output . '</div>';
356
						break;
357
					default:
358
						$field_settings .= '<div class="gv-setting-container gv-setting-container-' . esc_attr( $key ) . '" ' . $show_if . '>' . $field_output . '</div>';
359
				}
360
			}
361
362
			if ( $grouped ) {
363
				$field_settings .= '</fieldset>';
364
			}
365
		}
366
367
		$item_details = '';
368
		$subtitle = '';
369
370
		if( 'field' === $field_type ) {
371
			$subtitle = ! empty( $item['subtitle'] ) ? '<div class="subtitle">' . $item['subtitle'] . '</div>' : '';
372
373
			$item_details .= '
374
			<div class="gv-field-details--container">
375
				<label class="gv-field-details--toggle">' . esc_html__( 'Field Details', 'gravityview' ) .' <i class="dashicons dashicons-arrow-down"></i></label>
376
				<section class="gv-field-details gv-field-details--closed">';
377
378
				if ( $field_id && is_numeric( $field_id ) ) {
379
				$item_details .= '
380
					<div class="gv-field-detail gv-field-detail--field">
381
						<span class="gv-field-detail--label">' . esc_html__( 'Field ID', 'gravityview' ) .'</span><span class="gv-field-detail--value">#{{field_id}}</span>
382
					</div>';
383
			    }
384
385
				$item_details .= '
386
					<div class="gv-field-detail gv-field-detail--type">
387
						<span class="gv-field-detail--label">' . esc_html_x( 'Type', 'The type of field being configured (eg: "Single Line Text")', 'gravityview' ) .'</span><span class="gv-field-detail--value">{{input_type_label}}</span>
388
					</div>';
389
390
				if( $form_id ) {
391
					$item_details .= '
392
					<div class="gv-field-detail gv-field-detail--form">
393
						<span class="gv-field-detail--label">' . esc_html__( 'Form', 'gravityview' ) .'</span><span class="gv-field-detail--value">{{form_title}} (#{{form_id}})</span>
394
					</div>';
395
				}
396
				$item_details .= '
397
				</section>
398
			</div>';
399
		} else {
400
			$widget_details_content = rgar( $item, 'description', '' );
401
			if ( ! empty( $item['subtitle'] ) ) {
402
				$widget_details_content .= ( '' !== $widget_details_content ) ? "\n\n" . $item['subtitle'] : $item['subtitle'];
403
			}
404
405
			// Intentionally not escaping to allow HTML.
406
			$item_details = '<div class="gv-field-details--container">' . wpautop( trim( $widget_details_content ) ) . '</div>';
407
		}
408
409
$template = <<<EOD
410
		<div class="gv-dialog-options" title="{{settings_title}}">
411
			{{item_details}}
412
			{{subtitle}}
413
			{{field_settings}}
414
			{{hidden_fields}}
415
		</div>
416
EOD;
417
418
		$output = $template;
419
420
		$replacements = array(
421
			'settings_title',
422
			'hidden_fields',
423
			'subtitle',
424
			'field_settings',
425
			'item_details',
426
			'input_type_label',
427
			'field_id',
428
			'form_title',
429
			'form_id',
430
		);
431
432
		foreach ( $replacements as $replacement ) {
433
			$output = str_replace( '{{' . $replacement . '}}', ${$replacement}, $output );
434
		}
435
436
		return $output;
437
438
	}
439
440
441
442
	/**
443
	 * Handle rendering a field option form element
444
	 *
445
	 * @param  string     $name    Input `name` attribute
446
	 * @param  array      $option  Associative array of options. See the $defaults variable for available keys.
447
	 * @param  mixed      $curr_value Current value of option
448
	 * @return string     HTML output of option
449
	 */
450
	public static function render_field_option( $name = '', $option = array(), $curr_value = NULL ) {
451
452
		$output = '';
453
454
		/**
455
		 * @deprecated setting index 'default' was replaced by 'value'
456
		 * @see GravityView_FieldType::get_field_defaults
457
		 */
458
		if( !empty( $option['default'] ) && empty( $option['value'] ) ) {
459
			$option['value'] = $option['default'];
460
			_deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[value] instead of [default] when defining the setting '. $name .' details' );
461
		}
462
463
		// prepare to render option field type
464
		if( isset( $option['type'] ) ) {
465
466
			$type_class = self::load_type_class( $option );
467
468
			if( class_exists( $type_class ) ) {
469
470
				/** @type GravityView_FieldType $render_type */
471
				$render_type = new $type_class( $name, $option, $curr_value );
472
473
				ob_start();
474
475
				$render_type->render_option();
476
477
				$output = ob_get_clean();
478
479
				/**
480
				 * @filter `gravityview/option/output/{option_type}` Modify the output for a GravityView setting.\n
481
				 * `$option_type` is the type of setting (`radio`, `text`, etc.)
482
				 * @param[in,out] string $output field class name
483
				 * @param[in] array $option  option field data
484
				 */
485
				$output = apply_filters( "gravityview/option/output/{$option['type']}" , $output, $option );
486
			}
487
488
		} // isset option[type]
489
490
		return $output;
491
	}
492
493
494
495
496
497
498
	/**
499
	 * Output a table row for view settings
500
	 * @param  string $key              The key of the input
501
	 * @param  array  $current_settings Associative array of current settings to use as input values, if set. If not set, the defaults are used.
502
	 * @param  string $override_input   [description]
503
	 * @param  string $name             [description]
504
	 * @param  string $id               [description]
505
	 * @return void                   [description]
506
	 */
507
	public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
508
509
		$settings = \GV\View_Settings::with_defaults( true );
510
511
		// If the key doesn't exist, there's something wrong.
512
		if ( ! $setting = $settings->get( $key ) ) {
513
			return;
514
		}
515
516
		/**
517
		 * @deprecated setting index 'name' was replaced by 'label'
518
		 * @see GravityView_FieldType::get_field_defaults
519
		 */
520
		if( isset( $setting['name'] ) && empty( $setting['label'] ) ) {
521
			$setting['label'] = $setting['name'];
522
			_deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[label] instead of [name] when defining the setting '. $key .' details' );
523
		}
524
525
		$name = esc_attr( sprintf( $name, $key ) );
526
		$setting['id'] = esc_attr( sprintf( $id, $key ) );
527
		$setting['tooltip'] = 'gv_' . $key;
528
529
		// Use default if current setting isn't set.
530
		$curr_value = isset( $current_settings[ $key ] ) ? $current_settings[ $key ] : $setting['value'];
531
532
		// default setting type = text
533
		$setting['type'] = empty( $setting['type'] ) ? 'text' : $setting['type'];
534
535
		// merge tags
536
		if( !isset( $setting['merge_tags'] ) ) {
537
			if( $setting['type'] === 'text' ) {
538
				$setting['merge_tags'] = true;
539
			} else {
540
				$setting['merge_tags'] = false;
541
			}
542
		}
543
544
		$output = '';
545
546
		// render the setting
547
		$type_class = self::load_type_class( $setting );
548
		if( class_exists( $type_class ) ) {
549
			/** @type GravityView_FieldType $render_type */
550
			$render_type = new $type_class( $name, $setting, $curr_value );
551
			ob_start();
552
			$render_type->render_setting( $override_input );
553
			$output = ob_get_clean();
554
		}
555
556
		// Check if setting is specific for a template
557
		if( !empty( $setting['show_in_template'] ) ) {
558
			if( !is_array( $setting['show_in_template'] ) ) {
559
				$setting['show_in_template'] = array( $setting['show_in_template'] );
560
			}
561
			$show_if = ' data-show-if="'. implode( ' ', $setting['show_in_template'] ).'"';
562
		} else {
563
			$show_if = '';
564
		}
565
566
		if( ! empty( $setting['requires'] ) ) {
567
			$show_if .= sprintf( ' data-requires="%s"', $setting['requires'] );
568
		}
569
570
		if( ! empty( $setting['requires_not'] ) ) {
571
			$show_if .= sprintf( ' data-requires-not="%s"', $setting['requires_not'] );
572
		}
573
574
		// output
575
		echo '<tr style="vertical-align: top;" '. $show_if .'>' . $output . '</tr>';
576
577
	}
578
579
580
	/**
581
	 * Given a field type calculates the php class. If not found try to load it.
582
	 * @param  array $field
583
	 * @return string type class name
584
	 */
585
	public static function load_type_class( $field = NULL ) {
586
587
		if( empty( $field['type'] ) ) {
588
			return NULL;
589
		}
590
591
		/**
592
		 * @filter `gravityview/setting/class/{field_type}`
593
		 * @param string $class_suffix  field class suffix; `GravityView_FieldType_{$class_suffix}`
594
		 * @param array $field   field data
595
		 */
596
		$type_class = apply_filters( "gravityview/setting/class/{$field['type']}", 'GravityView_FieldType_' . $field['type'], $field );
597
598
		if( class_exists( $type_class ) ) {
599
			return $type_class;
600
		}
601
602
		/**
603
		 * @filter `gravityview/setting/class_file/{field_type}`
604
		 * @param string  $field_type_include_path field class file path
605
		 * @param array $field  field data
606
		 */
607
		$class_file = apply_filters( "gravityview/setting/class_file/{$field['type']}", GRAVITYVIEW_DIR . "includes/admin/field-types/type_{$field['type']}.php", $field );
608
609
		if( $class_file && file_exists( $class_file ) ) {
610
			require_once( $class_file );
611
		}
612
613
		return $type_class;
614
	}
615
616
617
618
619
620
	/**
621
	 * @deprecated 1.2
622
	 * Render the HTML for a checkbox input to be used on the field & widgets options
623
	 * @param  string $name , name attribute
624
	 * @param  string $current current value
625
	 * @return string         html tags
626
	 */
627
	public static function render_checkbox_option( $name = '', $id = '', $current = '' ) {
628
629
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_checkbox::render_input' );
630
631
		$output  = '<input name="'. esc_attr( $name ) .'" type="hidden" value="0">';
632
		$output .= '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="checkbox" value="1" '. checked( $current, '1', false ) .' >';
633
634
		return $output;
635
	}
636
637
638
	/**
639
	 * @deprecated 1.2
640
	 * Render the HTML for an input text to be used on the field & widgets options
641
	 * @param  string $name    Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
642
	 * @param  string $current [current value]
643
	 * @param string $add_merge_tags Add merge tags to the input?
644
	 * @param array $args Field settings, including `class` key for CSS class
645
	 * @return string         [html tags]
646
	 */
647
	public static function render_text_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
648
649
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_text::render_input' );
650
651
		// Show the merge tags if the field is a list view
652
		$is_list = ( preg_match( '/_list-/ism', $name ));
653
654
		// Or is a single entry view
655
		$is_single = ( preg_match( '/single_/ism', $name ));
656
		$show = ( $is_single || $is_list );
657
658
		$class = '';
659
		// and $add_merge_tags is not false
660
		if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
661
			$class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
662
		}
663
664
		$class .= !empty( $args['class'] ) ? $args['class'] : 'widefat';
665
		$type = !empty( $args['type'] ) ? $args['type'] : 'text';
666
667
		return '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="'.esc_attr($type).'" value="'. esc_attr( $current ) .'" class="'.esc_attr( $class ).'">';
668
	}
669
670
	/**
671
	 * @deprecated 1.2
672
	 * Render the HTML for an textarea input to be used on the field & widgets options
673
	 * @param  string $name    Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
674
	 * @param  string $current [current value]
675
	 * @param string|boolean $add_merge_tags Add merge tags to the input?
676
	 * @param array $args Field settings, including `class` key for CSS class
677
	 * @return string         [html tags]
678
	 */
679
	public static function render_textarea_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
680
681
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_textarea::render_input' );
682
683
		// Show the merge tags if the field is a list view
684
		$is_list = ( preg_match( '/_list-/ism', $name ));
685
686
		// Or is a single entry view
687
		$is_single = ( preg_match( '/single_/ism', $name ));
688
		$show = ( $is_single || $is_list );
689
690
		$class = '';
691
		// and $add_merge_tags is not false
692
		if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
693
			$class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
694
		}
695
696
		$class .= !empty( $args['class'] ) ? 'widefat '.$args['class'] : 'widefat';
697
698
		return '<textarea name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" class="'.esc_attr( $class ).'">'. esc_textarea( $current ) .'</textarea>';
699
	}
700
701
	/**
702
	 *
703
	 * Render the HTML for a select box to be used on the field & widgets options
704
	 * @deprecated 1.2
705
	 * @param  string $name    [name attribute]
706
	 * @param  array $choices [select options]
707
	 * @param  string $current [current value]
708
	 * @return string          [html tags]
709
	 */
710
	public static function render_select_option( $name = '', $id = '', $choices, $current = '' ) {
711
712
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_select::render_input' );
713
714
		$output = '<select name="'. $name .'" id="'. $id .'">';
715
		foreach( $choices as $value => $label ) {
716
			$output .= '<option value="'. esc_attr( $value ) .'" '. selected( $value, $current, false ) .'>'. esc_html( $label ) .'</option>';
717
		}
718
		$output .= '</select>';
719
720
		return $output;
721
	}
722
723
724
}
725