Esempio n. 1
0
 /**
  * @param Types_Field_Type_Definition $type
  * @return Types_Field_Type_Definition[]
  */
 public function get_possible_conversions($type)
 {
     if (!$type instanceof Types_Field_Type_Definition) {
         throw new InvalidArgumentException('Not a field type definition');
     }
     $matrix = $this->get_conversion_matrix();
     $allowed_slugs = wpcf_ensarr(wpcf_getarr($matrix, $type->get_slug()));
     $allowed_types = Types_Field_Type_Definition_Factory::get_instance()->load_multiple_definitions($allowed_slugs);
     return $allowed_types;
 }
 private function check_successful_loading($field_slug)
 {
     $field_type_definition = $this->get_instance()->load_field_type_definition($field_slug);
     $this->assertNotNull($field_type_definition, "Field type definition '{$field_slug}' was not loaded properly.");
     $this->assertInstanceOf('Types_Field_Type_Definition', $field_type_definition);
     $this->assertEquals($field_slug, $field_type_definition->get_slug());
     $field_type_definition_statically_loaded = Types_Field_Type_Definition_Factory::load($field_slug);
     $this->assertNotNull($field_type_definition_statically_loaded);
     $this->assertSame($field_type_definition_statically_loaded, $field_type_definition);
     $field_type_definition_with_prefix = $this->get_instance()->load_field_type_definition('wpcf-' . $field_slug);
     $this->assertNotNull($field_type_definition_with_prefix);
     $this->assertSame($field_type_definition, $field_type_definition_with_prefix);
 }
 /**
  * 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;
 }
Esempio n. 4
0
 /**
  * Change a field type for given field definition.
  *
  * Performs checks if the conversion is allowed, and if not, generate a proper error message.
  *
  * @param string $field_slug
  * @param string $domain
  * @param string[] $arguments Needs to contain the 'field_type' key with target type slug.
  * @return false|WP_Error|WPCF_Field_Definition The updated definition on succes, error/false otherwise.
  * @since 2.0
  */
 public static function change_field_type($field_slug, $domain, $arguments)
 {
     // Load all information we need, fail if it doesn't exist.
     $factory = Types_Field_Utils::get_definition_factory_by_domain($domain);
     $definition = $factory->load_field_definition($field_slug);
     if (null == $definition) {
         return new WP_Error(42, sprintf(__('Field definition for field "%s" not found in options.', 'wpcf'), sanitize_text_field($field_slug)));
     } else {
         if (!$definition->is_managed_by_types()) {
             return new WP_Error(42, sprintf(__('Field "%s" will not be converted because it is not managed by Types.', 'wpcf'), sanitize_text_field($field_slug)));
         }
     }
     $type_slug = wpcf_getarr($arguments, 'field_type');
     $target_type = Types_Field_Type_Definition_Factory::get_instance()->load_field_type_definition($type_slug);
     if (null == $target_type) {
         return new WP_Error(42, sprintf(__('Unknown field type "%s".', 'wpcf'), $type_slug));
     }
     // Check if we can convert between types
     $is_conversion_possible = Types_Field_Type_Converter::get_instance()->is_conversion_possible($definition->get_type(), $target_type);
     if (!$is_conversion_possible) {
         return new WP_Error(42, sprintf(__('Conversion from type "%s" to "%s" is currently not allowed.', 'wpcf'), $definition->get_type()->get_display_name(), $target_type->get_display_name()));
     }
     // Check if we can do the conversion with current field's cardinality
     $is_cardinality_sustainable = !$definition->get_is_repetitive() || $target_type->can_be_repetitive();
     if (!$is_cardinality_sustainable) {
         return new WP_Error(42, sprintf(__('Field "%s" cannot be converted from "%s" to "%s" because it is repetitive and the target type doesn\'t support that.', 'wpcf'), $definition->get_display_name(), $definition->get_type()->get_display_name(), $target_type->get_display_name()));
     }
     // All is fine, proceed.
     $result = $definition->change_type($target_type);
     if ($result) {
         return $definition;
     } else {
         // Something unexpected went wrong.
         return false;
     }
 }
Esempio n. 5
0
 /**
  * Prepare assets for all dialogs that are going to be used on the page.
  * 
  * @since 2.0
  */
 public function prepare_dialogs()
 {
     new Types_Dialog_Box('types-change-assignment-dialog', $this->get_twig(), array('groups' => $this->build_group_data(), 'strings' => array('noFieldGroups' => __('No field groups exist yet. You have to create one first.', 'wpcf'))), '@field_control/change_assignment_dialog.twig');
     new Types_Dialog_Box('types-delete-field-dialog', $this->get_twig(), array('strings' => array('deletingWillRemoveDefinitionAndData' => __('Deleting fields will remove them from all groups and delete the field data from the database as well.', 'wpcf'), 'cannotBeUndone' => __('This cannot be undone!', 'wpcf'), 'doYouReallyWantDelete' => __('Do you really want to delete?', 'wpcf'))), '@field_control/delete_dialog.twig');
     new Types_Dialog_Box('types-change-field-type-dialog', $this->get_twig(), array('fieldTypeDefinitions' => Types_Field_Type_Definition_Factory::get_instance()->get_field_type_definitions(), 'strings' => array('aboutFieldTypeChanging' => __('Select a new type for this field.', 'wpcf'), 'someTypesAreDisabled' => __('Note: Some of the field types are disabled for conversion because they\'re using a significantly different data format, which is not compatible with the current field type.', 'wpcf'), 'potentiallyRiskyOperation' => __('Changing field type is a potentially risky operation. Make sure you know what you are doing.', 'wpcf'), 'singleOrRepeatingField' => __('Single or repeating field', 'wpcf'), 'repetitiveField' => __('Allow multiple instances of this field', 'wpcf'), 'singleField' => __('This field can have only one value', 'wpcf'), 'targetSupportsSingleOnly' => __('Selected field type supports only single fields.', 'wpcf'), 'repetitiveToSingleWarning' => __('Changing from repeating to single field <strong>will cause partial data loss</strong> if there already are fields with multiple values stored in the database. In such case, only one of those value will be saved on update and some inconsistencies appear when displaying values of this field.', 'wpcf'))), '@field_control/change_type_dialog.twig');
     new Types_Dialog_Box('types-bulk-change-management-status-dialog', $this->get_twig(), array('strings' => array('youAreAboutToManageFields' => __('You are about to start managing these fields with Types:', 'wpcf'), 'youAreAboutToStopManagingFields' => __('You are about to stop managing these fields with Types:', 'wpcf'), 'confirmContinue' => __('Do you want to continue?', 'wpcf'))), '@field_control/bulk_change_management_status_dialog.twig');
 }
 /**
  * Static shortcut to load_field_type_definition.
  *
  * @param string $field_type_slug
  * @return null|Types_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 Types_Field_Type_Definition_Factory like the original get_called_class() function does
     // ends in an error because of parents (abstract) $var = new self();
     return Types_Field_Type_Definition_Factory::get_instance()->load_field_type_definition($field_type_slug);
 }