/** * Load an existing field definition. * * For now, we're using legacy code to read fields from the options table. * * @param string $field_key Key used to store the field configuration in options, or field slug (which should be * equal to the key). * @return null|WPCF_Field_Definition Field definition or null if it can't be loaded. */ public final function load_field_definition($field_key) { if (!is_string($field_key) || empty($field_key)) { return null; } // Can we use cached version? if (!in_array($field_key, $this->field_definitions)) { // Get all field definitions for the option name we're using. No performance worries, it uses caching. $fields_from_options = $this->get_fields_from_options(); $field_configuration = null; if (in_array($field_key, array_keys($fields_from_options))) { $field_configuration = $fields_from_options[$field_key]; } else { // Theoretically, the field key may differ from the field slug (we have no invariants defined anywhere). // We can search the array and look for slugs. foreach ($fields_from_options as $field_from_options) { if (wpcf_getarr($field_from_options, 'slug') == $field_key) { $field_configuration = $fields_from_options; break; } } if (null == $field_configuration) { // No such field is defined. return null; } } // Prepare the field type information, fail if we can't. $field_type_slug = wpcf_getarr($field_configuration, 'type', null); $field_type = WPCF_Field_Type_Definition_Factory::load($field_type_slug); if (null == $field_type) { return null; } // Create the object and save it to cache. try { $class_name = $this->get_class_name(); /** @var WPCF_Field_Definition $field_definition */ $field_definition = new $class_name($field_type, $field_configuration); } catch (Exception $e) { return null; } $this->field_definitions[$field_key] = $field_definition; } return $this->field_definitions[$field_key]; }
/** * Static shortcut to load_field_type_definition. * * @param string $field_type_slug * @return null|WPCF_Field_Type_Definition */ public static function load($field_type_slug) { // we cannot use self::get_instance here, because of low PHP requirements and missing get_called_class function // we have a fallback class for get_called_class but that scans files by debug_backtrace and return 'self' // instead of WPCF_Field_Type_Definition_Factory like the original get_called_class() function does // ends in an error because of parents (abstract) $var = new self(); return WPCF_Field_Type_Definition_Factory::get_instance()->load_field_type_definition($field_type_slug); }