public function testBrokenFieldIsReturnedWhenDebugIsDisabledAndFieldIsInvalid() { $old_val = Incorrect_Syntax_Exception::$throw_errors; Incorrect_Syntax_Exception::$throw_errors = false; $field = Field::make('__no_such_field_type__', $this->fieldName); $this->assertInstanceOf('Carbon_Fields\\Field\\Broken_Field', $field); Incorrect_Syntax_Exception::$throw_errors = $old_val; }
/** * @covers Carbon_Fields\Container\Container::make * @covers Carbon_Fields\Container\Container::factory * @covers Carbon_Fields\Container\Container::__construct */ public function testBrokenContainerIsReturnedWhenDebugIsDisabledAndContainerIsInvalid() { $old_val = Incorrect_Syntax_Exception::$throw_errors; Incorrect_Syntax_Exception::$throw_errors = false; $container = Container::make('__no_such_container_type__', $this->containerTitle); $this->assertInstanceOf('Carbon_Fields\\Container\\Broken_Container', $container); Incorrect_Syntax_Exception::$throw_errors = $old_val; }
/** * Create a new datastore of type $type. * * @param string $type * @return Datastore $datastore **/ public static function factory($type) { $type = str_replace(' ', '_', ucwords(str_replace('_', ' ', $type))); $class = __NAMESPACE__ . '\\' . $type . '_Datastore'; if (!class_exists($class)) { Incorrect_Syntax_Exception::raise('Unknown data store type "' . $type . '".'); } $field = new $class(); return $field; }
/** * Add new options to this field. * Accepts an array of data. * * @param array|callable $options */ public function add_options($options) { if (is_array($options)) { $old_options = is_callable($this->options) ? array() : $this->options; if (!empty($old_options)) { $this->options = array_merge($old_options, $options); } else { $this->options = $options; } } else { $this->options = array(); Incorrect_Syntax_Exception::raise('Only arrays are allowed in the <code>add_options()</code> method.'); } return $this; }
/** * Perform checks whether there is a field registered with the name $name. * If not, the field name is recorded. * * @param string $name **/ public function verify_unique_field_name($name) { if (in_array($name, self::$registered_field_names)) { Incorrect_Syntax_Exception::raise('Field name "' . $name . '" already registered'); } self::$registered_field_names[] = $name; }
/** * Make sure a field certain name can't be registered multiple times. **/ public function verify_unique_field_name($name) { $page_id = $this->settings['parent'] . '/' . $this->settings['file']; if (!isset(self::$registered_field_names[$page_id])) { self::$registered_field_names[$page_id] = array(); } if (in_array($name, self::$registered_field_names[$page_id])) { Incorrect_Syntax_Exception::raise('Field name "' . $name . '" already registered'); } self::$registered_field_names[$page_id][] = $name; }
/** * Validate and parse the conditional logic rules. * * @param array $rules * @return array */ protected function parse_conditional_rules($rules) { if (!is_array($rules)) { Incorrect_Syntax_Exception::raise('Conditional logic rules argument should be an array.'); } $allowed_operators = array('=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN'); $allowed_relations = array('AND', 'OR'); $parsed_rules = array('relation' => 'AND', 'rules' => array()); foreach ($rules as $key => $rule) { // Check if we have a relation key if ($key === 'relation') { $relation = strtoupper($rule); if (!in_array($relation, $allowed_relations)) { Incorrect_Syntax_Exception::raise('Invalid relation type ' . $rule . '. ' . 'The rule should be one of the following: "' . implode('", "', $allowed_relations) . '"'); } $parsed_rules['relation'] = $relation; continue; } // Check if the rule is valid if (!is_array($rule) || empty($rule['field'])) { Incorrect_Syntax_Exception::raise('Invalid conditional logic rule format. ' . 'The rule should be an array with the "field" key set.'); } // Check the compare operator if (empty($rule['compare'])) { $rule['compare'] = '='; } if (!in_array($rule['compare'], $allowed_operators)) { Incorrect_Syntax_Exception::raise('Invalid conditional logic compare operator: <code>' . $rule['compare'] . '</code><br>Allowed operators are: <code>' . implode(', ', $allowed_operators) . '</code>'); } if ($rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN') { if (!is_array($rule['value'])) { Incorrect_Syntax_Exception::raise('Invalid conditional logic value format. ' . 'An array is expected, when using the "' . $rule['compare'] . '" operator.'); } } // Check the value if (!isset($rule['value'])) { $rule['value'] = ''; } $parsed_rules['rules'][] = $rule; } return $parsed_rules; }
/** * Modify the layout of this field. * * @param string $layout */ public function set_layout($layout) { $available_layouts = array(self::LAYOUT_GRID, self::LAYOUT_TABBED_HORIZONTAL, self::LAYOUT_TABBED_VERTICAL, self::LAYOUT_LIST); if ($layout === self::LAYOUT_TABBED) { // The library used to provide just one kind of tabs -- horizontal ones. Later vertical tabs were added. // So the "tabbed" name was renamed to "tabbed-horizontal" and "tabbed-vertical" layout was introduced. _doing_it_wrong(__METHOD__, sprintf(__('Complex field "%1$s" layout is deprecated, please use "%2$s" or "%3$s" instead.', 'carbon_fields'), self::LAYOUT_TABBED, self::LAYOUT_TABBED_HORIZONTAL, self::LAYOUT_TABBED_VERTICAL), null); $layout = self::LAYOUT_TABBED_HORIZONTAL; } if (!in_array($layout, $available_layouts)) { $error_message = 'Incorrect layout ``' . $layout . '" specified. ' . 'Available layouts: ' . implode(', ', $available_layouts); Incorrect_Syntax_Exception::raise($error_message); } if ($layout === self::LAYOUT_LIST) { _doing_it_wrong(__METHOD__, __('Complex field <code>' . self::LAYOUT_LIST . '</code> layout is deprecated, please use <code>set_width()</code> instead.', 'carbon_fields'), null); } $this->layout = $layout; return $this; }
/** * Modify the layout of this field. * Deprecated in favor of set_width(). * * @deprecated * * @param string $layout */ public function set_layout($layout) { if (!in_array($layout, array(self::LAYOUT_GRID, self::LAYOUT_TABBED, self::LAYOUT_LIST))) { Incorrect_Syntax_Exception::raise('Incorrect layout specified. Available values are "<code>' . self::LAYOUT_GRID . '</code>" and "<code>' . self::LAYOUT_TABBED . '</code>"'); } if ($layout === self::LAYOUT_LIST) { _doing_it_wrong(__METHOD__, __('Complex field <code>' . self::LAYOUT_LIST . '</code> layout is deprecated, please use <code>set_width()</code> instead.', 'carbon_fields'), null); } $this->layout = $layout; return $this; }
/** * Show the container only on hierarchical posts of level $level. * Levels start from 1 (top level post) * * @param int $level * @return object $this **/ public function show_on_level($level) { if ($level < 0) { Incorrect_Syntax_Exception::raise('Invalid level limitation (' . $level . ')'); } $this->settings['show_on']['level_limit'] = $level; return $this; }
/** * Perform checks whether there is a field registered with the name $name. * If not, the field name is recorded. * * @param string $name **/ public function verify_unique_field_name($name) { if (empty($this->settings['taxonomy'])) { Incorrect_Syntax_Exception::raise('Panel instance is not setup correctly (missing taxonomy)'); } foreach ($this->settings['taxonomy'] as $taxonomy) { if (!isset(self::$registered_field_names[$taxonomy])) { self::$registered_field_names[$taxonomy] = array(); } if (in_array($name, self::$registered_field_names[$taxonomy])) { Incorrect_Syntax_Exception::raise('Field name "' . $name . '" already registered'); } self::$registered_field_names[$taxonomy][] = $name; } }
/** * Verify widget IDs are unique. * * @param string $id Widget ID */ public function verify_unique_widget_id($id) { if (in_array($id, self::$registered_widget_ids)) { Incorrect_Syntax_Exception::raise('Widget with ID "' . $id . '" already registered. Please change the widget title'); } self::$registered_widget_ids[] = $id; }
/** * Make sure that there are no duplicate containers with the same name. **/ public function verify_unique_page() { $file = $this->settings['file']; $parent = $this->settings['parent']; // Register top level page if (!$parent) { if (isset(self::$registered_pages[$file])) { Incorrect_Syntax_Exception::raise('Page "' . $file . '" already registered'); } self::$registered_pages[$file] = array(); return; } // Register sub-page if (!isset(self::$registered_pages[$parent])) { self::$registered_pages[$parent] = array($file); } elseif (in_array($file, self::$registered_pages[$parent])) { Incorrect_Syntax_Exception::raise('Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a different file name using setup()'); } else { self::$registered_pages[$parent][] = $file; } }