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 |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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 ) { |
||
| 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() ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 = '' ) { |
||
| 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() ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 431 | |||
| 432 | private function field_support_options() { |
||
| 486 | |||
| 487 | function add_field_support( $key = '', &$field_options ) { |
||
| 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 ) { |
||
| 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() { |
||
| 575 | |||
| 576 | } |
||
| 577 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: