/** * This method is to be used only for bringing existing fields under Types control. * * At this point it is assumed that there doesn't exist any field definition for given meta_key. * See Types_Field_Utils::start_managing_field() for details. * * Maybe the usage could be wider, but that is not yet clear from the legacy code. The behaviour is slightly * different for meta_keys with the wpcf- prefix from the ones without it. More details in the code. * * The field will be created as a text field. * * @param string $meta_key Field meta key. * * @return string|false New field slug on success, false otherwise. * @since 2.0 */ public final function create_field_definition_for_existing_fields($meta_key) { // If the meta_key has our wpcf- prefix, we will not use it in the slug. $field_slug = preg_replace('/^wpcf\\-/', '', $meta_key); $definition_array = array('slug' => $field_slug, 'meta_key' => $meta_key, 'meta_type' => Types_Field_Utils::domain_to_legacy_meta_type($this->get_domain()), 'type' => Types_Field_Type_Definition_Factory::TEXTFIELD, 'name' => $field_slug, 'description' => '', 'data' => array(), 'id' => $field_slug); // Now comes a funny part that I don't fully understand. When the field's meta_key does contain the Types // prefix ('wpcf-'), we assume that this was most probably a Types field whose definition got lost. If not, // it's a completely "foreign" field that we'll only manage from now on. // // In the first case, the legacy code says "let's take full control" and that means setting 'controlled' to 0, // while for other fields (apparently without "full control") we set 'controlled' to 1... well, ok... // // But it also says "WATCH THIS! MUST NOT BE DROPPED IN ANY CASE", so let's not drop it. // // Name of the legacy function is: wpcf_types_cf_under_control(). // // I assume this setting is somehow related to toolset-forms. $adding_field_with_prefix = substr($meta_key, 0, strlen(WPCF_Field_Definition::FIELD_META_KEY_PREFIX)) == WPCF_Field_Definition::FIELD_META_KEY_PREFIX; $definition_array['data']['controlled'] = $adding_field_with_prefix ? 0 : 1; // Sanitize the definition array by type $textfield_type = Types_Field_Type_Definition_Factory::get_instance()->load_field_type_definition(Types_Field_Type_Definition_Factory::TEXTFIELD); if (null == $textfield_type) { return false; } $definition_array = $textfield_type->sanitize_field_definition_array($definition_array); // Save the data $this->set_field_definition($field_slug, $definition_array); // Indicate success return $field_slug; }