WC_Structured_Data::set_data │ public │ WC 3.0.0

Sets data.

Method of the class: WC_Structured_Data{}

No Hooks.

Returns

bool.

Usage

$WC_Structured_Data = new WC_Structured_Data();
$WC_Structured_Data->set_data( $data, $reset );
$data(array) (required)
Structured data. The @type value accepts a string or an array of strings. Every type must contain 1 to 20 ASCII letters; any invalid array member rejects the node.
$reset(true|false)
Unset data .
Default: false)

Changelog

Since 3.0.0 Introduced.
Since 11.2.0 Added support for multiple schema types in @type.

WC_Structured_Data::set_data() code WC 11.2.2

public function set_data( $data, $reset = false ) {
	if ( isset( $data['@type'] ) && is_array( $data['@type'] ) ) {
		if ( empty( $data['@type'] ) ) {
			return false;
		}

		foreach ( $data['@type'] as $type ) {
			if ( ! is_string( $type ) || ! preg_match( '|^[a-zA-Z]{1,20}$|', $type ) ) {
				return false;
			}
		}

		$data['@type'] = array_values( $data['@type'] );
	} elseif ( ! isset( $data['@type'] ) || ! preg_match( '|^[a-zA-Z]{1,20}$|', $data['@type'] ) ) {
		return false;
	}

	if ( $reset && isset( $this->_data ) ) {
		unset( $this->_data );
	}

	$this->_data[] = $data;

	return true;
}