/** * Prepares type and item_id based on a single string * * @param string|int The type * @return stdClass An object containing the type and item ID */ public function prepare_type($type = null) { $item_id = 0; if ($type) { # There is something passed, process it if (preg_match('~^\\d+$~', $type)) { # It's just a number $item_id = intval($type); $type = 'meta'; } elseif (preg_match('~^([A-Z_-]+)_(\\d+)$~i', $type, $matches)) { # There is type + ID $type = $matches[1]; $item_id = $matches[2]; if (!isset($this->datastores[$matches[1]])) { UF_Exceptions::add(__('Invalid type argument provided to get_uf!', 'uf')); } } elseif (!isset($this->datastores[$type])) { UF_Exceptions::add(__('Invalid type argument provided to get_uf!', 'uf')); } } else { $type = 'meta'; $item_id = get_the_ID(); } $data = new stdClass(); $data->type = $type; $data->id = $item_id; return $data; }
/** * Check if a field with the same key has been registered * * @param string $key The key of the field */ function check_field_id($key) { if (isset(UF_Datastore_Options::$field_keys[$key])) { UF_Exceptions::add(sprintf(__('Error: Trying to register an option field with the %s key twice!', 'uf'), $key), 'unavailable_field_key'); return false; } else { UF_Datastore_Options::$field_keys[$key] = 1; return true; } }
/** * Creates a specific field and returns it * * @param string $type The type of the field, like text or google_font * @param string $id The ID of the field. Containers of single types cannot have identical field IDs * @param string $title The title that's to be assigned to the field. If skipped, it's generated from the id * * @return UF_Field the newly created field, ready for chaining */ public static function factory($type, $id, $title = '') { $class_name = self::get_class($type); if (!class_exists($class_name) || !is_subclass_of($class_name, 'UF_Field')) { $message = __('<strong>Ultimate Fields:</strong> A "%s" field class does not exist. The reason for this could be a missing or deactivated plugin. If you can't solve the issue, you might want to contact the author of the last theme or plugin that you either activated or updated.', 'uf'); $message = sprintf($message, ucwords(str_replace('_', ' ', $type))); UF_Exceptions::add($message, 'non_existing_field'); return null; } return new $class_name($id, $title); }