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);
 }
Ejemplo n.º 2
0
 /**
  * Load an existing field definition.
  *
  * For now, we're using legacy code to read fields from the options table.
  *
  * Note that field definitions for fields not currently managed by Types may be loaded as well.
  *
  * @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 (!array_key_exists($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 = Types_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, $this);
         } catch (Exception $e) {
             return null;
         }
         $this->field_definitions[$field_key] = $field_definition;
     }
     return $this->field_definitions[$field_key];
 }