acf_field_oembed::format_value_for_jsonldpublicACF 6.8.0

Formats the field value for JSON-LD output.

Method of the class: acf_field_oembed{}

No Hooks.

Returns

Mixed.

Usage

$acf_field_oembed = new acf_field_oembed();
$acf_field_oembed->format_value_for_jsonld( $value, $post_id, $field );
$value(mixed) (required)
The value of the field (URL).
$post_id(int|string) (required)
The ID of the post.
$field(array) (required)
The field array.

Changelog

Since 6.8.0 Introduced.

acf_field_oembed::format_value_for_jsonld() code ACF 6.8.8

public function format_value_for_jsonld( $value, $post_id, $field ) {
	if ( empty( $value ) ) {
		return null;
	}

	// Get output format with fallback.
	$output_format = $field['schema_output_format'] ?? '';
	if ( empty( $output_format ) ) {
		$property      = $field['schema_property'] ?? '';
		$output_format = \ACF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
	}

	// Default to VideoObject if no format determined.
	if ( empty( $output_format ) ) {
		$output_format = 'VideoObject';
	}

	// Get oEmbed data for richer output.
	$oembed_data = _wp_oembed_get_object()->get_data( $value, array() );

	$result = array(
		'@type' => $output_format,
		'url'   => $value,
	);

	if ( $oembed_data ) {
		// Add name/title.
		if ( ! empty( $oembed_data->title ) ) {
			$result['name'] = $oembed_data->title;
		}

		// Add thumbnail.
		if ( ! empty( $oembed_data->thumbnail_url ) ) {
			$result['thumbnailUrl'] = $oembed_data->thumbnail_url;
		}

		// Add provider information.
		if ( ! empty( $oembed_data->provider_name ) ) {
			$result['publisher'] = array(
				'@type' => 'Organization',
				'name'  => $oembed_data->provider_name,
			);
			if ( ! empty( $oembed_data->provider_url ) ) {
				$result['publisher']['url'] = $oembed_data->provider_url;
			}
		}

		// Add dimensions for video.
		if ( 'VideoObject' === $output_format || 'video' === ( $oembed_data->type ?? '' ) ) {
			if ( ! empty( $oembed_data->width ) ) {
				$result['width'] = (int) $oembed_data->width;
			}
			if ( ! empty( $oembed_data->height ) ) {
				$result['height'] = (int) $oembed_data->height;
			}
		}

		// Add author if available.
		if ( ! empty( $oembed_data->author_name ) ) {
			$author = array(
				'@type' => 'Person',
				'name'  => $oembed_data->author_name,
			);
			if ( ! empty( $oembed_data->author_url ) ) {
				$author['url'] = $oembed_data->author_url;
			}
			$result['author'] = $author;
		}
	}

	return $result;
}