public static function get_instance()
 {
     if (null == self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Example #2
0
 /**
  * Change type of this field definition.
  *
  * @param Types_Field_Type_Definition $target_type One of the allowed field types to convert to.
  * @throws InvalidArgumentException when $target_type is not a field type definition.
  * @return bool True if the conversion was successful, false otherwise.
  * @since 2.0
  */
 public function change_type($target_type)
 {
     if (!$target_type instanceof Types_Field_Type_Definition) {
         throw new InvalidArgumentException('Not a field type definition');
     }
     if (!Types_Field_Type_Converter::get_instance()->is_conversion_possible($this->get_type(), $target_type)) {
         return false;
     }
     if ($this->get_is_repetitive() && !$target_type->can_be_repetitive()) {
         return false;
     }
     // We need to immediately update this model.
     $this->type = $target_type;
     $this->definition_array['type'] = $target_type->get_slug();
     return $this->update_self();
 }
Example #3
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;
     }
 }
Example #4
0
/**
 * Get a matrix of allowed conversions between field types.
 *
 * @since unknown
 * @return string[][]|mixed Associative array of string arrays (unless the used filters return something broken).
 * @deprecated Since 2.0 use Types_Field_Type_Converter instead.
 */
function wpcf_admin_custom_fields_change_type_allowed_matrix()
{
    return Types_Field_Type_Converter::get_instance()->get_conversion_matrix();
}
 /**
  * Build data to be passed to JavaScript.
  * 
  * @return array
  * @since 2.0
  */
 private function build_js_data()
 {
     $field_action_name = Types_Ajax::get_instance()->get_action_js_name(Types_Ajax::CALLBACK_FIELD_CONTROL_ACTION);
     return array('jsIncludePath' => TYPES_RELPATH . '/public/page/field_control', 'fieldDefinitions' => $this->build_field_definitions(), 'fieldTypeDefinitions' => Types_Field_Type_Definition_Factory::get_instance()->get_field_type_definitions(), 'templates' => $this->build_templates(), 'strings' => $this->build_strings_for_js(), 'ajaxInfo' => array('fieldAction' => array('name' => $field_action_name, 'nonce' => wp_create_nonce($field_action_name))), 'currentDomain' => $this->get_current_domain(), 'groups' => $this->build_group_data(), 'typeConversionMatrix' => Types_Field_Type_Converter::get_instance()->get_conversion_matrix(), 'itemsPerPage' => $this->get_items_per_page_setting());
 }