Form_Settings_Page::build_rest_schema_for_field( Leaves_And_Love\Plugin_Lib\Fields\Field $field )

Builds the REST schema array data for a field.

Description

Parameters

$field

(Leaves_And_Love\Plugin_Lib\Fields\Field) (Required) Field to build the schema for.

Return

(array) Schema data for the field.

Source

File: src/db-objects/forms/form-settings-page.php

	protected function build_rest_schema_for_field( $field ) {
		$schema = array();

		switch ( $field->slug ) {
			case 'group':
				$schema['type']       = 'object';
				$schema['properties'] = array();
				foreach ( $field->fields as $sub_field ) {
					$schema['properties'][ $sub_field->id ] = $this->build_rest_schema_for_field( $sub_field );
				}
				break;

			case 'multiselect':
			case 'multibox':
				$schema['type']  = 'array';
				$schema['items'] = array(
					'type' => 'string',
					'enum' => array_map( 'strval', array_keys( $field->choices ) ),
				);
				break;

			case 'select':
			case 'radio':
				$schema['type'] = 'string';
				$schema['enum'] = array_map( 'strval', array_keys( $field->choices ) );
				break;

			case 'checkbox':
				$schema['type'] = 'boolean';
				break;

			case 'number':
			case 'range':
				$input_attrs    = $field->input_attrs;
				$schema['type'] = ( ! empty( $input_attrs['step'] ) && is_int( $input_attrs['step'] ) ) ? 'integer' : 'number';
				if ( isset( $input_attrs['min'] ) ) {
					$schema['minimum'] = $input_attrs['min'];
				}
				if ( isset( $input_attrs['max'] ) ) {
					$schema['maximum'] = $input_attrs['max'];
				}
				break;

			case 'media':
				if ( 'url' === $field->store ) {
					$schema['type']   = 'string';
					$schema['format'] = 'uri';
				} else {
					$schema['type']    = 'integer';
					$schema['minimum'] = 0;
				}
				break;

			case 'url':
				$schema['type'] = 'string';
				$schema['format'] = 'uri';
				break;

			case 'email':
				$schema['type'] = 'string';
				$schema['format'] = 'email';
				break;

			default:
				$schema['type'] = 'string';
		}

		if ( $field->repeatable ) {
			$schema['items'] = $schema;
			$schema['type']  = 'array';
		}

		if ( ! empty( $field->description ) ) {
			$schema['description'] = strip_tags( $field->description );
		} elseif ( 'checkbox' === $field->slug && ! empty( $field->label ) ) {
			$schema['description'] = strip_tags( $field->label );
		}

		return $schema;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.