Completed
Pull Request — develop (#1573)
by Zack
20:26
created

GravityView_Field   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 566
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 29.52%

Importance

Changes 0
Metric Value
dl 0
loc 566
ccs 31
cts 105
cp 0.2952
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 2
cbo 5

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 38 9
A as_array() 0 11 1
A get_icon() 0 16 6
A add_sortable_field() 0 11 1
A set_default_search_label() 0 8 3
A _filter_gform_replace_merge_tags() 0 12 2
A replace_merge_tag() 0 26 4
A _filter_gform_custom_merge_tags() 0 8 1
A custom_merge_tags() 0 12 1
A _filter_sortable_fields() 0 8 2
A add_entry_meta() 0 22 4
A field_support_options() 0 54 1
A add_field_support() 0 10 2
A field_options() 0 7 1
A is_choice_value_enabled() 0 26 4

How to fix   Complexity   

Complex Class

Complex classes like GravityView_Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GravityView_Field, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @file class-gravityview-field.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * Modify field settings by extending this class.
10
 */
11
abstract class GravityView_Field {
12
13
	/**
14
	 * The name of the GravityView field type
15
	 * Example: `created_by`, `text`, `fileupload`, `address`, `entry_link`
16
	 * @var string
17
	 */
18
	public $name;
19
20
	/**
21
	 * @internal Not yet implemented
22
	 * @since 1.15.2
23
	 * @type string The description of the field in the field picker
24
	 */
25
	public $description;
26
27
	/**
28
	 * @since 1.15.2
29
	 * @type string The label of the field in the field picker
30
	 */
31
	public $label;
32
33
	/**
34
	 * @var string The default search label used by the search widget, if not set
35
	 */
36
	public $default_search_label;
37
38
	/**
39
	 * `standard`, `advanced`, `post`, `pricing`, `meta`, `gravityview`, or `add-ons`
40
	 *
41
	 * @since 1.15.2
42
	 * @type string The group belongs to this field in the field picker
43
	 */
44
	public $group;
45
46
	/**
47
	 * @internal Not yet implemented
48
	 * @type boolean Can the field be searched?
49
	 * @since 1.15.2
50
	 */
51
	public $is_searchable = true;
52
53
	/**
54
	 * @internal Not yet implemented
55
	 * @type array $search_operators The type of search operators available for this field
56
	 * @since 1.15.2
57
	 */
58
	public $search_operators;
59
60
	/**
61
	 * @type boolean Can the field be sorted in search?
62
	 * @since 1.15.2
63
	 */
64
	public $is_sortable = true;
65
66
	/**
67
	 * @type boolean Is field content number-based?
68
	 * @since 1.15.2
69
	 */
70
	public $is_numeric;
71
72
	/**
73
	 * @var null|string The key used to search and sort entry meta in Gravity Forms. Used if the field stores data as custom entry meta.
74
	 * @see https://www.gravityhelp.com/documentation/article/gform_entry_meta/
75
	 * @since 1.19
76
	 */
77
	public $entry_meta_key = null;
78
79
	/**
80
	 * @var string|array Optional. The callback function after entry meta is updated, only used if $entry_meta_key is set.
81
	 * @see https://www.gravityhelp.com/documentation/article/gform_entry_meta/
82
	 * @since 1.19
83
	 */
84
	var $entry_meta_update_callback = null;
85
86
	/**
87
	 * @var bool Whether to show meta when set to true automatically adds the column to the entry list, without having to edit and add the column for display
88
	 * @since 1.19
89
	 */
90
	var $entry_meta_is_default_column = false;
91
92
	/**
93
	 * @internal Not yet implemented
94
	 * @todo implement supports_context() method
95
	 * The contexts in which a field is available. Some fields aren't editable, for example.
96
	 * - `singular` is an alias for both `single` and `edit`
97
	 * - `multiple` is an alias for `directory` (backward compatibility)
98
	 * @type array
99
	 * @since 1.15.2
100
	 */
101
	public $contexts = array( 'single', 'multiple', 'edit', 'export' );
102
103
	/**
104
	 * @var string An icon that represents the field type in the field picker
105
	 */
106
	public $icon = 'dashicons-admin-generic';
107
108
	/**
109
	 * @since 1.15.2
110
	 * @since 1.16.2.2 Changed access to public (previously, protected)
111
	 * @type string The name of a corresponding Gravity Forms GF_Field class, if exists
112
	 */
113
	public $_gf_field_class_name;
114
115
	/**
116
	 * @var string The field ID being requested
117
	 * @since 1.14
118
	 */
119
	protected $_field_id = '';
120
121
	/**
122
	 * @var string Field options to be rendered
123
	 * @since 1.14
124
	 */
125
	protected $_field_options = array();
126
127
	/**
128
	 * @var bool|string Name of merge tag (without curly brackets), if the field has custom GravityView merge tags to add. Otherwise, false.
129
	 * @since 1.16
130
	 */
131
	protected $_custom_merge_tag = false;
132
133
	/**
134
	 * GravityView_Field constructor.
135
	 */
136
	public function __construct() {
137
138
		/**
139
		 * Modify the field options based on the name of the field type
140
		 * @see GravityView_Render_Settings::get_default_field_options
141
		 */
142
		add_filter( sprintf( 'gravityview_template_%s_options', $this->name ), array( &$this, 'field_options' ), 10, 6 );
143
144
		add_filter( 'gravityview/sortable/field_blacklist', array( $this, '_filter_sortable_fields' ), 1 );
145
146
		if( $this->entry_meta_key ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entry_meta_key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
147
			add_filter( 'gform_entry_meta', array( $this, 'add_entry_meta' ) );
148
			add_filter( 'gravityview/common/sortable_fields', array( $this, 'add_sortable_field' ), 10, 2 );
149
		}
150
151
		if( $this->_custom_merge_tag ) {
152
			add_filter( 'gform_custom_merge_tags', array( $this, '_filter_gform_custom_merge_tags' ), 10, 4 );
153
			add_filter( 'gform_replace_merge_tags', array( $this, '_filter_gform_replace_merge_tags' ), 10, 7 );
154
		}
155
156
		if( 'meta' === $this->group || '' !== $this->default_search_label ) {
157
			add_filter( 'gravityview_search_field_label', array( $this, 'set_default_search_label' ), 10, 3 );
158
		}
159
160
		/**
161
		 * Auto-assign label from Gravity Forms label, if exists
162
		 * @since 1.20
163
		 */
164
		if( empty( $this->label ) && ! empty( $this->_gf_field_class_name ) && class_exists( $this->_gf_field_class_name ) ) {
165
			$this->label = ucfirst( GF_Fields::get( $this->name )->get_form_editor_field_title() );
166
		}
167
168
		try {
169
			GravityView_Fields::register( $this );
170
		} catch ( Exception $exception ) {
171
			gravityview()->log->critical( $exception->getMessage() );
172
		}
173
	}
174
175
	/**
176
	 * Returns the field as an array to be used in field pickers
177
	 *
178
	 * @since 2.10
179
	 *
180
	 * @return array[]
181
	 */
182
	public function as_array() {
183
		return array(
184
			$this->name => array(
185
				'label' => $this->label,
186
				'desc'  => $this->description,
187
				'type'  => $this->name,
188
				'icon'  => $this->icon,
189
				'group' => $this->group,
190
			),
191
		);
192
	}
193
194
	/**
195
	 * Returns the icon for a field
196
	 *
197
	 * @since 2.10.1
198
	 *
199
	 * @return string
200 4
	 */
201
	public function get_icon() {
202 4
203
		$icon = $this->icon;
204
205
		if ( gravityview()->plugin->is_GF_25() && ! empty( $this->_gf_field_class_name ) && class_exists( $this->_gf_field_class_name ) ) {
206 4
207
			/** @var GF_Field $gf_field */
208
			$gf_field = GF_Fields::get( $this->name );
209
210
			if( $gf_field && $gf_field instanceof GF_Field ) {
0 ignored issues
show
Bug introduced by
The class GF_Field does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
211
				$icon = $gf_field->get_form_editor_field_icon();
212
			}
213
		}
214
215
		return $icon;
216
	}
217
218
	/**
219
	 * Add the field to the Filter & Sort available fields
220
	 *
221
	 * @since 1.19
222
	 *
223 48
	 * @param array $fields Sub-set of GF form fields that are sortable
224
	 *
225
	 * @return array Modified $fields array to include approval status in the sorting dropdown
226 48
	 */
227
	public function add_sortable_field( $fields ) {
228
229 48
		$added_field = array(
230 48
			'label' => $this->label,
231
			'type'  => $this->name
232
		);
233 2
234
		$fields["{$this->entry_meta_key}"] = $added_field;
235
236
		return $fields;
237
	}
238
239
	/**
240
	 * Allow setting a default search label for search fields based on the field type
241
	 *
242
	 * Useful for entry meta "fields" that don't have Gravity Forms labels, like `created_by`
243
	 *
244
	 * @since 1.17.3
245
	 *
246
	 * @param string $label Existing label text, sanitized.
247
	 * @param array $gf_field Gravity Forms field array, as returned by `GFFormsModel::get_field()`
248
	 * @param array $field Field setting as sent by the GV configuration - has `field`, `input` (input type), and `label` keys
249
	 *
250
	 * @return string
251
	 */
252
	function set_default_search_label( $label = '', $gf_field = null, $field = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
253
254 1
		if( $this->name === $field['field'] && '' === $label ) {
255
			$label = esc_html( $this->default_search_label );
256 1
		}
257
258 1
		return $label;
259
	}
260
261 1
	/**
262
	 * Match the merge tag in replacement text for the field.  DO NOT OVERRIDE.
263
	 *
264 1
	 * @see replace_merge_tag Override replace_merge_tag() to handle any matches
265
	 *
266 1
	 * @since 1.16
267
	 *
268 1
	 * @param string $text Text to replace
269 1
	 * @param array $form Gravity Forms form array
270
	 * @param array $entry Entry array
271
	 * @param bool $url_encode Whether to URL-encode output
272 1
	 *
273
	 * @return string Original text if {_custom_merge_tag} isn't found. Otherwise, replaced text.
274
	 */
275
	public function _filter_gform_replace_merge_tags( $text, $form = array(), $entry = array(), $url_encode = false, $esc_html = false  ) {
276 1
277
		// Is there is field merge tag? Strip whitespace off the ned, too.
278 1
		preg_match_all( '/{' . preg_quote( $this->_custom_merge_tag ) . ':?(.*?)(?:\s)?}/ism', $text, $matches, PREG_SET_ORDER );
279
280
		// If there are no matches, return original text
281
		if ( empty( $matches ) ) {
282
			return $text;
283
		}
284
285
		return $this->replace_merge_tag( $matches, $text, $form, $entry, $url_encode, $esc_html );
286
	}
287
288
	/**
289
	 * Run GravityView filters when using GFCommon::replace_variables()
290
	 *
291
	 * Instead of adding multiple hooks, add all hooks into this one method to improve speed
292
	 *
293
	 * @since 1.8.4
294
	 *
295
	 * @see GFCommon::replace_variables()
296
	 *
297
	 * @param array $matches Array of Merge Tag matches found in text by preg_match_all
298
	 * @param string $text Text to replace
299
	 * @param array|bool $form Gravity Forms form array. When called inside {@see GFCommon::replace_variables()} (now deprecated), `false`
300
	 * @param array|bool $entry Entry array.  When called inside {@see GFCommon::replace_variables()} (now deprecated), `false`
301
	 * @param bool $url_encode Whether to URL-encode output
302
	 * @param bool $esc_html Whether to apply `esc_html()` to output
303
	 *
304
	 * @return mixed
305
	 */
306
	public function replace_merge_tag( $matches = array(), $text = '', $form = array(), $entry = array(), $url_encode = false, $esc_html = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
307
308
		foreach( $matches as $match ) {
309
310
			$full_tag = $match[0];
311
312
			// Strip the Merge Tags
313
			$tag = str_replace( array( '{', '}'), '', $full_tag );
314
315
			// Replace the value from the entry, if exists
316
			if( isset( $entry[ $tag ] ) ) {
317
318
				$value = $entry[ $tag ];
319
320
				if( is_callable( array( $this, 'get_content') ) ) {
321
					$value = $this->get_content( $value );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class GravityView_Field as the method get_content() does only exist in the following sub-classes of GravityView_Field: GravityView_Field_Date_Created, GravityView_Field_Date_Updated, GravityView_Field_Is_Fulfilled, GravityView_Field_Is_Starred, GravityView_Field_Payment_Amount, GravityView_Field_Payment_Date, GravityView_Field_Transaction_Type. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
322
				}
323
324
				$text = str_replace( $full_tag, $value, $text );
325
			}
326
		}
327
328
		unset( $value, $tag, $full_tag );
329
330
		return $text;
331
	}
332
333
	/**
334
	 * Add custom merge tags to merge tag options. DO NOT OVERRIDE.
335
	 *
336
	 * @internal Not to be overridden by fields
337
	 *
338
	 * @since 1.8.4
339 1
	 *
340
	 * @param array $custom_merge_tags
341 1
	 * @param int $form_id GF Form ID
342 1
	 * @param GF_Field[] $fields Array of fields in the form
343
	 * @param string $element_id The ID of the input that Merge Tags are being used on
344
	 *
345 1
	 * @return array Modified merge tags
346
	 */
347
	public function _filter_gform_custom_merge_tags( $custom_merge_tags = array(), $form_id, $fields = array(), $element_id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $element_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
348
349
		$form = GVCommon::get_form( $form_id );
350
351
		$field_merge_tags = $this->custom_merge_tags( $form, $fields );
352
353
		return array_merge( $custom_merge_tags, $field_merge_tags );
354
	}
355
356
	/**
357 163
	 * Add custom Merge Tags to Merge Tag options, if custom Merge Tags exist
358
	 *
359 163
	 * Should be overridden if there's more than one Merge Tag to add or if the Merge Tag isn't {_custom_merge_tag}
360
	 *
361
	 * @since 1.16
362 163
	 *
363 163
	 * @param array $form GF Form array
364 163
	 * @param GF_Field[] $fields Array of fields in the form
365
	 *
366
	 * @return array Merge tag array with `label` and `tag` keys based on class `label` and `_custom_merge_tag` variables
367 163
	 */
368
	protected function custom_merge_tags( $form = array(), $fields = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
369
370
		// Use variables to make it unnecessary for other fields to override
371 163
		$merge_tags = array(
372
			array(
373
				'label' => $this->label,
374
				'tag' => '{' . $this->_custom_merge_tag . '}',
375
			),
376
		);
377 163
378
		return $merge_tags;
379
	}
380
381
	/**
382
	 * Use field settings to modify whether a field is sortable
383
	 *
384
	 * @see GravityView_frontend::is_field_sortable
385
	 * @since 1.15.3
386
	 *
387
	 * @param array $not_sortable Existing field types that aren't sortable
388
	 *
389
	 * @return array
390
	 */
391
	public function _filter_sortable_fields( $not_sortable ) {
392
393
		if( ! $this->is_sortable ) {
394
			$not_sortable[] = $this->name;
395
		}
396
397
		return $not_sortable;
398
	}
399
400
	/**
401
	 * Add the custom entry meta key to make it searchable and sortable
402
	 *
403
	 * @see https://www.gravityhelp.com/documentation/article/gform_entry_meta/
404
	 *
405
	 * @param array $entry_meta Array of custom entry meta keys with associative arrays
406
	 *
407
	 * @return mixed
408
	 */
409
	function add_entry_meta( $entry_meta ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
410
411
		if( ! isset( $entry_meta["{$this->entry_meta_key}"] ) ) {
412
413
			$added_meta = array(
414
				'label'             => $this->label,
415
				'is_numeric'        => $this->is_numeric,
416
				'is_default_column' => $this->entry_meta_is_default_column,
417
			);
418
419
			if ( $this->entry_meta_update_callback && is_callable( $this->entry_meta_update_callback ) ) {
420
				$added_meta['update_entry_meta_callback'] = $this->entry_meta_update_callback;
421
			}
422
423
			$entry_meta["{$this->entry_meta_key}"] = $added_meta;
424
425
		} else {
426
			gravityview()->log->error( 'Entry meta already set: {meta_key}', array( 'meta_key' => $this->entry_meta_key, 'data' =>  $entry_meta["{$this->entry_meta_key}"] ) );
427
		}
428
429
		return $entry_meta;
430
	}
431
432
	private function field_support_options() {
433
		$options = array(
434
			'link_to_post' => array(
435
				'type' => 'checkbox',
436
				'label' => __( 'Link to the post', 'gravityview' ),
437
				'desc' => __( 'Link to the post created by the entry.', 'gravityview' ),
438
				'value' => false,
439
				'priority' => 1200,
440
				'group' => 'display',
441
			),
442
			'link_to_term' => array(
443
				'type' => 'checkbox',
444
				'label' => __( 'Link to the category or tag', 'gravityview' ),
445
				'desc' => __( 'Link to the current category or tag. "Link to single entry" must be unchecked.', 'gravityview' ),
446
				'value' => false,
447
				'priority' => 1210,
448
				'group' => 'display',
449
			),
450
			'dynamic_data' => array(
451
				'type' => 'checkbox',
452
				'label' => __( 'Use the live post data', 'gravityview' ),
453
				'desc' => __( 'Instead of using the entry data, instead use the current post data.', 'gravityview' ),
454
				'value' => true,
455
				'priority' => 1100,
456
				'group' => 'display',
457
			),
458
			'date_display' => array(
459
				'type' => 'text',
460
				'label' => __( 'Override Date Format', 'gravityview' ),
461
				'desc' => sprintf( __( 'Define how the date is displayed (using %sthe PHP date format%s)', 'gravityview'), '<a href="https://wordpress.org/support/article/formatting-date-and-time/" rel="external">', '</a>' ),
462
				/**
463
				 * @filter `gravityview_date_format` Override the date format with a [PHP date format](https://codex.wordpress.org/Formatting_Date_and_Time)
464
				 * @param[in,out] null|string $date_format Date Format (default: null)
465
				 */
466
				'value' => apply_filters( 'gravityview_date_format', null ),
467
				'class' => 'code widefat',
468
				'priority' => 1500,
469
				'group' => 'display',
470
			),
471
			'new_window' => array(
472
				'type' => 'checkbox',
473
				'label' => __( 'Open link in a new tab or window?', 'gravityview' ),
474
				'value' => false,
475
				'group' => 'display',
476
				'priority' => 1300,
477
			),
478
		);
479
480
		/**
481
		 * @filter `gravityview_field_support_options` Modify the settings that a field supports
482
		 * @param array $options Options multidimensional array with each key being the input name, with each array setting having `type`, `label`, `desc` and `value` (default values) keys
483
		 */
484
		return apply_filters( 'gravityview_field_support_options', $options );
485
	}
486
487
	function add_field_support( $key = '', &$field_options ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
488
489
		$options = $this->field_support_options();
490
491
		if( isset( $options[ $key ] ) ) {
492
			$field_options[ $key ] = $options[ $key ];
493
		}
494
495
		return $field_options;
496
	}
497
498
	/**
499
	 * Tap in here to modify field options.
500
	 *
501
	 * Here's an example:
502
	 *
503
	 * <pre>
504
	 * $field_options['name_display'] = array(
505
	 * 	'type' => 'select',
506
	 * 	'label' => __( 'User Format', 'gravityview' ),
507
	 * 	'desc' => __( 'How should the User information be displayed?', 'gravityview'),
508
	 * 	'choices' => array(
509
	 * 		array(
510
	 *		 	'value' => 'display_name',
511
	 *		  	'label' => __('Display Name (Example: "Ellen Ripley")', 'gravityview'),
512
	 *		),
513
	 *  	array(
514
	 *			'value' => 'user_login',
515
	 *			'label' => __('Username (Example: "nostromo")', 'gravityview')
516
	 *		),
517
	 * 	 'value' => 'display_name'
518
	 * );
519
	 * </pre>
520
	 *
521
	 * @param  array      $field_options [description]
522
	 * @param  string      $template_id   [description]
523
	 * @param  string      $field_id      [description]
524
	 * @param  string      $context       [description]
525
	 * @param  string      $input_type    [description]
526
	 * @return array                     [description]
527
	 */
528
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
529
530
		$this->_field_options = $field_options;
531
		$this->_field_id = $field_id;
532
533
		return $field_options;
534
	}
535
536
	/**
537
	 * Check whether the `enableChoiceValue` flag is set for a GF field
538
	 *
539
	 * Gets the current form ID, gets the field at that ID, then checks for the enableChoiceValue value.
540
	 *
541
	 * @access protected
542
	 *
543
	 * @uses GFAPI::get_form
544
	 *
545
	 * @since 1.17
546
	 *
547
	 * @return bool True: Enable Choice Value is active for field; False: not active, or form invalid, or form not found.
548
	 */
549
	protected function is_choice_value_enabled() {
550
551
		// If "Add Field" button is processing, get the Form ID
552
		$connected_form = \GV\Utils::_POST( 'form_id' );
553
554
		// Otherwise, get the Form ID from the Post page
555
		if( empty( $connected_form ) ) {
556
			$connected_form = gravityview_get_form_id( get_the_ID() );
557
		}
558
559
		if( empty( $connected_form ) ) {
560
			gravityview()->log->error( 'Form not found for form ID "{form_id}"', array( 'form_id' => $connected_form ) );
561
			return false;
562
		}
563
564
		$form = GFAPI::get_form( $connected_form );
565
566
		if ( ! $form ) {
567
			gravityview()->log->error( 'Form not found for field ID of "{field_id}", when checking for a form with ID of "{form_id}"', array( 'field_id' => $this->_field_id, 'form_id' => $connected_form ) );
568
			return false;
569
		}
570
571
		$field = gravityview_get_field( $form, $this->_field_id );
572
573
		return ! empty( $field->enableChoiceValue );
574
	}
575
576
}
577