/** * Render the field inner content. * * @since 0.1.0 * @param Ev_Field $field A field object. */ public function render_inner($field = false) { $field_types = ev_field_types(); $value = $this->value(); $handle = $this->handle(); if ($field !== false) { $value = $field->value(); $handle = $field->handle(); } echo '<div class="ev-bundle-fields-wrapper">'; echo '<div class="ev-field-panel-controls-wrapper">'; echo '<div class="ev-field-panel-controls-inner-wrapper">'; echo '<span class="ev-repeatable-remove"></span>'; echo '<span class="ev-sortable-handle"></span>'; echo '</div>'; echo '</div>'; if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('prepend', 'medium'); } foreach ($this->_fields as $index => $field_data) { $field_class = $field_types[$field_data['type']]; $field_data['bundle'] = $handle; $fld = new $field_class($field_data); if (isset($value[$field_data['handle']])) { $fld->value($value[$field_data['handle']]); } $fld->render(); } if (!ev_is_skipped_on_saving($this->_type)) { $this->_render_repeatable_controls('append', 'medium'); } echo '</div>'; }
/** * Render the field inner content. * * @since 0.1.0 * @param Ev_Field $field A field object. */ public function render_inner($field = false) { $field_types = ev_field_types(); $value = $this->value(); $handle = $this->handle(); if ($field !== false) { $value = $field->value(); $handle = $field->handle(); } echo '<div class="ev-bundle-fields-wrapper">'; echo '<span class="ev-sortable-handle"></span>'; foreach ($this->_fields as $index => $field_data) { $field_class = $field_types[$field_data['type']]; $field_data['bundle'] = $handle; $fld = new $field_class($field_data); if (isset($value[$field_data['handle']])) { $fld->value($value[$field_data['handle']]); } $fld->render(); } echo '<span class="ev-repeatable-remove"></span>'; echo '</div>'; }
/** * Validate the field declaration structure. * * @static * @since 0.1.0 * @param array $field The field declaration structure. * @return boolean */ public static function validate_structure($field) { $messages = array(); $field_types = array_keys(ev_field_types()); if (!is_array($field) || empty($field)) { /* Ensuring that the field data structure is valid. */ $messages[] = 'Invalid field structure.'; } elseif (!array_key_exists('handle', $field) || empty($field['handle'])) { /* Ensuring that the field has a valid handle. */ $messages[] = 'Field is missing handle parameter.'; } elseif (!array_key_exists('type', $field) || empty($field['type'])) { /* Ensuring that the field has a type. */ $messages[] = sprintf('Field "%s": missing type parameter.', $field['handle']); } elseif (array_search($field['type'], $field_types, true) === false) { /* Ensuring that the field's type is valid. */ $messages[] = sprintf('Field "%s": invalid type.', $field['handle']); } elseif (!array_key_exists('label', $field) || empty($field['label'])) { /* Ensuring that the field has a valid label. */ $messages[] = sprintf('Field "%s": missing label parameter.', $field['handle']); } elseif (array_key_exists('fields', $field)) { if (!is_array($field['fields'])) { /* Ensuring that the field has a valid set of fields, if any. */ $messages[] = sprintf('Field "%s": subfields must be in array form.', $field['handle']); } elseif (ev_check_multi_key_exists($field['fields'], 'repeatable')) { $messages[] = sprintf('Field "%s": repeatable fields cannot be nested.', $field['handle']); } } elseif (array_key_exists('config', $field) && !is_array($field['config'])) { /* Ensuring that the field has a valid set of configuration options, if any. */ $messages[] = sprintf('Field "%s": config must be in array form.', $field['handle']); } elseif (array_key_exists('repeatable', $field) && (!is_array($field['repeatable']) && !is_bool($field['repeatable']))) { /* Ensuring that the field has a valid repeatable configuration. */ $messages[] = sprintf('Field "%s": repeatable parameter must be in array/boolean form.', $field['handle']); } elseif (array_key_exists('size', $field)) { $allowed_sizes = array('full', 'large', 'medium', 'small', 'one-half', 'two-fourths', 'one-third', 'one-fourth', 'three-fourths', 'two-thirds'); if (!empty($field['size']) && !in_array($field['size'], $allowed_sizes)) { /* Ensuring that the field has a valid value for its size, if any. */ $messages[] = sprintf('Field "%s": invalid size parameter.', $field['handle']); } } $messages = apply_filters("ev_field_validate_structure[type:{$field['type']}]", $messages, $field); return !empty($messages) ? $messages : true; }
/** * Validate a fields container fields structure. This method ensures that * the provided structure for the fields container doesn't lead to * inconsistencies. * If the validator fails, the fields container will display no fields at * all. * * @since 0.1.0 * @param array $elements The fields container fields structure. * @return boolean */ protected static function _validate_fields_structure($elements) { $groups = 0; $messages = array(); foreach ($elements as $index => $element) { if (isset($element['type']) && $element['type'] === 'group' && array_key_exists('fields', $element) && is_array($element['fields'])) { $groups++; if (!array_key_exists('handle', $element) || empty($element['handle'])) { $messages[] = 'Group is missing handle parameter.'; } elseif (!array_key_exists('label', $element) || empty($element['label'])) { $messages[] = sprintf('Group "%s": missing label.', $element['handle']); } else { $field_messages = self::_validate_fields_structure($element['fields']); if ($field_messages !== true) { foreach ($field_messages as $field_message) { $messages[] = $field_message; } } } } else { $field_types = ev_field_types(); $field_types_keys = array_keys($field_types); if (!is_array($element) || empty($element)) { /* Ensuring that the field data structure is valid. */ $messages[] = 'Invalid field structure.'; } elseif (!array_key_exists('type', $element) || empty($element['type'])) { /* Ensuring that the field has a type. */ if (array_key_exists('handle', $element) && !empty($element['handle'])) { $messages[] = sprintf('Field "%s": missing type parameter.', $element['handle']); } else { $messages[] = 'Field: missing type parameter.'; } } elseif (array_search($element['type'], $field_types_keys, true) === false) { /* Ensuring that the field's type is valid. */ if (array_key_exists('handle', $element) && !empty($element['handle'])) { $messages[] = sprintf('Field "%s": invalid type.', $element['handle']); } else { $messages[] = sprintf('%s field: invalid type.', $element['type']); } } elseif (array_key_exists('capability', $element) && empty($element['capability'])) { /* Ensuring that the field has a valid capability, if any. */ if (array_key_exists('handle', $element) && !empty($element['handle'])) { $messages[] = sprintf('Field "%s": invalid capability.', $element['handle']); } else { $messages[] = sprintf('%s field: invalid capability.', $element['type']); } } else { $field_class = $field_types[$element['type']]; $field_messages = call_user_func(array($field_class, 'validate_structure'), $element); if ($field_messages !== true) { foreach ($field_messages as $field_message) { $messages[] = $field_message; } } } } } if ($groups > 0 && $groups !== count($elements)) { return array('All first-level elements must be field groups.'); } return !empty($messages) ? $messages : true; }